Skip to main content

compile

Function compile 

Source
pub fn compile(sql: &str) -> Result<Compilation, SqlError>
Expand description

Compile a whole SQL schema file into a Compilation.

Whole-file semantics: statements accumulate per table (a table’s CREATE INDEXes fold into its single TABLE.DECLARE), then each CREATE VIEW is checked against the table’s declared access paths. The compiler never plans — a view whose WHERE has no declared path errors naming the exact CREATE INDEX to add.

let c = kevy_sql::compile(
    "CREATE TABLE orders (id bigint PRIMARY KEY, status text);
     CREATE INDEX ON orders (status);
     CREATE VIEW paid AS SELECT * FROM orders WHERE status = 'paid';",
)?;
assert_eq!(c.commands[1][..2], ["VIEW.CREATE", "paid"]);

// without the index the view has no access path, and the error says what to add
let e = kevy_sql::compile(
    "CREATE TABLE orders (id bigint PRIMARY KEY, status text);
     CREATE VIEW paid AS SELECT * FROM orders WHERE status = 'paid';",
)
.unwrap_err();
assert!(e.message.contains("add: CREATE INDEX ON orders (status)"));