Skip to content

Commit 9cb170e

Browse files
committed
Support complext where with parenthesis
1 parent df43363 commit 9cb170e

File tree

4 files changed

+129
-3
lines changed

4 files changed

+129
-3
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.0.5"
3+
version = "1.0.6"
44
author = "ThomasTJdev"
55
description = "SQL builder"
66
license = "MIT"

src/sqlbuilderpkg/select.nim

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ proc sqlSelectConstWhere(where: varargs[string], usePrepared: NimNode): string =
7777

7878

7979
if v.len() > 0:
80+
#
81+
# If this is self-contained where with parenthesis in front and back
82+
# add it and continue
83+
#
84+
# => (xx = yy AND zz = qq)
85+
if v[0] == '(' and v[v.high] == ')':
86+
wes.add(v)
87+
continue
88+
8089

8190
if needParenthesis:
8291
wes.add("(")
@@ -137,7 +146,12 @@ proc sqlSelectConstWhere(where: varargs[string], usePrepared: NimNode): string =
137146
# Value included already
138147
if eSplit.len() == 2 and eSplit[0].strip().len() > 0 and eSplit[1].strip().len() > 0:
139148
if boolVal(usePrepared):
140-
prepareCount += 1
149+
wes.add(v)
150+
else:
151+
wes.add(v)
152+
# If there's multiple elements
153+
elif eSplit.len() > 2 and eSplit[eSplit.high].len() > 1:
154+
if boolVal(usePrepared):
141155
wes.add(v)
142156
else:
143157
wes.add(v)
@@ -528,6 +542,15 @@ proc sqlSelect*(
528542
wes.add(" AND ")
529543

530544
if d != "":
545+
#
546+
# If this is self-contained where with parenthesis in front and back
547+
# add it and continue
548+
#
549+
# => (xx = yy AND zz = qq)
550+
if d[0] == '(' and d[d.high] == ')':
551+
wes.add(d)
552+
continue
553+
531554
let dataUpper = d.toUpperAscii()
532555
let needParenthesis = dataUpper.contains(" OR ") or dataUpper.contains(" AND ")
533556

@@ -594,7 +617,12 @@ proc sqlSelect*(
594617
# Value included already
595618
if eSplit.len() == 2 and eSplit[0].strip().len() > 0 and eSplit[1].strip().len() > 0:
596619
if usePrepared:
597-
prepareCount += 1
620+
wes.add(d)
621+
else:
622+
wes.add(d)
623+
# If there's multiple elements
624+
elif eSplit.len() > 2 and eSplit[eSplit.high].len() > 1:
625+
if usePrepared:
598626
wes.add(d)
599627
else:
600628
wes.add(d)

tests/select/test_select.nim

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,54 @@ suite "test where cases custom formatting":
619619

620620

621621

622+
test "where - complex where item - with parenthesis around":
623+
624+
let test = sqlSelect(
625+
table = "history",
626+
tableAs = "history",
627+
select = [
628+
"person.name as user_id",
629+
"history.creation"
630+
],
631+
where = [
632+
"history.project_id =",
633+
"history.item_id =",
634+
"history.is_deleted IS NULL",
635+
"(history.choice = 'Comment' OR history.choice = 'Picture' OR history.choice = 'File' OR history.choice = 'Design' OR history.choice = 'Update' OR history.choice = 'Create')"
636+
],
637+
joinargs = [
638+
(table: "person", tableAs: "", on: @["history.user_id = person.id"])
639+
],
640+
customSQL = "ORDER BY history.creation DESC, history.id DESC"
641+
)
642+
643+
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"))
644+
645+
646+
647+
test "where - complex where item - without parenthesis around":
648+
649+
let test = sqlSelect(
650+
table = "history",
651+
tableAs = "history",
652+
select = [
653+
"person.name as user_id",
654+
"history.creation"
655+
],
656+
where = [
657+
"history.project_id =",
658+
"history.item_id =",
659+
"history.is_deleted IS NULL",
660+
"history.choice = 'Comment' OR history.choice = 'Picture' OR history.choice = 'File' OR history.choice = 'Design' OR history.choice = 'Update' OR history.choice = 'Create'"
661+
],
662+
joinargs = [
663+
(table: "person", tableAs: "", on: @["history.user_id = person.id"])
664+
],
665+
customSQL = "ORDER BY history.creation DESC, history.id DESC"
666+
)
667+
668+
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"))
669+
622670

623671

624672
suite "test using DB names for columns":

tests/select/test_select_const_where.nim

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,53 @@ suite "test where cases custom formatting":
202202

203203

204204

205+
test "where - complex where item - with parenthesis around":
206+
207+
let test = sqlSelectConst(
208+
table = "history",
209+
tableAs = "history",
210+
select = [
211+
"person.name as user_id",
212+
"history.creation"
213+
],
214+
where = [
215+
"history.project_id =",
216+
"history.item_id =",
217+
"history.is_deleted IS NULL",
218+
"(history.choice = 'Comment' OR history.choice = 'Picture' OR history.choice = 'File' OR history.choice = 'Design' OR history.choice = 'Update' OR history.choice = 'Create')"
219+
],
220+
joinargs = [
221+
(table: "person", tableAs: "", on: @["history.user_id = person.id"])
222+
],
223+
customSQL = "ORDER BY history.creation DESC, history.id DESC"
224+
)
225+
226+
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"))
227+
228+
229+
230+
test "where - complex where item - without parenthesis around":
231+
232+
let test = sqlSelectConst(
233+
table = "history",
234+
tableAs = "history",
235+
select = [
236+
"person.name as user_id",
237+
"history.creation"
238+
],
239+
where = [
240+
"history.project_id =",
241+
"history.item_id =",
242+
"history.is_deleted IS NULL",
243+
"history.choice = 'Comment' OR history.choice = 'Picture' OR history.choice = 'File' OR history.choice = 'Design' OR history.choice = 'Update' OR history.choice = 'Create'"
244+
],
245+
joinargs = [
246+
(table: "person", tableAs: "", on: @["history.user_id = person.id"])
247+
],
248+
customSQL = "ORDER BY history.creation DESC, history.id DESC"
249+
)
250+
251+
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"))
252+
253+
254+

0 commit comments

Comments
 (0)