orm: fix gen sql complex where (fix #24136) (#24138)

This commit is contained in:
André Luiz 2025-04-05 09:52:59 -03:00 committed by GitHub
parent 4872e3cff0
commit 6832df175d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 55 additions and 12 deletions

View file

@ -392,17 +392,13 @@ pub fn orm_select_gen(cfg SelectConfig, q string, num bool, qm string, start_pos
fn gen_where_clause(where QueryData, q string, qm string, num bool, mut c &int) string {
mut str := ''
for i, field in where.fields {
mut pre_par := false
mut post_par := false
for par in where.parentheses {
if i in par {
pre_par = par[0] == i
post_par = par[1] == i
}
}
if pre_par {
str += '('
current_pre_par := where.parentheses.count(it[0] == i)
current_post_par := where.parentheses.count(it[1] == i)
if current_pre_par > 0 {
str += ' ( '.repeat(current_pre_par)
}
str += '${q}${field}${q} ${where.kinds[i].to_str()}'
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)
c++
}
}
if post_par {
str += ')'
if current_post_par > 0 {
str += ' ) '.repeat(current_post_par)
}
if i < where.fields.len - 1 {
if where.is_and[i] {

View file

@ -0,0 +1,45 @@
// vtest build: present_sqlite3?
import db.sqlite
struct ComplexWhere {
pub mut:
id int
name string
rank f32
}
fn test_create_without_id_field() {
db := sqlite.connect(':memory:')!
sql db {
create table ComplexWhere
}!
datas := [
ComplexWhere{
id: 0
name: 'test1'
rank: 1.5
},
ComplexWhere{
id: 1
name: 'test2'
rank: 2.5
},
ComplexWhere{
id: 2
name: 'test3'
rank: 3.5
},
]
for data in datas {
sql db {
insert data into ComplexWhere
}!
}
res := sql db {
select from ComplexWhere where name == 'a' && (id > 1 || (rank > 2.5 && rank < 3.33))
} or { assert false, err.msg() }
}