Skip to content

Commit

Permalink
Allow for specifying specific values in where=[] to avoid ? substitution
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasTJdev committed Jan 31, 2024
1 parent 9fb8222 commit 52c61d6
Show file tree
Hide file tree
Showing 5 changed files with 326 additions and 10 deletions.
3 changes: 2 additions & 1 deletion sqlbuilder.nimble
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Package

version = "1.0.4"
version = "1.0.5"
author = "ThomasTJdev"
description = "SQL builder"
license = "MIT"
Expand Down Expand Up @@ -33,6 +33,7 @@ proc runSelect() =
exec "nim c -d:dev -r tests/select/test_select_deletemarker.nim"
exec "nim c -d:dev -r tests/select/test_select_const.nim"
exec "nim c -d:dev -r tests/select/test_select_const_deletemarker.nim"
exec "nim c -d:dev -r tests/select/test_select_const_where.nim"

task testselect, "Test select statement":
runSelect()
Expand Down
42 changes: 41 additions & 1 deletion src/sqlbuilderpkg/select.nim
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,11 @@ proc sqlSelectConstWhere(where: varargs[string], usePrepared: NimNode): string =
elif v.len() >= 5 and dataUpper[(v.high - 4)..v.high] == " NULL":
wes.add(v)

# => ... = TRUE
elif v.len() >= 5 and dataUpper[(v.high - 4)..v.high] == " TRUE":
wes.add(v)

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

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

# => x = y
elif v.len() >= 5 and v.contains(" = "):
let eSplit = v.split(" = ")
# Value included already
if eSplit.len() == 2 and eSplit[0].strip().len() > 0 and eSplit[1].strip().len() > 0:
if boolVal(usePrepared):
prepareCount += 1
wes.add(v)
else:
wes.add(v)
# Insert ?
else:
if boolVal(usePrepared):
prepareCount += 1
wes.add(v & " $" & $prepareCount)
else:
wes.add(v & " ?")

# => ... = ?
else:
if boolVal(usePrepared):
Expand Down Expand Up @@ -514,7 +534,7 @@ proc sqlSelect*(
if needParenthesis:
wes.add("(")


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

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

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

Expand Down Expand Up @@ -566,6 +588,24 @@ proc sqlSelect*(
else:
wes.add("? " & d)

# => x = y
elif d.len() >= 5 and d.contains(" = "):
let eSplit = d.split(" = ")
# Value included already
if eSplit.len() == 2 and eSplit[0].strip().len() > 0 and eSplit[1].strip().len() > 0:
if usePrepared:
prepareCount += 1
wes.add(d)
else:
wes.add(d)
# Insert ?
else:
if usePrepared:
prepareCount += 1
wes.add(d & " $" & $prepareCount)
else:
wes.add(d & " ?")

# => ... = ?
else:
if usePrepared:
Expand Down
85 changes: 78 additions & 7 deletions tests/select/test_select.nim
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,6 @@ suite "test where cases custom formatting":
check querycompare(test, resPrepared)



test "where OR OR":
var test: SqlQuery

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


test "where x = 'y'":

let test = sqlSelect(
table = "history",
tableAs = "h",
select = [
"h.uuid",
"h.text",
"h.creation",
"person.name"
],
where = [
"h.project_id =",
"h.item_id =",
"h.element = 'tasks'",
],
joinargs = [
(table: "person", tableAs: "person", on: @["person.id = h.user_id"]),
],
customSQL = "ORDER BY h.creation DESC",
)

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"))


test "where x = 'y' and x = 'y' and x = ::int":

let test = sqlSelect(
table = "history",
tableAs = "h",
select = [
"h.uuid",
"h.text",
"h.creation",
"person.name"
],
where = [
"h.project_id =",
"h.item_id = 33",
"h.element = 'tasks'",
"h.data = 'special'",
"h.ident = 99",
],
joinargs = [
(table: "person", tableAs: "person", on: @["person.id = h.user_id"]),
],
customSQL = "ORDER BY h.creation DESC",
)

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"))



test "where x = 'y' and x = 'y' and x = ::int with fake spaces":

let test = sqlSelect(
table = "history",
tableAs = "h",
select = [
"h.uuid",
"h.text",
"h.creation",
"person.name"
],
where = [
"h.project_id = ",
"h.item_id = ",
"h.data = ",
"h.ident = 33 ",
],
joinargs = [
(table: "person", tableAs: "person", on: @["person.id = h.user_id"]),
],
customSQL = "ORDER BY h.creation DESC",
)

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"))




Expand Down Expand Up @@ -577,9 +654,3 @@ suite "catch bad formats":









2 changes: 1 addition & 1 deletion tests/select/test_select_const.nim
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ suite "test sqlSelectConst":




suite "test sqlSelectConst - joins":

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

Loading

0 comments on commit 52c61d6

Please sign in to comment.