From b7b6c2368e5c51adbdac11e13074f70d2b1b4b23 Mon Sep 17 00:00:00 2001 From: walking devel <104449470+walkingdevel@users.noreply.github.com> Date: Mon, 27 Feb 2023 21:54:03 +0000 Subject: [PATCH] orm: allow using connections, that were explicitly casted to `orm.Connection` too (#17427) --- cmd/tools/vtest-self.v | 2 ++ vlib/orm/orm_interface_test.v | 31 +++++++++++++++++++++++++++++++ vlib/v/gen/c/sql.v | 31 +++++++++++++++++++++++-------- 3 files changed, 56 insertions(+), 8 deletions(-) create mode 100644 vlib/orm/orm_interface_test.v diff --git a/cmd/tools/vtest-self.v b/cmd/tools/vtest-self.v index b0971ff615..ea68d3f2b2 100644 --- a/cmd/tools/vtest-self.v +++ b/cmd/tools/vtest-self.v @@ -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', @@ -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', diff --git a/vlib/orm/orm_interface_test.v b/vlib/orm/orm_interface_test.v new file mode 100644 index 0000000000..ca72766ec0 --- /dev/null +++ b/vlib/orm/orm_interface_test.v @@ -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 +} diff --git a/vlib/v/gen/c/sql.v b/vlib/v/gen/c/sql.v index 93ca50b13a..47aa6e22f5 100644 --- a/vlib/v/gen/c/sql.v +++ b/vlib/v/gen/c/sql.v @@ -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) } @@ -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) }