parent
77a65d3c33
commit
f56085f21e
5 changed files with 34 additions and 34 deletions
|
|
@ -90,7 +90,7 @@ import strutils, mysql
|
||||||
import db_common
|
import db_common
|
||||||
export db_common
|
export db_common
|
||||||
|
|
||||||
import std/private/since
|
import std/private/[since, dbutils]
|
||||||
|
|
||||||
type
|
type
|
||||||
DbConn* = distinct PMySQL ## encapsulates a database connection
|
DbConn* = distinct PMySQL ## encapsulates a database connection
|
||||||
|
|
@ -138,14 +138,7 @@ proc dbQuote*(s: string): string =
|
||||||
add(result, '\'')
|
add(result, '\'')
|
||||||
|
|
||||||
proc dbFormat(formatstr: SqlQuery, args: varargs[string]): string =
|
proc dbFormat(formatstr: SqlQuery, args: varargs[string]): string =
|
||||||
result = ""
|
dbFormatImpl(formatstr, dbQuote, args)
|
||||||
var a = 0
|
|
||||||
for c in items(string(formatstr)):
|
|
||||||
if c == '?':
|
|
||||||
add(result, dbQuote(args[a]))
|
|
||||||
inc(a)
|
|
||||||
else:
|
|
||||||
add(result, c)
|
|
||||||
|
|
||||||
proc tryExec*(db: DbConn, query: SqlQuery, args: varargs[string, `$`]): bool {.
|
proc tryExec*(db: DbConn, query: SqlQuery, args: varargs[string, `$`]): bool {.
|
||||||
tags: [ReadDbEffect, WriteDbEffect].} =
|
tags: [ReadDbEffect, WriteDbEffect].} =
|
||||||
|
|
@ -358,7 +351,7 @@ proc getValue*(db: DbConn, query: SqlQuery,
|
||||||
result = getRow(db, query, args)[0]
|
result = getRow(db, query, args)[0]
|
||||||
|
|
||||||
proc tryInsertId*(db: DbConn, query: SqlQuery,
|
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
|
## executes the query (typically "INSERT") and returns the
|
||||||
## generated ID for the row or -1 in case of an error.
|
## generated ID for the row or -1 in case of an error.
|
||||||
var q = dbFormat(query, args)
|
var q = dbFormat(query, args)
|
||||||
|
|
@ -376,7 +369,7 @@ proc insertId*(db: DbConn, query: SqlQuery,
|
||||||
|
|
||||||
proc tryInsert*(db: DbConn, query: SqlQuery, pkName: string,
|
proc tryInsert*(db: DbConn, query: SqlQuery, pkName: string,
|
||||||
args: varargs[string, `$`]): int64
|
args: varargs[string, `$`]): int64
|
||||||
{.tags: [WriteDbEffect], raises: [], since: (1, 3).} =
|
{.tags: [WriteDbEffect], raises: [DbError], since: (1, 3).} =
|
||||||
## same as tryInsertID
|
## same as tryInsertID
|
||||||
tryInsertID(db, query, args)
|
tryInsertID(db, query, args)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -92,7 +92,7 @@ import strutils, odbcsql
|
||||||
import db_common
|
import db_common
|
||||||
export db_common
|
export db_common
|
||||||
|
|
||||||
import std/private/since
|
import std/private/[since, dbutils]
|
||||||
|
|
||||||
type
|
type
|
||||||
OdbcConnTyp = tuple[hDb: SqlHDBC, env: SqlHEnv, stmt: SqlHStmt]
|
OdbcConnTyp = tuple[hDb: SqlHDBC, env: SqlHEnv, stmt: SqlHStmt]
|
||||||
|
|
@ -197,14 +197,7 @@ proc dbFormat(formatstr: SqlQuery, args: varargs[string]): string {.
|
||||||
noSideEffect.} =
|
noSideEffect.} =
|
||||||
## Replace any `?` placeholders with `args`,
|
## Replace any `?` placeholders with `args`,
|
||||||
## and quotes the arguments
|
## and quotes the arguments
|
||||||
result = ""
|
dbFormatImpl(formatstr, dbQuote, args)
|
||||||
var a = 0
|
|
||||||
for c in items(string(formatstr)):
|
|
||||||
if c == '?':
|
|
||||||
add(result, dbQuote(args[a]))
|
|
||||||
inc(a)
|
|
||||||
else:
|
|
||||||
add(result, c)
|
|
||||||
|
|
||||||
proc prepareFetch(db: var DbConn, query: SqlQuery,
|
proc prepareFetch(db: var DbConn, query: SqlQuery,
|
||||||
args: varargs[string, `$`]): TSqlSmallInt {.
|
args: varargs[string, `$`]): TSqlSmallInt {.
|
||||||
|
|
|
||||||
|
|
@ -88,7 +88,7 @@ import strutils, postgres
|
||||||
import db_common
|
import db_common
|
||||||
export db_common
|
export db_common
|
||||||
|
|
||||||
import std/private/since
|
import std/private/[since, dbutils]
|
||||||
|
|
||||||
type
|
type
|
||||||
DbConn* = PPGconn ## encapsulates a database connection
|
DbConn* = PPGconn ## encapsulates a database connection
|
||||||
|
|
@ -116,19 +116,7 @@ proc dbQuote*(s: string): string =
|
||||||
add(result, '\'')
|
add(result, '\'')
|
||||||
|
|
||||||
proc dbFormat(formatstr: SqlQuery, args: varargs[string]): string =
|
proc dbFormat(formatstr: SqlQuery, args: varargs[string]): string =
|
||||||
result = ""
|
dbFormatImpl(formatstr, dbQuote, args)
|
||||||
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)
|
|
||||||
|
|
||||||
proc tryExec*(db: DbConn, query: SqlQuery,
|
proc tryExec*(db: DbConn, query: SqlQuery,
|
||||||
args: varargs[string, `$`]): bool {.tags: [ReadDbEffect, WriteDbEffect].} =
|
args: varargs[string, `$`]): bool {.tags: [ReadDbEffect, WriteDbEffect].} =
|
||||||
|
|
|
||||||
25
tests/stdlib/tdb.nim
Normal file
25
tests/stdlib/tdb.nim
Normal file
|
|
@ -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
tests/stdlib/tdb.nims
Normal file
1
tests/stdlib/tdb.nims
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
--styleCheck:off
|
||||||
Loading…
Add table
Add a link
Reference in a new issue