Skip to content

Commit

Permalink
add whereIn
Browse files Browse the repository at this point in the history
Signed-off-by: George Lemon <georgelemon@protonmail.com>
  • Loading branch information
georgelemon committed Mar 6, 2025
1 parent 257e54c commit ec1a942
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/enimsql/collection.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import pkg/jsony
type
Entry*[T] = OrderedTable[string, T]
Collection*[T] = ref object
entries: seq[Entry[T]]
entries*: seq[Entry[T]]

proc initCollection*[T]: Collection[T] =
new(result)
Expand Down
26 changes: 26 additions & 0 deletions src/enimsql/private/query.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type
ntUpdate
ntUpdateAll
ntWhere
ntWhereIn
ntWhereLike
ntWhereExistsStmt
ntOr
Expand Down Expand Up @@ -228,6 +229,9 @@ type
infixLeft, infixRight: string
of ntWhere:
whereBranches*: seq[Query] # ntInfix
of ntWhereIn:
whereInField: string
whereInBranches: seq[string] # where in (x, y, z)
of ntOr:
orBranches*: seq[Query] # ntInfix
of ntAnd:
Expand Down Expand Up @@ -302,6 +306,7 @@ proc newInsertStmt*: Query = Query(nt: ntInsert)
proc newDeleteStmt*: Query = Query(nt: ntDelete)
proc newUpsertStmt*: Query = Query(nt: ntUpsert)
proc newWhereStmt*: Query = Query(nt: ntWhere)
proc newWhereInStmt*: Query = Query(nt: ntWhereIn)
proc newSelectStmt*: Query = Query(nt: ntSelect)
proc newUpdateStmt*: Query = Query(nt: ntUpdate)
proc newUpdateAllStmt*: Query = Query(nt: ntUpdateAll)
Expand Down Expand Up @@ -352,6 +357,7 @@ var
"select": "SELECT",
"insert": "INSERT INTO $1",
"where": "WHERE",
"whereIn": "WHERE $1 in ($2)",
"update": "UPDATE $1 ",
"set": "SET $1",
"orderby": "ORDER BY $1",
Expand Down Expand Up @@ -460,6 +466,14 @@ proc sql*(node: Query, k: string, values: var seq[string]): string =
add result, infixExprs.join(indent("AND", 1))
if infixGroups.len > 0:
add result, infixGroups.join(" ")
of ntWhereIn:
var x: string
for branch in node.whereInBranches:
add x, "?"
add values, branch
add result, (q("whereIn").indent(1) % [
node.whereInField, x.join(",")
])
of ntOr, ntAnd:
var branches: seq[Query]
result =
Expand Down Expand Up @@ -696,6 +710,18 @@ proc where*(q: QueryBuilder, kv: varargs[(string, SQLOperator, string)]): QueryB
q.where(x[0], x[1], x[2])
result = q

proc whereIn*(q: QueryBuilder, key: string, vals: seq[string]): QueryBuilder {.discardable.} =
result = q
checkColumn key:
case q[1].nt
of ntSelect:
q[1].selectCondition = newWhereInStmt()
q[1].selectCondition.whereInField = key
q[1].selectCondition.whereInBranches = vals
else:
raise newException(EnimsqlQueryDefect,
"Invalid use of `WHERE` statement for " & $q[1].nt)

proc orWhere*(q: QueryBuilder, handle: proc(q: QueryBuilder)): QueryBuilder =
## Use the `orWhere` proc to join a clause to the
## query using the `or` operator
Expand Down

0 comments on commit ec1a942

Please sign in to comment.