updated db*.nim modules
This commit is contained in:
parent
cb10f30656
commit
a3c8bb9376
4 changed files with 36 additions and 53 deletions
|
|
@ -49,7 +49,7 @@ export db_common
|
||||||
type
|
type
|
||||||
DbConn* = PMySQL ## encapsulates a database connection
|
DbConn* = PMySQL ## encapsulates a database connection
|
||||||
Row* = seq[string] ## a row of a dataset. NULL database values will be
|
Row* = seq[string] ## a row of a dataset. NULL database values will be
|
||||||
## transformed always to the empty string.
|
## converted to nil.
|
||||||
InstantRow* = tuple[row: cstringArray, len: int] ## a handle that can be
|
InstantRow* = tuple[row: cstringArray, len: int] ## a handle that can be
|
||||||
## used to get a row's
|
## used to get a row's
|
||||||
## column text on demand
|
## column text on demand
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,12 @@
|
||||||
#
|
#
|
||||||
|
#
|
||||||
|
# Nim's Runtime Library
|
||||||
|
# (c) Copyright 2015 Nim Contributors
|
||||||
|
#
|
||||||
# See the file "copying.txt", included in this
|
# See the file "copying.txt", included in this
|
||||||
# distribution, for details about the copyright.
|
# distribution, for details about the copyright.
|
||||||
#
|
#
|
||||||
#
|
|
||||||
## A higher level `ODBC` database wrapper.
|
## A higher level `ODBC` database wrapper.
|
||||||
##
|
##
|
||||||
## This is the same interface that is implemented for other databases.
|
## This is the same interface that is implemented for other databases.
|
||||||
|
|
@ -45,46 +49,32 @@
|
||||||
|
|
||||||
import strutils, odbcsql
|
import strutils, odbcsql
|
||||||
|
|
||||||
|
import db_common
|
||||||
|
export db_common
|
||||||
|
|
||||||
type
|
type
|
||||||
OdbcConnTyp = tuple[hDb: SqlHDBC, env: SqlHEnv, stmt: SqlHStmt]
|
OdbcConnTyp = tuple[hDb: SqlHDBC, env: SqlHEnv, stmt: SqlHStmt]
|
||||||
DbConn* = OdbcConnTyp ## encapsulates a database connection
|
DbConn* = OdbcConnTyp ## encapsulates a database connection
|
||||||
Row* = seq[string] ## a row of a dataset. NULL database values will be
|
Row* = seq[string] ## a row of a dataset. NULL database values will be
|
||||||
## transformed always to the empty string.
|
## converted to nil.
|
||||||
InstantRow* = tuple[row: seq[string], len: int] ## a handle that can be
|
InstantRow* = tuple[row: seq[string], len: int] ## a handle that can be
|
||||||
## used to get a row's
|
## used to get a row's
|
||||||
## column text on demand
|
## column text on demand
|
||||||
EDb* = object of IOError ## exception that is raised if a database error occurs
|
|
||||||
|
|
||||||
SqlQuery* = distinct string ## an SQL query string
|
|
||||||
|
|
||||||
FDb* = object of IOEffect ## effect that denotes a database operation
|
|
||||||
FReadDb* = object of FDb ## effect that denotes a read operation
|
|
||||||
FWriteDb* = object of FDb ## effect that denotes a write operation
|
|
||||||
FReadODBC* = object of FDb ## effect that denotes a read of ODBC driver
|
|
||||||
FWriteODBC* = object of FDb ## effect that denotes a write of ODBC driver
|
|
||||||
{.deprecated: [TRow: Row, TSqlQuery: SqlQuery, TDbConn: DbConn].}
|
{.deprecated: [TRow: Row, TSqlQuery: SqlQuery, TDbConn: DbConn].}
|
||||||
|
|
||||||
var
|
var
|
||||||
buf: array[0..4096, char]
|
buf: array[0..4096, char]
|
||||||
|
|
||||||
proc properFreeResult(hType: int, sqlres: var SqlHandle) {.
|
proc properFreeResult(hType: int, sqlres: var SqlHandle) {.
|
||||||
tags: [FWriteODBC], raises: [].} =
|
tags: [WriteDbEffect], raises: [].} =
|
||||||
try:
|
try:
|
||||||
discard SQLFreeHandle(hType.TSqlSmallInt, sqlres)
|
discard SQLFreeHandle(hType.TSqlSmallInt, sqlres)
|
||||||
sqlres = nil
|
sqlres = nil
|
||||||
except: discard
|
except: discard
|
||||||
|
|
||||||
proc sql*(query: string): SqlQuery {.noSideEffect, inline.} =
|
|
||||||
## Constructs a SqlQuery from the string `query`. This is supposed to be
|
|
||||||
## used as a raw-string-literal modifier:
|
|
||||||
## ``sql"update user set counter = counter + 1"``
|
|
||||||
##
|
|
||||||
## If assertions are turned off, it does nothing. If assertions are turned
|
|
||||||
## on, later versions will check the string for valid syntax.
|
|
||||||
result = SqlQuery(query)
|
|
||||||
|
|
||||||
proc getErrInfo(db: var DbConn): tuple[res: int, ss, ne, msg: string] {.
|
proc getErrInfo(db: var DbConn): tuple[res: int, ss, ne, msg: string] {.
|
||||||
tags: [FReadODBC], raises: [].} =
|
tags: [ReadDbEffect], raises: [].} =
|
||||||
## Returns ODBC error information
|
## Returns ODBC error information
|
||||||
var
|
var
|
||||||
sqlState: array[0..512, char]
|
sqlState: array[0..512, char]
|
||||||
|
|
@ -106,10 +96,10 @@ proc getErrInfo(db: var DbConn): tuple[res: int, ss, ne, msg: string] {.
|
||||||
return (res.int, $sqlState, $nativeErr, $errMsg)
|
return (res.int, $sqlState, $nativeErr, $errMsg)
|
||||||
|
|
||||||
proc dbError*(db: var DbConn) {.
|
proc dbError*(db: var DbConn) {.
|
||||||
tags: [FReadODBC,FWriteODBC], raises: [EDb] .} =
|
tags: [ReadDbEffect, WriteDbEffect], raises: [DbError] .} =
|
||||||
## Raises an `[EDb]` exception with ODBC error information
|
## Raises an `[DbError]` exception with ODBC error information
|
||||||
var
|
var
|
||||||
e: ref EDb
|
e: ref DbError
|
||||||
ss, ne, msg: string = ""
|
ss, ne, msg: string = ""
|
||||||
isAnError = false
|
isAnError = false
|
||||||
res: int = 0
|
res: int = 0
|
||||||
|
|
@ -137,19 +127,12 @@ proc dbError*(db: var DbConn) {.
|
||||||
properFreeResult(SQL_HANDLE_ENV, db.env)
|
properFreeResult(SQL_HANDLE_ENV, db.env)
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
proc dbError*(msg: string) {.noreturn.} =
|
proc SqlCheck(db: var DbConn, resVal: TSqlSmallInt) {.raises: [DbError]} =
|
||||||
## Raises an EDb exception with message `msg`.
|
|
||||||
var e: ref EDb
|
|
||||||
new(e)
|
|
||||||
e.msg = msg
|
|
||||||
raise e
|
|
||||||
|
|
||||||
proc SqlCheck(db: var DbConn, resVal: TSqlSmallInt) {.raises: [EDb]} =
|
|
||||||
## Wrapper that checks if ``resVal`` is not SQL_SUCCESS and if so, raises [EDb]
|
## Wrapper that checks if ``resVal`` is not SQL_SUCCESS and if so, raises [EDb]
|
||||||
if resVal != SQL_SUCCESS: dbError(db)
|
if resVal != SQL_SUCCESS: dbError(db)
|
||||||
|
|
||||||
proc SqlGetDBMS(db: var DbConn): string {.
|
proc SqlGetDBMS(db: var DbConn): string {.
|
||||||
tags: [FReadODBC, FWriteODBC], raises: [] .} =
|
tags: [ReadDbEffect, WriteDbEffect], raises: [] .} =
|
||||||
## Returns the ODBC SQL_DBMS_NAME string
|
## Returns the ODBC SQL_DBMS_NAME string
|
||||||
const
|
const
|
||||||
SQL_DBMS_NAME = 17.SqlUSmallInt
|
SQL_DBMS_NAME = 17.SqlUSmallInt
|
||||||
|
|
@ -188,7 +171,7 @@ proc dbFormat(formatstr: SqlQuery, args: varargs[string]): string {.
|
||||||
|
|
||||||
proc prepareFetch(db: var DbConn, query: SqlQuery,
|
proc prepareFetch(db: var DbConn, query: SqlQuery,
|
||||||
args: varargs[string, `$`]) {.
|
args: varargs[string, `$`]) {.
|
||||||
tags: [FReadDb, FReadODBC, FWriteODBC], raises: [EDb].} =
|
tags: [ReadDbEffect, WriteDbEffect], raises: [DbError].} =
|
||||||
# Prepare a statement, execute it and fetch the data to the driver
|
# Prepare a statement, execute it and fetch the data to the driver
|
||||||
# ready for retrieval of the data
|
# ready for retrieval of the data
|
||||||
# Used internally by iterators and retrieval procs
|
# Used internally by iterators and retrieval procs
|
||||||
|
|
@ -203,7 +186,7 @@ proc prepareFetch(db: var DbConn, query: SqlQuery,
|
||||||
|
|
||||||
proc prepareFetchDirect(db: var DbConn, query: SqlQuery,
|
proc prepareFetchDirect(db: var DbConn, query: SqlQuery,
|
||||||
args: varargs[string, `$`]) {.
|
args: varargs[string, `$`]) {.
|
||||||
tags: [FReadDb, FWriteDb, FReadODBC, FWriteODBC], raises: [EDb].} =
|
tags: [ReadDbEffect, WriteDbEffect], raises: [DbError].} =
|
||||||
# Prepare a statement, execute it and fetch the data to the driver
|
# Prepare a statement, execute it and fetch the data to the driver
|
||||||
# ready for retrieval of the data
|
# ready for retrieval of the data
|
||||||
# Used internally by iterators and retrieval procs
|
# Used internally by iterators and retrieval procs
|
||||||
|
|
@ -216,7 +199,7 @@ proc prepareFetchDirect(db: var DbConn, query: SqlQuery,
|
||||||
db.SqlCheck(SQLFetch(db.stmt))
|
db.SqlCheck(SQLFetch(db.stmt))
|
||||||
|
|
||||||
proc tryExec*(db: var DbConn, query: SqlQuery, args: varargs[string, `$`]): bool {.
|
proc tryExec*(db: var DbConn, query: SqlQuery, args: varargs[string, `$`]): bool {.
|
||||||
tags: [FReadDB, FWriteDb, FReadODBC, FWriteODBC], raises: [].} =
|
tags: [ReadDbEffect, WriteDbEffect], raises: [].} =
|
||||||
## Tries to execute the query and returns true if successful, false otherwise.
|
## Tries to execute the query and returns true if successful, false otherwise.
|
||||||
var
|
var
|
||||||
res:TSqlSmallInt = -1
|
res:TSqlSmallInt = -1
|
||||||
|
|
@ -231,11 +214,11 @@ proc tryExec*(db: var DbConn, query: SqlQuery, args: varargs[string, `$`]): bool
|
||||||
return res == SQL_SUCCESS
|
return res == SQL_SUCCESS
|
||||||
|
|
||||||
proc rawExec(db: var DbConn, query: SqlQuery, args: varargs[string, `$`]) {.
|
proc rawExec(db: var DbConn, query: SqlQuery, args: varargs[string, `$`]) {.
|
||||||
tags: [FReadDb, FWriteDb, FReadODBC, FWriteODBC], raises: [EDb].} =
|
tags: [ReadDbEffect, WriteDbEffect], raises: [DbError].} =
|
||||||
db.prepareFetchDirect(query, args)
|
db.prepareFetchDirect(query, args)
|
||||||
|
|
||||||
proc exec*(db: var DbConn, query: SqlQuery, args: varargs[string, `$`]) {.
|
proc exec*(db: var DbConn, query: SqlQuery, args: varargs[string, `$`]) {.
|
||||||
tags: [FReadDb, FWriteDb, FReadODBC, FWriteODBC], raises: [EDb].} =
|
tags: [ReadDbEffect, WriteDbEffect], raises: [DbError].} =
|
||||||
## Executes the query and raises EDB if not successful.
|
## Executes the query and raises EDB if not successful.
|
||||||
db.prepareFetchDirect(query, args)
|
db.prepareFetchDirect(query, args)
|
||||||
properFreeResult(SQL_HANDLE_STMT, db.stmt)
|
properFreeResult(SQL_HANDLE_STMT, db.stmt)
|
||||||
|
|
@ -246,7 +229,7 @@ proc newRow(L: int): Row {.noSideEFfect.} =
|
||||||
|
|
||||||
iterator fastRows*(db: var DbConn, query: SqlQuery,
|
iterator fastRows*(db: var DbConn, query: SqlQuery,
|
||||||
args: varargs[string, `$`]): Row {.
|
args: varargs[string, `$`]): Row {.
|
||||||
tags: [FReadDB, FReadODBC, FWriteODBC], raises: [EDb].} =
|
tags: [ReadDbEffect, WriteDbEffect], raises: [DbError].} =
|
||||||
## Executes the query and iterates over the result dataset.
|
## Executes the query and iterates over the result dataset.
|
||||||
##
|
##
|
||||||
## This is very fast, but potentially dangerous. Use this iterator only
|
## This is very fast, but potentially dangerous. Use this iterator only
|
||||||
|
|
@ -278,7 +261,7 @@ iterator fastRows*(db: var DbConn, query: SqlQuery,
|
||||||
|
|
||||||
iterator instantRows*(db: var DbConn, query: SqlQuery,
|
iterator instantRows*(db: var DbConn, query: SqlQuery,
|
||||||
args: varargs[string, `$`]): InstantRow
|
args: varargs[string, `$`]): InstantRow
|
||||||
{.tags: [FReadDb, FReadODBC, FWriteODBC].} =
|
{.tags: [ReadDbEffect, WriteDbEffect].} =
|
||||||
## Same as fastRows but returns a handle that can be used to get column text
|
## Same as fastRows but returns a handle that can be used to get column text
|
||||||
## on demand using []. Returned handle is valid only within the interator body.
|
## on demand using []. Returned handle is valid only within the interator body.
|
||||||
var
|
var
|
||||||
|
|
@ -310,7 +293,7 @@ proc len*(row: InstantRow): int {.inline.} =
|
||||||
|
|
||||||
proc getRow*(db: var DbConn, query: SqlQuery,
|
proc getRow*(db: var DbConn, query: SqlQuery,
|
||||||
args: varargs[string, `$`]): Row {.
|
args: varargs[string, `$`]): Row {.
|
||||||
tags: [FReadDB, FReadODBC, FWriteODBC], raises: [EDb].} =
|
tags: [ReadDbEffect, WriteDbEffect], raises: [DbError].} =
|
||||||
## Retrieves a single row. If the query doesn't return any rows, this proc
|
## Retrieves a single row. If the query doesn't return any rows, this proc
|
||||||
## will return a Row with empty strings for each column.
|
## will return a Row with empty strings for each column.
|
||||||
var
|
var
|
||||||
|
|
@ -331,7 +314,7 @@ proc getRow*(db: var DbConn, query: SqlQuery,
|
||||||
|
|
||||||
proc getAllRows*(db: var DbConn, query: SqlQuery,
|
proc getAllRows*(db: var DbConn, query: SqlQuery,
|
||||||
args: varargs[string, `$`]): seq[Row] {.
|
args: varargs[string, `$`]): seq[Row] {.
|
||||||
tags: [FReadDB, FReadODBC, FWriteODBC], raises: [EDb].} =
|
tags: [ReadDbEffect, WriteDbEffect], raises: [DbError].} =
|
||||||
## Executes the query and returns the whole result dataset.
|
## Executes the query and returns the whole result dataset.
|
||||||
var
|
var
|
||||||
rowRes: Row
|
rowRes: Row
|
||||||
|
|
@ -355,7 +338,7 @@ proc getAllRows*(db: var DbConn, query: SqlQuery,
|
||||||
|
|
||||||
iterator rows*(db: var DbConn, query: SqlQuery,
|
iterator rows*(db: var DbConn, query: SqlQuery,
|
||||||
args: varargs[string, `$`]): Row {.
|
args: varargs[string, `$`]): Row {.
|
||||||
tags: [FReadDB, FReadODBC, FWriteODBC], raises: [EDb].} =
|
tags: [ReadDbEffect, WriteDbEffect], raises: [DbError].} =
|
||||||
## Same as `fastRows`, but slower and safe.
|
## Same as `fastRows`, but slower and safe.
|
||||||
##
|
##
|
||||||
## This retrieves ALL rows into memory before
|
## This retrieves ALL rows into memory before
|
||||||
|
|
@ -365,7 +348,7 @@ iterator rows*(db: var DbConn, query: SqlQuery,
|
||||||
|
|
||||||
proc getValue*(db: var DbConn, query: SqlQuery,
|
proc getValue*(db: var DbConn, query: SqlQuery,
|
||||||
args: varargs[string, `$`]): string {.
|
args: varargs[string, `$`]): string {.
|
||||||
tags: [FReadDB, FReadODBC, FWriteODBC], raises: [].} =
|
tags: [ReadDbEffect, WriteDbEffect], raises: [].} =
|
||||||
## Executes the query and returns the first column of the first row of the
|
## Executes the query and returns the first column of the first row of the
|
||||||
## result dataset. Returns "" if the dataset contains no rows or the database
|
## result dataset. Returns "" if the dataset contains no rows or the database
|
||||||
## value is NULL.
|
## value is NULL.
|
||||||
|
|
@ -376,7 +359,7 @@ proc getValue*(db: var DbConn, query: SqlQuery,
|
||||||
|
|
||||||
proc tryInsertId*(db: var DbConn, query: SqlQuery,
|
proc tryInsertId*(db: var DbConn, query: SqlQuery,
|
||||||
args: varargs[string, `$`]): int64 {.
|
args: varargs[string, `$`]): int64 {.
|
||||||
tags: [FReadDb, FWriteDb, FReadODBC, FWriteODBC], raises: [].} =
|
tags: [ReadDbEffect, WriteDbEffect], raises: [].} =
|
||||||
## 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.
|
||||||
if not tryExec(db, query, args):
|
if not tryExec(db, query, args):
|
||||||
|
|
@ -401,7 +384,7 @@ proc tryInsertId*(db: var DbConn, query: SqlQuery,
|
||||||
|
|
||||||
proc insertId*(db: var DbConn, query: SqlQuery,
|
proc insertId*(db: var DbConn, query: SqlQuery,
|
||||||
args: varargs[string, `$`]): int64 {.
|
args: varargs[string, `$`]): int64 {.
|
||||||
tags: [FReadDb, FWriteDb, FReadODBC, FWriteODBC], raises: [EDb].} =
|
tags: [ReadDbEffect, WriteDbEffect], raises: [DbError].} =
|
||||||
## Executes the query (typically "INSERT") and returns the
|
## Executes the query (typically "INSERT") and returns the
|
||||||
## generated ID for the row.
|
## generated ID for the row.
|
||||||
result = tryInsertID(db, query, args)
|
result = tryInsertID(db, query, args)
|
||||||
|
|
@ -409,7 +392,7 @@ proc insertId*(db: var DbConn, query: SqlQuery,
|
||||||
|
|
||||||
proc execAffectedRows*(db: var DbConn, query: SqlQuery,
|
proc execAffectedRows*(db: var DbConn, query: SqlQuery,
|
||||||
args: varargs[string, `$`]): int64 {.
|
args: varargs[string, `$`]): int64 {.
|
||||||
tags: [FReadDB, FWriteDb, FReadODBC, FWriteODBC], raises: [EDb].} =
|
tags: [ReadDbEffect, WriteDbEffect], raises: [DbError].} =
|
||||||
## Runs the query (typically "UPDATE") and returns the
|
## Runs the query (typically "UPDATE") and returns the
|
||||||
## number of affected rows
|
## number of affected rows
|
||||||
result = -1
|
result = -1
|
||||||
|
|
@ -426,7 +409,7 @@ proc execAffectedRows*(db: var DbConn, query: SqlQuery,
|
||||||
result = rCnt
|
result = rCnt
|
||||||
|
|
||||||
proc close*(db: var DbConn) {.
|
proc close*(db: var DbConn) {.
|
||||||
tags: [FWriteODBC], raises: [].} =
|
tags: [WriteDbEffect], raises: [].} =
|
||||||
## Closes the database connection.
|
## Closes the database connection.
|
||||||
if db.hDb != nil:
|
if db.hDb != nil:
|
||||||
try:
|
try:
|
||||||
|
|
@ -440,7 +423,7 @@ proc close*(db: var DbConn) {.
|
||||||
discard
|
discard
|
||||||
|
|
||||||
proc open*(connection, user, password, database: string): DbConn {.
|
proc open*(connection, user, password, database: string): DbConn {.
|
||||||
tags: [FReadODBC, FWriteODBC], raises: [EDb].} =
|
tags: [ReadDbEffect, WriteDbEffect], raises: [DbError].} =
|
||||||
## Opens a database connection.
|
## Opens a database connection.
|
||||||
##
|
##
|
||||||
## Raises `EDb` if the connection could not be established.
|
## Raises `EDb` if the connection could not be established.
|
||||||
|
|
@ -471,7 +454,7 @@ proc open*(connection, user, password, database: string): DbConn {.
|
||||||
result.dbError()
|
result.dbError()
|
||||||
|
|
||||||
proc setEncoding*(connection: DbConn, encoding: string): bool {.
|
proc setEncoding*(connection: DbConn, encoding: string): bool {.
|
||||||
tags: [FReadODBC, FWriteODBC], raises: [EDb].} =
|
tags: [ReadDbEffect, WriteDbEffect], raises: [DbError].} =
|
||||||
## Currently not implemented for ODBC.
|
## Currently not implemented for ODBC.
|
||||||
##
|
##
|
||||||
## Sets the encoding of a database connection, returns true for
|
## Sets the encoding of a database connection, returns true for
|
||||||
|
|
|
||||||
|
|
@ -68,7 +68,7 @@ export db_common
|
||||||
type
|
type
|
||||||
DbConn* = PPGconn ## encapsulates a database connection
|
DbConn* = PPGconn ## encapsulates a database connection
|
||||||
Row* = seq[string] ## a row of a dataset. NULL database values will be
|
Row* = seq[string] ## a row of a dataset. NULL database values will be
|
||||||
## transformed always to the empty string.
|
## converted to nil.
|
||||||
InstantRow* = tuple[res: PPGresult, line: int32] ## a handle that can be
|
InstantRow* = tuple[res: PPGresult, line: int32] ## a handle that can be
|
||||||
## used to get a row's
|
## used to get a row's
|
||||||
## column text on demand
|
## column text on demand
|
||||||
|
|
|
||||||
|
|
@ -48,7 +48,7 @@ export db_common
|
||||||
type
|
type
|
||||||
DbConn* = PSqlite3 ## encapsulates a database connection
|
DbConn* = PSqlite3 ## encapsulates a database connection
|
||||||
Row* = seq[string] ## a row of a dataset. NULL database values will be
|
Row* = seq[string] ## a row of a dataset. NULL database values will be
|
||||||
## transformed always to the empty string.
|
## converted to nil.
|
||||||
InstantRow* = Pstmt ## a handle that can be used to get a row's column
|
InstantRow* = Pstmt ## a handle that can be used to get a row's column
|
||||||
## text on demand
|
## text on demand
|
||||||
{.deprecated: [TRow: Row, TDbConn: DbConn].}
|
{.deprecated: [TRow: Row, TDbConn: DbConn].}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue