use crate::{
binder::bound::BoundCreateTableStmt,
executor::{ExecutionError, ExecutionResult, Executor},
};
impl Executor {
pub fn execute_create_table(
&mut self,
stmt: BoundCreateTableStmt,
) -> Result<ExecutionResult, ExecutionError> {
let name = stmt.name;
let db_sym = stmt.db;
let schema_sym = stmt.schema;
let db_name = self.catalog.interner.resolve(db_sym).to_string();
let schema_name = self.catalog.interner.resolve(schema_sym).to_string();
let table_name = self.catalog.interner.resolve(name).to_string();
let storage = self
.storage
.as_ref()
.ok_or_else(|| ExecutionError::Storage("storage engine not initialized".to_string()))?;
if !self.catalog.table_exists(db_sym, schema_sym, stmt.name) {
storage
.create_table_file(&db_name, &schema_name, &table_name)
.map_err(|e| ExecutionError::Storage(e.to_string()))?;
}
self.catalog
.create_table(
storage,
db_sym,
schema_sym,
stmt.name,
stmt.columns,
stmt.constraints,
stmt.if_not_exists,
)
.map_err(ExecutionError::from)?;
if let Some(sys) = &self.system_catalog {
let entry = self
.catalog
.get_table(db_sym, schema_sym, name)
.map_err(ExecutionError::from)?
.clone();
sys.write_table(&db_name, &schema_name, &entry, &mut self.catalog.interner)
.map_err(|e| ExecutionError::Storage(e.to_string()))?;
}
Ok(ExecutionResult::TableCreated { name })
}
}