alex

/

v Public
0 Issues 1 Contributor 0 Releases 4 Branches
Additions: 56 Deletions: 28 View patch
1 'vlib/orm/orm_create_and_drop_test.v',
2 'vlib/orm/orm_insert_test.v',
3 'vlib/orm/orm_fn_calls_test.v',
4+ 'vlib/orm/orm_last_id_test.v',
5 'vlib/orm/orm_string_interpolation_in_where_test.v',
6 'vlib/db/sqlite/sqlite_test.v',
7 'vlib/db/sqlite/sqlite_orm_test.v',
8 'vlib/orm/orm_create_and_drop_test.v',
9 'vlib/orm/orm_insert_test.v',
10 'vlib/orm/orm_fn_calls_test.v',
11+ 'vlib/orm/orm_last_id_test.v',
12 'vlib/orm/orm_string_interpolation_in_where_test.v',
13 'vlib/v/tests/orm_sub_struct_test.v',
14 'vlib/v/tests/orm_sub_array_struct_test.v',
15
1 }
2
3 // last_id is used internally by V's ORM for post-processing `INSERT ` queries
4-pub fn (db Connection) last_id() orm.Primitive {
5+pub fn (db Connection) last_id() int {
6 query := 'SELECT last_insert_id();'
7- id := db.query(query) or {
8- Result{
9- result: 0
10- }
11- }
12- return orm.Primitive(id.rows()[0].vals[0].int())
13+ id := db.query(query) or { return 0 }
14+
15+ return id.rows()[0].vals[0].int()
16 }
17
18 // DDL (table creation/destroying etc)
19
1 }
2
3 // last_id is used internally by V's ORM for post-processing `INSERT ` queries
4-pub fn (db DB) last_id() orm.Primitive {
5+pub fn (db DB) last_id() int {
6 query := 'SELECT LASTVAL();'
7- id := db.q_int(query) or { 0 }
8- return orm.Primitive(id)
9+
10+ return db.q_int(query) or { 0 }
11 }
12
13 // DDL (table creation/destroying etc)
14
1 }
2
3 // last_id is used internally by V's ORM for post-processing `INSERT ` queries
4-pub fn (db DB) last_id() orm.Primitive {
5+pub fn (db DB) last_id() int {
6 query := 'SELECT last_insert_rowid();'
7- id := db.q_int(query)
8- return orm.Primitive(id)
9+
10+ return db.q_int(query)
11 }
12
13 // DDL (table creation/destroying etc)
14
1 mysql_stmt_worker(db, query, orm.QueryData{}, where)!
2 }
3
4-pub fn (db Connection) last_id() orm.Primitive {
5+pub fn (db Connection) last_id() int {
6 query := 'SELECT last_insert_id();'
7- id := db.query(query) or {
8- Result{
9- result: 0
10- }
11- }
12+ id := db.query(query) or { return 0 }
13+
14 return orm.Primitive(id.rows()[0].vals[0].int())
15 }
16
17
1 delete(table string, where QueryData) !
2 create(table string, fields []TableField) !
3 drop(table string) !
4- last_id() Primitive
5+ last_id() int
6 }
7
8 // Generates an sql stmt, from universal parameter
9
1new file mode 100644
2+import db.sqlite
3+
4+struct User {
5+ id int [primary; sql: serial]
6+ name string
7+}
8+
9+fn test_last_id() {
10+ mut db := sqlite.connect(':memory:') or { panic(err) }
11+
12+ sql db {
13+ create table User
14+ }
15+
16+ first_user := User{
17+ name: 'first'
18+ }
19+
20+ second_user := User{
21+ name: 'second'
22+ }
23+
24+ sql db {
25+ insert first_user into User
26+ insert second_user into User
27+ }
28+
29+ last_id := db.last_id()
30+
31+ assert typeof(last_id).name == 'int'
32+ assert last_id > 0
33+}
34
1 pg_stmt_worker(db, query, orm.QueryData{}, where)!
2 }
3
4-pub fn (db DB) last_id() orm.Primitive {
5+pub fn (db DB) last_id() int {
6 query := 'SELECT LASTVAL();'
7- id := db.q_int(query) or { 0 }
8- return orm.Primitive(id)
9+
10+ return db.q_int(query) or { 0 }
11 }
12
13 // table
14
1 sqlite_stmt_worker(db, query, orm.QueryData{}, where)!
2 }
3
4-pub fn (db DB) last_id() orm.Primitive {
5+pub fn (db DB) last_id() int {
6 query := 'SELECT last_insert_rowid();'
7- id := db.q_int(query)
8- return orm.Primitive(id)
9+
10+ return db.q_int(query)
11 }
12
13 // table
14
1
2 for sub in subs {
3 g.sql_stmt_line(sub, expr, or_expr)
4- g.writeln('array_push(&${last_ids_arr}, _MOV((orm__Primitive[]){orm__Connection_name_table[${expr}._typ]._method_last_id(${expr}._object)}));')
5+ g.writeln('array_push(&${last_ids_arr}, _MOV((orm__Primitive[]){orm__int_to_primitive(orm__Connection_name_table[${expr}._typ]._method_last_id(${expr}._object))}));')
6 }
7
8 g.write('${result_name}_void ${res} = orm__Connection_name_table[${expr}._typ]._method_')
9
10 if arrs.len > 0 {
11 mut id_name := g.new_tmp_var()
12- g.writeln('orm__Primitive ${id_name} = orm__Connection_name_table[${expr}._typ]._method_last_id(${expr}._object);')
13+ g.writeln('orm__Primitive ${id_name} = orm__int_to_primitive(orm__Connection_name_table[${expr}._typ]._method_last_id(${expr}._object));')
14 for i, mut arr in arrs {
15 idx := g.new_tmp_var()
16 g.writeln('for (int ${idx} = 0; ${idx} < ${arr.object_var_name}.${field_names[i]}.len; ${idx}++) {')
17