feat: ADR-0035 4a — SQL CREATE TABLE command, worker, and exit gate
Command + builder + worker for advanced-mode SQL CREATE TABLE (sub-phase 4a), executed structurally through do_create_table: - Command::SqlCreateTable + build_sql_create_table (ddl.rs): aliases via from_sql_name (incl. double precision), column- and table-level PRIMARY KEY, redundant-flag de-dup off a sole PK, IF NOT EXISTS. Advanced REGISTRY entry on the shared `create` word (SQL-first, DSL fallback); no-PK tables allowed (user-confirmed). - Worker (db.rs): Request::SqlCreateTable + CreateOutcome + snapshot_then (one undo step); IF NOT EXISTS no-op (no snapshot, but journalled, like read-only commands). do_create_table inline-PK rule aligned with the rebuild generator schema_to_ddl — no round-trip DDL drift; serial autoincrement is independent of inline-PK (verified by round-trip tests). - Runtime/App: dispatch + CommandOutcome::SchemaSkipped + AppEvent::DslCreateSkipped (structure + "already exists — skipped" note). Friendly catalog keys added (engine-neutral). DEFAULT/CHECK/table-level UNIQUE are absent from the 4a grammar (parse error with usage skeleton; friendly message + support land in the 4a.2 constraint slice) — user-confirmed. Tests: type resolver, grammar shape, builder (incl. the PK detection bug they caught), and tests/sql_create_table.rs (worker round-trip, serial autoincrement first/non-first across rebuild, IF NOT EXISTS no-op + journalling, no-PK table, one undo step) + a replay-as- write test. 1739 pass / 0 fail / 1 ignored; clippy clean. Exit gate: ADR-0035 Proposed -> Accepted (validated end-to-end by 4a); README + requirements.md Q1 updated.
This commit is contained in:
@@ -1288,6 +1288,135 @@ pub static CREATE: CommandNode = CommandNode {
|
||||
help_id: Some("ddl.create"),
|
||||
usage_ids: &["parse.usage.create_table"],};
|
||||
|
||||
/// Build a `Command::SqlCreateTable` from the advanced-mode SQL
|
||||
/// `CREATE TABLE` shape (ADR-0035 §1, sub-phase 4a). Executes
|
||||
/// structurally — extracts the same `ColumnSpec`/`primary_key` the
|
||||
/// simple-mode builder produces so the worker reuses `do_create_table`.
|
||||
///
|
||||
/// 4a surface: columns + types (the §3 alias map incl. `double
|
||||
/// precision`) + `NOT NULL` / `UNIQUE` / column- and table-level
|
||||
/// `PRIMARY KEY` + `IF NOT EXISTS`. `DEFAULT` / `CHECK` /
|
||||
/// table-level `UNIQUE` are absent from the grammar (4a.2), so they
|
||||
/// never reach this builder.
|
||||
fn build_sql_create_table(path: &MatchedPath, _source: &str) -> Result<Command, ValidationError> {
|
||||
let name = require_ident(path, "table_name")?;
|
||||
// `if` only appears in the `IF NOT EXISTS` prefix (the `not` of
|
||||
// `NOT NULL` never carries an `if`), so its presence is the flag.
|
||||
let if_not_exists = path
|
||||
.items
|
||||
.iter()
|
||||
.any(|i| matches!(i.kind, MatchedKind::Word("if")));
|
||||
|
||||
let mut columns: Vec<ColumnSpec> = Vec::new();
|
||||
let mut primary_key: Vec<String> = Vec::new();
|
||||
let mut pending_name: Option<String> = None;
|
||||
let mut items = path.items.iter().peekable();
|
||||
while let Some(item) = items.next() {
|
||||
match &item.kind {
|
||||
// A column name stashes until its type finalises the spec.
|
||||
MatchedKind::Ident { role: "col_name", .. } => {
|
||||
pending_name = Some(item.text.clone());
|
||||
}
|
||||
// Single-word type — resolve through the SQL alias map.
|
||||
MatchedKind::Ident { role: "col_type", .. } => {
|
||||
let ty = Type::from_sql_name(&item.text).ok_or_else(|| ValidationError {
|
||||
message_key: "parse.error_wrapper",
|
||||
args: vec![("detail", "unknown type".to_string())],
|
||||
})?;
|
||||
let col_name = pending_name.take().ok_or_else(|| ValidationError {
|
||||
message_key: "parse.error_wrapper",
|
||||
args: vec![("detail", "column type without a name".to_string())],
|
||||
})?;
|
||||
columns.push(ColumnSpec::new(col_name, ty));
|
||||
}
|
||||
// `double precision` — the two-word alias maps to `real`.
|
||||
// The grammar guarantees `precision` follows `double`.
|
||||
MatchedKind::Word("double") => {
|
||||
if matches!(
|
||||
items.peek().map(|i| &i.kind),
|
||||
Some(MatchedKind::Word("precision"))
|
||||
) {
|
||||
items.next();
|
||||
}
|
||||
let col_name = pending_name.take().ok_or_else(|| ValidationError {
|
||||
message_key: "parse.error_wrapper",
|
||||
args: vec![("detail", "column type without a name".to_string())],
|
||||
})?;
|
||||
columns.push(ColumnSpec::new(col_name, Type::Real));
|
||||
}
|
||||
// A table-level `PRIMARY KEY (col, …)` column reference.
|
||||
MatchedKind::Ident { role: "pk_column", .. } => {
|
||||
primary_key.push(item.text.clone());
|
||||
}
|
||||
// `not null` column constraint (only once a column exists;
|
||||
// the `IF NOT EXISTS` `not` precedes every column).
|
||||
MatchedKind::Word("not") => {
|
||||
if matches!(
|
||||
items.peek().map(|i| &i.kind),
|
||||
Some(MatchedKind::Word("null"))
|
||||
) {
|
||||
items.next();
|
||||
if let Some(last) = columns.last_mut() {
|
||||
last.not_null = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
MatchedKind::Word("unique") => {
|
||||
if let Some(last) = columns.last_mut() {
|
||||
last.unique = true;
|
||||
}
|
||||
}
|
||||
// `primary key` — either a column-level constraint (mark
|
||||
// the most recent column) or the table-level clause (whose
|
||||
// `pk_column` idents follow and are collected above).
|
||||
MatchedKind::Word("primary") => {
|
||||
if matches!(items.peek().map(|i| &i.kind), Some(MatchedKind::Word("key"))) {
|
||||
items.next();
|
||||
// Table-level `PRIMARY KEY (…)` is followed by `(`
|
||||
// (then `pk_column` idents, collected above);
|
||||
// column-level `PRIMARY KEY` is not, and marks the
|
||||
// most-recent column.
|
||||
let table_level = matches!(
|
||||
items.peek().map(|i| &i.kind),
|
||||
Some(MatchedKind::Punct('('))
|
||||
);
|
||||
if !table_level && let Some(last) = columns.last() {
|
||||
primary_key.push(last.name.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
// De-dup redundant flags off a sole primary-key column (ADR-0035
|
||||
// §6.5): a single-column PK is already NOT NULL + UNIQUE, so
|
||||
// emitting them again would create a spurious index. Advanced mode
|
||||
// accepts the redundant spelling (real SQL does) rather than
|
||||
// rejecting it like simple mode (ADR-0029 §9).
|
||||
if primary_key.len() == 1
|
||||
&& let Some(c) = columns.iter_mut().find(|c| c.name == primary_key[0])
|
||||
{
|
||||
c.not_null = false;
|
||||
c.unique = false;
|
||||
}
|
||||
|
||||
Ok(Command::SqlCreateTable {
|
||||
name,
|
||||
columns,
|
||||
primary_key,
|
||||
if_not_exists,
|
||||
})
|
||||
}
|
||||
|
||||
pub static SQL_CREATE_TABLE: CommandNode = CommandNode {
|
||||
entry: Word::keyword("create"),
|
||||
shape: Node::Subgrammar(&super::sql_create_table::SQL_CREATE_TABLE_SHAPE),
|
||||
ast_builder: build_sql_create_table,
|
||||
help_id: Some("ddl.sql_create_table"),
|
||||
usage_ids: &["parse.usage.sql_create_table"],
|
||||
};
|
||||
|
||||
// =================================================================
|
||||
// Tests — `create table` column constraints (ADR-0029 §2.1, §9)
|
||||
// =================================================================
|
||||
|
||||
Reference in New Issue
Block a user