Skip to content

Commit

Permalink
Add more test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasTJdev committed Jan 17, 2024
1 parent 9606803 commit 2cdd835
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 9 deletions.
2 changes: 1 addition & 1 deletion sqlbuilder.nimble
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Package

version = "1.0.2"
version = "1.0.3"
author = "ThomasTJdev"
description = "SQL builder"
license = "MIT"
Expand Down
12 changes: 6 additions & 6 deletions src/sqlbuilderpkg/select.nim
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ proc sqlSelectConstWhere(where: varargs[string], usePrepared: NimNode): string =
wes.add(d)

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

# => ? = ANY(...)
Expand All @@ -108,15 +108,15 @@ proc sqlSelectConstWhere(where: varargs[string], usePrepared: NimNode): string =
wes.add("? " & v)

# => ... IN (?)
elif dataUpper[(v.high - 2)..v.high] == " IN":
elif v.len() >= 3 and dataUpper[(v.high - 2)..v.high] == " IN":
if boolVal(usePrepared):
prepareCount += 1
wes.add(v & " ($" & $prepareCount & ")")
else:
wes.add(v & " (?)")

# => ? IN (...)
elif v.len() > 2 and dataUpper[0..1] == "IN":
elif v.len() > 2 and dataUpper[0..2] == "IN ":
if boolVal(usePrepared):
prepareCount += 1
wes.add("$" & $prepareCount & " " & v)
Expand Down Expand Up @@ -525,7 +525,7 @@ proc sqlSelect*(
wes.add(d & " NULL")

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

# => ? = ANY(...)
Expand All @@ -537,15 +537,15 @@ proc sqlSelect*(
wes.add("? " & d)

# => ... IN (?)
elif dataUpper[(d.high - 2)..d.high] == " IN":
elif d.len() >= 3 and dataUpper[(d.high - 2)..d.high] == " IN":
if usePrepared:
prepareCount += 1
wes.add(d & " ($" & $prepareCount & ")")
else:
wes.add(d & " (?)")

# => ? IN (...)
elif d.len() > 2 and dataUpper[0..1] == "IN":
elif d.len() > 2 and dataUpper[0..2] == "IN ":
if usePrepared:
prepareCount += 1
wes.add("$" & $prepareCount & " " & d)
Expand Down
40 changes: 38 additions & 2 deletions src/sqlbuilderpkg/utils_private.nim
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,44 @@ proc hasIllegalFormats*(query: string): string =
#
let noSpaces = query.strip().replace(" ", "")

if "??" in noSpaces:
return "double insert detected. (??)"
const nospaceBad = [
"??",
"=?,?,",
"=?,AND",
"=?,OR",
"AND?,",
"OR?,",
]

for b in nospaceBad:
if b in noSpaces:
return "wrong position of ?. (" & b & ")"



#
# Bad ? substittution
#
const badSubstitutions = [
"= ? AND ?",
"= ? OR ?"
]
const badSubstitutionsAccept = [
" = ? AND ? ANY ",
" = ? AND ? IN ",
" = ? AND ? = "
]
for o in badSubstitutions:
if o in query:
var pass: bool
for b in badSubstitutionsAccept:
if b in query:
pass = true
break
if not pass:
return "bad ? substitution. (= ? AND ?)"



proc sqlWhere*(where: varargs[string]): string =
## the WHERE part of the query.
Expand Down
11 changes: 11 additions & 0 deletions tests/legacy_convert/test_legacy.nim
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,14 @@ suite "test sqlSelectMacro":
check querycompare(q1, sql("SELECT name, age FROM my-table WHERE id = ?"))




suite "test various":

test "xxx":

let q1 = sqlSelect("locker", ["name"], [""], ["project_id =", "name =", "info ="], "", "", "")

check querycompare(q1, sql("SELECT name FROM locker WHERE project_id = ? AND name = ? AND info = ?"))


36 changes: 36 additions & 0 deletions tests/select/test_select.nim
Original file line number Diff line number Diff line change
Expand Up @@ -541,3 +541,39 @@ suite "test where cases custom formatting":






suite "test using DB names for columns":

test "info => in, nonull => null, anything => any":

let test = sqlSelect(
table = "tasks",
select = @["id", "name"],
where = @["id =", "user =", "info =", "IN info", "anything =", "IN nonull"],
)
check querycompare(test, sql(" SELECT id, name FROM tasks WHERE id = ? AND user = ? AND info = ? AND ? IN info AND anything = ? AND ? IN nonull"))




suite "catch bad formats":

test "malicious ?":

const mal = [
"id = ?, AND ?",
"id = ?, OR ?",
"id = ? ?",
"id = ? AND ?",
"id = ? OR ?",
]

for m in mal:
check hasIllegalFormats(m) != ""





0 comments on commit 2cdd835

Please sign in to comment.