Skip to content

Commit 6832df1

Browse files
authored
orm: fix gen sql complex where (fix #24136) (#24138)
1 parent 4872e3c commit 6832df1

3 files changed

Lines changed: 55 additions & 12 deletions

File tree

‎.github/workflows/docker_ci.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ jobs:
7373
echo $VFLAGS
7474
./v cmd/tools/test_if_v_test_system_works.v
7575
./cmd/tools/test_if_v_test_system_works
76+
- name: Add dependencies
77+
run: apt install libsqlite3-dev
7678
- name: All code is formatted
7779
run: ./v -silent test-cleancode
7880
- name: Test V fixed tests

‎vlib/orm/orm.v‎

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -392,17 +392,13 @@ pub fn orm_select_gen(cfg SelectConfig, q string, num bool, qm string, start_pos
392392

393393
fn gen_where_clause(where QueryData, q string, qm string, num bool, mut c &int) string {
394394
mut str := ''
395+
395396
for i, field in where.fields {
396-
mut pre_par := false
397-
mut post_par := false
398-
for par in where.parentheses {
399-
if i in par {
400-
pre_par = par[0] == i
401-
post_par = par[1] == i
402-
}
403-
}
404-
if pre_par {
405-
str += '('
397+
current_pre_par := where.parentheses.count(it[0] == i)
398+
current_post_par := where.parentheses.count(it[1] == i)
399+
400+
if current_pre_par > 0 {
401+
str += ' ( '.repeat(current_pre_par)
406402
}
407403
str += '${q}${field}${q} ${where.kinds[i].to_str()}'
408404
if !where.kinds[i].is_unary() {
@@ -412,8 +408,8 @@ fn gen_where_clause(where QueryData, q string, qm string, num bool, mut c &int)
412408
c++
413409
}
414410
}
415-
if post_par {
416-
str += ')'
411+
if current_post_par > 0 {
412+
str += ' ) '.repeat(current_post_par)
417413
}
418414
if i < where.fields.len - 1 {
419415
if where.is_and[i] {

‎vlib/orm/orm_complex_where_test.v‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// vtest build: present_sqlite3?
2+
import db.sqlite
3+
4+
struct ComplexWhere {
5+
pub mut:
6+
id int
7+
name string
8+
rank f32
9+
}
10+
11+
fn test_create_without_id_field() {
12+
db := sqlite.connect(':memory:')!
13+
14+
sql db {
15+
create table ComplexWhere
16+
}!
17+
18+
datas := [
19+
ComplexWhere{
20+
id: 0
21+
name: 'test1'
22+
rank: 1.5
23+
},
24+
ComplexWhere{
25+
id: 1
26+
name: 'test2'
27+
rank: 2.5
28+
},
29+
ComplexWhere{
30+
id: 2
31+
name: 'test3'
32+
rank: 3.5
33+
},
34+
]
35+
36+
for data in datas {
37+
sql db {
38+
insert data into ComplexWhere
39+
}!
40+
}
41+
42+
res := sql db {
43+
select from ComplexWhere where name == 'a' && (id > 1 || (rank > 2.5 && rank < 3.33))
44+
} or { assert false, err.msg() }
45+
}

0 commit comments

Comments
 (0)