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