Skip to content

Commit

Permalink
orm: allow using connections, that were explicitly casted to `orm.Con…
Browse files Browse the repository at this point in the history
…nection` too (#17427)
  • Loading branch information
walking devel authored Feb 27, 2023
1 parent 864e199 commit b7b6c23
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 8 deletions.
2 changes: 2 additions & 0 deletions cmd/tools/vtest-self.v
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ const (
'vlib/orm/orm_fn_calls_test.v',
'vlib/orm/orm_last_id_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_orm_test.v',
'vlib/db/sqlite/sqlite_vfs_lowlevel_test.v',
Expand Down Expand Up @@ -197,6 +198,7 @@ const (
'vlib/orm/orm_fn_calls_test.v',
'vlib/orm/orm_last_id_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_array_struct_test.v',
'vlib/v/tests/orm_joined_tables_select_test.v',
Expand Down
31 changes: 31 additions & 0 deletions vlib/orm/orm_interface_test.v
Original file line number Diff line number Diff line change
@@ -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
}
31 changes: 23 additions & 8 deletions vlib/v/gen/c/sql.v
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,19 @@ fn (mut g Gen) sql_stmt(node ast.SqlStmt) {
conn := g.new_tmp_var()
g.writeln('')
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)
g.write('${db_expr_ctype_name} = &')
g.expr(node.db_expr)
g.writeln(', ._typ = _orm__Connection_${db_expr_ctype_name}_index};')

if db_expr_ctype_name == 'orm__Connection' {
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 {
g.sql_stmt_line(line, conn, node.or_expr)
}
Expand Down Expand Up @@ -534,14 +542,21 @@ fn (mut g Gen) sql_select_expr(node ast.SqlExpr) {
conn := g.new_tmp_var()
g.writeln('')
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 {
verror('sql orm error - unknown db type for ${node.db_expr}')
}
db_expr_ctype_name := g.typ(db_expr_type)
g.write('${db_expr_ctype_name} = &')
g.expr(node.db_expr)
g.writeln(', ._typ = _orm__Connection_${db_expr_ctype_name}_index};')

if db_expr_ctype_name == 'orm__Connection' {
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)
}

Expand Down

0 comments on commit b7b6c23

Please sign in to comment.