Skip to content

Commit 31f433e

Browse files
committed
query: utils
1 parent dfc7fc0 commit 31f433e

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/sqlbuilderpkg/utils.nim

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ type
2121
RIGHT
2222
FULL
2323

24+
SQLQueryType* = enum
25+
INSERT
26+
UPDATE
27+
2428
SQLJoinObject* = ref object
2529
joinType*: SQLJoinType
2630
table*: string
@@ -127,7 +131,7 @@ template genArgsSetNull*[T](arguments: varargs[T, argType]): ArgsContainer =
127131
argsContainer
128132

129133

130-
template genArgsColumns*[T](arguments: varargs[T, argFormat]): tuple[select: seq[string], args: ArgsContainer] =
134+
template genArgsColumns*[T](queryType: SQLQueryType, arguments: varargs[T, argFormat]): tuple[select: seq[string], args: ArgsContainer] =
131135
## Create argument container for query and passed args and selecting which
132136
## columns to update. It's and expansion of `genArgs()`, since you can
133137
## decide which columns, there should be included.
@@ -145,6 +149,9 @@ template genArgsColumns*[T](arguments: varargs[T, argFormat]): tuple[select: seq
145149
## columns which shall be updated. When importing your spreadsheet, check
146150
## if the column exists (bool), and pass that as the `use: bool` param. If
147151
## the column does not exists, it will be skipped in the query.
152+
##
153+
## The objects `ArgsFormat` is:
154+
## (use: bool, column: string, value: ArgObj)
148155
var
149156
select: seq[string]
150157
argsContainer: ArgsContainer
@@ -154,10 +161,15 @@ template genArgsColumns*[T](arguments: varargs[T, argFormat]): tuple[select: seq
154161
let argObject = argType(arg.value)
155162
if not arg.use:
156163
continue
164+
if (
165+
queryType == SQLQueryType.INSERT and
166+
(argObject.isNull or argObject.val.len() == 0)
167+
):
168+
continue
157169
if arg.column != "":
158170
select.add(arg.column)
159-
if argObject.isNull:
160-
argsContainer.query.add(argObject)
171+
if argObject.isNull or argObject.val.len() == 0:
172+
argsContainer.query.add(dbNullVal)
161173
else:
162174
argsContainer.query.add(argObject)
163175
argsContainer.args.add(argObject.val)

0 commit comments

Comments
 (0)