1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
//! kevy-sql — the OUT-OF-ENGINE, declaration-time SQL compiler.
//!
//! This tool compiles a schema ONCE, at declaration time, into explicit
//! kevy engine commands (`TABLE.DECLARE`, `VIEW.CREATE`) plus *query
//! cards* — ready-made `IDX.QUERY` templates an application binds
//! parameters into at runtime. It is a build step, like a schema
//! migration tool. **Nothing here ever runs per-query inside a serving
//! process**: ad-hoc runtime SQL stays refused by the engine itself
//! (unknown command), and this compiler refuses — by name, with
//! line/column — every SQL construct that would need query-time
//! evaluation (JOIN, subqueries, GROUP BY, expressions…). That is
//! kevy's Law 3: meaning and planning never enter the engine.
//!
//! The honest pitch: **your schema's access paths, compiled — not a
//! drop-in PG.** `CREATE TABLE` becomes a typed, verifiable
//! `TABLE.DECLARE`; `CREATE [UNIQUE] INDEX` becomes declared Range /
//! Unique / composite ORDERPATH access paths; single-table
//! `CREATE VIEW … AS SELECT` becomes either an engine view (constant
//! predicates) or a query card (parameterized / clause-bearing). The
//! compiler never plans: a WHERE clause either matches a declared
//! access path (leading-prefix rule) or the compile errors naming the
//! exact declaration to add.
//!
//! ```
//! let sql = "
//! CREATE TABLE users (id bigint PRIMARY KEY, email text);
//! CREATE UNIQUE INDEX ON users (email);
//! ";
//! let c = kevy_sql::compile(sql).unwrap();
//! assert_eq!(c.commands[0][0], "TABLE.DECLARE");
//! println!("{}", c.render_script());
//! ```
/// A compile error, anchored to the source: `line N, col C: message`.
///
/// Every refusal is *named* and teaches the kevy-shaped alternative —
/// e.g. `JOIN is not compilable — kevy refuses query-time joins
/// (Law 3); model the lookup with an indexed FK column …`.
///
/// ```
/// let e = kevy_sql::compile("CREATE TABLE t (id bigint PRIMARY KEY, x money);").unwrap_err();
/// assert_eq!((e.line, e.col), (1, 40));
/// assert!(e.to_string().starts_with("line 1, col 40: "));
/// ```
/// One `$N` parameter slot of a [`QueryCard`], with the column it binds
/// and that column's declared type.
///
/// ```
/// let c = kevy_sql::compile(
/// "CREATE TABLE orders (id bigint PRIMARY KEY, user_id bigint);
/// CREATE INDEX ON orders (user_id);
/// CREATE VIEW by_user AS SELECT * FROM orders WHERE user_id = $1;",
/// )?;
/// let p = &c.query_cards[0].params[0];
/// assert_eq!((p.n, p.column.as_str(), p.ty), (1, "user_id", kevy_sql::ValType::I64));
/// # Ok::<(), kevy_sql::SqlError>(())
/// ```
/// A compiled runtime template: the exact `IDX.QUERY …` argv with `$N`
/// slots left in place. The application substitutes real values for the
/// slots and sends the argv as-is — there is no runtime SQL.
///
/// ```
/// let c = kevy_sql::compile(
/// "CREATE TABLE orders (id bigint PRIMARY KEY, user_id bigint);
/// CREATE INDEX ON orders (user_id);
/// CREATE VIEW by_user AS SELECT * FROM orders WHERE user_id = $1;",
/// )?;
/// let card = &c.query_cards[0];
/// // bind $1 = 42, then send the argv as-is
/// let argv: Vec<String> =
/// card.argv.iter().map(|a| if a == "$1" { "42".into() } else { a.clone() }).collect();
/// assert_eq!(argv[..4], ["IDX.QUERY", "orders.user_id", "EQ", "42"]);
/// # Ok::<(), kevy_sql::SqlError>(())
/// ```
/// The result of [`compile`]: engine commands, query cards, and notes.
///
/// ```
/// let c = kevy_sql::compile(
/// "CREATE TABLE orders (id bigint PRIMARY KEY, user_id bigint);
/// CREATE INDEX ON orders (user_id);
/// CREATE VIEW mine AS SELECT * FROM orders WHERE user_id = 7;
/// CREATE VIEW by_user AS SELECT * FROM orders WHERE user_id = $1;",
/// )?;
/// assert_eq!(c.commands.len(), 2); // TABLE.DECLARE, then VIEW.CREATE
/// assert_eq!(c.query_cards.len(), 1);
/// # Ok::<(), kevy_sql::SqlError>(())
/// ```
pub use DeclarationError;
pub use ;
pub use ValType;
pub use Scalar;
pub use ;
pub use ;
/// Compile a whole SQL schema file into a [`Compilation`].
///
/// Whole-file semantics: statements accumulate per table (a table's
/// `CREATE INDEX`es 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)"));
/// # Ok::<(), kevy_sql::SqlError>(())
/// ```
const _: = ;