-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate_generated_code.rs
467 lines (443 loc) · 19 KB
/
generate_generated_code.rs
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
use crate::configuration::{Config, Visibility};
use crate::custom_mapping::CustomMapping;
use crate::parse::parse;
use grdb_orm_lib::dyn_query::DynamicQuery;
use grdb_orm_lib::room::Room;
use regex::Regex;
use sqlite_parser::Metadata;
use std::env::current_dir;
use std::fs::File;
/// Run this to fill the generated folder with the most up to date code
/// Annotated as a test so it can be executed
#[test]
fn update_generated_code() {
let (metadata, path) = create_db(
"
create table User
(
userUuid TEXT PRIMARY KEY NOT NULL,
firstName TEXT,
jsonStruct TEXT NOT NULL,
jsonStructOptional TEXT,
jsonStructArray TEXT NOT NULL,
jsonStructArrayOptional TEXT,
integer INTEGER NOT NULL,
bool INTEGER NOT NULL,
serializedInfo BLOB NOT NULL,
serializedInfoNullable BLOB
);
create table Book
(
bookUuid TEXT PRIMARY KEY NOT NULL,
userUuid TEXT,
integerOptional INTEGER,
tsCreated INTEGER NOT NULL,
FOREIGN KEY(userUuid) REFERENCES User(userUuid)
);
create table UserBook
(
bookUuid TEXT NOT NULL,
userUuid TEXT NOT NULL,
realToDouble REAL,
PRIMARY KEY (bookUuid, userUuid),
FOREIGN KEY(bookUuid) REFERENCES Book(bookUuid),
FOREIGN KEY(userUuid) REFERENCES User(userUuid)
);
create table Parent
(
parentUuid TEXT NOT NULL,
userUuid TEXT,
PRIMARY KEY (parentUuid),
FOREIGN KEY(userUuid) REFERENCES User(userUuid)
);
",
);
let config = Config {
visibility: Visibility::Public,
output_dir: current_dir().unwrap(),
custom_mapping: vec![
CustomMapping {
the_type: "[JsonType]".to_string(),
regexes: vec![Regex::new("jsonStructArray.*").unwrap()],
},
CustomMapping {
the_type: "JsonType".to_string(),
regexes: vec![Regex::new("jsonStruct.*").unwrap()],
},
CustomMapping {
the_type: "UUID".to_string(),
regexes: vec![Regex::new(".*Uuid").unwrap()],
},
CustomMapping {
the_type: "Int64".to_string(),
regexes: vec![Regex::new("tsCreated").unwrap()],
},
CustomMapping {
the_type: "Bool".to_string(),
regexes: vec![Regex::new("bool").unwrap()],
},
CustomMapping {
the_type: "SerializedInfo".to_string(),
regexes: vec![Regex::new("serializedInfo*").unwrap()],
},
],
dynamic_queries: vec![
DynamicQuery {
extension: "Book".to_string(),
func_name: "booksForUserWithSpecificUuid".to_string(),
parameter_types: vec![(
"User".to_string(),
"userUuid".to_string(),
"userUuid".to_string(),
)],
return_types: vec![
"Book".to_string(),
"User.integer".to_string(),
"User.jsonStructArrayOptional".to_string(),
"Int".to_string(),
],
return_types_is_array: true,
query: "select Book.*, User.integer, User.jsonStructArrayOptional, 1 from Book
join User on User.userUuid = Book.userUuid
where User.userUuid = ?"
.to_string(),
map_to_different_type: None,
bypass_index_optimizer: false,
ignore_query_sanitizing_android: false,
},
DynamicQuery {
extension: "Book".to_string(),
func_name: "booksWithOptionalUser".to_string(),
parameter_types: vec![],
return_types: vec![
"Book".to_string(),
"User?".to_string(),
"Bool?".to_string(),
],
return_types_is_array: true,
query: "select Book.*, User.*, Book.integerOptional
from Book
left join User on User.userUuid = Book.userUuid"
.to_string(),
map_to_different_type: None,
bypass_index_optimizer: false,
ignore_query_sanitizing_android: false,
},
DynamicQuery {
extension: "User".to_string(),
func_name: "like".to_string(),
parameter_types: vec![
("User".to_string(), "firstName".to_string(), "firstName0".to_string()),
("User".to_string(), "firstName".to_string(), "firstName1".to_string()),
("User".to_string(), "firstName".to_string(), "firstName2".to_string()),
("User".to_string(), "firstName".to_string(), "firstName3".to_string()),
("User".to_string(), "firstName".to_string(), "firstName4".to_string()),
],
return_types: vec![
"User".to_string(),
],
return_types_is_array: true,
query: "select User.* from User where User.firstName LIKE '%?%' or User.firstName = ? or User.firstName LIKE '%?' or User.firstName LIKE '?%' or User.firstName = ?".to_string(),
map_to_different_type: None,
bypass_index_optimizer: false,
ignore_query_sanitizing_android: false,
},
DynamicQuery {
extension: "User".to_string(),
func_name: "findByUsername".to_string(),
parameter_types: vec![(
"User".to_string(),
"firstName".to_string(),
"firstName".to_string(),
)],
return_types: vec!["User".to_string()],
return_types_is_array: false,
query: "select * from User where firstName = ?".to_string(),
map_to_different_type: None,
bypass_index_optimizer: false,
ignore_query_sanitizing_android: false,
},
DynamicQuery {
extension: "User".to_string(),
func_name: "findUserUuidByUsername".to_string(),
parameter_types: vec![(
"User".to_string(),
"firstName".to_string(),
"firstName".to_string(),
)],
return_types: vec!["User.userUuid".to_string()],
return_types_is_array: false,
query: "select userUuid from User where firstName = ?".to_string(),
map_to_different_type: None,
bypass_index_optimizer: false,
ignore_query_sanitizing_android: false,
},
DynamicQuery {
extension: "User".to_string(),
func_name: "amountOfUsers".to_string(),
parameter_types: vec![],
return_types: vec!["Int".to_string()],
return_types_is_array: false,
query: "select count(*) from User".to_string(),
map_to_different_type: None,
bypass_index_optimizer: false,
ignore_query_sanitizing_android: false,
},
DynamicQuery {
extension: "Book".to_string(),
func_name: "deleteByUserUuid".to_string(),
parameter_types: vec![(
"Book".to_string(),
"userUuid".to_string(),
"userUuid".to_string(),
)],
return_types: vec![],
return_types_is_array: false,
query: "delete from Book where userUuid = ?".to_string(),
map_to_different_type: None,
bypass_index_optimizer: false,
ignore_query_sanitizing_android: false,
},
DynamicQuery {
extension: "Book".to_string(),
func_name: "hasAtLeastOneBook".to_string(),
parameter_types: vec![],
return_types: vec!["Bool".to_string()],
return_types_is_array: false,
query: "select exists(select 1 from Book)".to_string(),
map_to_different_type: None,
bypass_index_optimizer: false,
ignore_query_sanitizing_android: false,
},
DynamicQuery {
extension: "User".to_string(),
func_name: "serializeInfoSingle".to_string(),
parameter_types: vec![],
return_types: vec![
"User.serializedInfo".to_string(),
"User.serializedInfoNullable".to_string(),
],
return_types_is_array: false,
query: "select serializedInfo, serializedInfoNullable from user limit 1"
.to_string(),
map_to_different_type: None,
bypass_index_optimizer: false,
ignore_query_sanitizing_android: false,
},
DynamicQuery {
extension: "User".to_string(),
func_name: "serializeInfoArray".to_string(),
parameter_types: vec![],
return_types: vec![
"User.serializedInfo".to_string(),
"User.serializedInfoNullable".to_string(),
],
return_types_is_array: true,
query: "select serializedInfo, serializedInfoNullable from user".to_string(),
map_to_different_type: None,
bypass_index_optimizer: false,
ignore_query_sanitizing_android: false,
},
DynamicQuery {
extension: "User".to_string(),
func_name: "serializeInfoArray".to_string(),
parameter_types: vec![
("User".to_string(), "serializedInfo".to_string(), "serializedInfo".to_string()),
("User".to_string(), "serializedInfoNullable".to_string(), "serializedInfoNullable".to_string()),
("User".to_string(), "firstName".to_string(), "firstName".to_string()),
],
return_types: vec![],
return_types_is_array: false,
query: "update user set serializedInfo = ? and serializedInfoNullable = ? where firstName = ?".to_string(),
map_to_different_type: None,
bypass_index_optimizer: false,
ignore_query_sanitizing_android: false,
},
DynamicQuery {
extension: "User".to_string(),
func_name: "allWithProvidedFirstNames".to_string(),
parameter_types: vec![
("User".to_string(), "firstName".to_string(), "firstName".to_string()),
],
return_types: vec!["User".to_string()],
return_types_is_array: true,
query: "select * from user where firstName in %PARAM_IN%".to_string(),
map_to_different_type: None,
bypass_index_optimizer: false,
ignore_query_sanitizing_android: false,
},
DynamicQuery {
extension: "User".to_string(),
func_name: "complex".to_string(),
parameter_types: vec![
("User".to_string(), "firstName".to_string(), "firstNames0".to_string()),
("User".to_string(), "jsonStructOptional".to_string(), "jsonStructOptional".to_string()),
("User".to_string(), "integer".to_string(), "integer".to_string()),
("User".to_string(), "serializedInfoNullable".to_string(), "serializedInfoNullable".to_string()),
],
return_types: vec!["User".to_string()],
return_types_is_array: true,
query: "select * from user where firstName in %PARAM_IN% and jsonStructOptional = ? and integer in %PARAM_IN% and serializedInfoNullable = ?".to_string(),
map_to_different_type: None,
bypass_index_optimizer: false,
ignore_query_sanitizing_android: false,
},
DynamicQuery {
extension: "Parent".to_string(),
func_name: "retrieveOptionalUserValues".to_string(),
parameter_types: vec![
("Parent".to_string(), "parentUuid".to_string(), "parentUuid".to_string()),
],
return_types: vec![
"Parent.parentUuid".to_string(),
"User.userUuid?".to_string(),
"User.jsonStructArray?".to_string(),
"User.jsonStructArrayOptional".to_string(),
],
return_types_is_array: true,
query: "select parentUuid, U.userUuid, jsonStructArray, jsonStructArrayOptional from Parent left join User U on U.userUuid = Parent.userUuid where parentUuid = ?".to_string(),
map_to_different_type: None,
bypass_index_optimizer: false,
ignore_query_sanitizing_android: false,
},
DynamicQuery {
extension: "Parent".to_string(),
func_name: "retrieveOptionalUserValuesMapped".to_string(),
parameter_types: vec![
("Parent".to_string(), "parentUuid".to_string(), "parentUuid".to_string()),
],
return_types: vec![
"Parent.parentUuid".to_string(),
"User.userUuid?".to_string(),
"User.jsonStructArray?".to_string(),
"User.jsonStructArrayOptional".to_string(),
],
return_types_is_array: true,
query: "select parentUuid, U.userUuid, jsonStructArray, jsonStructArrayOptional from Parent left join User U on U.userUuid = Parent.userUuid where parentUuid = ? order by Parent.userUuid".to_string(),
map_to_different_type: Some("retrieveOptionalUserValues".to_string()),
bypass_index_optimizer: false,
ignore_query_sanitizing_android: false,
},
DynamicQuery {
extension: "Parent".to_string(),
func_name: "limited".to_string(),
parameter_types: vec![
("Int".to_string(), "limit".to_string(), "limit".to_string()),
],
return_types: vec![
"Parent".to_string(),
],
return_types_is_array: true,
query: "select * from Parent limit ?".to_string(),
map_to_different_type: None,
bypass_index_optimizer: false,
ignore_query_sanitizing_android: false,
},
],
suffix_swift_structs: "",
prefix_swift_structs: "Db",
use_swiftformat: true,
use_swiftlint: true,
sqlite_location: path.clone(),
all_immutable: false,
imports: "import Foundation\nimport GRDB".to_string(),
index_optimizer: false,
output_dir_android: Default::default(),
room: Room { imports: vec![], disallow_default_dao_methods: false, skip_type_converters: vec![], convert_with_gson_type_converters: vec![], unique_indexes: vec![], gson_type_adapters: vec![], },
type_interfaces_custom_code: vec![],
android_package_name: "".to_string(),
android_verbose_sql_logging: false,
};
parse(metadata, config);
delete_db(path);
}
pub fn create_db(batch: &str) -> (Metadata, String) {
let db_path = current_dir().unwrap().join("generatedfortest.sqlite3");
File::create(&db_path).unwrap();
let con = rusqlite::Connection::open(&db_path).unwrap();
con.execute_batch(batch).unwrap();
let tables = sqlite_parser::parse_no_parser(&db_path);
(tables, db_path.to_str().unwrap().to_string())
}
pub fn delete_db(path: String) {
std::fs::remove_file(path).unwrap();
}
mod index_optimizer_test {
use crate::generate_generated_code::create_db;
use crate::parse::parse;
use crate::{Config, Visibility};
use grdb_orm_lib::dyn_query::DynamicQuery;
use grdb_orm_lib::room::Room;
use std::env::current_dir;
fn setup(query: &str, add_index: bool) {
let mut table_create = "create table User(
userUuid TEXT PRIMARY KEY NOT NULL,
name TEXT,
something_random TEXT
);"
.to_string();
if add_index {
table_create += "\nCREATE INDEX user_name
ON User (name);";
}
let (metadata, path) = create_db(&table_create);
let config = Config {
visibility: Visibility::Public,
output_dir: current_dir().unwrap(),
custom_mapping: vec![],
dynamic_queries: vec![DynamicQuery {
parameter_types: vec![],
extension: "User".to_string(),
func_name: "selectAll".to_string(),
return_types: vec!["User".to_string()],
return_types_is_array: true,
query: query.to_string(),
map_to_different_type: None,
bypass_index_optimizer: false,
ignore_query_sanitizing_android: false,
}],
suffix_swift_structs: "",
prefix_swift_structs: "",
use_swiftformat: true,
use_swiftlint: true,
sqlite_location: path,
all_immutable: false,
imports: "".to_string(),
index_optimizer: true,
output_dir_android: Default::default(),
room: Room {
imports: vec![],
disallow_default_dao_methods: false,
skip_type_converters: vec![],
convert_with_gson_type_converters: vec![],
unique_indexes: vec![],
gson_type_adapters: vec![],
},
type_interfaces_custom_code: vec![],
android_package_name: "".to_string(),
android_verbose_sql_logging: false,
};
parse(metadata, config);
}
#[test]
#[should_panic]
fn test_unused_index() {
setup("select * from User", true);
}
#[test]
#[should_panic]
fn test_missing_index() {
setup("select * from User where name = 'test'", false);
}
#[test]
fn correct() {
setup("select * from User where name = 'test'", true);
}
#[test]
fn correct_join() {
setup(
"select *, (select 1 from User where name = 'x') from User where name = 'test'",
true,
);
}
}