1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

orm: support parenthesized expressions as in select from User where (name == 'Sam' && is_customer == true) || id == 1 (#15693)

This commit is contained in:
Louis Schmieder
2022-09-09 20:08:48 +02:00
committed by GitHub
parent 61a4b469a3
commit f249feb9da
5 changed files with 104 additions and 18 deletions

View File

@ -107,13 +107,15 @@ fn (kind OrderType) to_str() string {
// => fields[abc, b]; data[3, 'test']; types[index of int, index of string]; kinds[.eq, .eq]; is_and[true];
// Every field, data, type & kind of operation in the expr share the same index in the arrays
// is_and defines how they're addicted to each other either and or or
// parentheses defines which fields will be inside ()
pub struct QueryData {
pub:
fields []string
data []Primitive
types []int
kinds []OperationKind
is_and []bool
fields []string
data []Primitive
types []int
parentheses [][]int
kinds []OperationKind
is_and []bool
}
pub struct InfixType {
@ -266,11 +268,25 @@ pub fn orm_stmt_gen(table string, q string, kind StmtKind, num bool, qm string,
}
if kind == .update || kind == .delete {
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 += '('
}
str += '$q$field$q ${where.kinds[i].to_str()} $qm'
if num {
str += '$c'
c++
}
if post_par {
str += ')'
}
if i < where.fields.len - 1 {
str += ' AND '
}
@ -311,11 +327,25 @@ pub fn orm_select_gen(orm SelectConfig, q string, num bool, qm string, start_pos
if orm.has_where {
str += ' WHERE '
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 += '('
}
str += '$q$field$q ${where.kinds[i].to_str()} $qm'
if num {
str += '$c'
c++
}
if post_par {
str += ')'
}
if i < where.fields.len - 1 {
if where.is_and[i] {
str += ' AND '