Skip to content

Commit

Permalink
refactor dbFormat (#19746)
Browse files Browse the repository at this point in the history
* refactor dbFormat

* add simple tests
  • Loading branch information
ringabout authored Sep 29, 2022
1 parent 77a65d3 commit f56085f
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 34 deletions.
15 changes: 4 additions & 11 deletions lib/impure/db_mysql.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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].} =
Expand Down Expand Up @@ -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)
Expand All @@ -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)

Expand Down
11 changes: 2 additions & 9 deletions lib/impure/db_odbc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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 {.
Expand Down
16 changes: 2 additions & 14 deletions lib/impure/db_postgres.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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].} =
Expand Down
25 changes: 25 additions & 0 deletions tests/stdlib/tdb.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
discard """
action: "compile"
"""


import db_mysql, db_odbc, db_postgres
import os
from stdtest/specialpaths import buildDir


block:
block:
const dbName = buildDir / "db.sqlite3"
var db = db_mysql.open(dbName, "", "", "")
discard tryInsertId(db, sql"INSERT INTO myTestTbl (name,i,f) VALUES (?,?,?)", "t")

block:
const dbName = buildDir / "db.odbc"
var db = db_odbc.open(dbName, "", "", "")
discard tryInsertId(db, sql"INSERT INTO myTestTbl (name,i,f) VALUES (?,?,?)", "t")

block:
const dbName = buildDir / "db.postgres"
var db = db_postgres.open(dbName, "", "", "")
discard tryInsertId(db, sql"INSERT INTO myTestTbl (name,i,f) VALUES (?,?,?)", "t")
1 change: 1 addition & 0 deletions tests/stdlib/tdb.nims
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--styleCheck:off

0 comments on commit f56085f

Please sign in to comment.