Skip to content

Commit 6a1462c

Browse files
authored
Merge pull request #3 from bung87/refactor2
remove `noJoin` and its usages
2 parents 5d0b04f + 4957d95 commit 6a1462c

File tree

3 files changed

+24
-26
lines changed

3 files changed

+24
-26
lines changed

src/sqlbuilderpkg/select.nim

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ import
1414

1515
from ./utils import SQLJoinType, ArgsContainer
1616

17-
const noJoin*: tuple[table: string, tableAs: string, on: seq[string]] = ("", "", @[])
18-
1917

2018
##
2119
## Constant generator utilities
@@ -33,7 +31,7 @@ proc sqlSelectConstJoin(
3331
): string =
3432
var lef = ""
3533

36-
if joinargs == [] or $(joinargs.repr) == "[]":
34+
if joinargs.len == 0:
3735
return
3836

3937
for d in joinargs:
@@ -235,7 +233,7 @@ macro sqlSelectConst*(
235233

236234
# Join table
237235
var joinTablesUsed: seq[string]
238-
if joinargs[0] != noJoin and joinargs != [] and $(joinargs.repr) != "[]":
236+
if joinargs.len != 0:
239237
for i, d in joinargs:
240238
if d.repr.len == 0:
241239
continue
@@ -276,7 +274,7 @@ macro sqlSelectConst*(
276274
# Joins
277275
#
278276
var lef = ""
279-
if joinargs[0] != noJoin:
277+
if joinargs.len != 0:
280278
lef = sqlSelectConstJoin(joinargs, jointype)
281279

282280

@@ -407,7 +405,7 @@ proc sqlSelect*(
407405

408406

409407
# Join table
410-
if joinargs.len() > 0 and joinargs[0] != noJoin:
408+
if joinargs.len() > 0:
411409
for d in joinargs:
412410
if d.table == "":
413411
continue
@@ -440,7 +438,7 @@ proc sqlSelect*(
440438
# Joins
441439
#
442440
var lef = ""
443-
if joinargs.len() > 0 and joinargs[0] != noJoin:
441+
if joinargs.len() > 0:
444442
for i, d in joinargs:
445443
lef.add(" " & $jointype & " JOIN ")
446444
lef.add(d.table & " ")
@@ -600,7 +598,7 @@ proc sqlSelect*(
600598
# Check for missing table alias
601599
if (
602600
(tableAs != "" and table != tableAs) or
603-
(joinargs.len() > 0 and joinargs[0] != noJoin)
601+
(joinargs.len() > 0)
604602
):
605603
var hit: bool
606604
for s in select:

tests/select/test_select_const.nim

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ suite "test sqlSelectConst":
2626
table = "tasks",
2727
select = ["id", "name", "description", "created", "updated", "completed"],
2828
where = ["id ="],
29-
joinargs = [noJoin],
29+
joinargs = [],
3030
useDeleteMarker = false
3131
)
3232
check querycompare(test, sql("SELECT id, name, description, created, updated, completed FROM tasks WHERE id = ? "))
@@ -41,7 +41,7 @@ suite "test sqlSelectConst":
4141
tableAs = "t",
4242
select = ["id", "name", "description", "created", "updated", "completed"],
4343
where = ["id ="],
44-
joinargs = [noJoin],
44+
joinargs = [],
4545
useDeleteMarker = false
4646
)
4747
check querycompare(test, sql("SELECT id, name, description, created, updated, completed FROM tasks AS t WHERE id = ? "))
@@ -52,7 +52,7 @@ suite "test sqlSelectConst":
5252
tableAs = "t",
5353
select = ["t.id", "t.name", "t.description", "t.created", "t.updated", "t.completed"],
5454
where = ["t.id ="],
55-
joinargs = [noJoin],
55+
joinargs = [],
5656
useDeleteMarker = false,
5757
customSQL = "ORDER BY t.created DESC"
5858
)
@@ -68,7 +68,7 @@ suite "test sqlSelectConst":
6868
tableAs = "t",
6969
select = ["id", "name", "description", "created", "updated", "completed"],
7070
where = ["id =", "name !=", "updated >", "completed IS", "description LIKE"],
71-
joinargs = [noJoin],
71+
joinargs = [],
7272
useDeleteMarker = false
7373
)
7474
check querycompare(test, sql("SELECT id, name, description, created, updated, completed FROM tasks AS t WHERE id = ? AND name != ? AND updated > ? AND completed IS ? AND description LIKE ? "))
@@ -80,7 +80,7 @@ suite "test sqlSelectConst":
8080
tableAs = "t",
8181
select = ["id", "name", "description", "created", "updated", "completed"],
8282
where = ["id =", "name !=", "updated >", "completed IS", "description LIKE"],
83-
joinargs = [noJoin],
83+
joinargs = [],
8484
customSQL = "AND name != 'test' AND created > ? ",
8585
useDeleteMarker = false
8686
)
@@ -97,7 +97,7 @@ suite "test sqlSelectConst":
9797
tableAs = "t",
9898
select = ["id", "ids_array"],
9999
where = ["id =", "= ANY(ids_array)"],
100-
joinargs = [noJoin],
100+
joinargs = [],
101101
useDeleteMarker = false
102102
)
103103
check querycompare(test, sql("SELECT id, ids_array FROM tasks AS t WHERE id = ? AND ? = ANY(ids_array) "))
@@ -113,7 +113,7 @@ suite "test sqlSelectConst":
113113
tableAs = "t",
114114
select = ["id", "ids_array"],
115115
where = ["id =", "= ANY(ids_array)", "= ANY(user_array)", "= ANY(tasks_array)"],
116-
joinargs = [noJoin],
116+
joinargs = [],
117117
useDeleteMarker = false
118118
)
119119
check querycompare(test, sql("SELECT id, ids_array FROM tasks AS t WHERE id = ? AND ? = ANY(ids_array) AND ? = ANY(user_array) AND ? = ANY(tasks_array) "))
@@ -129,7 +129,7 @@ suite "test sqlSelectConst":
129129
tableAs = "t",
130130
select = ["id", "ids_array"],
131131
where = ["id =", "IN (ids_array)"],
132-
joinargs = [noJoin],
132+
joinargs = [],
133133
useDeleteMarker = false
134134
)
135135
check querycompare(test, sql("SELECT id, ids_array FROM tasks AS t WHERE id = ? AND ? IN (ids_array) "))
@@ -141,7 +141,7 @@ suite "test sqlSelectConst":
141141
tableAs = "t",
142142
select = ["id", "ids_array"],
143143
where = ["id =", "id IN"],
144-
joinargs = [noJoin],
144+
joinargs = [],
145145
useDeleteMarker = false
146146
)
147147
check querycompare(test, sql("SELECT id, ids_array FROM tasks AS t WHERE id = ? AND id IN (?) "))
@@ -157,7 +157,7 @@ suite "test sqlSelectConst":
157157
tableAs = "t",
158158
select = ["id", "name", "description", "created", "updated", "completed"],
159159
where = ["id =", "name != NULL", "description = NULL"],
160-
joinargs = [noJoin],
160+
joinargs = [],
161161
useDeleteMarker = false
162162
)
163163
check querycompare(test, sql("SELECT id, name, description, created, updated, completed FROM tasks AS t WHERE id = ? AND name != NULL AND description = NULL "))
@@ -169,7 +169,7 @@ suite "test sqlSelectConst":
169169
tableAs = "t",
170170
select = ["id", "name", "description", "created", "updated", "completed"],
171171
where = ["id =", "name !=", "description = NULL"],
172-
joinargs = [noJoin],
172+
joinargs = [],
173173
useDeleteMarker = false
174174
)
175175
check querycompare(test, sql("SELECT id, name, description, created, updated, completed FROM tasks AS t WHERE id = ? AND name != ? AND description = NULL "))
@@ -297,7 +297,7 @@ suite "test sqlSelectConst - joins":
297297
tableAs = "t",
298298
select = ["t.id", "t.name", "t.description", "t.created", "t.updated", "t.completed"],
299299
where = ["t.id ="],
300-
# joinargs = [noJoin],
300+
# joinargs = [],
301301
joinargs = [
302302
(table: "projects", tableAs: "", on: @["projects.id = t.project_id", "projects.status = 1"]),
303303
(table: "projects", tableAs: "", on: @["projects.id = t.project_id", "projects.status = 1"])
@@ -389,7 +389,7 @@ suite "test sqlSelectConst - deletemarkers / softdelete":
389389
table = "tasks",
390390
select = ["id", "name"],
391391
where = ["id ="],
392-
joinargs = [noJoin],
392+
joinargs = [],
393393
# joinargs = [(table: "projects", tableAs: "", on: @["projects.id = tasks.project_id", "projects.status = 1"])],
394394
# tablesWithDeleteMarker = [] #tableWithDeleteMarkerLet,
395395
tablesWithDeleteMarker = ["tasks", "history", "tasksitems"]
@@ -552,7 +552,7 @@ suite "sqlSelectConst":
552552
tableAs = "t",
553553
select = ["t.id", "t.name", "t.description", "t.created", "t.updated", "t.completed"],
554554
where = ["t.id ="],
555-
joinargs = [noJoin],
555+
joinargs = [],
556556
tablesWithDeleteMarker = ["tasks", "history", "tasksitems"], #tableWithDeleteMarker
557557
)
558558

@@ -662,7 +662,7 @@ suite "sqlSelectConst":
662662
tableAs = "t",
663663
select = ["t.id", "t.name"],
664664
where = ["t.id ="],
665-
joinargs = [noJoin],
665+
joinargs = [],
666666
whereInField = "t.name",
667667
whereInValue = ["'1aa'", "'2bb'", "'3cc'"],
668668
tablesWithDeleteMarker = ["tasksQ", "history", "tasksitems"], #tableWithDeleteMarker
@@ -684,7 +684,7 @@ suite "sqlSelectConst":
684684
tableAs = "t",
685685
select = ["t.id", "t.name"],
686686
where = ["t.id ="],
687-
joinargs = [noJoin],
687+
joinargs = [],
688688
whereInField = "t.id",
689689
whereInValue = [""],
690690
tablesWithDeleteMarker = ["tasksQ", "history", "tasksitems"], #tableWithDeleteMarker

tests/select/test_select_const_deletemarker.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ suite "select with tablesWithDeleteMarkerInit init":
3030
table = "tasks",
3131
select = ["id", "name", "description", "created", "updated", "completed"],
3232
where = ["id ="],
33-
joinargs = [noJoin],
33+
joinargs = [],
3434
useDeleteMarker = true
3535
)
3636
check querycompare(test, sql("SELECT id, name, description, created, updated, completed FROM tasks WHERE id = ? AND tasks.is_deleted IS NULL "))
@@ -43,7 +43,7 @@ suite "select with tablesWithDeleteMarkerInit init":
4343
table = "tasks",
4444
select = ["id", "name", "description", "created", "updated", "completed"],
4545
where = ["id ="],
46-
joinargs = [noJoin],
46+
joinargs = [],
4747
useDeleteMarker = true
4848
)
4949
check querycompare(test, sql("SELECT id, name, description, created, updated, completed FROM tasks WHERE id = ? AND tasks.is_deleted IS NULL "))

0 commit comments

Comments
 (0)