Skip to content

Commit 52abb8c

Browse files
committed
runtime insert
Signed-off-by: George Lemon <georgelemon@protonmail.com>
1 parent 4f89e9c commit 52abb8c

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

src/enimsql/model.nim

+3-2
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ macro clear*(model: untyped, then: untyped) =
353353
executeSQL sql(clearStmt, table):
354354
then
355355

356-
macro insert*(model: untyped, row: untyped,
356+
macro insertRow*(model: untyped, row: untyped,
357357
then, err: untyped = nil) =
358358
## Insert a new row in a table
359359
checkModelExists(model)
@@ -374,7 +374,8 @@ macro insert*(model: untyped, row: untyped,
374374
if not model.checkColumn($kv[0]):
375375
raise newException(EnimsqlModelDefect,
376376
"Unknown column `" & $row[i][0] & "` (Model: " & $model & ")")
377-
insertStmt.insertFields[$kv[0]] = newValue(kv[1], model.getColumn($kv[0]).cType)
377+
# insertStmt.insertFields[$kv[0]] = newValue(kv[1], model.getColumn($kv[0]).cType)
378+
insertStmt.insertFields[$kv[0]] = kv[1].strVal
378379
add values, kv[1]
379380
var
380381
callExec = newCall(ident"tryInsertID", ident"dbcon")

src/enimsql/private/query.nim

+41-6
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ type
8282
selectCondition*: Query # ntWhere
8383
selectOrder*: seq[(string, Order)]
8484
of ntInsert, ntUpsert:
85-
insertFields*: OrderedTable[string, SQLValue]
85+
insertFields*: OrderedTable[string, string]
8686
insertReturn*: Query # ntReturn node
8787
of ntInfix:
8888
infixOp: SQLOperator
@@ -512,6 +512,21 @@ proc updateAll*(model: Model,
512512
checkColumn pair[0]:
513513
add result[1].updateFields, (result[0].tColumns[pair[0]], pair[1])
514514

515+
proc insert*(model: Model, pairs: varargs[(string, string)]): QueryBuilder =
516+
## Create an `INSERT` statement
517+
result = (model, newInsertStmt())
518+
for pair in pairs:
519+
checkColumn pair[0]:
520+
result[1].insertFields[pair[0]] = pair[1]
521+
522+
proc insert*(model: Model, entry: OrderedTable[string, string]): QueryBuilder =
523+
## Create an `INSERT` statement
524+
result = (model, newInsertStmt())
525+
for k, v in entry:
526+
checkColumn k:
527+
discard
528+
result[1].insertFields = entry
529+
515530
proc orderBy*(q: QueryBuilder, key: string, order: Order = Order.ASC): QueryBuilder =
516531
## Add `orderBy` clause to the current `QueryBuilder`
517532
assert q[1].nt == ntSelect
@@ -547,6 +562,26 @@ macro initModel*(T: typedesc, x: seq[string]): untyped =
547562
add result, quote do:
548563
`callNode`(`x`)
549564

565+
macro `%*`*(x: untyped): untyped =
566+
## Convert expression to pairs of `column_key: some value`
567+
## This macro is similar with `%*` from `std/json`
568+
case x.kind
569+
of nnkTableConstr:
570+
var x = x
571+
for i in 0..<x.len:
572+
x[i].expectKind nnkExprColonExpr
573+
case x[i][1].kind
574+
of nnkIntLit:
575+
x[i][1] = newLit($(x[i][1].intVal))
576+
of nnkFloatLit:
577+
x[i][1] = newLit($(x[i][1].floatVal))
578+
of nnkIdent:
579+
if x[i][1].eqIdent"true" or x[i][1].eqIdent "false":
580+
x[i][1] = newLit(parseBool(x[i][1].strVal))
581+
else: discard
582+
return newCall(ident"toOrderedTable", x)
583+
else: error("Invalid expression, expected curly braces")
584+
550585
template getAll*(q: QueryBuilder, T: typedesc): untyped =
551586
## Execute the query and returns a collection of objects `T`.
552587
## This works only for a `Model` defined at compile-time
@@ -562,15 +597,15 @@ template exec*(q: QueryBuilder): untyped =
562597
case q[1].nt
563598
of ntUpdate:
564599
assert q[1].updateCondition != nil
600+
dbcon.exec(SQLQuery sql(q[1], q[0].tName))
601+
of ntInsert:
602+
dbcon.exec(SQLQuery sql(q[1], q[0].tName), q[1].insertFields.values.toSeq)
565603
else: discard # todo other final checks before executing the query
566-
dbcon.exec(SQLQuery sql(q[1], q[0].tName))
567604

568605
template exec*(q: SQLQuery): untyped =
569-
## Use it inside a `withDB` context to
570-
## execute a query
606+
## Use it inside a `withDB` context to execute a query
571607
dbcon.exec(q)
572608

573609
template tryExec*(q: SQLQuery): untyped =
574-
## Use it inside a `withDB` context to
575-
## try execute a query
610+
## Use it inside a `withDB` context to try execute a query
576611
dbcon.tryExec(q)

0 commit comments

Comments
 (0)