mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
orm: allow using connections, that were explicitly casted to orm.Connection
too (#17427)
This commit is contained in:
parent
864e1994b0
commit
b7b6c2368e
@ -127,6 +127,7 @@ const (
|
|||||||
'vlib/orm/orm_fn_calls_test.v',
|
'vlib/orm/orm_fn_calls_test.v',
|
||||||
'vlib/orm/orm_last_id_test.v',
|
'vlib/orm/orm_last_id_test.v',
|
||||||
'vlib/orm/orm_string_interpolation_in_where_test.v',
|
'vlib/orm/orm_string_interpolation_in_where_test.v',
|
||||||
|
'vlib/orm/orm_interface_test.v',
|
||||||
'vlib/db/sqlite/sqlite_test.v',
|
'vlib/db/sqlite/sqlite_test.v',
|
||||||
'vlib/db/sqlite/sqlite_orm_test.v',
|
'vlib/db/sqlite/sqlite_orm_test.v',
|
||||||
'vlib/db/sqlite/sqlite_vfs_lowlevel_test.v',
|
'vlib/db/sqlite/sqlite_vfs_lowlevel_test.v',
|
||||||
@ -197,6 +198,7 @@ const (
|
|||||||
'vlib/orm/orm_fn_calls_test.v',
|
'vlib/orm/orm_fn_calls_test.v',
|
||||||
'vlib/orm/orm_last_id_test.v',
|
'vlib/orm/orm_last_id_test.v',
|
||||||
'vlib/orm/orm_string_interpolation_in_where_test.v',
|
'vlib/orm/orm_string_interpolation_in_where_test.v',
|
||||||
|
'vlib/orm/orm_interface_test.v',
|
||||||
'vlib/v/tests/orm_sub_struct_test.v',
|
'vlib/v/tests/orm_sub_struct_test.v',
|
||||||
'vlib/v/tests/orm_sub_array_struct_test.v',
|
'vlib/v/tests/orm_sub_array_struct_test.v',
|
||||||
'vlib/v/tests/orm_joined_tables_select_test.v',
|
'vlib/v/tests/orm_joined_tables_select_test.v',
|
||||||
|
31
vlib/orm/orm_interface_test.v
Normal file
31
vlib/orm/orm_interface_test.v
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
import db.sqlite
|
||||||
|
import orm
|
||||||
|
|
||||||
|
struct User {
|
||||||
|
id int [primary; sql: serial]
|
||||||
|
name string
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_orm_interface() {
|
||||||
|
sqlite_db := sqlite.connect(':memory:') or { panic(err) }
|
||||||
|
db := orm.Connection(sqlite_db)
|
||||||
|
|
||||||
|
sql db {
|
||||||
|
create table User
|
||||||
|
}
|
||||||
|
|
||||||
|
user := User{
|
||||||
|
name: 'test'
|
||||||
|
}
|
||||||
|
|
||||||
|
sql db {
|
||||||
|
insert user into User
|
||||||
|
}
|
||||||
|
|
||||||
|
users := sql db {
|
||||||
|
select from User
|
||||||
|
}
|
||||||
|
|
||||||
|
assert users.len == 1
|
||||||
|
assert users.first().name == user.name
|
||||||
|
}
|
@ -15,11 +15,19 @@ fn (mut g Gen) sql_stmt(node ast.SqlStmt) {
|
|||||||
conn := g.new_tmp_var()
|
conn := g.new_tmp_var()
|
||||||
g.writeln('')
|
g.writeln('')
|
||||||
g.writeln('// orm')
|
g.writeln('// orm')
|
||||||
g.write('orm__Connection ${conn} = (orm__Connection){._')
|
g.write('orm__Connection ${conn} = ')
|
||||||
|
|
||||||
db_expr_ctype_name := g.typ(node.db_expr_type)
|
db_expr_ctype_name := g.typ(node.db_expr_type)
|
||||||
g.write('${db_expr_ctype_name} = &')
|
|
||||||
g.expr(node.db_expr)
|
if db_expr_ctype_name == 'orm__Connection' {
|
||||||
g.writeln(', ._typ = _orm__Connection_${db_expr_ctype_name}_index};')
|
g.expr(node.db_expr)
|
||||||
|
g.writeln(';')
|
||||||
|
} else {
|
||||||
|
g.write('(orm__Connection){._${db_expr_ctype_name} = &')
|
||||||
|
g.expr(node.db_expr)
|
||||||
|
g.writeln(', ._typ = _orm__Connection_${db_expr_ctype_name}_index};')
|
||||||
|
}
|
||||||
|
|
||||||
for line in node.lines {
|
for line in node.lines {
|
||||||
g.sql_stmt_line(line, conn, node.or_expr)
|
g.sql_stmt_line(line, conn, node.or_expr)
|
||||||
}
|
}
|
||||||
@ -534,14 +542,21 @@ fn (mut g Gen) sql_select_expr(node ast.SqlExpr) {
|
|||||||
conn := g.new_tmp_var()
|
conn := g.new_tmp_var()
|
||||||
g.writeln('')
|
g.writeln('')
|
||||||
g.writeln('// orm')
|
g.writeln('// orm')
|
||||||
g.write('orm__Connection ${conn} = (orm__Connection){._')
|
g.write('orm__Connection ${conn} = ')
|
||||||
db_expr_type := g.get_db_type(node.db_expr) or {
|
db_expr_type := g.get_db_type(node.db_expr) or {
|
||||||
verror('sql orm error - unknown db type for ${node.db_expr}')
|
verror('sql orm error - unknown db type for ${node.db_expr}')
|
||||||
}
|
}
|
||||||
db_expr_ctype_name := g.typ(db_expr_type)
|
db_expr_ctype_name := g.typ(db_expr_type)
|
||||||
g.write('${db_expr_ctype_name} = &')
|
|
||||||
g.expr(node.db_expr)
|
if db_expr_ctype_name == 'orm__Connection' {
|
||||||
g.writeln(', ._typ = _orm__Connection_${db_expr_ctype_name}_index};')
|
g.expr(node.db_expr)
|
||||||
|
g.writeln(';')
|
||||||
|
} else {
|
||||||
|
g.write('(orm__Connection){._${db_expr_ctype_name} = &')
|
||||||
|
g.expr(node.db_expr)
|
||||||
|
g.writeln(', ._typ = _orm__Connection_${db_expr_ctype_name}_index};')
|
||||||
|
}
|
||||||
|
|
||||||
g.sql_select(node, conn, left, node.or_expr)
|
g.sql_select(node, conn, left, node.or_expr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user