From e102f4fb86649d04f3d63eacdd039f21157264c7 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Thu, 29 Sep 2022 18:16:42 +0800 Subject: [PATCH] refactor dbFormat (#19746) * refactor dbFormat * add simple tests --- src/db_postgres.nim | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/src/db_postgres.nim b/src/db_postgres.nim index e629b79454..ef530c6054 100644 --- a/src/db_postgres.nim +++ b/src/db_postgres.nim @@ -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].} =