Skip to content

Commit 52c61d6

Browse files
committed
Allow for specifying specific values in where=[] to avoid ? substitution
1 parent 9fb8222 commit 52c61d6

5 files changed

Lines changed: 326 additions & 10 deletions

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.4"
3+
version = "1.0.5"
44
author = "ThomasTJdev"
55
description = "SQL builder"
66
license = "MIT"
@@ -33,6 +33,7 @@ proc runSelect() =
3333
exec "nim c -d:dev -r tests/select/test_select_deletemarker.nim"
3434
exec "nim c -d:dev -r tests/select/test_select_const.nim"
3535
exec "nim c -d:dev -r tests/select/test_select_const_deletemarker.nim"
36+
exec "nim c -d:dev -r tests/select/test_select_const_where.nim"
3637

3738
task testselect, "Test select statement":
3839
runSelect()

src/sqlbuilderpkg/select.nim

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,11 @@ 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+
# => ... = TRUE
102103
elif v.len() >= 5 and dataUpper[(v.high - 4)..v.high] == " TRUE":
103104
wes.add(v)
104105

106+
# => ... = FALSE
105107
elif v.len() >= 6 and dataUpper[(v.high - 5)..v.high] == " FALSE":
106108
wes.add(v)
107109

@@ -129,6 +131,24 @@ proc sqlSelectConstWhere(where: varargs[string], usePrepared: NimNode): string =
129131
else:
130132
wes.add("? " & v)
131133

134+
# => x = y
135+
elif v.len() >= 5 and v.contains(" = "):
136+
let eSplit = v.split(" = ")
137+
# Value included already
138+
if eSplit.len() == 2 and eSplit[0].strip().len() > 0 and eSplit[1].strip().len() > 0:
139+
if boolVal(usePrepared):
140+
prepareCount += 1
141+
wes.add(v)
142+
else:
143+
wes.add(v)
144+
# Insert ?
145+
else:
146+
if boolVal(usePrepared):
147+
prepareCount += 1
148+
wes.add(v & " $" & $prepareCount)
149+
else:
150+
wes.add(v & " ?")
151+
132152
# => ... = ?
133153
else:
134154
if boolVal(usePrepared):
@@ -514,7 +534,7 @@ proc sqlSelect*(
514534
if needParenthesis:
515535
wes.add("(")
516536

517-
537+
# Parameter substitutions included
518538
if d.contains("?"):
519539
if usePrepared:
520540
var t: string
@@ -536,9 +556,11 @@ proc sqlSelect*(
536556
elif d.len() >= 5 and dataUpper[(d.high - 4)..d.high] == " NULL":
537557
wes.add(d)
538558

559+
# => ... = TRUE
539560
elif d.len() > 5 and dataUpper[(d.high - 4)..d.high] == " TRUE":
540561
wes.add(d)
541562

563+
# => ... = FALSE
542564
elif d.len() > 6 and dataUpper[(d.high - 5)..d.high] == " FALSE":
543565
wes.add(d)
544566

@@ -566,6 +588,24 @@ proc sqlSelect*(
566588
else:
567589
wes.add("? " & d)
568590

591+
# => x = y
592+
elif d.len() >= 5 and d.contains(" = "):
593+
let eSplit = d.split(" = ")
594+
# Value included already
595+
if eSplit.len() == 2 and eSplit[0].strip().len() > 0 and eSplit[1].strip().len() > 0:
596+
if usePrepared:
597+
prepareCount += 1
598+
wes.add(d)
599+
else:
600+
wes.add(d)
601+
# Insert ?
602+
else:
603+
if usePrepared:
604+
prepareCount += 1
605+
wes.add(d & " $" & $prepareCount)
606+
else:
607+
wes.add(d & " ?")
608+
569609
# => ... = ?
570610
else:
571611
if usePrepared:

tests/select/test_select.nim

Lines changed: 78 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,6 @@ suite "test where cases custom formatting":
472472
check querycompare(test, resPrepared)
473473

474474

475-
476475
test "where OR OR":
477476
var test: SqlQuery
478477

@@ -540,6 +539,84 @@ suite "test where cases custom formatting":
540539
check querycompare(test, resPrepared)
541540

542541

542+
test "where x = 'y'":
543+
544+
let test = sqlSelect(
545+
table = "history",
546+
tableAs = "h",
547+
select = [
548+
"h.uuid",
549+
"h.text",
550+
"h.creation",
551+
"person.name"
552+
],
553+
where = [
554+
"h.project_id =",
555+
"h.item_id =",
556+
"h.element = 'tasks'",
557+
],
558+
joinargs = [
559+
(table: "person", tableAs: "person", on: @["person.id = h.user_id"]),
560+
],
561+
customSQL = "ORDER BY h.creation DESC",
562+
)
563+
564+
check querycompare(test, sql("SELECT h.uuid, h.text, h.creation, person.name FROM history AS h LEFT JOIN person ON (person.id = h.user_id) WHERE h.project_id = ? AND h.item_id = ? AND h.element = 'tasks' ORDER BY h.creation DESC"))
565+
566+
567+
test "where x = 'y' and x = 'y' and x = ::int":
568+
569+
let test = sqlSelect(
570+
table = "history",
571+
tableAs = "h",
572+
select = [
573+
"h.uuid",
574+
"h.text",
575+
"h.creation",
576+
"person.name"
577+
],
578+
where = [
579+
"h.project_id =",
580+
"h.item_id = 33",
581+
"h.element = 'tasks'",
582+
"h.data = 'special'",
583+
"h.ident = 99",
584+
],
585+
joinargs = [
586+
(table: "person", tableAs: "person", on: @["person.id = h.user_id"]),
587+
],
588+
customSQL = "ORDER BY h.creation DESC",
589+
)
590+
591+
check querycompare(test, sql("SELECT h.uuid, h.text, h.creation, person.name FROM history AS h LEFT JOIN person ON (person.id = h.user_id) WHERE h.project_id = ? AND h.item_id = 33 AND h.element = 'tasks' AND h.data = 'special' AND h.ident = 99 ORDER BY h.creation DESC"))
592+
593+
594+
595+
test "where x = 'y' and x = 'y' and x = ::int with fake spaces":
596+
597+
let test = sqlSelect(
598+
table = "history",
599+
tableAs = "h",
600+
select = [
601+
"h.uuid",
602+
"h.text",
603+
"h.creation",
604+
"person.name"
605+
],
606+
where = [
607+
"h.project_id = ",
608+
"h.item_id = ",
609+
"h.data = ",
610+
"h.ident = 33 ",
611+
],
612+
joinargs = [
613+
(table: "person", tableAs: "person", on: @["person.id = h.user_id"]),
614+
],
615+
customSQL = "ORDER BY h.creation DESC",
616+
)
617+
618+
check querycompare(test, sql("SELECT h.uuid, h.text, h.creation, person.name FROM history AS h LEFT JOIN person ON (person.id = h.user_id) WHERE h.project_id = ? AND h.item_id = ? AND h.data = ? AND h.ident = 33 ORDER BY h.creation DESC"))
619+
543620

544621

545622

@@ -577,9 +654,3 @@ suite "catch bad formats":
577654

578655

579656

580-
581-
582-
583-
584-
585-

tests/select/test_select_const.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,6 @@ suite "test sqlSelectConst":
177177

178178

179179

180-
181180
suite "test sqlSelectConst - joins":
182181

183182
test "LEFT JOIN using AS values":
@@ -690,3 +689,4 @@ suite "sqlSelectConst":
690689
tablesWithDeleteMarker = ["tasksQ", "history", "tasksitems"], #tableWithDeleteMarker
691690
)
692691
check querycompare(f, sql("SELECT t.id, t.name FROM tasks AS t WHERE t.id = ? AND t.id in (0)"))
692+

0 commit comments

Comments
 (0)