Skip to content

Commit

Permalink
Support complext where with parenthesis
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasTJdev committed Feb 24, 2024
1 parent df43363 commit 9cb170e
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 3 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.5"
version = "1.0.6"
author = "ThomasTJdev"
description = "SQL builder"
license = "MIT"
Expand Down
32 changes: 30 additions & 2 deletions src/sqlbuilderpkg/select.nim
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ proc sqlSelectConstWhere(where: varargs[string], usePrepared: NimNode): string =


if v.len() > 0:
#
# If this is self-contained where with parenthesis in front and back
# add it and continue
#
# => (xx = yy AND zz = qq)
if v[0] == '(' and v[v.high] == ')':
wes.add(v)
continue


if needParenthesis:
wes.add("(")
Expand Down Expand Up @@ -137,7 +146,12 @@ proc sqlSelectConstWhere(where: varargs[string], usePrepared: NimNode): string =
# 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)
# If there's multiple elements
elif eSplit.len() > 2 and eSplit[eSplit.high].len() > 1:
if boolVal(usePrepared):
wes.add(v)
else:
wes.add(v)
Expand Down Expand Up @@ -528,6 +542,15 @@ proc sqlSelect*(
wes.add(" AND ")

if d != "":
#
# If this is self-contained where with parenthesis in front and back
# add it and continue
#
# => (xx = yy AND zz = qq)
if d[0] == '(' and d[d.high] == ')':
wes.add(d)
continue

let dataUpper = d.toUpperAscii()
let needParenthesis = dataUpper.contains(" OR ") or dataUpper.contains(" AND ")

Expand Down Expand Up @@ -594,7 +617,12 @@ proc sqlSelect*(
# 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)
# If there's multiple elements
elif eSplit.len() > 2 and eSplit[eSplit.high].len() > 1:
if usePrepared:
wes.add(d)
else:
wes.add(d)
Expand Down
48 changes: 48 additions & 0 deletions tests/select/test_select.nim
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,54 @@ suite "test where cases custom formatting":



test "where - complex where item - with parenthesis around":

let test = sqlSelect(
table = "history",
tableAs = "history",
select = [
"person.name as user_id",
"history.creation"
],
where = [
"history.project_id =",
"history.item_id =",
"history.is_deleted IS NULL",
"(history.choice = 'Comment' OR history.choice = 'Picture' OR history.choice = 'File' OR history.choice = 'Design' OR history.choice = 'Update' OR history.choice = 'Create')"
],
joinargs = [
(table: "person", tableAs: "", on: @["history.user_id = person.id"])
],
customSQL = "ORDER BY history.creation DESC, history.id DESC"
)

check querycompare(test, sql("SELECT person.name as user_id, history.creation FROM history LEFT JOIN person ON (history.user_id = person.id) WHERE history.project_id = ? AND history.item_id = ? AND history.is_deleted IS NULL AND (history.choice = 'Comment' OR history.choice = 'Picture' OR history.choice = 'File' OR history.choice = 'Design' OR history.choice = 'Update' OR history.choice = 'Create') ORDER BY history.creation DESC, history.id DESC"))



test "where - complex where item - without parenthesis around":

let test = sqlSelect(
table = "history",
tableAs = "history",
select = [
"person.name as user_id",
"history.creation"
],
where = [
"history.project_id =",
"history.item_id =",
"history.is_deleted IS NULL",
"history.choice = 'Comment' OR history.choice = 'Picture' OR history.choice = 'File' OR history.choice = 'Design' OR history.choice = 'Update' OR history.choice = 'Create'"
],
joinargs = [
(table: "person", tableAs: "", on: @["history.user_id = person.id"])
],
customSQL = "ORDER BY history.creation DESC, history.id DESC"
)

check querycompare(test, sql("SELECT person.name as user_id, history.creation FROM history LEFT JOIN person ON (history.user_id = person.id) WHERE history.project_id = ? AND history.item_id = ? AND history.is_deleted IS NULL AND (history.choice = 'Comment' OR history.choice = 'Picture' OR history.choice = 'File' OR history.choice = 'Design' OR history.choice = 'Update' OR history.choice = 'Create') ORDER BY history.creation DESC, history.id DESC"))



suite "test using DB names for columns":
Expand Down
50 changes: 50 additions & 0 deletions tests/select/test_select_const_where.nim
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,53 @@ suite "test where cases custom formatting":



test "where - complex where item - with parenthesis around":

let test = sqlSelectConst(
table = "history",
tableAs = "history",
select = [
"person.name as user_id",
"history.creation"
],
where = [
"history.project_id =",
"history.item_id =",
"history.is_deleted IS NULL",
"(history.choice = 'Comment' OR history.choice = 'Picture' OR history.choice = 'File' OR history.choice = 'Design' OR history.choice = 'Update' OR history.choice = 'Create')"
],
joinargs = [
(table: "person", tableAs: "", on: @["history.user_id = person.id"])
],
customSQL = "ORDER BY history.creation DESC, history.id DESC"
)

check querycompare(test, sql("SELECT person.name as user_id, history.creation FROM history LEFT JOIN person ON (history.user_id = person.id) WHERE history.project_id = ? AND history.item_id = ? AND history.is_deleted IS NULL AND (history.choice = 'Comment' OR history.choice = 'Picture' OR history.choice = 'File' OR history.choice = 'Design' OR history.choice = 'Update' OR history.choice = 'Create') ORDER BY history.creation DESC, history.id DESC"))



test "where - complex where item - without parenthesis around":

let test = sqlSelectConst(
table = "history",
tableAs = "history",
select = [
"person.name as user_id",
"history.creation"
],
where = [
"history.project_id =",
"history.item_id =",
"history.is_deleted IS NULL",
"history.choice = 'Comment' OR history.choice = 'Picture' OR history.choice = 'File' OR history.choice = 'Design' OR history.choice = 'Update' OR history.choice = 'Create'"
],
joinargs = [
(table: "person", tableAs: "", on: @["history.user_id = person.id"])
],
customSQL = "ORDER BY history.creation DESC, history.id DESC"
)

check querycompare(test, sql("SELECT person.name as user_id, history.creation FROM history LEFT JOIN person ON (history.user_id = person.id) WHERE history.project_id = ? AND history.item_id = ? AND history.is_deleted IS NULL AND (history.choice = 'Comment' OR history.choice = 'Picture' OR history.choice = 'File' OR history.choice = 'Design' OR history.choice = 'Update' OR history.choice = 'Create') ORDER BY history.creation DESC, history.id DESC"))



0 comments on commit 9cb170e

Please sign in to comment.