refactor dbFormat (#19746)

* refactor dbFormat

* add simple tests
This commit is contained in:
ringabout
2022-09-29 18:16:42 +08:00
parent fb463f545f
commit e102f4fb86

View File

@@ -88,7 +88,7 @@ import strutils, postgres
import db_common
export db_common
import std/private/since
import std/private/[since, dbutils]
type
DbConn* = PPGconn ## encapsulates a database connection
@@ -116,19 +116,7 @@ proc dbQuote*(s: string): string =
add(result, '\'')
proc dbFormat(formatstr: SqlQuery, args: varargs[string]): string =
result = ""
var a = 0
if args.len > 0 and not string(formatstr).contains("?"):
dbError("""parameter substitution expects "?" """)
if args.len == 0:
return string(formatstr)
else:
for c in items(string(formatstr)):
if c == '?':
add(result, dbQuote(args[a]))
inc(a)
else:
add(result, c)
dbFormatImpl(formatstr, dbQuote, args)
proc tryExec*(db: DbConn, query: SqlQuery,
args: varargs[string, `$`]): bool {.tags: [ReadDbEffect, WriteDbEffect].} =