mirror of
https://github.com/vlang/v.git
synced 2025-09-13 22:42:26 +03:00
orm: fix subquery without where expr (#21598)
This commit is contained in:
parent
a2ce55d922
commit
1e86e06eb6
3 changed files with 133 additions and 45 deletions
|
@ -167,6 +167,7 @@ const skip_with_fsanitize_memory = [
|
||||||
'vlib/orm/orm_option_time_test.v',
|
'vlib/orm/orm_option_time_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/parent_child_test.v',
|
||||||
'vlib/db/sqlite/sqlite_vfs_lowlevel_test.v',
|
'vlib/db/sqlite/sqlite_vfs_lowlevel_test.v',
|
||||||
'vlib/v/tests/orm_enum_test.v',
|
'vlib/v/tests/orm_enum_test.v',
|
||||||
'vlib/v/tests/orm_sub_struct_test.v',
|
'vlib/v/tests/orm_sub_struct_test.v',
|
||||||
|
@ -238,6 +239,7 @@ const skip_on_ubuntu_musl = [
|
||||||
'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',
|
||||||
|
'vlib/db/sqlite/parent_child_test.v',
|
||||||
'vlib/orm/orm_test.v',
|
'vlib/orm/orm_test.v',
|
||||||
'vlib/orm/orm_sql_or_blocks_test.v',
|
'vlib/orm/orm_sql_or_blocks_test.v',
|
||||||
'vlib/orm/orm_create_and_drop_test.v',
|
'vlib/orm/orm_create_and_drop_test.v',
|
||||||
|
|
83
vlib/db/sqlite/parent_child_test.v
Normal file
83
vlib/db/sqlite/parent_child_test.v
Normal file
|
@ -0,0 +1,83 @@
|
||||||
|
import db.sqlite
|
||||||
|
|
||||||
|
struct Parent {
|
||||||
|
id int @[primary; sql: serial]
|
||||||
|
name string
|
||||||
|
children []Child @[fkey: 'parent_id']
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Child {
|
||||||
|
id int @[primary; sql: serial]
|
||||||
|
name string
|
||||||
|
parent_id int
|
||||||
|
babies []Baby @[fkey: 'child_id']
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Baby {
|
||||||
|
id int @[primary; sql: serial]
|
||||||
|
name string
|
||||||
|
child_id int
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_main() {
|
||||||
|
db := sqlite.connect(':memory:')!
|
||||||
|
|
||||||
|
sql db {
|
||||||
|
create table Parent
|
||||||
|
create table Child
|
||||||
|
create table Baby
|
||||||
|
} or { panic(err) }
|
||||||
|
|
||||||
|
new_parent := Parent{
|
||||||
|
name: 'first parent'
|
||||||
|
children: [
|
||||||
|
Child{
|
||||||
|
name: 'first child'
|
||||||
|
},
|
||||||
|
Child{
|
||||||
|
name: 'second_child'
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
sql db {
|
||||||
|
insert new_parent into Parent
|
||||||
|
} or { panic(err) }
|
||||||
|
|
||||||
|
babies := [
|
||||||
|
Baby{
|
||||||
|
name: 'first baby'
|
||||||
|
child_id: 1
|
||||||
|
},
|
||||||
|
Baby{
|
||||||
|
name: 'second baby'
|
||||||
|
child_id: 1
|
||||||
|
},
|
||||||
|
Baby{
|
||||||
|
name: 'third baby'
|
||||||
|
child_id: 2
|
||||||
|
},
|
||||||
|
Baby{
|
||||||
|
name: 'fourth baby'
|
||||||
|
child_id: 2
|
||||||
|
},
|
||||||
|
Baby{
|
||||||
|
name: 'fifth baby'
|
||||||
|
child_id: 2
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
for v in babies {
|
||||||
|
sql db {
|
||||||
|
insert v into Baby
|
||||||
|
} or { panic(err) }
|
||||||
|
}
|
||||||
|
|
||||||
|
parent := sql db {
|
||||||
|
select from Parent
|
||||||
|
} or { panic(err) }
|
||||||
|
|
||||||
|
assert parent[0].children[0].id == 1
|
||||||
|
assert parent[0].children[1].id == 2
|
||||||
|
assert parent[0].children.len == 2
|
||||||
|
}
|
|
@ -1068,6 +1068,7 @@ fn (mut g Gen) write_orm_select(node ast.SqlExpr, connection_var_name string, re
|
||||||
verror('missing fkey attribute')
|
verror('missing fkey attribute')
|
||||||
}
|
}
|
||||||
sub := node.sub_structs[field.typ]
|
sub := node.sub_structs[field.typ]
|
||||||
|
if sub.has_where {
|
||||||
mut where_expr := sub.where_expr as ast.InfixExpr
|
mut where_expr := sub.where_expr as ast.InfixExpr
|
||||||
mut left_where_expr := where_expr.left as ast.Ident
|
mut left_where_expr := where_expr.left as ast.Ident
|
||||||
mut right_where_expr := where_expr.right as ast.Ident
|
mut right_where_expr := where_expr.right as ast.Ident
|
||||||
|
@ -1083,6 +1084,7 @@ fn (mut g Gen) write_orm_select(node ast.SqlExpr, connection_var_name string, re
|
||||||
typ: (right_where_expr.info as ast.IdentVar).typ
|
typ: (right_where_expr.info as ast.IdentVar).typ
|
||||||
scope: unsafe { nil }
|
scope: unsafe { nil }
|
||||||
}
|
}
|
||||||
|
|
||||||
mut sql_expr_select_array := ast.SqlExpr{
|
mut sql_expr_select_array := ast.SqlExpr{
|
||||||
typ: field.typ.set_flag(.result)
|
typ: field.typ.set_flag(.result)
|
||||||
is_count: sub.is_count
|
is_count: sub.is_count
|
||||||
|
@ -1115,6 +1117,7 @@ fn (mut g Gen) write_orm_select(node ast.SqlExpr, connection_var_name string, re
|
||||||
g.writeln('\t${field_var} = *(${unwrapped_c_typ}*)${sub_result_var}.data;')
|
g.writeln('\t${field_var} = *(${unwrapped_c_typ}*)${sub_result_var}.data;')
|
||||||
}
|
}
|
||||||
g.writeln('}')
|
g.writeln('}')
|
||||||
|
}
|
||||||
} else if field.typ.has_flag(.option) {
|
} else if field.typ.has_flag(.option) {
|
||||||
prim_var := g.new_tmp_var()
|
prim_var := g.new_tmp_var()
|
||||||
g.writeln('orm__Primitive *${prim_var} = &${array_get_call_code};')
|
g.writeln('orm__Primitive *${prim_var} = &${array_get_call_code};')
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue