Something like this:
import options, strutils
from sequtils import map
proc queryStr(query: string, args: varargs[Option[string]]):
(string, seq[string]) =
## Transform '?' in query string according to optioned arguments given
if '?' notin query:
return (query, args.map get)
var
qpos = -1
prevq = -1
builder = ""
outargs = newseq[string]()
for arg in args:
prevq = qpos
# still need to make sure it's skipped when ? within ''
qpos = query.find('?', start = qpos+1)
if qpos == -1:
builder &= query[prevq+1 .. ^1]
break
if arg.isNone:
builder &= query[prevq+1 .. qpos].replace("?", "NULL")
continue
outargs.add get(arg)
builder &= query[prevq+1 .. qpos]
# the left over string slice
if qpos != -1 or qpos+1 < query.high:
builder &= query[qpos+1 .. ^1]
(builder, outargs)
let (q, args) = queryStr(
"INSERT INTO table_name(name, age, info) VALUES(?, ?, ?);",
none(string), some $19, some "info")
echo q
echo args
Run
- Re: Insert 'NULL' value in Postgres with Nim 0.19.1 without 'n... mikra
- Re: Insert 'NULL' value in Postgres with Nim 0.19.1 without 'n... timothee
- Re: Insert 'NULL' value in Postgres with Nim 0.19.1 without 'n... ThomasTJdev
- Re: Insert 'NULL' value in Postgres with Nim 0.19.1 without 'n... mikra
- Re: Insert 'NULL' value in Postgres with Nim 0.19.1 without 'n... ThomasTJdev
- Re: Insert 'NULL' value in Postgres with Nim 0.19.1 without 'n... mikra
- Re: Insert 'NULL' value in Postgres with Nim 0.19.1 without 'n... ThomasTJdev
- Re: Insert 'NULL' value in Postgres with Nim 0.19.1 without 'n... mikra
- Re: Insert 'NULL' value in Postgres with Nim 0.19.1 without 'n... rayman22201
- Re: Insert 'NULL' value in Postgres with Nim 0.19.1 without 'n... ThomasTJdev
- Re: Insert 'NULL' value in Postgres with Nim 0.19.1 without 'n... mashingan
