mirror of
https://github.com/vlang/v.git
synced 2025-09-13 14:32:26 +03:00
parent
4872e3cff0
commit
6832df175d
3 changed files with 55 additions and 12 deletions
|
@ -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] {
|
||||
|
|
45
vlib/orm/orm_complex_where_test.v
Normal file
45
vlib/orm/orm_complex_where_test.v
Normal 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() }
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue