Skip to content

Commit fcd2eed

Browse files
authored
orm: fix default value quote (fix #24052) (#24057)
1 parent 0056d55 commit fcd2eed

2 files changed

Lines changed: 27 additions & 13 deletions

File tree

‎vlib/orm/orm_null_test.v‎

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,13 @@ struct Foo {
103103
mut:
104104
id u64 @[primary; sql: serial]
105105
a string
106-
// b string [default: '"yes"']
107-
c ?string
108-
d ?string = 'hi'
109-
e int
110-
// f int [default: 33]
111-
g ?int
112-
h ?int = 55
106+
b string @[default: '"yes"']
107+
c ?string
108+
d ?string = 'hi'
109+
e int
110+
f int @[default: 33]
111+
g ?int
112+
h ?int = 55
113113
}
114114

115115
fn test_option_struct_fields_and_none() {
@@ -118,12 +118,12 @@ fn test_option_struct_fields_and_none() {
118118
sql db {
119119
create table Foo
120120
}!
121-
assert db.st.last == 'CREATE TABLE IF NOT EXISTS `foo` (`id` serial-type NOT NULL, `a` string-type NOT NULL, `c` string-type, `d` string-type, `e` int-type NOT NULL, `g` int-type, `h` int-type, PRIMARY KEY(`id`));'
121+
assert db.st.last == 'CREATE TABLE IF NOT EXISTS `foo` (`id` serial-type NOT NULL, `a` string-type NOT NULL, `b` string-type DEFAULT "yes" NOT NULL, `c` string-type, `d` string-type, `e` int-type NOT NULL, `f` int-type DEFAULT 33 NOT NULL, `g` int-type, `h` int-type, PRIMARY KEY(`id`));'
122122

123123
_ := sql db {
124124
select from Foo where e > 5 && c is none && c !is none && h == 2
125125
}!
126-
assert db.st.last == 'SELECT `id`, `a`, `c`, `d`, `e`, `g`, `h` FROM `foo` WHERE `e` > ? AND `c` IS NULL AND `c` IS NOT NULL AND `h` = ?;'
126+
assert db.st.last == 'SELECT `id`, `a`, `b`, `c`, `d`, `e`, `f`, `g`, `h` FROM `foo` WHERE `e` > ? AND `c` IS NULL AND `c` IS NOT NULL AND `h` = ?;'
127127
assert db.st.data.len == 0
128128
assert db.st.where.len == 2
129129
assert db.st.where == [orm.Primitive(int(5)), orm.Primitive(int(2))]
@@ -141,17 +141,19 @@ fn test_option_struct_fields_and_none() {
141141
res1 := sql db {
142142
select from Foo where id == id
143143
}!
144-
assert db.st.last == 'SELECT `id`, `a`, `c`, `d`, `e`, `g`, `h` FROM `foo` WHERE `id` = ?;'
144+
assert db.st.last == 'SELECT `id`, `a`, `b`, `c`, `d`, `e`, `f`, `g`, `h` FROM `foo` WHERE `id` = ?;'
145145
assert db.st.data.len == 0
146146
assert db.st.where.len == 1
147147
assert db.st.where == [orm.Primitive(int(id))]
148148
assert res1.len == 1
149149
assert res1[0] == Foo{
150150
id: 1
151151
a: ''
152+
b: 'yes'
152153
c: none
153154
d: 'hi'
154155
e: 0
156+
f: 33
155157
g: none
156158
h: 55
157159
}
@@ -168,27 +170,35 @@ fn test_option_struct_fields_and_none() {
168170
res2 := sql db {
169171
select from Foo where id == id
170172
}!
171-
assert db.st.last == 'SELECT `id`, `a`, `c`, `d`, `e`, `g`, `h` FROM `foo` WHERE `id` = ?;'
173+
assert db.st.last == 'SELECT `id`, `a`, `b`, `c`, `d`, `e`, `f`, `g`, `h` FROM `foo` WHERE `id` = ?;'
172174
assert db.st.data.len == 0
173175
assert db.st.where.len == 1
174176
assert db.st.where == [orm.Primitive(int(id))]
175177
assert res2.len == 1
176178
assert res2[0] == Foo{
177179
id: 1
178180
a: ''
181+
b: 'yes'
179182
c: 'yo'
180183
d: none
181184
e: 0
185+
f: 33
182186
g: 44
183187
h: none
184188
}
185189

186190
assert sql db {
187191
select count from Foo where a == 'yo'
188192
}! == 0
193+
assert sql db {
194+
select count from Foo where b == 'yes'
195+
}! == 1
189196
assert sql db {
190197
select count from Foo where d == 'yo'
191198
}! == 0
199+
assert sql db {
200+
select count from Foo where f == 33
201+
}! == 1
192202
assert sql db {
193203
select count from Foo where c == 'yo'
194204
}! == 1

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,13 @@ fn (mut g Gen) write_orm_create_table(node ast.SqlStmtLine, table_name string, c
177177
for attr in field.attrs {
178178
g.write('(VAttribute){')
179179
g.indent++
180-
g.write(' .name = _SLIT("${attr.name}"),')
180+
name1 := util.smart_quote(attr.name, false)
181+
name := cescape_nonascii(name1)
182+
g.write(' .name = _SLIT("${name}"),')
181183
g.write(' .has_arg = ${attr.has_arg},')
182-
g.write(' .arg = _SLIT("${attr.arg}"),')
184+
arg1 := util.smart_quote(attr.arg, false)
185+
arg := cescape_nonascii(arg1)
186+
g.write(' .arg = _SLIT("${arg}"),')
183187
g.write(' .kind = ${int(attr.kind)},')
184188
g.indent--
185189
g.write('},')

0 commit comments

Comments
 (0)