INSERT/UPDATE/DELETE + value model + auto-show, with polish
DSL data operations (ADR-0014): - insert into T [(cols)] values (vals); short form insert into T (vals) omits values keyword for friendlier syntax. - update T set ... where col=val | --all-rows; delete from T where col=val | --all-rows; show data T. - Value AST (Number/Text/Bool/Null) with per-column-type validation in the executor: int/real/decimal/bool/date/ datetime/shortid each accept a documented literal shape and produce friendly format errors naming the column. - INSERT short form fills non-auto-generated columns in schema order; auto-fills serial via SQLite and shortid via the new generator (T2). - `add column [to table] T: c (type)` -- `to table` now optional. Database: - insert/update/delete via prepared statements with bound rusqlite::types::Value parameters. - InsertResult/UpdateResult/DeleteResult: writes return rows_affected plus the affected row(s) only (not the whole table), so users see exactly what changed. - INSERT shows the just-inserted row via last_insert_rowid. - UPDATE captures matching rowids up-front and fetches them post-update -- works even if the UPDATE changed the WHERE column. - DELETE reports per-relationship cascade effects by row- count diffing inbound child tables; UPDATE-side cascades are not yet detected (would need value diffing). - query_data formats cells (booleans true/false, NULLs as None). FK error enrichment: - Now lists both outbound (INSERT/UPDATE relevance) and inbound (DELETE/UPDATE on parent relevance) FKs from the metadata, so RESTRICT errors point at the children blocking the delete. - RelationshipSelector has a proper Display impl -- "no such relationship" reads cleanly. Relationship display: - target_table for AddRelationship/DropRelationship now returns the parent (1-side); structure rendering after add/drop shows that side's "Referenced by:" entry, matching the `from <Parent>` direction of the command. - [ok] summary uses display_subject so relationship commands show both endpoints (`from P.col to C.col`) rather than a single misleading table name. - Auto-name format `<Parent>_<pcol>_to_<Child>_<ccol>` (matches the from..to direction). Output rendering and scrolling: - Wrap-aware scroll: renderer reports both visible-row count and total wrapped-row count to App; scroll math caps against actual displayable rows. Long lines wrap; the bottom line is always reachable; PageUp/PageDown work correctly even after paging past the buffer top. - Multi-line messages (FK error enrichment, cascade summary) split into single-line OutputLines at creation time so wrap/scroll math agree. Runtime / events: - New AppEvent variants for Insert/Update/Delete success carrying typed result structs; DslDataSucceeded reserved for show-data queries. Docs: - ADR-0014 covers data-op grammar, value model, --all-rows safety, auto-show. - requirements.md: C5 done, T2 done, V2 partial (basic data view), V5 partial (show data added). New entries: C5a complex WHERE expressions; H1 progress note for FK enrichment; H1a (strong syntax-help in parse errors). Tests: 200 passing (183 lib + 17 integration), 0 skipped. Includes parser, type-validation, DB write/read, FK-failure enrichment, cascade-delete propagation, focused-auto-show behaviour, scroll-cap invariants. Clippy clean with nursery enabled.
This commit is contained in:
+81
-7
@@ -13,6 +13,7 @@
|
||||
|
||||
use crate::dsl::action::ReferentialAction;
|
||||
use crate::dsl::types::Type;
|
||||
use crate::dsl::value::Value;
|
||||
|
||||
/// A column at table-creation time: a name and a user-facing
|
||||
/// type. Constraints beyond `PRIMARY KEY` (NOT NULL, UNIQUE,
|
||||
@@ -70,6 +71,38 @@ pub enum Command {
|
||||
ShowTable {
|
||||
name: String,
|
||||
},
|
||||
/// Insert a single row. `columns` is `None` for the natural-
|
||||
/// order short form (`insert into T values (...)`); the
|
||||
/// executor fills in the column list by walking the schema.
|
||||
Insert {
|
||||
table: String,
|
||||
columns: Option<Vec<String>>,
|
||||
values: Vec<Value>,
|
||||
},
|
||||
/// Update rows matching the WHERE clause (or all rows when
|
||||
/// `all_rows` is set, per ADR-0009 opt-in convention).
|
||||
Update {
|
||||
table: String,
|
||||
assignments: Vec<(String, Value)>,
|
||||
filter: RowFilter,
|
||||
},
|
||||
Delete {
|
||||
table: String,
|
||||
filter: RowFilter,
|
||||
},
|
||||
/// Render the rows of a table as a data view in the output.
|
||||
ShowData {
|
||||
name: String,
|
||||
},
|
||||
}
|
||||
|
||||
/// How an UPDATE / DELETE selects which rows to operate on.
|
||||
/// `Where` is the default safe form. `AllRows` is the explicit
|
||||
/// `--all-rows` flag opt-in for unfiltered operations.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum RowFilter {
|
||||
Where { column: String, value: Value },
|
||||
AllRows,
|
||||
}
|
||||
|
||||
/// How a `drop relationship` command identifies the relationship
|
||||
@@ -114,6 +147,10 @@ impl Command {
|
||||
Self::AddRelationship { .. } => "add relationship",
|
||||
Self::DropRelationship { .. } => "drop relationship",
|
||||
Self::ShowTable { .. } => "show table",
|
||||
Self::Insert { .. } => "insert into",
|
||||
Self::Update { .. } => "update",
|
||||
Self::Delete { .. } => "delete from",
|
||||
Self::ShowData { .. } => "show data",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -126,16 +163,53 @@ impl Command {
|
||||
match self {
|
||||
Self::CreateTable { name, .. }
|
||||
| Self::DropTable { name }
|
||||
| Self::ShowTable { name } => name,
|
||||
Self::AddColumn { table, .. } => table,
|
||||
Self::AddRelationship { child_table, .. } => child_table,
|
||||
| Self::ShowTable { name }
|
||||
| Self::ShowData { name } => name,
|
||||
Self::AddColumn { table, .. }
|
||||
| Self::Insert { table, .. }
|
||||
| Self::Update { table, .. }
|
||||
| Self::Delete { table, .. } => table,
|
||||
// For relationships we focus on the parent (1-side):
|
||||
// the structure rendering after add/drop shows that
|
||||
// table's "Referenced by" entry, which is what the
|
||||
// user looks at to confirm the relationship.
|
||||
Self::AddRelationship { parent_table, .. } => parent_table,
|
||||
Self::DropRelationship { selector } => match selector {
|
||||
RelationshipSelector::Endpoints { child_table, .. } => child_table,
|
||||
// For a named drop we don't know the child table
|
||||
// until the executor resolves it; the verb is
|
||||
// still a sensible fallback for logging.
|
||||
RelationshipSelector::Endpoints { parent_table, .. } => parent_table,
|
||||
// For a named drop we don't know the parent table
|
||||
// until the executor resolves it; the name itself
|
||||
// is a sensible fallback for logging.
|
||||
RelationshipSelector::Named { name } => name,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// Human-readable subject for the `[ok] <verb> <subject>`
|
||||
/// summary line. Most commands target a single table, but
|
||||
/// relationship commands are better described by their
|
||||
/// endpoints than by either side alone.
|
||||
#[must_use]
|
||||
pub fn display_subject(&self) -> String {
|
||||
match self {
|
||||
Self::AddRelationship {
|
||||
parent_table,
|
||||
parent_column,
|
||||
child_table,
|
||||
child_column,
|
||||
..
|
||||
} => format!("from {parent_table}.{parent_column} to {child_table}.{child_column}"),
|
||||
Self::DropRelationship { selector } => match selector {
|
||||
RelationshipSelector::Named { name } => name.clone(),
|
||||
RelationshipSelector::Endpoints {
|
||||
parent_table,
|
||||
parent_column,
|
||||
child_table,
|
||||
child_column,
|
||||
} => format!(
|
||||
"from {parent_table}.{parent_column} to {child_table}.{child_column}"
|
||||
),
|
||||
},
|
||||
_ => self.target_table().to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+4
-1
@@ -12,9 +12,12 @@
|
||||
pub mod action;
|
||||
pub mod command;
|
||||
pub mod parser;
|
||||
pub mod shortid;
|
||||
pub mod types;
|
||||
pub mod value;
|
||||
|
||||
pub use action::ReferentialAction;
|
||||
pub use command::{ColumnSpec, Command, RelationshipSelector};
|
||||
pub use command::{ColumnSpec, Command, RelationshipSelector, RowFilter};
|
||||
pub use parser::{ParseError, parse_command};
|
||||
pub use types::Type;
|
||||
pub use value::Value;
|
||||
|
||||
+378
-3
@@ -14,8 +14,9 @@ use chumsky::error::RichReason;
|
||||
use chumsky::prelude::*;
|
||||
|
||||
use crate::dsl::action::ReferentialAction;
|
||||
use crate::dsl::command::{ColumnSpec, Command, RelationshipSelector};
|
||||
use crate::dsl::command::{ColumnSpec, Command, RelationshipSelector, RowFilter};
|
||||
use crate::dsl::types::Type;
|
||||
use crate::dsl::value::Value;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
|
||||
pub enum ParseError {
|
||||
@@ -128,10 +129,14 @@ fn command_parser<'a>()
|
||||
.ignore_then(identifier())
|
||||
.map(|name| Command::DropTable { name });
|
||||
|
||||
// `to table` is optional — both `add column to table T: c (text)`
|
||||
// and `add column T: c (text)` parse identically.
|
||||
let to_table_optional = keyword_ci("to")
|
||||
.ignore_then(keyword_ci("table"))
|
||||
.or_not();
|
||||
let add_column = keyword_ci("add")
|
||||
.ignore_then(keyword_ci("column"))
|
||||
.ignore_then(keyword_ci("to"))
|
||||
.ignore_then(keyword_ci("table"))
|
||||
.ignore_then(to_table_optional)
|
||||
.ignore_then(identifier())
|
||||
.then_ignore(just(':').padded())
|
||||
.then(identifier())
|
||||
@@ -143,23 +148,211 @@ fn command_parser<'a>()
|
||||
let add_relationship = add_relationship_parser();
|
||||
let drop_relationship = drop_relationship_parser();
|
||||
|
||||
let show_data = keyword_ci("show")
|
||||
.ignore_then(keyword_ci("data"))
|
||||
.ignore_then(identifier())
|
||||
.map(|name| Command::ShowData { name });
|
||||
|
||||
let show_table = keyword_ci("show")
|
||||
.ignore_then(keyword_ci("table"))
|
||||
.ignore_then(identifier())
|
||||
.map(|name| Command::ShowTable { name });
|
||||
|
||||
let insert_cmd = insert_parser();
|
||||
let update_cmd = update_parser();
|
||||
let delete_cmd = delete_parser();
|
||||
|
||||
choice((
|
||||
create_table,
|
||||
drop_table,
|
||||
add_column,
|
||||
add_relationship,
|
||||
drop_relationship,
|
||||
// Order: `show data` before `show table` because both
|
||||
// start with `show` and the longer keyword is checked
|
||||
// first via this ordering.
|
||||
show_data,
|
||||
show_table,
|
||||
insert_cmd,
|
||||
update_cmd,
|
||||
delete_cmd,
|
||||
))
|
||||
.padded()
|
||||
.then_ignore(end())
|
||||
}
|
||||
|
||||
/// INSERT, accepting three shapes:
|
||||
/// `insert into T (cols) values (vals)` — explicit columns
|
||||
/// `insert into T values (vals)` — implicit column order
|
||||
/// `insert into T (vals)` — short form, omits `values`
|
||||
///
|
||||
/// The short form is disambiguated from the column-list form by
|
||||
/// trying both alternatives in order; chumsky's `choice`
|
||||
/// backtracks, and only the all-literals form parses without
|
||||
/// `values`.
|
||||
fn insert_parser<'a>()
|
||||
-> impl Parser<'a, &'a str, Command, extra::Err<Rich<'a, char>>> + Clone {
|
||||
let column_list = just('(')
|
||||
.padded()
|
||||
.ignore_then(
|
||||
identifier()
|
||||
.separated_by(just(',').padded())
|
||||
.at_least(1)
|
||||
.collect::<Vec<_>>(),
|
||||
)
|
||||
.then_ignore(just(')').padded());
|
||||
|
||||
let value_list = just('(')
|
||||
.padded()
|
||||
.ignore_then(
|
||||
value_literal()
|
||||
.separated_by(just(',').padded())
|
||||
.at_least(1)
|
||||
.collect::<Vec<_>>(),
|
||||
)
|
||||
.then_ignore(just(')').padded());
|
||||
|
||||
let with_columns_and_values = column_list
|
||||
.clone()
|
||||
.then_ignore(keyword_ci("values"))
|
||||
.then(value_list.clone())
|
||||
.map(|(cols, vals)| (Some(cols), vals));
|
||||
|
||||
let with_values_keyword_only = keyword_ci("values")
|
||||
.ignore_then(value_list.clone())
|
||||
.map(|vals| (None, vals));
|
||||
|
||||
let bare_value_list = value_list.map(|vals| (None, vals));
|
||||
|
||||
keyword_ci("insert")
|
||||
.ignore_then(keyword_ci("into"))
|
||||
.ignore_then(identifier())
|
||||
.then(choice((
|
||||
with_columns_and_values,
|
||||
with_values_keyword_only,
|
||||
bare_value_list,
|
||||
)))
|
||||
.map(|(table, (columns, values))| Command::Insert {
|
||||
table,
|
||||
columns,
|
||||
values,
|
||||
})
|
||||
}
|
||||
|
||||
/// `update <T> set <col>=<val>[, <col>=<val>...] (where <col>=<val> | --all-rows)`.
|
||||
fn update_parser<'a>()
|
||||
-> impl Parser<'a, &'a str, Command, extra::Err<Rich<'a, char>>> + Clone {
|
||||
let assignment = identifier()
|
||||
.then_ignore(just('=').padded())
|
||||
.then(value_literal());
|
||||
|
||||
let assignments = assignment
|
||||
.separated_by(just(',').padded())
|
||||
.at_least(1)
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
keyword_ci("update")
|
||||
.ignore_then(identifier())
|
||||
.then_ignore(keyword_ci("set"))
|
||||
.then(assignments)
|
||||
.then(filter_clause())
|
||||
.map(|((table, assignments), filter)| Command::Update {
|
||||
table,
|
||||
assignments,
|
||||
filter,
|
||||
})
|
||||
}
|
||||
|
||||
/// `delete from <T> (where <col>=<val> | --all-rows)`.
|
||||
fn delete_parser<'a>()
|
||||
-> impl Parser<'a, &'a str, Command, extra::Err<Rich<'a, char>>> + Clone {
|
||||
keyword_ci("delete")
|
||||
.ignore_then(keyword_ci("from"))
|
||||
.ignore_then(identifier())
|
||||
.then(filter_clause())
|
||||
.map(|(table, filter)| Command::Delete { table, filter })
|
||||
}
|
||||
|
||||
/// Parse the row-filter portion of UPDATE/DELETE: either
|
||||
/// `where <col>=<val>` or the `--all-rows` flag, with the two
|
||||
/// being mutually exclusive (specifying both is a parse error).
|
||||
fn filter_clause<'a>()
|
||||
-> impl Parser<'a, &'a str, RowFilter, extra::Err<Rich<'a, char>>> + Clone {
|
||||
let where_clause = keyword_ci("where")
|
||||
.ignore_then(identifier())
|
||||
.then_ignore(just('=').padded())
|
||||
.then(value_literal())
|
||||
.map(|(column, value)| RowFilter::Where { column, value });
|
||||
|
||||
let all_rows = just("--all-rows").padded().to(RowFilter::AllRows);
|
||||
|
||||
where_clause.or(all_rows).labelled("where clause or --all-rows")
|
||||
}
|
||||
|
||||
/// Parse a value literal: number, single-quoted string, `null`,
|
||||
/// `true`, or `false`.
|
||||
fn value_literal<'a>()
|
||||
-> impl Parser<'a, &'a str, Value, extra::Err<Rich<'a, char>>> + Clone {
|
||||
choice((
|
||||
keyword_ci("null").to(Value::Null),
|
||||
keyword_ci("true").to(Value::Bool(true)),
|
||||
keyword_ci("false").to(Value::Bool(false)),
|
||||
number_literal(),
|
||||
string_literal(),
|
||||
))
|
||||
.padded()
|
||||
}
|
||||
|
||||
fn number_literal<'a>()
|
||||
-> impl Parser<'a, &'a str, Value, extra::Err<Rich<'a, char>>> + Clone {
|
||||
let sign = just('-').or_not();
|
||||
let digits = any()
|
||||
.filter(|c: &char| c.is_ascii_digit())
|
||||
.repeated()
|
||||
.at_least(1)
|
||||
.collect::<String>();
|
||||
let fraction = just('.')
|
||||
.ignore_then(
|
||||
any()
|
||||
.filter(|c: &char| c.is_ascii_digit())
|
||||
.repeated()
|
||||
.at_least(1)
|
||||
.collect::<String>(),
|
||||
)
|
||||
.or_not();
|
||||
sign.then(digits)
|
||||
.then(fraction)
|
||||
.map(|((s, whole), frac)| {
|
||||
let mut out = String::new();
|
||||
if s.is_some() {
|
||||
out.push('-');
|
||||
}
|
||||
out.push_str(&whole);
|
||||
if let Some(f) = frac {
|
||||
out.push('.');
|
||||
out.push_str(&f);
|
||||
}
|
||||
Value::Number(out)
|
||||
})
|
||||
}
|
||||
|
||||
fn string_literal<'a>()
|
||||
-> impl Parser<'a, &'a str, Value, extra::Err<Rich<'a, char>>> + Clone {
|
||||
// Single-quoted SQL string. `''` inside the literal escapes
|
||||
// a literal single quote.
|
||||
let body = just('\'')
|
||||
.ignore_then(
|
||||
choice((
|
||||
just("''").to('\''),
|
||||
any().filter(|c: &char| *c != '\''),
|
||||
))
|
||||
.repeated()
|
||||
.collect::<String>(),
|
||||
)
|
||||
.then_ignore(just('\''));
|
||||
body.map(Value::Text)
|
||||
}
|
||||
|
||||
/// `add 1:n relationship [<name>] from <P>.<col> to <C>.<col>
|
||||
/// [on delete <action>] [on update <action>] [--create-fk]`.
|
||||
fn add_relationship_parser<'a>()
|
||||
@@ -805,6 +998,188 @@ mod tests {
|
||||
assert!(matches!(e, ParseError::Invalid { .. }), "got {e:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn insert_with_explicit_column_list() {
|
||||
assert_eq!(
|
||||
ok("insert into Customers (Name, Email) values ('Alice', 'a@b.com')"),
|
||||
Command::Insert {
|
||||
table: "Customers".to_string(),
|
||||
columns: Some(vec!["Name".to_string(), "Email".to_string()]),
|
||||
values: vec![
|
||||
Value::Text("Alice".to_string()),
|
||||
Value::Text("a@b.com".to_string()),
|
||||
],
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn insert_short_form_omitting_values_keyword() {
|
||||
// User typed `insert into T (vals)` without `values`.
|
||||
// Equivalent to `insert into T values (vals)`.
|
||||
assert_eq!(
|
||||
ok("insert into Customers ('Alice')"),
|
||||
Command::Insert {
|
||||
table: "Customers".to_string(),
|
||||
columns: None,
|
||||
values: vec![Value::Text("Alice".to_string())],
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn insert_short_form_without_column_list() {
|
||||
assert_eq!(
|
||||
ok("insert into Customers values ('Alice', 'a@b.com')"),
|
||||
Command::Insert {
|
||||
table: "Customers".to_string(),
|
||||
columns: None,
|
||||
values: vec![
|
||||
Value::Text("Alice".to_string()),
|
||||
Value::Text("a@b.com".to_string()),
|
||||
],
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn insert_accepts_mixed_value_kinds() {
|
||||
assert_eq!(
|
||||
ok("insert into T values (1, 3.14, 'hi', true, null)"),
|
||||
Command::Insert {
|
||||
table: "T".to_string(),
|
||||
columns: None,
|
||||
values: vec![
|
||||
Value::Number("1".to_string()),
|
||||
Value::Number("3.14".to_string()),
|
||||
Value::Text("hi".to_string()),
|
||||
Value::Bool(true),
|
||||
Value::Null,
|
||||
],
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn insert_supports_negative_numbers() {
|
||||
assert_eq!(
|
||||
ok("insert into T values (-5, -3.14)"),
|
||||
Command::Insert {
|
||||
table: "T".to_string(),
|
||||
columns: None,
|
||||
values: vec![
|
||||
Value::Number("-5".to_string()),
|
||||
Value::Number("-3.14".to_string()),
|
||||
],
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn string_literal_supports_escaped_single_quote() {
|
||||
// SQL convention: '' inside a quoted string is a literal '.
|
||||
assert_eq!(
|
||||
ok("insert into T values ('don''t panic')"),
|
||||
Command::Insert {
|
||||
table: "T".to_string(),
|
||||
columns: None,
|
||||
values: vec![Value::Text("don't panic".to_string())],
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn update_with_where() {
|
||||
assert_eq!(
|
||||
ok("update Customers set Name='Alice' where id=1"),
|
||||
Command::Update {
|
||||
table: "Customers".to_string(),
|
||||
assignments: vec![("Name".to_string(), Value::Text("Alice".to_string()))],
|
||||
filter: RowFilter::Where {
|
||||
column: "id".to_string(),
|
||||
value: Value::Number("1".to_string()),
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn update_with_multiple_assignments() {
|
||||
assert_eq!(
|
||||
ok("update Customers set Name='Alice', Email='a@b.com' where id=1"),
|
||||
Command::Update {
|
||||
table: "Customers".to_string(),
|
||||
assignments: vec![
|
||||
("Name".to_string(), Value::Text("Alice".to_string())),
|
||||
("Email".to_string(), Value::Text("a@b.com".to_string())),
|
||||
],
|
||||
filter: RowFilter::Where {
|
||||
column: "id".to_string(),
|
||||
value: Value::Number("1".to_string()),
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn update_with_all_rows_flag() {
|
||||
assert_eq!(
|
||||
ok("update Customers set Active=false --all-rows"),
|
||||
Command::Update {
|
||||
table: "Customers".to_string(),
|
||||
assignments: vec![("Active".to_string(), Value::Bool(false))],
|
||||
filter: RowFilter::AllRows,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn update_without_where_or_flag_errors() {
|
||||
let e = err("update Customers set Active=false");
|
||||
assert!(matches!(e, ParseError::Invalid { .. }), "got {e:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn delete_with_where() {
|
||||
assert_eq!(
|
||||
ok("delete from Customers where id=1"),
|
||||
Command::Delete {
|
||||
table: "Customers".to_string(),
|
||||
filter: RowFilter::Where {
|
||||
column: "id".to_string(),
|
||||
value: Value::Number("1".to_string()),
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn delete_with_all_rows_flag() {
|
||||
assert_eq!(
|
||||
ok("delete from Customers --all-rows"),
|
||||
Command::Delete {
|
||||
table: "Customers".to_string(),
|
||||
filter: RowFilter::AllRows,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn delete_without_where_or_flag_errors() {
|
||||
let e = err("delete from Customers");
|
||||
assert!(matches!(e, ParseError::Invalid { .. }), "got {e:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn show_data_command() {
|
||||
assert_eq!(
|
||||
ok("show data Customers"),
|
||||
Command::ShowData {
|
||||
name: "Customers".to_string()
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn drop_relationship_by_name() {
|
||||
assert_eq!(
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
//! Shortid generation and validation.
|
||||
//!
|
||||
//! Per ADR-0005, the `shortid` user-facing type is a base58
|
||||
//! random identifier of 10–12 characters with no ambiguous
|
||||
//! glyphs (no `0`/`O`/`I`/`l`). The generator is small enough
|
||||
//! to live in the DSL crate alongside the type definition.
|
||||
|
||||
use rand::RngExt;
|
||||
|
||||
/// Base58 alphabet — Bitcoin-style. 0 / O / I / l are excluded
|
||||
/// because they are easily confused in print.
|
||||
const ALPHABET: &[u8; 58] =
|
||||
b"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
||||
|
||||
const DEFAULT_LEN: usize = 10;
|
||||
|
||||
/// Length bounds accepted on user-supplied shortid values.
|
||||
pub const MIN_LEN: usize = 10;
|
||||
pub const MAX_LEN: usize = 12;
|
||||
|
||||
/// Generate a fresh shortid using thread-local RNG.
|
||||
#[must_use]
|
||||
pub fn generate() -> String {
|
||||
generate_len(DEFAULT_LEN)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
fn generate_len(len: usize) -> String {
|
||||
let mut rng = rand::rng();
|
||||
let mut out = String::with_capacity(len);
|
||||
for _ in 0..len {
|
||||
let idx = rng.random_range(0..ALPHABET.len());
|
||||
out.push(ALPHABET[idx] as char);
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Validate a user-supplied shortid value.
|
||||
pub fn validate(value: &str) -> Result<(), String> {
|
||||
if value.len() < MIN_LEN || value.len() > MAX_LEN {
|
||||
return Err(format!(
|
||||
"shortid must be {MIN_LEN}–{MAX_LEN} characters; got {} character(s)",
|
||||
value.chars().count()
|
||||
));
|
||||
}
|
||||
for c in value.chars() {
|
||||
if !ALPHABET.contains(&(c as u8)) {
|
||||
return Err(format!(
|
||||
"shortid contains '{c}', which is not in the base58 alphabet \
|
||||
(no 0, O, I, or l; ASCII letters and digits otherwise)"
|
||||
));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
#[test]
|
||||
fn generated_ids_have_default_length() {
|
||||
let id = generate();
|
||||
assert_eq!(id.len(), DEFAULT_LEN);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn generated_ids_use_only_base58_alphabet() {
|
||||
for _ in 0..100 {
|
||||
let id = generate();
|
||||
for c in id.chars() {
|
||||
assert!(
|
||||
ALPHABET.contains(&(c as u8)),
|
||||
"char {c:?} not in base58 alphabet"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn generated_ids_are_not_all_identical() {
|
||||
// Probabilistically extremely unlikely with a good RNG;
|
||||
// catches a wholly broken generator (constant output).
|
||||
let a = generate();
|
||||
let b = generate();
|
||||
let c = generate();
|
||||
assert!(
|
||||
a != b || b != c,
|
||||
"all three generated ids were identical: {a}, {b}, {c}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validate_accepts_well_formed_values() {
|
||||
assert!(validate("23456789Ab").is_ok()); // 10 chars
|
||||
assert!(validate("23456789AbCD").is_ok()); // 12 chars
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validate_rejects_too_short_or_too_long() {
|
||||
let err = validate("short").unwrap_err();
|
||||
assert!(err.contains("characters"));
|
||||
let err = validate("waytoolongafornow").unwrap_err();
|
||||
assert!(err.contains("characters"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validate_rejects_ambiguous_glyphs() {
|
||||
for bad in ["0aaaaaaaaa", "Oaaaaaaaaa", "Iaaaaaaaaa", "laaaaaaaaa"] {
|
||||
let err = validate(bad).unwrap_err();
|
||||
assert!(err.contains("base58"), "for {bad}: {err}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,390 @@
|
||||
//! User-facing value literals for INSERT / UPDATE / DELETE.
|
||||
//!
|
||||
//! The parser produces a small `Value` enum carrying just the
|
||||
//! shape of the literal as written. Per-column-type validation
|
||||
//! happens at execute time, where the schema is known and
|
||||
//! errors can name the offending column.
|
||||
|
||||
use std::fmt;
|
||||
|
||||
use crate::dsl::shortid;
|
||||
use crate::dsl::types::Type;
|
||||
|
||||
/// A literal value as parsed from DSL input.
|
||||
///
|
||||
/// `Number` carries the original string so a literal like
|
||||
/// `3.14` can be stored as a decimal (TEXT) or a real (f64)
|
||||
/// depending on the destination column. The conversion happens
|
||||
/// in `bind_for_column`.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum Value {
|
||||
Number(String),
|
||||
Text(String),
|
||||
Bool(bool),
|
||||
Null,
|
||||
}
|
||||
|
||||
impl fmt::Display for Value {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::Number(n) => f.write_str(n),
|
||||
Self::Text(s) => write!(f, "'{}'", s.replace('\'', "''")),
|
||||
Self::Bool(b) => f.write_str(if *b { "true" } else { "false" }),
|
||||
Self::Null => f.write_str("null"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Validated value ready to be bound as a parameter to a SQLite
|
||||
/// statement. Mirrors the storage choices made in ADR-0005.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum Bound {
|
||||
Integer(i64),
|
||||
Real(f64),
|
||||
Text(String),
|
||||
Null,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
|
||||
pub enum ValueError {
|
||||
#[error("column `{column}` expects {expected_human}, got {got}")]
|
||||
TypeMismatch {
|
||||
column: String,
|
||||
expected_human: String,
|
||||
got: String,
|
||||
},
|
||||
#[error("column `{column}`: {message}")]
|
||||
Format { column: String, message: String },
|
||||
}
|
||||
|
||||
impl Value {
|
||||
/// Validate `self` against `column`'s user-facing type and
|
||||
/// produce a value ready for binding.
|
||||
pub fn bind_for_column(&self, column: &str, ty: Type) -> Result<Bound, ValueError> {
|
||||
if matches!(self, Self::Null) {
|
||||
return Ok(Bound::Null);
|
||||
}
|
||||
match ty {
|
||||
Type::Text | Type::ShortId => self.bind_text(column, ty),
|
||||
Type::Int | Type::Serial => self.bind_int(column, ty),
|
||||
Type::Real => self.bind_real(column),
|
||||
Type::Decimal => self.bind_decimal(column),
|
||||
Type::Bool => self.bind_bool(column),
|
||||
Type::Date => self.bind_date(column),
|
||||
Type::DateTime => self.bind_datetime(column),
|
||||
Type::Blob => Err(ValueError::Format {
|
||||
column: column.to_string(),
|
||||
message: "literal `blob` values are not supported in DSL yet".to_string(),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
fn bind_text(&self, column: &str, ty: Type) -> Result<Bound, ValueError> {
|
||||
match self {
|
||||
Self::Text(s) => {
|
||||
if ty == Type::ShortId {
|
||||
shortid::validate(s).map_err(|message| ValueError::Format {
|
||||
column: column.to_string(),
|
||||
message,
|
||||
})?;
|
||||
}
|
||||
Ok(Bound::Text(s.clone()))
|
||||
}
|
||||
other => Err(ValueError::TypeMismatch {
|
||||
column: column.to_string(),
|
||||
expected_human: format!("a quoted string for `{ty}`"),
|
||||
got: other.kind_name().to_string(),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
fn bind_int(&self, column: &str, ty: Type) -> Result<Bound, ValueError> {
|
||||
match self {
|
||||
Self::Number(n) => n
|
||||
.parse::<i64>()
|
||||
.map(Bound::Integer)
|
||||
.map_err(|_| ValueError::Format {
|
||||
column: column.to_string(),
|
||||
message: format!("`{n}` is not a valid {ty} (whole number expected)"),
|
||||
}),
|
||||
other => Err(ValueError::TypeMismatch {
|
||||
column: column.to_string(),
|
||||
expected_human: format!("a whole number for `{ty}`"),
|
||||
got: other.kind_name().to_string(),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
fn bind_real(&self, column: &str) -> Result<Bound, ValueError> {
|
||||
match self {
|
||||
Self::Number(n) => n
|
||||
.parse::<f64>()
|
||||
.map(Bound::Real)
|
||||
.map_err(|_| ValueError::Format {
|
||||
column: column.to_string(),
|
||||
message: format!("`{n}` is not a valid real number"),
|
||||
}),
|
||||
other => Err(ValueError::TypeMismatch {
|
||||
column: column.to_string(),
|
||||
expected_human: "a real number".to_string(),
|
||||
got: other.kind_name().to_string(),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
fn bind_decimal(&self, column: &str) -> Result<Bound, ValueError> {
|
||||
match self {
|
||||
Self::Number(n) => {
|
||||
// Validate parse-ability so a typo like `3..14` is rejected;
|
||||
// we still store the original string to preserve precision.
|
||||
if n.parse::<f64>().is_err() {
|
||||
return Err(ValueError::Format {
|
||||
column: column.to_string(),
|
||||
message: format!("`{n}` is not a valid decimal number"),
|
||||
});
|
||||
}
|
||||
Ok(Bound::Text(n.clone()))
|
||||
}
|
||||
other => Err(ValueError::TypeMismatch {
|
||||
column: column.to_string(),
|
||||
expected_human: "a decimal number".to_string(),
|
||||
got: other.kind_name().to_string(),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
fn bind_bool(&self, column: &str) -> Result<Bound, ValueError> {
|
||||
match self {
|
||||
Self::Bool(b) => Ok(Bound::Integer(i64::from(*b))),
|
||||
other => Err(ValueError::TypeMismatch {
|
||||
column: column.to_string(),
|
||||
expected_human: "`true` or `false`".to_string(),
|
||||
got: other.kind_name().to_string(),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
fn bind_date(&self, column: &str) -> Result<Bound, ValueError> {
|
||||
match self {
|
||||
Self::Text(s) => {
|
||||
validate_date(s).map_err(|message| ValueError::Format {
|
||||
column: column.to_string(),
|
||||
message,
|
||||
})?;
|
||||
Ok(Bound::Text(s.clone()))
|
||||
}
|
||||
other => Err(ValueError::TypeMismatch {
|
||||
column: column.to_string(),
|
||||
expected_human: "a quoted date 'YYYY-MM-DD'".to_string(),
|
||||
got: other.kind_name().to_string(),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
fn bind_datetime(&self, column: &str) -> Result<Bound, ValueError> {
|
||||
match self {
|
||||
Self::Text(s) => {
|
||||
validate_datetime(s).map_err(|message| ValueError::Format {
|
||||
column: column.to_string(),
|
||||
message,
|
||||
})?;
|
||||
Ok(Bound::Text(s.clone()))
|
||||
}
|
||||
other => Err(ValueError::TypeMismatch {
|
||||
column: column.to_string(),
|
||||
expected_human: "a quoted datetime 'YYYY-MM-DDTHH:MM:SS'".to_string(),
|
||||
got: other.kind_name().to_string(),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
const fn kind_name(&self) -> &'static str {
|
||||
match self {
|
||||
Self::Number(_) => "number",
|
||||
Self::Text(_) => "string",
|
||||
Self::Bool(_) => "boolean",
|
||||
Self::Null => "null",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_date(s: &str) -> Result<(), String> {
|
||||
// Expect YYYY-MM-DD: 10 chars, two dashes at fixed positions.
|
||||
let bytes = s.as_bytes();
|
||||
if bytes.len() != 10 || bytes[4] != b'-' || bytes[7] != b'-' {
|
||||
return Err(format!(
|
||||
"`{s}` is not a date in `YYYY-MM-DD` form"
|
||||
));
|
||||
}
|
||||
let year = parse_digits(&s[0..4]).ok_or_else(|| format!("`{s}`: invalid year"))?;
|
||||
let month = parse_digits(&s[5..7]).ok_or_else(|| format!("`{s}`: invalid month"))?;
|
||||
let day = parse_digits(&s[8..10]).ok_or_else(|| format!("`{s}`: invalid day"))?;
|
||||
if !(1..=9999).contains(&year) {
|
||||
return Err(format!("`{s}`: year {year} out of range 1..=9999"));
|
||||
}
|
||||
if !(1..=12).contains(&month) {
|
||||
return Err(format!("`{s}`: month {month} out of range 1..=12"));
|
||||
}
|
||||
if !(1..=31).contains(&day) {
|
||||
return Err(format!("`{s}`: day {day} out of range 1..=31"));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn validate_datetime(s: &str) -> Result<(), String> {
|
||||
// Minimum: YYYY-MM-DDTHH:MM:SS = 19 chars. Allow optional
|
||||
// fractional seconds (.fff) and optional Z or ±HH:MM offset.
|
||||
if s.len() < 19 {
|
||||
return Err(format!(
|
||||
"`{s}` is too short for a datetime in `YYYY-MM-DDTHH:MM:SS` form"
|
||||
));
|
||||
}
|
||||
let date_part = &s[0..10];
|
||||
validate_date(date_part)?;
|
||||
let bytes = s.as_bytes();
|
||||
if bytes[10] != b'T' {
|
||||
return Err(format!("`{s}`: missing `T` separator between date and time"));
|
||||
}
|
||||
if bytes[13] != b':' || bytes[16] != b':' {
|
||||
return Err(format!("`{s}`: time portion must be `HH:MM:SS`"));
|
||||
}
|
||||
let hour = parse_digits(&s[11..13]).ok_or_else(|| format!("`{s}`: invalid hour"))?;
|
||||
let min = parse_digits(&s[14..16]).ok_or_else(|| format!("`{s}`: invalid minute"))?;
|
||||
let sec = parse_digits(&s[17..19]).ok_or_else(|| format!("`{s}`: invalid second"))?;
|
||||
if hour > 23 {
|
||||
return Err(format!("`{s}`: hour {hour} out of range 0..=23"));
|
||||
}
|
||||
if min > 59 {
|
||||
return Err(format!("`{s}`: minute {min} out of range 0..=59"));
|
||||
}
|
||||
if sec > 60 {
|
||||
return Err(format!(
|
||||
"`{s}`: second {sec} out of range 0..=60 (60 allowed for leap second)"
|
||||
));
|
||||
}
|
||||
// Anything after position 19 is optional fractional / timezone
|
||||
// suffix; we don't strictly validate it here (a future iteration
|
||||
// can tighten this if needed).
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn parse_digits(s: &str) -> Option<u32> {
|
||||
if s.is_empty() || !s.chars().all(|c| c.is_ascii_digit()) {
|
||||
return None;
|
||||
}
|
||||
s.parse::<u32>().ok()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
fn n(s: &str) -> Value {
|
||||
Value::Number(s.to_string())
|
||||
}
|
||||
fn t(s: &str) -> Value {
|
||||
Value::Text(s.to_string())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn null_binds_to_null_for_any_type() {
|
||||
for ty in Type::all() {
|
||||
// Skip blob — null still works there too.
|
||||
assert_eq!(Value::Null.bind_for_column("c", *ty).unwrap(), Bound::Null);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn integer_for_int_column() {
|
||||
assert_eq!(n("42").bind_for_column("c", Type::Int).unwrap(), Bound::Integer(42));
|
||||
assert_eq!(n("-7").bind_for_column("c", Type::Int).unwrap(), Bound::Integer(-7));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn non_integer_for_int_column_is_format_error() {
|
||||
let err = n("3.14").bind_for_column("c", Type::Int).unwrap_err();
|
||||
match err {
|
||||
ValueError::Format { message, .. } => assert!(message.contains("whole number")),
|
||||
other => panic!("unexpected: {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn string_for_int_column_is_type_mismatch() {
|
||||
let err = t("hello").bind_for_column("c", Type::Int).unwrap_err();
|
||||
assert!(matches!(err, ValueError::TypeMismatch { .. }));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn text_for_text_column() {
|
||||
assert_eq!(
|
||||
t("hi").bind_for_column("c", Type::Text).unwrap(),
|
||||
Bound::Text("hi".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shortid_validation_runs_on_text_for_shortid_column() {
|
||||
let err = t("toolong_xyz_more").bind_for_column("c", Type::ShortId).unwrap_err();
|
||||
assert!(matches!(err, ValueError::Format { .. }));
|
||||
|
||||
// Well-formed shortid binds fine.
|
||||
assert_eq!(
|
||||
t("23456789Ab").bind_for_column("c", Type::ShortId).unwrap(),
|
||||
Bound::Text("23456789Ab".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bool_for_bool_column_maps_to_zero_or_one() {
|
||||
assert_eq!(Value::Bool(true).bind_for_column("c", Type::Bool).unwrap(), Bound::Integer(1));
|
||||
assert_eq!(Value::Bool(false).bind_for_column("c", Type::Bool).unwrap(), Bound::Integer(0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn date_iso_only() {
|
||||
assert_eq!(
|
||||
t("2025-01-15").bind_for_column("c", Type::Date).unwrap(),
|
||||
Bound::Text("2025-01-15".to_string())
|
||||
);
|
||||
let err = t("2025/01/15").bind_for_column("c", Type::Date).unwrap_err();
|
||||
assert!(matches!(err, ValueError::Format { .. }));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn date_range_check() {
|
||||
let err = t("2025-13-01").bind_for_column("c", Type::Date).unwrap_err();
|
||||
assert!(matches!(err, ValueError::Format { message, .. } if message.contains("month")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn datetime_iso_only() {
|
||||
assert_eq!(
|
||||
t("2025-01-15T14:30:00")
|
||||
.bind_for_column("c", Type::DateTime)
|
||||
.unwrap(),
|
||||
Bound::Text("2025-01-15T14:30:00".to_string())
|
||||
);
|
||||
let err = t("2025-01-15 14:30:00")
|
||||
.bind_for_column("c", Type::DateTime)
|
||||
.unwrap_err();
|
||||
assert!(matches!(err, ValueError::Format { .. }));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decimal_validates_numeric_string() {
|
||||
assert_eq!(
|
||||
n("3.14").bind_for_column("c", Type::Decimal).unwrap(),
|
||||
Bound::Text("3.14".to_string())
|
||||
);
|
||||
let err = n("3..14").bind_for_column("c", Type::Decimal).unwrap_err();
|
||||
assert!(matches!(err, ValueError::Format { .. }));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn blob_inserts_are_explicitly_unsupported_for_now() {
|
||||
let err = t("0xdead").bind_for_column("c", Type::Blob).unwrap_err();
|
||||
assert!(matches!(err, ValueError::Format { message, .. } if message.contains("blob")));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user