diff options
Diffstat (limited to 'lib/impure')
-rw-r--r-- | lib/impure/db_mysql.nim | 15 | ||||
-rw-r--r-- | lib/impure/db_odbc.nim | 11 | ||||
-rw-r--r-- | lib/impure/db_postgres.nim | 16 |
3 files changed, 8 insertions, 34 deletions
diff --git a/lib/impure/db_mysql.nim b/lib/impure/db_mysql.nim index 9a98cb9c5..279aebda5 100644 --- a/lib/impure/db_mysql.nim +++ b/lib/impure/db_mysql.nim @@ -90,7 +90,7 @@ import strutils, mysql import db_common export db_common -import std/private/since +import std/private/[since, dbutils] type DbConn* = distinct PMySQL ## encapsulates a database connection @@ -138,14 +138,7 @@ proc dbQuote*(s: string): string = add(result, '\'') proc dbFormat(formatstr: SqlQuery, args: varargs[string]): string = - result = "" - var a = 0 - 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].} = @@ -358,7 +351,7 @@ proc getValue*(db: DbConn, query: SqlQuery, result = getRow(db, query, args)[0] proc tryInsertId*(db: DbConn, query: SqlQuery, - args: varargs[string, `$`]): int64 {.tags: [WriteDbEffect].} = + args: varargs[string, `$`]): int64 {.tags: [WriteDbEffect], raises: [DbError].} = ## executes the query (typically "INSERT") and returns the ## generated ID for the row or -1 in case of an error. var q = dbFormat(query, args) @@ -376,7 +369,7 @@ proc insertId*(db: DbConn, query: SqlQuery, proc tryInsert*(db: DbConn, query: SqlQuery, pkName: string, args: varargs[string, `$`]): int64 - {.tags: [WriteDbEffect], raises: [], since: (1, 3).} = + {.tags: [WriteDbEffect], raises: [DbError], since: (1, 3).} = ## same as tryInsertID tryInsertID(db, query, args) diff --git a/lib/impure/db_odbc.nim b/lib/impure/db_odbc.nim index da1b1e9b5..756957acb 100644 --- a/lib/impure/db_odbc.nim +++ b/lib/impure/db_odbc.nim @@ -92,7 +92,7 @@ import strutils, odbcsql import db_common export db_common -import std/private/since +import std/private/[since, dbutils] type OdbcConnTyp = tuple[hDb: SqlHDBC, env: SqlHEnv, stmt: SqlHStmt] @@ -197,14 +197,7 @@ proc dbFormat(formatstr: SqlQuery, args: varargs[string]): string {. noSideEffect.} = ## Replace any `?` placeholders with `args`, ## and quotes the arguments - result = "" - var a = 0 - for c in items(string(formatstr)): - if c == '?': - add(result, dbQuote(args[a])) - inc(a) - else: - add(result, c) + dbFormatImpl(formatstr, dbQuote, args) proc prepareFetch(db: var DbConn, query: SqlQuery, args: varargs[string, `$`]): TSqlSmallInt {. diff --git a/lib/impure/db_postgres.nim b/lib/impure/db_postgres.nim index e629b7945..ef530c605 100644 --- a/lib/impure/db_postgres.nim +++ b/lib/impure/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].} = |