Skip to content

Commit 3811112

Browse files
committed
SELECT support where <=, >= etc.
1 parent c76ad6d commit 3811112

File tree

3 files changed

+85
-42
lines changed

3 files changed

+85
-42
lines changed

sqlbuilder.nimble

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

3-
version = "1.1.0"
3+
version = "1.1.1"
44
author = "ThomasTJdev"
55
description = "SQL builder"
66
license = "MIT"

src/sqlbuilderpkg/select.nim

Lines changed: 70 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -156,27 +156,42 @@ proc sqlSelectConstWhere(where: varargs[string], usePrepared: NimNode): string =
156156
wes.add("? " & v)
157157

158158
# => x = y
159-
elif v.len() >= 5 and v.contains(" = "):
160-
let eSplit = v.split(" = ")
161-
# Value included already
162-
if eSplit.len() == 2 and eSplit[0].strip().len() > 0 and eSplit[1].strip().len() > 0:
163-
if boolVal(usePrepared):
164-
wes.add(v)
165-
else:
166-
wes.add(v)
167-
# If there's multiple elements
168-
elif eSplit.len() > 2 and eSplit[eSplit.high].len() > 1:
169-
if boolVal(usePrepared):
170-
wes.add(v)
171-
else:
172-
wes.add(v)
173-
# Insert ?
174-
else:
175-
if boolVal(usePrepared):
176-
prepareCount += 1
177-
wes.add(v & " $" & $prepareCount)
159+
elif v.len() >= 5 and (
160+
v.contains(" = ") or
161+
v.contains(" != ") or
162+
v.contains(" >= ") or
163+
v.contains(" <= ") or
164+
v.contains(" <> ") or
165+
v.contains(" > ") or
166+
v.contains(" < ")
167+
):
168+
const whereTypes = [" = ", " != ", " >= ", " <= ", " > ", " < "]
169+
for wt in whereTypes:
170+
if wt notin v:
171+
continue
172+
173+
let eSplit = v.split(wt) #" = ")
174+
# Value included already
175+
if eSplit.len() == 2 and eSplit[0].strip().len() > 0 and eSplit[1].strip().len() > 0:
176+
if boolVal(usePrepared):
177+
wes.add(v)
178+
else:
179+
wes.add(v)
180+
# If there's multiple elements
181+
elif eSplit.len() > 2 and eSplit[eSplit.high].len() > 1:
182+
if boolVal(usePrepared):
183+
wes.add(v)
184+
else:
185+
wes.add(v)
186+
# Insert ?
178187
else:
179-
wes.add(v & " ?")
188+
if boolVal(usePrepared):
189+
prepareCount += 1
190+
wes.add(v & " $" & $prepareCount)
191+
else:
192+
wes.add(v & " ?")
193+
194+
break
180195

181196
# => ... = ?
182197
else:
@@ -641,27 +656,42 @@ proc sqlSelect*(
641656
# !! Waring = pfl.action IN (2,3,4) <== not supported
642657

643658
# => x = y
644-
elif d.len() >= 5 and d.contains(" = "):
645-
let eSplit = d.split(" = ")
646-
# Value included already
647-
if eSplit.len() == 2 and eSplit[0].strip().len() > 0 and eSplit[1].strip().len() > 0:
648-
if usePrepared:
649-
wes.add(d)
650-
else:
651-
wes.add(d)
652-
# If there's multiple elements
653-
elif eSplit.len() > 2 and eSplit[eSplit.high].len() > 1:
654-
if usePrepared:
655-
wes.add(d)
656-
else:
657-
wes.add(d)
658-
# Insert ?
659-
else:
660-
if usePrepared:
661-
prepareCount += 1
662-
wes.add(d & " $" & $prepareCount)
659+
elif d.len() >= 5 and (
660+
d.contains(" = ") or
661+
d.contains(" != ") or
662+
d.contains(" >= ") or
663+
d.contains(" <= ") or
664+
d.contains(" <> ") or
665+
d.contains(" > ") or
666+
d.contains(" < ")
667+
): #d.contains(" = "):
668+
const whereTypes = [" = ", " != ", " >= ", " <= ", " > ", " < "]
669+
for wt in whereTypes:
670+
if wt notin d:
671+
continue
672+
673+
let eSplit = d.split(wt)
674+
# Value included already
675+
if eSplit.len() == 2 and eSplit[0].strip().len() > 0 and eSplit[1].strip().len() > 0:
676+
if usePrepared:
677+
wes.add(d)
678+
else:
679+
wes.add(d)
680+
# If there's multiple elements
681+
elif eSplit.len() > 2 and eSplit[eSplit.high].len() > 1:
682+
if usePrepared:
683+
wes.add(d)
684+
else:
685+
wes.add(d)
686+
# Insert ?
663687
else:
664-
wes.add(d & " ?")
688+
if usePrepared:
689+
prepareCount += 1
690+
wes.add(d & " $" & $prepareCount)
691+
else:
692+
wes.add(d & " ?")
693+
694+
break
665695

666696
# => ... = ?
667697
else:

tests/select/test_select.nim

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,18 @@ suite "test sqlSelect":
9393
check string(test).count("?") == 6
9494

9595

96+
test "WHERE statements: <=":
97+
var test: SqlQuery
98+
test = sqlSelect(
99+
table = "tasks",
100+
select = @["id", "name", "description", "created", "updated", "completed"],
101+
where = @["id =", "name !=", "updated <= NOW()", "completed IS", "description LIKE"],
102+
useDeleteMarker = false
103+
)
104+
check querycompare(test, sql("SELECT id, name, description, created, updated, completed FROM tasks WHERE id = ? AND name != ? AND updated <= NOW() AND completed IS ? AND description LIKE ? "))
105+
106+
107+
96108

97109
test "WHERE statements: = ANY(...)":
98110
var test: SqlQuery
@@ -832,4 +844,5 @@ suite "catch bad formats":
832844
p.name ASC
833845
"""
834846
)
835-
echo string(a)
847+
echo string(a)
848+

0 commit comments

Comments
 (0)