82
82
selectCondition* : Query # ntWhere
83
83
selectOrder* : seq [(string , Order )]
84
84
of ntInsert, ntUpsert:
85
- insertFields* : OrderedTable [string , SQLValue ]
85
+ insertFields* : OrderedTable [string , string ]
86
86
insertReturn* : Query # ntReturn node
87
87
of ntInfix:
88
88
infixOp: SQLOperator
@@ -512,6 +512,21 @@ proc updateAll*(model: Model,
512
512
checkColumn pair[0 ]:
513
513
add result [1 ].updateFields, (result [0 ].tColumns[pair[0 ]], pair[1 ])
514
514
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
+
515
530
proc orderBy * (q: QueryBuilder , key: string , order: Order = Order .ASC ): QueryBuilder =
516
531
# # Add `orderBy` clause to the current `QueryBuilder`
517
532
assert q[1 ].nt == ntSelect
@@ -547,6 +562,26 @@ macro initModel*(T: typedesc, x: seq[string]): untyped =
547
562
add result , quote do :
548
563
`callNode` (`x`)
549
564
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
+
550
585
template getAll * (q: QueryBuilder , T: typedesc ): untyped =
551
586
# # Execute the query and returns a collection of objects `T`.
552
587
# # This works only for a `Model` defined at compile-time
@@ -562,15 +597,15 @@ template exec*(q: QueryBuilder): untyped =
562
597
case q[1 ].nt
563
598
of ntUpdate:
564
599
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)
565
603
else : discard # todo other final checks before executing the query
566
- dbcon.exec (SQLQuery sql (q[1 ], q[0 ].tName))
567
604
568
605
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
571
607
dbcon.exec (q)
572
608
573
609
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
576
611
dbcon.tryExec (q)
0 commit comments