From 4747e70d9de4965212c836df5c23f9293ef3172f Mon Sep 17 00:00:00 2001 From: walking devel <104449470+walkingdevel@users.noreply.github.com> Date: Sun, 29 Jan 2023 14:00:23 +0000 Subject: [PATCH] orm: make last_id() return `int`, instead of `orm.Primitive` (fix #12110) (#17151) --- cmd/tools/vtest-self.v | 2 ++ vlib/db/mysql/orm.v | 11 ++++------- vlib/db/pg/orm.v | 6 +++--- vlib/db/sqlite/orm.v | 6 +++--- vlib/mysql/orm.v | 9 +++------ vlib/orm/orm.v | 2 +- vlib/orm/orm_last_id_test.v | 32 ++++++++++++++++++++++++++++++++ vlib/pg/orm.v | 6 +++--- vlib/sqlite/orm.v | 6 +++--- vlib/v/gen/c/sql.v | 4 ++-- 10 files changed, 56 insertions(+), 28 deletions(-) create mode 100644 vlib/orm/orm_last_id_test.v diff --git a/cmd/tools/vtest-self.v b/cmd/tools/vtest-self.v index 5eb918cc85..b4c680837f 100644 --- a/cmd/tools/vtest-self.v +++ b/cmd/tools/vtest-self.v @@ -126,6 +126,7 @@ const ( 'vlib/orm/orm_create_and_drop_test.v', 'vlib/orm/orm_insert_test.v', 'vlib/orm/orm_fn_calls_test.v', + 'vlib/orm/orm_last_id_test.v', 'vlib/orm/orm_string_interpolation_in_where_test.v', 'vlib/db/sqlite/sqlite_test.v', 'vlib/db/sqlite/sqlite_orm_test.v', @@ -195,6 +196,7 @@ const ( 'vlib/orm/orm_create_and_drop_test.v', 'vlib/orm/orm_insert_test.v', 'vlib/orm/orm_fn_calls_test.v', + 'vlib/orm/orm_last_id_test.v', 'vlib/orm/orm_string_interpolation_in_where_test.v', 'vlib/v/tests/orm_sub_struct_test.v', 'vlib/v/tests/orm_sub_array_struct_test.v', diff --git a/vlib/db/mysql/orm.v b/vlib/db/mysql/orm.v index 5753a09031..fef217057c 100644 --- a/vlib/db/mysql/orm.v +++ b/vlib/db/mysql/orm.v @@ -153,14 +153,11 @@ pub fn (db Connection) delete(table string, where orm.QueryData) ! { } // last_id is used internally by V's ORM for post-processing `INSERT ` queries -pub fn (db Connection) last_id() orm.Primitive { +pub fn (db Connection) last_id() int { query := 'SELECT last_insert_id();' - id := db.query(query) or { - Result{ - result: 0 - } - } - return orm.Primitive(id.rows()[0].vals[0].int()) + id := db.query(query) or { return 0 } + + return id.rows()[0].vals[0].int() } // DDL (table creation/destroying etc) diff --git a/vlib/db/pg/orm.v b/vlib/db/pg/orm.v index 0cd4ce63bb..e21272c7e8 100644 --- a/vlib/db/pg/orm.v +++ b/vlib/db/pg/orm.v @@ -51,10 +51,10 @@ pub fn (db DB) delete(table string, where orm.QueryData) ! { } // last_id is used internally by V's ORM for post-processing `INSERT ` queries -pub fn (db DB) last_id() orm.Primitive { +pub fn (db DB) last_id() int { query := 'SELECT LASTVAL();' - id := db.q_int(query) or { 0 } - return orm.Primitive(id) + + return db.q_int(query) or { 0 } } // DDL (table creation/destroying etc) diff --git a/vlib/db/sqlite/orm.v b/vlib/db/sqlite/orm.v index dd9a1d1ced..19c1b1bade 100644 --- a/vlib/db/sqlite/orm.v +++ b/vlib/db/sqlite/orm.v @@ -71,10 +71,10 @@ pub fn (db DB) delete(table string, where orm.QueryData) ! { } // last_id is used internally by V's ORM for post-processing `INSERT ` queries -pub fn (db DB) last_id() orm.Primitive { +pub fn (db DB) last_id() int { query := 'SELECT last_insert_rowid();' - id := db.q_int(query) - return orm.Primitive(id) + + return db.q_int(query) } // DDL (table creation/destroying etc) diff --git a/vlib/mysql/orm.v b/vlib/mysql/orm.v index c7f4337157..e0b2e2c9f8 100644 --- a/vlib/mysql/orm.v +++ b/vlib/mysql/orm.v @@ -148,13 +148,10 @@ pub fn (db Connection) delete(table string, where orm.QueryData) ! { mysql_stmt_worker(db, query, orm.QueryData{}, where)! } -pub fn (db Connection) last_id() orm.Primitive { +pub fn (db Connection) last_id() int { query := 'SELECT last_insert_id();' - id := db.query(query) or { - Result{ - result: 0 - } - } + id := db.query(query) or { return 0 } + return orm.Primitive(id.rows()[0].vals[0].int()) } diff --git a/vlib/orm/orm.v b/vlib/orm/orm.v index 98a33df297..66b70bd467 100644 --- a/vlib/orm/orm.v +++ b/vlib/orm/orm.v @@ -175,7 +175,7 @@ pub interface Connection { delete(table string, where QueryData) ! create(table string, fields []TableField) ! drop(table string) ! - last_id() Primitive + last_id() int } // Generates an sql stmt, from universal parameter diff --git a/vlib/orm/orm_last_id_test.v b/vlib/orm/orm_last_id_test.v new file mode 100644 index 0000000000..9376837fa4 --- /dev/null +++ b/vlib/orm/orm_last_id_test.v @@ -0,0 +1,32 @@ +import db.sqlite + +struct User { + id int [primary; sql: serial] + name string +} + +fn test_last_id() { + mut db := sqlite.connect(':memory:') or { panic(err) } + + sql db { + create table User + } + + first_user := User{ + name: 'first' + } + + second_user := User{ + name: 'second' + } + + sql db { + insert first_user into User + insert second_user into User + } + + last_id := db.last_id() + + assert typeof(last_id).name == 'int' + assert last_id > 0 +} diff --git a/vlib/pg/orm.v b/vlib/pg/orm.v index b866cad194..0c8799b4f3 100644 --- a/vlib/pg/orm.v +++ b/vlib/pg/orm.v @@ -46,10 +46,10 @@ pub fn (db DB) delete(table string, where orm.QueryData) ! { pg_stmt_worker(db, query, orm.QueryData{}, where)! } -pub fn (db DB) last_id() orm.Primitive { +pub fn (db DB) last_id() int { query := 'SELECT LASTVAL();' - id := db.q_int(query) or { 0 } - return orm.Primitive(id) + + return db.q_int(query) or { 0 } } // table diff --git a/vlib/sqlite/orm.v b/vlib/sqlite/orm.v index 559f0a0fa9..07b76a0646 100644 --- a/vlib/sqlite/orm.v +++ b/vlib/sqlite/orm.v @@ -68,10 +68,10 @@ pub fn (db DB) delete(table string, where orm.QueryData) ! { sqlite_stmt_worker(db, query, orm.QueryData{}, where)! } -pub fn (db DB) last_id() orm.Primitive { +pub fn (db DB) last_id() int { query := 'SELECT last_insert_rowid();' - id := db.q_int(query) - return orm.Primitive(id) + + return db.q_int(query) } // table diff --git a/vlib/v/gen/c/sql.v b/vlib/v/gen/c/sql.v index 03efa9ddb7..90642b4a83 100644 --- a/vlib/v/gen/c/sql.v +++ b/vlib/v/gen/c/sql.v @@ -156,7 +156,7 @@ fn (mut g Gen) sql_insert(node ast.SqlStmtLine, expr string, table_name string, for sub in subs { g.sql_stmt_line(sub, expr, or_expr) - g.writeln('array_push(&${last_ids_arr}, _MOV((orm__Primitive[]){orm__Connection_name_table[${expr}._typ]._method_last_id(${expr}._object)}));') + 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))}));') } g.write('${result_name}_void ${res} = orm__Connection_name_table[${expr}._typ]._method_') @@ -207,7 +207,7 @@ fn (mut g Gen) sql_insert(node ast.SqlStmtLine, expr string, table_name string, if arrs.len > 0 { mut id_name := g.new_tmp_var() - g.writeln('orm__Primitive ${id_name} = orm__Connection_name_table[${expr}._typ]._method_last_id(${expr}._object);') + g.writeln('orm__Primitive ${id_name} = orm__int_to_primitive(orm__Connection_name_table[${expr}._typ]._method_last_id(${expr}._object));') for i, mut arr in arrs { idx := g.new_tmp_var() g.writeln('for (int ${idx} = 0; ${idx} < ${arr.object_var_name}.${field_names[i]}.len; ${idx}++) {')