Skip to content

Commit 9fb8222

Browse files
committed
Implement IS TRUE and IS FALSE in SELECT where
1 parent 2513c49 commit 9fb8222

4 files changed

Lines changed: 115 additions & 1 deletion

File tree

sqlbuilder.nimble

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Package
22

3-
version = "1.0.3"
3+
version = "1.0.4"
44
author = "ThomasTJdev"
55
description = "SQL builder"
66
license = "MIT"
@@ -29,6 +29,7 @@ task testlegacy, "Test legacy":
2929
proc runSelect() =
3030
exec "nim c -d:dev -r tests/select/test_select.nim"
3131
exec "nim c -d:dev -r tests/select/test_select_arrays.nim"
32+
exec "nim c -d:dev -r tests/select/test_select_is.nim"
3233
exec "nim c -d:dev -r tests/select/test_select_deletemarker.nim"
3334
exec "nim c -d:dev -r tests/select/test_select_const.nim"
3435
exec "nim c -d:dev -r tests/select/test_select_const_deletemarker.nim"

src/sqlbuilderpkg/select.nim

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ proc sqlSelectConstWhere(where: varargs[string], usePrepared: NimNode): string =
9999
elif v.len() >= 5 and dataUpper[(v.high - 4)..v.high] == " NULL":
100100
wes.add(v)
101101

102+
elif v.len() >= 5 and dataUpper[(v.high - 4)..v.high] == " TRUE":
103+
wes.add(v)
104+
105+
elif v.len() >= 6 and dataUpper[(v.high - 5)..v.high] == " FALSE":
106+
wes.add(v)
107+
102108
# => ? = ANY(...)
103109
elif v.len() > 5 and dataUpper[0..4] == "= ANY":
104110
if boolVal(usePrepared):
@@ -530,6 +536,12 @@ proc sqlSelect*(
530536
elif d.len() >= 5 and dataUpper[(d.high - 4)..d.high] == " NULL":
531537
wes.add(d)
532538

539+
elif d.len() > 5 and dataUpper[(d.high - 4)..d.high] == " TRUE":
540+
wes.add(d)
541+
542+
elif d.len() > 6 and dataUpper[(d.high - 5)..d.high] == " FALSE":
543+
wes.add(d)
544+
533545
# => ? = ANY(...)
534546
elif d.len() > 5 and dataUpper[0..4] == "= ANY":
535547
if usePrepared:

tests/select/test_select.nim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,3 +582,4 @@ suite "catch bad formats":
582582

583583

584584

585+

tests/select/test_select_is.nim

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# Copyright Thomas T. Jarløv (TTJ)
2+
3+
when NimMajor >= 2:
4+
import db_connector/db_common
5+
else:
6+
import std/db_common
7+
8+
import
9+
std/strutils,
10+
std/unittest
11+
12+
import
13+
src/sqlbuilder,
14+
src/sqlbuilderpkg/utils_private
15+
16+
17+
18+
19+
20+
suite "IS TRUE and IS FALSE":
21+
22+
test "Simple IS TRUE":
23+
24+
let test = sqlSelect(
25+
table = "person",
26+
tableAs = "p",
27+
select = [
28+
"p.name",
29+
],
30+
where = [
31+
"p.is_active IS TRUE"
32+
]
33+
)
34+
35+
check querycompare(test, sql("SELECT p.name FROM person AS p WHERE p.is_active IS TRUE"))
36+
37+
38+
test "Simple IS NOT TRUE":
39+
40+
let test = sqlSelect(
41+
table = "person",
42+
tableAs = "p",
43+
select = [
44+
"p.name",
45+
],
46+
where = [
47+
"p.is_active IS NOT TRUE"
48+
]
49+
)
50+
51+
check querycompare(test, sql("SELECT p.name FROM person AS p WHERE p.is_active IS NOT TRUE"))
52+
53+
54+
test "Simple IS FALSE":
55+
56+
let test = sqlSelect(
57+
table = "person",
58+
tableAs = "p",
59+
select = [
60+
"p.name",
61+
],
62+
where = [
63+
"p.is_active IS FALSE"
64+
]
65+
)
66+
67+
check querycompare(test, sql("SELECT p.name FROM person AS p WHERE p.is_active IS FALSE"))
68+
69+
70+
test "Simple IS NOT FALSE":
71+
72+
let test = sqlSelect(
73+
table = "person",
74+
tableAs = "p",
75+
select = [
76+
"p.name",
77+
],
78+
where = [
79+
"p.is_active IS NOT FALSE"
80+
]
81+
)
82+
83+
check querycompare(test, sql("SELECT p.name FROM person AS p WHERE p.is_active IS NOT FALSE"))
84+
85+
86+
test "Mixed IS TRUE and IS FALSE":
87+
88+
let test = sqlSelect(
89+
table = "person",
90+
tableAs = "p",
91+
select = [
92+
"p.name",
93+
],
94+
where = [
95+
"p.is_active IS TRUE",
96+
"p.is_active IS FALSE"
97+
]
98+
)
99+
100+
check querycompare(test, sql("SELECT p.name FROM person AS p WHERE p.is_active IS TRUE AND p.is_active IS FALSE"))

0 commit comments

Comments
 (0)