1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

orm: make last_id() return int, instead of orm.Primitive (fix #12110) (#17151)

This commit is contained in:
walking devel 2023-01-29 14:00:23 +00:00 committed by GitHub
parent e6a4f76f82
commit 4747e70d9d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 56 additions and 28 deletions

View File

@ -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',

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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())
}

View File

@ -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

View File

@ -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
}

View File

@ -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

View File

@ -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

View File

@ -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}++) {')