Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions src/sqlbuilderpkg/select.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import

from ./utils import SQLJoinType, ArgsContainer

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


##
## Constant generator utilities
Expand All @@ -33,7 +31,7 @@ proc sqlSelectConstJoin(
): string =
var lef = ""

if joinargs == [] or $(joinargs.repr) == "[]":
if joinargs.len == 0:
return

for d in joinargs:
Expand Down Expand Up @@ -235,7 +233,7 @@ macro sqlSelectConst*(

# Join table
var joinTablesUsed: seq[string]
if joinargs[0] != noJoin and joinargs != [] and $(joinargs.repr) != "[]":
if joinargs.len != 0:
for i, d in joinargs:
if d.repr.len == 0:
continue
Expand Down Expand Up @@ -276,7 +274,7 @@ macro sqlSelectConst*(
# Joins
#
var lef = ""
if joinargs[0] != noJoin:
if joinargs.len != 0:
lef = sqlSelectConstJoin(joinargs, jointype)


Expand Down Expand Up @@ -407,7 +405,7 @@ proc sqlSelect*(


# Join table
if joinargs.len() > 0 and joinargs[0] != noJoin:
if joinargs.len() > 0:
for d in joinargs:
if d.table == "":
continue
Expand Down Expand Up @@ -440,7 +438,7 @@ proc sqlSelect*(
# Joins
#
var lef = ""
if joinargs.len() > 0 and joinargs[0] != noJoin:
if joinargs.len() > 0:
for i, d in joinargs:
lef.add(" " & $jointype & " JOIN ")
lef.add(d.table & " ")
Expand Down Expand Up @@ -600,7 +598,7 @@ proc sqlSelect*(
# Check for missing table alias
if (
(tableAs != "" and table != tableAs) or
(joinargs.len() > 0 and joinargs[0] != noJoin)
(joinargs.len() > 0)
):
var hit: bool
for s in select:
Expand Down
32 changes: 16 additions & 16 deletions tests/select/test_select_const.nim
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ suite "test sqlSelectConst":
table = "tasks",
select = ["id", "name", "description", "created", "updated", "completed"],
where = ["id ="],
joinargs = [noJoin],
joinargs = [],
useDeleteMarker = false
)
check querycompare(test, sql("SELECT id, name, description, created, updated, completed FROM tasks WHERE id = ? "))
Expand All @@ -41,7 +41,7 @@ suite "test sqlSelectConst":
tableAs = "t",
select = ["id", "name", "description", "created", "updated", "completed"],
where = ["id ="],
joinargs = [noJoin],
joinargs = [],
useDeleteMarker = false
)
check querycompare(test, sql("SELECT id, name, description, created, updated, completed FROM tasks AS t WHERE id = ? "))
Expand All @@ -52,7 +52,7 @@ suite "test sqlSelectConst":
tableAs = "t",
select = ["t.id", "t.name", "t.description", "t.created", "t.updated", "t.completed"],
where = ["t.id ="],
joinargs = [noJoin],
joinargs = [],
useDeleteMarker = false,
customSQL = "ORDER BY t.created DESC"
)
Expand All @@ -68,7 +68,7 @@ suite "test sqlSelectConst":
tableAs = "t",
select = ["id", "name", "description", "created", "updated", "completed"],
where = ["id =", "name !=", "updated >", "completed IS", "description LIKE"],
joinargs = [noJoin],
joinargs = [],
useDeleteMarker = false
)
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 ? "))
Expand All @@ -80,7 +80,7 @@ suite "test sqlSelectConst":
tableAs = "t",
select = ["id", "name", "description", "created", "updated", "completed"],
where = ["id =", "name !=", "updated >", "completed IS", "description LIKE"],
joinargs = [noJoin],
joinargs = [],
customSQL = "AND name != 'test' AND created > ? ",
useDeleteMarker = false
)
Expand All @@ -97,7 +97,7 @@ suite "test sqlSelectConst":
tableAs = "t",
select = ["id", "ids_array"],
where = ["id =", "= ANY(ids_array)"],
joinargs = [noJoin],
joinargs = [],
useDeleteMarker = false
)
check querycompare(test, sql("SELECT id, ids_array FROM tasks AS t WHERE id = ? AND ? = ANY(ids_array) "))
Expand All @@ -113,7 +113,7 @@ suite "test sqlSelectConst":
tableAs = "t",
select = ["id", "ids_array"],
where = ["id =", "= ANY(ids_array)", "= ANY(user_array)", "= ANY(tasks_array)"],
joinargs = [noJoin],
joinargs = [],
useDeleteMarker = false
)
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) "))
Expand All @@ -129,7 +129,7 @@ suite "test sqlSelectConst":
tableAs = "t",
select = ["id", "ids_array"],
where = ["id =", "IN (ids_array)"],
joinargs = [noJoin],
joinargs = [],
useDeleteMarker = false
)
check querycompare(test, sql("SELECT id, ids_array FROM tasks AS t WHERE id = ? AND ? IN (ids_array) "))
Expand All @@ -141,7 +141,7 @@ suite "test sqlSelectConst":
tableAs = "t",
select = ["id", "ids_array"],
where = ["id =", "id IN"],
joinargs = [noJoin],
joinargs = [],
useDeleteMarker = false
)
check querycompare(test, sql("SELECT id, ids_array FROM tasks AS t WHERE id = ? AND id IN (?) "))
Expand All @@ -157,7 +157,7 @@ suite "test sqlSelectConst":
tableAs = "t",
select = ["id", "name", "description", "created", "updated", "completed"],
where = ["id =", "name != NULL", "description = NULL"],
joinargs = [noJoin],
joinargs = [],
useDeleteMarker = false
)
check querycompare(test, sql("SELECT id, name, description, created, updated, completed FROM tasks AS t WHERE id = ? AND name != NULL AND description = NULL "))
Expand All @@ -169,7 +169,7 @@ suite "test sqlSelectConst":
tableAs = "t",
select = ["id", "name", "description", "created", "updated", "completed"],
where = ["id =", "name !=", "description = NULL"],
joinargs = [noJoin],
joinargs = [],
useDeleteMarker = false
)
check querycompare(test, sql("SELECT id, name, description, created, updated, completed FROM tasks AS t WHERE id = ? AND name != ? AND description = NULL "))
Expand Down Expand Up @@ -286,7 +286,7 @@ suite "test sqlSelectConst - joins":
tableAs = "t",
select = ["t.id", "t.name", "t.description", "t.created", "t.updated", "t.completed"],
where = ["t.id ="],
# joinargs = [noJoin],
# joinargs = [],
joinargs = [
(table: "projects", tableAs: "", on: @["projects.id = t.project_id", "projects.status = 1"]),
(table: "projects", tableAs: "", on: @["projects.id = t.project_id", "projects.status = 1"])
Expand Down Expand Up @@ -378,7 +378,7 @@ suite "test sqlSelectConst - deletemarkers / softdelete":
table = "tasks",
select = ["id", "name"],
where = ["id ="],
joinargs = [noJoin],
joinargs = [],
# joinargs = [(table: "projects", tableAs: "", on: @["projects.id = tasks.project_id", "projects.status = 1"])],
# tablesWithDeleteMarker = [] #tableWithDeleteMarkerLet,
tablesWithDeleteMarker = ["tasks", "history", "tasksitems"]
Expand Down Expand Up @@ -541,7 +541,7 @@ suite "sqlSelectConst":
tableAs = "t",
select = ["t.id", "t.name", "t.description", "t.created", "t.updated", "t.completed"],
where = ["t.id ="],
joinargs = [noJoin],
joinargs = [],
tablesWithDeleteMarker = ["tasks", "history", "tasksitems"], #tableWithDeleteMarker
)

Expand Down Expand Up @@ -651,7 +651,7 @@ suite "sqlSelectConst":
tableAs = "t",
select = ["t.id", "t.name"],
where = ["t.id ="],
joinargs = [noJoin],
joinargs = [],
whereInField = "t.name",
whereInValue = ["'1aa'", "'2bb'", "'3cc'"],
tablesWithDeleteMarker = ["tasksQ", "history", "tasksitems"], #tableWithDeleteMarker
Expand All @@ -673,7 +673,7 @@ suite "sqlSelectConst":
tableAs = "t",
select = ["t.id", "t.name"],
where = ["t.id ="],
joinargs = [noJoin],
joinargs = [],
whereInField = "t.id",
whereInValue = [""],
tablesWithDeleteMarker = ["tasksQ", "history", "tasksitems"], #tableWithDeleteMarker
Expand Down
4 changes: 2 additions & 2 deletions tests/select/test_select_const_deletemarker.nim
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ suite "select with tablesWithDeleteMarkerInit init":
table = "tasks",
select = ["id", "name", "description", "created", "updated", "completed"],
where = ["id ="],
joinargs = [noJoin],
joinargs = [],
useDeleteMarker = true
)
check querycompare(test, sql("SELECT id, name, description, created, updated, completed FROM tasks WHERE id = ? AND tasks.is_deleted IS NULL "))
Expand All @@ -43,7 +43,7 @@ suite "select with tablesWithDeleteMarkerInit init":
table = "tasks",
select = ["id", "name", "description", "created", "updated", "completed"],
where = ["id ="],
joinargs = [noJoin],
joinargs = [],
useDeleteMarker = true
)
check querycompare(test, sql("SELECT id, name, description, created, updated, completed FROM tasks WHERE id = ? AND tasks.is_deleted IS NULL "))