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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
use TokenStream;
/// const CREATE_INDEX_SQL: &'static str = "CREATE INDEX ..."
///
/// Create index for every column with a [foreign-key-clause](https://www.sqlite.org/syntax/foreign-key-clause.html).\
/// Works only if constraint is in all caps, lowercase serves as escape hatch.
///
/// ```rust
/// # use wb_sqlite::CreateIndexSql;
/// #[derive(CreateIndexSql)]
/// struct Cat {
/// #[sql(constraint = "PRIMARY KEY")]
/// name: String,
/// #[sql(constraint = "REFERENCES parent(id) ON UPDATE RESTRICT ON DELETE RESTRICT")]
/// mother: i64,
/// // lowercase first char prevents generation
/// #[sql(constraint = "rEFERENCES parent(id) ON UPDATE RESTRICT ON DELETE RESTRICT")]
/// father: i64,
/// #[sql(constraint = "REFERENCES human(id) ON UPDATE RESTRICT ON DELETE RESTRICT")]
/// owner: i64,
/// }
/// assert_eq!(
/// Cat::CREATE_INDEX_SQL,
/// concat!(
/// "CREATE INDEX IF NOT EXISTS cat_mother_idx ON cat(mother); ",
/// "CREATE INDEX IF NOT EXISTS cat_owner_idx ON cat(owner); "
/// )
/// );
/// ```
/// const CREATE_TABLE_SQL: &'static str = "CREATE TABLE ..."
///
/// `"CREATE TABLE IF NOT EXISTS {tab_name} ({col_defs}{tab_constraint}) STRICT{tab_option};"`\
/// `col_defs = {field_name} {col_typ} {col_constraint},`
///
/// ## Struct attributes
///
/// #[sql(
/// constraint = "[table constraint](https://www.sqlite.org/syntax/table-constraint.html)",
/// option = "[table option](https://www.sqlite.org/syntax/table-options.html)"
/// )]
///
/// ## Field attributes
///
/// #[sql(
/// typ = "[datatype](https://www.sqlite.org/stricttables.html)",
/// constraint = "[column constraint](https://www.sqlite.org/syntax/column-constraint.html)"
/// )]
///
/// ## Table Name creation: PascalCase with digits as lowercase to snake_case
///
/// ```rust
/// # use wb_sqlite::CreateTableSql;
/// # #[derive(CreateTableSql)]
/// struct MyDog {
/// # name: String,
/// # }
/// # assert_eq!(
/// # MyDog::CREATE_TABLE_SQL,
/// "CREATE TABLE IF NOT EXISTS my_dog (name TEXT NOT NULL) STRICT;"
/// # );
/// # assert!(rusqlite::Connection::open_in_memory().unwrap().execute_batch(MyDog::CREATE_TABLE_SQL).is_ok());
///
/// # #[derive(CreateTableSql)]
/// struct M2yDog {
/// # name: String,
/// # }
/// # assert_eq!(
/// # M2yDog::CREATE_TABLE_SQL,
/// "CREATE TABLE IF NOT EXISTS m2y_dog (name TEXT NOT NULL) STRICT;"
/// # );
/// # assert!(rusqlite::Connection::open_in_memory().unwrap().execute_batch(M2yDog::CREATE_TABLE_SQL).is_ok());
///
/// # #[derive(CreateTableSql)]
/// struct My2Dog {
/// # name: String,
/// # }
/// # assert_eq!(
/// # My2Dog::CREATE_TABLE_SQL,
/// "CREATE TABLE IF NOT EXISTS my2_dog (name TEXT NOT NULL) STRICT;"
/// # );
/// # assert!(rusqlite::Connection::open_in_memory().unwrap().execute_batch(My2Dog::CREATE_TABLE_SQL).is_ok());
///
/// # #[derive(CreateTableSql)]
/// struct MyD2og {
/// # name: String,
/// # }
/// # assert_eq!(
/// # MyD2og::CREATE_TABLE_SQL,
/// "CREATE TABLE IF NOT EXISTS my_d2og (name TEXT NOT NULL) STRICT;"
/// # );
/// # assert!(rusqlite::Connection::open_in_memory().unwrap().execute_batch(MyD2og::CREATE_TABLE_SQL).is_ok());
/// # #[derive(CreateTableSql)]
///
/// struct MyDo2g {
/// # name: String,
/// # }
/// # assert_eq!(
/// # MyDo2g::CREATE_TABLE_SQL,
/// "CREATE TABLE IF NOT EXISTS my_do2g (name TEXT NOT NULL) STRICT;"
/// # );
/// # assert!(rusqlite::Connection::open_in_memory().unwrap().execute_batch(MyDo2g::CREATE_TABLE_SQL).is_ok());
/// ```
///
/// ## Example
///
/// ```rust
/// # use wb_sqlite::CreateTableSql;
/// #[derive(CreateTableSql)]
/// #[sql(constraint = "UNIQUE(vendor,brand)")]
/// struct WineBottle {
/// #[sql(constraint = "PRIMARY KEY")]
/// id: i64,
/// #[sql(constraint = "UNIQUE")]
/// serial_no: Option<String>,
/// #[sql(constraint = "REFERENCES vendor(id) ON UPDATE RESTRICT ON DELETE RESTRICT")]
/// vendor: i64,
/// #[sql(constraint = "CHECK(volume > 0)")]
/// volume: f64,
/// #[sql(constraint = "DEFAULT 'red'")]
/// color: String,
/// brand: Option<String>,
/// #[sql(typ = "ANY")]
/// data: Option<Vec<u8>>
/// }
/// assert_eq!(
/// WineBottle::CREATE_TABLE_SQL,
/// concat!(
/// "CREATE TABLE IF NOT EXISTS wine_bottle (id INTEGER NOT NULL PRIMARY KEY, ",
/// "serial_no TEXT UNIQUE, ",
/// "vendor INTEGER NOT NULL REFERENCES vendor(id) ON UPDATE RESTRICT ON DELETE RESTRICT, ",
/// "volume REAL NOT NULL CHECK(volume > 0), ",
/// "color TEXT NOT NULL DEFAULT 'red', ",
/// "brand TEXT, data ANY, UNIQUE(vendor,brand)) STRICT;",
/// ));
/// # assert!(rusqlite::Connection::open_in_memory().unwrap().execute_batch(WineBottle::CREATE_TABLE_SQL).is_ok());
/// ```
///
/// ## SQLite / Rust type mapping
///
/// `INTEGER NOT NULL = bool, u8, u16, u32, i8, i16, i32, i64`\
/// `REAL NOT NULL = f32, f64`\
/// `TEXT NOT NULL = &str, String`\
/// `BLOB NOT NULL = &[u8], Vec<u8>`\
/// `INTEGER = Option<bool>, Option<u8>, Option<u16> ... Option<i64>`\
/// `REAL = Option<f32>, Option<f64>`\
/// `TEXT = Option<String>`\
/// `BLOB = Option<Vec<u8>>`\
/// `ANY = all other`
/// const CREATE_TABLE_LOG_SQL: &'static str = "CREATE ..."
///
/// Create logging-table + trigger to log all table row modifications to the logging-table.
///
/// ```rust
/// # use wb_sqlite::CreateTableLogSql;
/// #[derive(CreateTableLogSql)]
/// struct FavoritePet {
/// #[sql(constraint = "PRIMARY KEY")]
/// id: i64,
/// name: String,
/// }
/// assert_eq!(
/// FavoritePet::CREATE_TABLE_LOG_SQL,
/// concat!(
/// "CREATE TABLE IF NOT EXISTS favorite_pet_log (id INTEGER NOT NULL, name TEXT NOT NULL) STRICT; ",
/// "CREATE INDEX IF NOT EXISTS favorite_pet_log_id_idx ON favorite_pet_log(id); ",
/// "CREATE TRIGGER IF NOT EXISTS favorite_pet_update UPDATE ON favorite_pet ",
/// "BEGIN INSERT INTO favorite_pet_log (id,name) VALUES (OLD.id,OLD.name); END; ",
/// "CREATE TRIGGER IF NOT EXISTS favorite_pet_delete DELETE ON favorite_pet ",
/// "BEGIN INSERT INTO favorite_pet_log (id,name) VALUES (OLD.id,OLD.name); END;"
/// )
/// );
/// ```
/// const SELECT_SQL : &'static str = "SELECT ..."
///
/// "SELECT {columns} FROM {tab_name}"
///
/// ```rust
/// # use wb_sqlite::SelectSql;
/// #[derive(SelectSql)]
/// struct Car {
/// #[sql(constraint = "PRIMARY KEY")]
/// id: i64,
/// model: String,
/// length: f64,
/// height: f64,
/// width: f64,
/// }
/// assert_eq!(
/// Car::SELECT_SQL,
/// "SELECT id,model,length,height,width FROM car"
/// );
/// ```
/// const SELECT_AS_SQL : &'static str = "SELECT ..."
///
/// Create a SELECT Statment with aliasing via AS.
///
/// ## Struct attributes
///
/// #[sqlas(from = "[select stmt after from](https://www.sqlite.org/lang_select.html)")]
///
/// if omitted defaults to {tab_name}
///
/// ## Field attributes
///
/// #[sqlas(col = "result column")]
///
/// if ommited defaults to {col_name}
///
/// ```rust
/// # use wb_sqlite::SelectAsSql;
/// #[derive(SelectAsSql)]
/// #[sqlas(from = "old_table1 AS t1 INNER JOIN old_table2 AS t2 on t1.pk = t2.fk")]
/// struct NewTable {
/// id: i64,
/// #[sqlas(col = "t1.moniker")]
/// name: String,
/// #[sqlas(col = "t2.description")]
/// info: String,
/// note: String,
/// #[sqlas(col = "t1.pressure_psi * 0.068947573")]
/// pressure_bar: f64,
/// }
/// assert_eq!(
/// NewTable::SELECT_AS_SQL,
/// concat!(
/// "SELECT id,t1.moniker AS name,t2.description AS info,note,",
/// "t1.pressure_psi * 0.068947573 AS pressure_bar FROM ",
/// "old_table1 AS t1 INNER JOIN old_table2 AS t2 on t1.pk = t2.fk"
/// )
/// );
/// ```
/// fn get_by_{field-name}({field-name}: {field-type}, exec: impl sqlx::SqliteExecutor<'_ >) -> Result<Self, sqlx::Error>
///
/// Generate fn for PRIMARY KEY and every UNIQUE constraint.\
/// Works only if constraint is in all caps, lowercase serves as escape hatch.
///
/// ```rust
/// # use wb_sqlite::{Get,CreateTableSql,Insert};
/// #[derive(CreateTableSql,Get,Insert,sqlx::FromRow)]
/// struct Cat {
/// #[sql(constraint = "PRIMARY KEY")]
/// id: i64,
/// #[sql(constraint = "UNIQUE")]
/// name: String,
/// #[sql(constraint = "uNIQUE")]
/// owner: String,
/// }
/// #[tokio::main(flavor = "current_thread")]
/// async fn main() -> Result<(), sqlx::Error> {
/// use sqlx::{Connection, Executor};
/// let mut conn = sqlx::SqliteConnection::connect(":memory:").await?;
/// conn.execute(Cat::CREATE_TABLE_SQL).await?;
///
/// let c = Cat {
/// id: 0,
/// name: "meouw".to_owned(),
/// owner: "nobody".to_owned()
/// };
/// let id = c.insert(&mut conn).await?;
/// assert!(id > 0);
///
/// let c2 = Cat::get_by_id(1,&mut conn).await?;
/// assert_eq!(c2.name,"meouw");
/// let c3 = Cat::get_by_name("meouw",&mut conn).await?;
/// assert_eq!(c3.owner,"nobody");
///
/// // there is no fn get_by_owner
/// // because the constraint contains lowercase-chars
///
/// Ok(())
/// }
/// ```
/// fn insert(&self, exec: impl sqlx::SqliteExecutor<'_ >) -> Result<i64, sqlx::Error>
///
/// Generate fn for INSERT with sqlx.
///
/// If there is a PRIMARY KEY and the rust-type is i64 then the insert depends on the value of the pk.\
/// If pk > 0 then do a full insert including the pk, else do a insert without the pk so sqlite assigns a new one,
/// which is returned as result.
///
/// Without PRIMARY KEY or rust-type != i64 do a full insert and return the last_insert_rowid.
///
/// PRIMARY KEY detection works only if constraint is in all caps, lowercase serves as escape hatch.
///
/// ```rust
/// # use wb_sqlite::{CreateTableSql,Insert};
/// #[derive(CreateTableSql,Insert)]
/// struct Cat {
/// #[sql(constraint = "PRIMARY KEY")]
/// id: i64,
/// name: String,
/// }
/// #[tokio::main(flavor = "current_thread")]
/// async fn main() -> Result<(), sqlx::Error> {
/// use sqlx::{Connection, Executor};
/// let mut conn = sqlx::SqliteConnection::connect(":memory:").await?;
/// conn.execute(Cat::CREATE_TABLE_SQL).await?;
///
/// let c = Cat {
/// id: 0,
/// name: "miau".to_owned(),
/// };
/// let id = c.insert(&mut conn).await?;
/// assert!(id == 1);
///
/// Ok(())
/// }
/// ```
/// fn insert_sync(&self, conn: &rusqlite::Connection) -> Result<i64, rusqlite::Error>
///
/// Generate fn for INSERT with rusqlite.
///
/// If there is a PRIMARY KEY and the rust-type is i64 then the insert depends on the value of the pk.\
/// If pk > 0 then do a full insert including the pk, else do a insert without the pk so sqlite assigns a new one,
/// which is returned as result.
///
/// Without PRIMARY KEY or rust-type != i64 do a full insert and return the last_insert_rowid.
///
/// PRIMARY KEY detection works only if constraint is in all caps, lowercase serves as escape hatch.
///
/// ```rust
/// # use wb_sqlite::{CreateTableSql,InsertSync};
/// #[derive(CreateTableSql,InsertSync)]
/// struct Cat {
/// #[sql(constraint = "PRIMARY KEY")]
/// id: i64,
/// name: String,
/// }
/// fn main() -> Result<(), rusqlite::Error> {
/// let conn = rusqlite::Connection::open_in_memory()?;
/// conn.execute_batch(Cat::CREATE_TABLE_SQL)?;
///
/// let c = Cat {
/// id: 0,
/// name: "miau".to_owned(),
/// };
/// let id = c.insert_sync(&conn)?;
/// assert!(id == 1);
///
/// Ok(())
/// }
/// ```
/// fn update(&self, exec: impl sqlx::SqliteExecutor<'_ >) -> Result<bool, sqlx::Error>
///
/// Generate fn for UPDATE with sqlx, if there is a PRIMARY KEY.\
/// PRIMARY KEY detection works only if constraint is in all caps, lowercase serves as escape hatch.
///
/// `UPDATE {tab_name} SET {cols} WHERE {pk}=`
///
/// ```rust
/// # use wb_sqlite::{CreateTableSql,Insert,Update};
/// #[derive(CreateTableSql,Insert,Update)]
/// struct Cat {
/// #[sql(constraint = "PRIMARY KEY")]
/// id: i64,
/// name: String,
/// }
/// #[tokio::main(flavor = "current_thread")]
/// async fn main() -> Result<(), sqlx::Error> {
/// use sqlx::{Connection, Executor};
/// let mut conn = sqlx::SqliteConnection::connect(":memory:").await?;
/// conn.execute(Cat::CREATE_TABLE_SQL).await?;
///
/// let c = Cat {
/// id: 0,
/// name: "miau".to_owned(),
/// };
/// let id = c.insert(&mut conn).await?;
/// assert!(id == 1);
///
/// let c2 = Cat {
/// id: 1,
/// name: "meouw".to_owned(),
/// };
/// let ok = c2.update(&mut conn).await?;
/// assert!(ok);
///
/// Ok(())
/// }
/// ```
/// fn update_sync(&self, conn: &rusqlite::Connection) -> Result<bool, rusqlite::Error>
///
/// Generate fn for UPDATE with rusqlite, if there is a PRIMARY KEY.\
/// PRIMARY KEY detection works only if constraint is in all caps, lowercase serves as escape hatch.
///
/// `UPDATE {tab_name} SET {cols} WHERE {pk}=`
///
/// ```rust
/// # use wb_sqlite::{CreateTableSql,InsertSync,UpdateSync};
/// #[derive(CreateTableSql,InsertSync,UpdateSync)]
/// struct Cat {
/// #[sql(constraint = "PRIMARY KEY")]
/// id: i64,
/// name: String,
/// }
/// fn main() -> Result<(), rusqlite::Error> {
/// let conn = rusqlite::Connection::open_in_memory()?;
/// conn.execute_batch(Cat::CREATE_TABLE_SQL)?;
///
/// let c = Cat {
/// id: 0,
/// name: "miau".to_owned(),
/// };
/// let id = c.insert_sync(&conn)?;
/// assert!(id == 1);
///
/// let c2 = Cat {
/// id: 1,
/// name: "meouw".to_owned(),
/// };
/// let ok = c2.update_sync(&conn)?;
/// assert!(ok);
///
/// Ok(())
/// }
/// ```