Phase D: insert value list mirrors do_insert's user_cols contract
Bug: hint at \`insert into Customers values (\` for a Customers
table with id:serial PK suggested typing an integer for \`id\`,
but the dispatch path (\`db::do_insert\`) deliberately doesn't
accept user-supplied values for auto-generated columns in
Form B. The grammar prompted for a value the dispatch would
refuse.
The fix aligns Phase D's \`column_value_list\` dynamic sub-grammar
with do_insert's three forms (ADR-0014 + ADR-0018 §3):
- **Form A** \`insert into <T> (col1, col2, …) values (…)\` —
user explicitly lists columns. Slot list mirrors that
selection; serial / shortid columns CAN appear if the user
lists them.
- **Form B** \`insert into <T> values (…)\` — bare values. Slot
list = non-auto-generated columns of the table in
declaration order. Serial / shortid get auto-filled by the
dispatch; the grammar doesn't prompt for them.
- **Form C** \`insert into <T> (v1, v2, …)\` — bare value list.
Not affected by this change (column_value_list isn't on this
path; Form C's literals route through the schemaless
INSERT_PAREN_LIST).
Implementation:
\`WalkContext.user_listed_columns: Option<Vec<String>>\` — when
\`Some\`, signals Form A; \`None\` is Form B. Populated by walking
the first paren's column-list idents.
\`Node::Ident.writes_user_listed_column: bool\` — new field;
\`true\` on the INSERT_PAREN_ITEM's Ident child. When the
walker matches that ident in Form A, it appends the
schema-canonical column name (case-corrected against the
schema) to user_listed_columns.
\`column_value_list\` factory:
- If user_listed_columns is Some → resolve each name from the
schema; one typed slot per listed column.
- Else → filter current_table_columns to non-auto-generated;
one typed slot per remaining column.
- Empty result → fall back to the schemaless value-literal
list (a serial-only table in Form B has nothing for the
user to type).
Tests:
- New \`phase_d_insert_form_b_skips_serial_column\` confirms the
bug: \`insert into Customers values (1, 'Alice')\` against a
Customers with serial id rejects at parse time (Form B
expects 1 value for Name, not 2).
- New \`phase_d_insert_form_a_accepts_serial_when_listed\`
confirms \`insert into Customers (id, Name) values (1, 'Alice')\`
works.
- New \`phase_d_insert_form_a_filters_to_user_listed_columns\`
confirms partial Form A (\`(Name) values ('Alice')\`).
- Updated \`phase_d_insert_with_schema_accepts_typed_values_per_column\`
to match the new Form B contract (2 user-typed values, not 3).
- Updated typed-hint test matrix split into form-B (8 types)
and form-A (serial / shortid).
- New \`typed_hint_form_b_skips_serial_column_to_generic_or_text_neighbor\`
pins the fallback behavior for a serial-only table.
For the user: \`insert into Customers values (\` for a Customers
with \`(id:serial, Name:text, Email:text)\` now hints
\`for \`Name\`: Type a quoted string …\` (skipping id entirely)
and accepts exactly 2 values. To set the serial explicitly,
use Form A: \`insert into Customers (id, Name, Email) values
(1, 'Alice', 'a@b.c')\`.
Tests: 851 passing, 0 failing, 1 ignored. Clippy clean.
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
//! actions; Phase D extends with `where_clause`,
|
||||
//! `column_value_list`, and the typed value slots.
|
||||
|
||||
use crate::completion::TableColumn;
|
||||
use crate::dsl::grammar::{
|
||||
IdentSource, IdentValidator, Node, NumberValidator, ValidationError, Word,
|
||||
};
|
||||
@@ -51,6 +52,7 @@ pub const TYPE_SLOT: Node = Node::Ident {
|
||||
highlight_override: None,
|
||||
writes_table: false,
|
||||
writes_column: false,
|
||||
writes_user_listed_column: false,
|
||||
};
|
||||
|
||||
// --- Qualified column reference (`<Table>.<Column>`) --------------
|
||||
@@ -63,6 +65,7 @@ const QUALIFIED_COLUMN_NODES: &[Node] = &[
|
||||
highlight_override: None,
|
||||
writes_table: false,
|
||||
writes_column: false,
|
||||
writes_user_listed_column: false,
|
||||
},
|
||||
Node::Punct('.'),
|
||||
Node::Ident {
|
||||
@@ -72,6 +75,7 @@ const QUALIFIED_COLUMN_NODES: &[Node] = &[
|
||||
highlight_override: None,
|
||||
writes_table: false,
|
||||
writes_column: false,
|
||||
writes_user_listed_column: false,
|
||||
},
|
||||
];
|
||||
pub const QUALIFIED_COLUMN: Node = Node::Seq(QUALIFIED_COLUMN_NODES);
|
||||
@@ -373,18 +377,56 @@ pub fn current_column_value(ctx: &WalkContext) -> Node {
|
||||
/// `Repeated(VALUE_LITERAL, ',', 1)` shape so existing
|
||||
/// callers/tests continue to work.
|
||||
pub fn column_value_list(ctx: &WalkContext) -> Node {
|
||||
let Some(cols) = ctx.current_table_columns.as_ref() else {
|
||||
let Some(table_cols) = ctx.current_table_columns.as_ref() else {
|
||||
return FALLBACK_VALUE_LIST;
|
||||
};
|
||||
if cols.is_empty() {
|
||||
if table_cols.is_empty() {
|
||||
return FALLBACK_VALUE_LIST;
|
||||
}
|
||||
// Build a Seq of typed slots interleaved with commas.
|
||||
// Each slot embeds its column name so the hint resolver
|
||||
// can mention the column by name ("for `Email`: Type a
|
||||
// quoted string …").
|
||||
let mut children: Vec<Node> = Vec::with_capacity(cols.len() * 2);
|
||||
for (i, col) in cols.iter().enumerate() {
|
||||
// Three dispatch shapes (ADR-0024 §Phase D §column_value_list,
|
||||
// matching `db::do_insert`'s user_cols logic):
|
||||
//
|
||||
// 1. Form A — user listed explicit columns
|
||||
// (`insert into T (col1, col2, …) values (…)`): one slot
|
||||
// per listed column, in the user's order, types resolved
|
||||
// from the schema.
|
||||
// 2. Form B — bare values keyword
|
||||
// (`insert into T values (…)`): one slot per non-auto-
|
||||
// generated column of T, in declaration order. Serial /
|
||||
// shortid columns are skipped because the dispatch path
|
||||
// auto-fills them (ADR-0018 §3).
|
||||
// 3. Schemaless / fallback: the generic value-literal list.
|
||||
let target_cols: Vec<&TableColumn> = ctx.user_listed_columns.as_ref().map_or_else(
|
||||
|| {
|
||||
// Form B — exclude auto-generated columns.
|
||||
table_cols
|
||||
.iter()
|
||||
.filter(|c| !matches!(c.user_type, Type::Serial | Type::ShortId))
|
||||
.collect()
|
||||
},
|
||||
|user_listed| {
|
||||
// Form A — resolve each listed name from the schema.
|
||||
// Names the schema doesn't know about silently drop;
|
||||
// the bind-time path catches unknown columns.
|
||||
user_listed
|
||||
.iter()
|
||||
.filter_map(|name| {
|
||||
table_cols
|
||||
.iter()
|
||||
.find(|c| c.name.eq_ignore_ascii_case(name))
|
||||
})
|
||||
.collect()
|
||||
},
|
||||
);
|
||||
if target_cols.is_empty() {
|
||||
return FALLBACK_VALUE_LIST;
|
||||
}
|
||||
// Build a Seq of typed slots interleaved with commas. Each
|
||||
// slot embeds its column name so the hint resolver can
|
||||
// mention the column by name ("for `Email`: Type a quoted
|
||||
// string …").
|
||||
let mut children: Vec<Node> = Vec::with_capacity(target_cols.len() * 2);
|
||||
for (i, col) in target_cols.iter().enumerate() {
|
||||
if i > 0 {
|
||||
children.push(Node::Punct(','));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user