Skip to content

Commit 2944710

Browse files
orm: add in and not in to orm_func (fix #24639) (#24642)
1 parent 8320da2 commit 2944710

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

vlib/orm/orm_func.v

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ module orm
33
import time
44
import strings.textscanner
55

6-
const operators = ['=', '!=', '<>', '>=', '<=', '>', '<', 'LIKE', 'ILIKE', 'IS NULL', 'IS NOT NULL']!
6+
const operators = ['=', '!=', '<>', '>=', '<=', '>', '<', 'LIKE', 'ILIKE', 'IS NULL', 'IS NOT NULL',
7+
'IN', 'NOT IN']!
78

89
@[heap]
910
pub struct QueryBuilder[T] {
@@ -45,7 +46,7 @@ pub fn (qb_ &QueryBuilder[T]) reset() &QueryBuilder[T] {
4546

4647
// where create a `where` clause, it will `AND` with previous `where` clause.
4748
// valid token in the `condition` include: `field's names`, `operator`, `(`, `)`, `?`, `AND`, `OR`, `||`, `&&`,
48-
// valid `operator` incldue: `=`, `!=`, `<>`, `>=`, `<=`, `>`, `<`, `LIKE`, `ILIKE`, `IS NULL`, `IS NOT NULL`
49+
// valid `operator` incldue: `=`, `!=`, `<>`, `>=`, `<=`, `>`, `<`, `LIKE`, `ILIKE`, `IS NULL`, `IS NOT NULL`, `IN`, `NOT IN`
4950
// example: `where('(a > ? AND b <= ?) OR (c <> ? AND (x = ? OR y = ?))', a, b, c, x, y)`
5051
pub fn (qb_ &QueryBuilder[T]) where(condition string, params ...Primitive) !&QueryBuilder[T] {
5152
mut qb := unsafe { qb_ }
@@ -102,9 +103,13 @@ fn (mut ss MyTextScanner) next_tok() string {
102103
ss.pos += 7
103104
return 'IS NULL'
104105
}
106+
if ss.input[ss.pos..].starts_with('NOT IN') {
107+
ss.pos += 6
108+
return 'NOT IN'
109+
}
105110
if ss.remaining() >= 2 {
106111
two_chars := ss.input[ss.pos..ss.pos + 2]
107-
if two_chars in ['>=', '<=', '<>', '!=', '||', '&&'] {
112+
if two_chars in ['>=', '<=', '<>', '!=', '||', '&&', 'IN'] {
108113
ss.pos += 2
109114
return two_chars
110115
}
@@ -215,6 +220,12 @@ fn (qb_ &QueryBuilder[T]) parse_conditions(conds string, params []Primitive) ! {
215220
'IS NOT NULL' {
216221
OperationKind.is_not_null
217222
}
223+
'IN' {
224+
OperationKind.in
225+
}
226+
'NOT IN' {
227+
OperationKind.not_in
228+
}
218229
else {
219230
parse_error('${@FN}(): unsupported operator: `${tok}`', s.last_tok_start,
220231
conds)!

vlib/orm/orm_func_test.v

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ fn test_orm_func_where() {
9090
qb.reset()
9191
qb.where('((age = ? OR (salary > ? AND id < ?)) AND (name LIKE ?))', 1, 2, 3, '%test%')!
9292
assert qb.where.parentheses == [[1, 2], [0, 2], [3, 3], [0, 3]]
93+
94+
// in and not in
95+
qb.reset()
96+
qb.where('name IN ? AND age NOT IN ?', ['Tom'], [2])!
97+
assert qb.where.fields == ['name', 'age']
98+
assert qb.where.kinds == [.in, .not_in]
9399
}
94100

95101
fn test_orm_func_stmts() {

0 commit comments

Comments
 (0)