Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cgen, checker: allow ORM to insert sumtype matching variant #23241

Merged
merged 5 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions vlib/orm/orm_sum_type_insert_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import db.sqlite

struct SomeStruct {
foo int
bar string
}

struct OtherStruct {
baz f64
}

type SomeSum = SomeStruct | OtherStruct

fn test_sum_type_insert() {
db := sqlite.connect(':memory:')!
sql db {
create table SomeStruct
}!

some := SomeSum(SomeStruct{})
spytheman marked this conversation as resolved.
Show resolved Hide resolved
if some is SomeStruct {
sql db {
insert some into SomeStruct
}!
}
}
3 changes: 2 additions & 1 deletion vlib/v/checker/orm.v
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,8 @@ fn (mut c Checker) sql_stmt_line(mut node ast.SqlStmtLine) ast.Type {
inserting_object_type = inserting_object.typ.deref()
}

if inserting_object_type != node.table_expr.typ {
if inserting_object_type != node.table_expr.typ
&& !c.table.sumtype_has_variant(inserting_object_type, node.table_expr.typ, false) {
table_name := table_sym.name
inserting_type_name := c.table.sym(inserting_object_type).name

Expand Down
7 changes: 7 additions & 0 deletions vlib/v/gen/c/orm.v
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ fn (mut g Gen) write_orm_insert_with_last_ids(node ast.SqlStmtLine, connection_v
is_serial := primary_field.attrs.contains_arg('sql', 'serial')
&& primary_field.typ == ast.int_type

mut inserting_object_type := ast.void_type
mut member_access_type := '.'
if node.scope != unsafe { nil } {
inserting_object := node.scope.find(node.object_var) or {
Expand All @@ -342,8 +343,10 @@ fn (mut g Gen) write_orm_insert_with_last_ids(node ast.SqlStmtLine, connection_v
if inserting_object.typ.is_ptr() {
member_access_type = '->'
}
inserting_object_type = inserting_object.typ
}

inserting_object_sym := g.table.sym(inserting_object_type)
for i, mut sub in subs {
if subs_unwrapped_c_typ[i].len > 0 {
var := '${node.object_var}${member_access_type}${sub.object_var}'
Expand Down Expand Up @@ -418,6 +421,10 @@ fn (mut g Gen) write_orm_insert_with_last_ids(node ast.SqlStmtLine, connection_v
var := '${node.object_var}${member_access_type}${c_name(field.name)}'
if field.typ.has_flag(.option) {
g.writeln('${var}.state == 2? _const_orm__null_primitive : orm__${typ}_to_primitive(*(${ctyp}*)(${var}.data)),')
} else if inserting_object_sym.kind == .sum_type {
table_sym := g.table.sym(node.table_expr.typ)
sum_type_var := '(*${node.object_var}._${table_sym.cname})${member_access_type}${c_name(field.name)}'
g.writeln('orm__${typ}_to_primitive(${sum_type_var}),')
} else {
g.writeln('orm__${typ}_to_primitive(${var}),')
}
Expand Down
Loading