Skip to content

Commit 690f845

Browse files
authored
cgen: fix nested array support for the orm (fix #19327) (#24080)
1 parent 0e93a12 commit 690f845

3 files changed

Lines changed: 136 additions & 1 deletion

File tree

‎cmd/tools/vtest-self.v‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ const skip_with_fsanitize_memory = [
149149
'vlib/orm/orm_result_test.v',
150150
'vlib/orm/orm_custom_operators_test.v',
151151
'vlib/orm/orm_fk_test.v',
152+
'vlib/orm/orm_nested_struct_test.v',
152153
'vlib/orm/orm_references_test.v',
153154
'vlib/orm/orm_option_array_test.v',
154155
'vlib/orm/orm_option_time_test.v',
@@ -238,6 +239,7 @@ const skip_on_ubuntu_musl = [
238239
'vlib/orm/orm_result_test.v',
239240
'vlib/orm/orm_custom_operators_test.v',
240241
'vlib/orm/orm_fk_test.v',
242+
'vlib/orm/orm_nested_struct_test.v',
241243
'vlib/orm/orm_references_test.v',
242244
'vlib/orm/orm_option_array_test.v',
243245
'vlib/orm/orm_option_time_test.v',

‎vlib/orm/orm_nested_struct_test.v‎

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
// vtest flaky: true
2+
// vtest retry: 3
3+
import db.sqlite
4+
5+
struct Address {
6+
id int @[primary; sql: serial]
7+
parent_id int
8+
create_at string
9+
update_at string
10+
street string
11+
city string
12+
state string
13+
zip_code string
14+
proximity string
15+
}
16+
17+
@[table: 'RealStates']
18+
struct RealState {
19+
id string @[primary; sql_type: 'uuid']
20+
parent_id int
21+
name string @[sql_type: 'varchar(80)']
22+
cnpj string @[sql_type: 'varchar(14)']
23+
address []Address @[fkey: 'parent_id']
24+
}
25+
26+
@[table: 'Realtors']
27+
struct Realtor {
28+
id ?string @[primary; sql_type: 'uuid']
29+
first_name string @[sql_type: 'VARCHAR(30)']
30+
last_name string @[sql_type: 'VARCHAR(30)']
31+
creci string @[sql_type: 'VARCHAR(8)']
32+
cnpj ?string @[sql_type: 'VARCHAR(15)']
33+
cpf ?string @[sql_type: 'VARCHAR(12)']
34+
phone string @[sql_type: 'VARCHAR(15)']
35+
real_state RealState @[fkey: 'id']
36+
}
37+
38+
fn test_orm_nested_struct() {
39+
mut db := sqlite.connect(':memory:')!
40+
41+
data := Realtor{
42+
first_name: 'John'
43+
last_name: 'Doe'
44+
creci: '12345678'
45+
cnpj: '12345678901234'
46+
cpf: '12345678901'
47+
phone: '1234567890'
48+
real_state: RealState{
49+
name: 'Teste'
50+
cnpj: '12345678901234'
51+
address: [
52+
Address{
53+
street: 'Teste'
54+
city: 'Teste'
55+
state: 'Teste'
56+
zip_code: 'Teste'
57+
proximity: 'Teste'
58+
},
59+
]
60+
}
61+
}
62+
63+
sql db {
64+
create table Address
65+
create table RealState
66+
create table Realtor
67+
}!
68+
69+
sql db {
70+
insert data into Realtor
71+
}!
72+
73+
x1 := sql db {
74+
select from Address
75+
}!
76+
assert x1.str() == "[Address{
77+
id: 1
78+
parent_id: 0
79+
create_at: ''
80+
update_at: ''
81+
street: 'Teste'
82+
city: 'Teste'
83+
state: 'Teste'
84+
zip_code: 'Teste'
85+
proximity: 'Teste'
86+
}]"
87+
88+
x2 := sql db {
89+
select from RealState
90+
}!
91+
assert x2.str() == "[RealState{
92+
id: ''
93+
parent_id: 0
94+
name: 'Teste'
95+
cnpj: '12345678901234'
96+
address: [Address{
97+
id: 1
98+
parent_id: 0
99+
create_at: ''
100+
update_at: ''
101+
street: 'Teste'
102+
city: 'Teste'
103+
state: 'Teste'
104+
zip_code: 'Teste'
105+
proximity: 'Teste'
106+
}]
107+
}]"
108+
109+
x3 := sql db {
110+
select from Realtor
111+
}!
112+
113+
// FIXME!
114+
// I think this result is not correct.
115+
assert x3.str() == "[Realtor{
116+
id: Option(none)
117+
first_name: 'John'
118+
last_name: 'Doe'
119+
creci: '12345678'
120+
cnpj: Option('12345678901234')
121+
cpf: Option('12345678901')
122+
phone: '1234567890'
123+
real_state: RealState{
124+
id: ''
125+
parent_id: 0
126+
name: ''
127+
cnpj: ''
128+
address: []
129+
}
130+
}]"
131+
132+
db.close()!
133+
}

‎vlib/v/gen/c/orm.v‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1140,7 +1140,7 @@ fn (mut g Gen) write_orm_select(node ast.SqlExpr, connection_var_name string, re
11401140
g.writeln('\t${field_var}.state = 0;')
11411141
g.writeln('\t*(${g.base_type(field.typ)}*)${field_var}.data = *(${g.base_type(field.typ)}*)${sub_result_var}.data;')
11421142
} else {
1143-
g.writeln('\t${field_var} = *(${unwrapped_c_typ}*)${sub_result_var}.data;')
1143+
g.writeln('\t${field_var} = *(${g.base_type(field.typ)}*)${sub_result_var}.data;')
11441144
}
11451145
g.writeln('}')
11461146
}

0 commit comments

Comments
 (0)