Merge branch 'master' of github.com:Araq/Nimrod

This commit is contained in:
Araq 2012-10-20 18:48:49 +02:00
commit b1c44c9ae1
3 changed files with 32 additions and 31 deletions

View file

@ -34,7 +34,7 @@ proc dbError*(msg: string) {.noreturn.} =
raise e raise e
when false: when false:
proc dbQueryOpt*(db: TDbConn, query: string, args: varargs[string]) = proc dbQueryOpt*(db: TDbConn, query: string, args: varargs[string, `$`]) =
var stmt = mysql_stmt_init(db) var stmt = mysql_stmt_init(db)
if stmt == nil: dbError(db) if stmt == nil: dbError(db)
if mysql_stmt_prepare(stmt, query, len(query)) != 0: if mysql_stmt_prepare(stmt, query, len(query)) != 0:
@ -60,12 +60,12 @@ proc dbFormat(formatstr: TSqlQuery, args: varargs[string]): string =
else: else:
add(result, c) add(result, c)
proc TryExec*(db: TDbConn, query: TSqlQuery, args: varargs[string]): bool = proc TryExec*(db: TDbConn, query: TSqlQuery, args: varargs[string, `$`]): bool =
## 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 q = dbFormat(query, args) var q = dbFormat(query, args)
return mysql.RealQuery(db, q, q.len) == 0'i32 return mysql.RealQuery(db, q, q.len) == 0'i32
proc Exec*(db: TDbConn, query: TSqlQuery, args: varargs[string]) = proc Exec*(db: TDbConn, query: TSqlQuery, args: varargs[string, `$`]) =
## executes the query and raises EDB if not successful. ## executes the query and raises EDB if not successful.
var q = dbFormat(query, args) var q = dbFormat(query, args)
if mysql.RealQuery(db, q, q.len) != 0'i32: dbError(db) if mysql.RealQuery(db, q, q.len) != 0'i32: dbError(db)
@ -80,7 +80,7 @@ proc properFreeResult(sqlres: mysql.PRES, row: cstringArray) =
mysql.FreeResult(sqlres) mysql.FreeResult(sqlres)
iterator FastRows*(db: TDbConn, query: TSqlQuery, iterator FastRows*(db: TDbConn, query: TSqlQuery,
args: varargs[string]): TRow = args: varargs[string, `$`]): TRow =
## executes the query and iterates over the result dataset. This is very ## executes the query and iterates over the result dataset. This is very
## fast, but potenially dangerous: If the for-loop-body executes another ## fast, but potenially dangerous: If the for-loop-body executes another
## query, the results can be undefined. For MySQL this is the case!. ## query, the results can be undefined. For MySQL this is the case!.
@ -100,7 +100,7 @@ iterator FastRows*(db: TDbConn, query: TSqlQuery,
properFreeResult(sqlres, row) properFreeResult(sqlres, row)
proc getRow*(db: TDbConn, query: TSqlQuery, proc getRow*(db: TDbConn, query: TSqlQuery,
args: varargs[string]): TRow = args: varargs[string, `$`]): TRow =
## retrieves a single row. ## retrieves a single row.
Exec(db, query, args) Exec(db, query, args)
var sqlres = mysql.UseResult(db) var sqlres = mysql.UseResult(db)
@ -115,7 +115,7 @@ proc getRow*(db: TDbConn, query: TSqlQuery,
properFreeResult(sqlres, row) properFreeResult(sqlres, row)
proc GetAllRows*(db: TDbConn, query: TSqlQuery, proc GetAllRows*(db: TDbConn, query: TSqlQuery,
args: varargs[string]): seq[TRow] = args: varargs[string, `$`]): seq[TRow] =
## executes the query and returns the whole result dataset. ## executes the query and returns the whole result dataset.
result = @[] result = @[]
Exec(db, query, args) Exec(db, query, args)
@ -134,12 +134,12 @@ proc GetAllRows*(db: TDbConn, query: TSqlQuery,
mysql.FreeResult(sqlres) mysql.FreeResult(sqlres)
iterator Rows*(db: TDbConn, query: TSqlQuery, iterator Rows*(db: TDbConn, query: TSqlQuery,
args: varargs[string]): TRow = args: varargs[string, `$`]): TRow =
## same as `FastRows`, but slower and safe. ## same as `FastRows`, but slower and safe.
for r in items(GetAllRows(db, query, args)): yield r for r in items(GetAllRows(db, query, args)): yield r
proc GetValue*(db: TDbConn, query: TSqlQuery, proc GetValue*(db: TDbConn, query: TSqlQuery,
args: varargs[string]): string = args: varargs[string, `$`]): string =
## executes the query and returns the result dataset's the first column ## executes the query and returns the result dataset's the first column
## of the first row. Returns "" if the dataset contains no rows. This uses ## of the first row. Returns "" if the dataset contains no rows. This uses
## `FastRows`, so it inherits its fragile behaviour. ## `FastRows`, so it inherits its fragile behaviour.
@ -149,7 +149,7 @@ proc GetValue*(db: TDbConn, query: TSqlQuery,
break break
proc TryInsertID*(db: TDbConn, query: TSqlQuery, proc TryInsertID*(db: TDbConn, query: TSqlQuery,
args: varargs[string]): int64 = args: varargs[string, `$`]): int64 =
## 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)
@ -158,14 +158,15 @@ proc TryInsertID*(db: TDbConn, query: TSqlQuery,
else: else:
result = mysql.InsertId(db) result = mysql.InsertId(db)
proc InsertID*(db: TDbConn, query: TSqlQuery, args: varargs[string]): int64 = proc InsertID*(db: TDbConn, query: TSqlQuery,
args: varargs[string, `$`]): int64 =
## 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)
if result < 0: dbError(db) if result < 0: dbError(db)
proc ExecAffectedRows*(db: TDbConn, query: TSqlQuery, proc ExecAffectedRows*(db: TDbConn, query: TSqlQuery,
args: varargs[string]): int64 = args: varargs[string, `$`]): int64 =
## runs the query (typically "UPDATE") and returns the ## runs the query (typically "UPDATE") and returns the
## number of affected rows ## number of affected rows
Exec(db, query, args) Exec(db, query, args)

View file

@ -60,14 +60,14 @@ proc dbFormat(formatstr: TSqlQuery, args: varargs[string]): string =
add(result, c) add(result, c)
proc TryExec*(db: TDbConn, query: TSqlQuery, proc TryExec*(db: TDbConn, query: TSqlQuery,
args: varargs[string]): bool = args: varargs[string, `$`]): bool =
## 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 q = dbFormat(query, args) var q = dbFormat(query, args)
var res = PQExec(db, q) var res = PQExec(db, q)
result = PQresultStatus(res) == PGRES_COMMAND_OK result = PQresultStatus(res) == PGRES_COMMAND_OK
PQclear(res) PQclear(res)
proc Exec*(db: TDbConn, query: TSqlQuery, args: varargs[string]) = proc Exec*(db: TDbConn, query: TSqlQuery, args: varargs[string, `$`]) =
## executes the query and raises EDB if not successful. ## executes the query and raises EDB if not successful.
var q = dbFormat(query, args) var q = dbFormat(query, args)
var res = PQExec(db, q) var res = PQExec(db, q)
@ -91,7 +91,7 @@ proc setRow(res: PPGresult, r: var TRow, line, cols: int32) =
add(r[col], x) add(r[col], x)
iterator FastRows*(db: TDbConn, query: TSqlQuery, iterator FastRows*(db: TDbConn, query: TSqlQuery,
args: varargs[string]): TRow = args: varargs[string, `$`]): TRow =
## executes the query and iterates over the result dataset. This is very ## executes the query and iterates over the result dataset. This is very
## fast, but potenially dangerous: If the for-loop-body executes another ## fast, but potenially dangerous: If the for-loop-body executes another
## query, the results can be undefined. For Postgres it is safe though. ## query, the results can be undefined. For Postgres it is safe though.
@ -104,7 +104,7 @@ iterator FastRows*(db: TDbConn, query: TSqlQuery,
PQclear(res) PQclear(res)
proc getRow*(db: TDbConn, query: TSqlQuery, proc getRow*(db: TDbConn, query: TSqlQuery,
args: varargs[string]): TRow = args: varargs[string, `$`]): TRow =
## retrieves a single row. ## retrieves a single row.
var res = setupQuery(db, query, args) var res = setupQuery(db, query, args)
var L = PQnfields(res) var L = PQnfields(res)
@ -113,26 +113,26 @@ proc getRow*(db: TDbConn, query: TSqlQuery,
PQclear(res) PQclear(res)
proc GetAllRows*(db: TDbConn, query: TSqlQuery, proc GetAllRows*(db: TDbConn, query: TSqlQuery,
args: varargs[string]): seq[TRow] = args: varargs[string, `$`]): seq[TRow] =
## executes the query and returns the whole result dataset. ## executes the query and returns the whole result dataset.
result = @[] result = @[]
for r in FastRows(db, query, args): for r in FastRows(db, query, args):
result.add(r) result.add(r)
iterator Rows*(db: TDbConn, query: TSqlQuery, iterator Rows*(db: TDbConn, query: TSqlQuery,
args: varargs[string]): TRow = args: varargs[string, `$`]): TRow =
## same as `FastRows`, but slower and safe. ## same as `FastRows`, but slower and safe.
for r in items(GetAllRows(db, query, args)): yield r for r in items(GetAllRows(db, query, args)): yield r
proc GetValue*(db: TDbConn, query: TSqlQuery, proc GetValue*(db: TDbConn, query: TSqlQuery,
args: varargs[string]): string = args: varargs[string, `$`]): string =
## executes the query and returns the result dataset's the first column ## executes the query and returns the result dataset's the first column
## of the first row. Returns "" if the dataset contains no rows. ## of the first row. Returns "" if the dataset contains no rows.
var x = PQgetvalue(setupQuery(db, query, args), 0, 0) var x = PQgetvalue(setupQuery(db, query, args), 0, 0)
result = if isNil(x): "" else: $x result = if isNil(x): "" else: $x
proc TryInsertID*(db: TDbConn, query: TSqlQuery, proc TryInsertID*(db: TDbConn, query: TSqlQuery,
args: varargs[string]): int64 = args: varargs[string, `$`]): int64 =
## 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. For Postgre this adds ## generated ID for the row or -1 in case of an error. For Postgre this adds
## ``RETURNING id`` to the query, so it only works if your primary key is ## ``RETURNING id`` to the query, so it only works if your primary key is
@ -144,7 +144,7 @@ proc TryInsertID*(db: TDbConn, query: TSqlQuery,
result = -1 result = -1
proc InsertID*(db: TDbConn, query: TSqlQuery, proc InsertID*(db: TDbConn, query: TSqlQuery,
args: varargs[string]): int64 = args: varargs[string, `$`]): int64 =
## executes the query (typically "INSERT") and returns the ## executes the query (typically "INSERT") and returns the
## generated ID for the row. For Postgre this adds ## generated ID for the row. For Postgre this adds
## ``RETURNING id`` to the query, so it only works if your primary key is ## ``RETURNING id`` to the query, so it only works if your primary key is
@ -153,7 +153,7 @@ proc InsertID*(db: TDbConn, query: TSqlQuery,
if result < 0: dbError(db) if result < 0: dbError(db)
proc ExecAffectedRows*(db: TDbConn, query: TSqlQuery, proc ExecAffectedRows*(db: TDbConn, query: TSqlQuery,
args: varargs[string]): int64 = args: varargs[string, `$`]): int64 =
## executes the query (typically "UPDATE") and returns the ## executes the query (typically "UPDATE") and returns the
## number of affected rows. ## number of affected rows.
var q = dbFormat(query, args) var q = dbFormat(query, args)

View file

@ -60,7 +60,7 @@ proc dbFormat(formatstr: TSqlQuery, args: varargs[string]): string =
add(result, c) add(result, c)
proc TryExec*(db: TDbConn, query: TSqlQuery, proc TryExec*(db: TDbConn, query: TSqlQuery,
args: varargs[string]): bool = args: varargs[string, `$`]): bool =
## 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 q = dbFormat(query, args) var q = dbFormat(query, args)
var stmt: sqlite3.PStmt var stmt: sqlite3.PStmt
@ -68,7 +68,7 @@ proc TryExec*(db: TDbConn, query: TSqlQuery,
if step(stmt) == SQLITE_DONE: if step(stmt) == SQLITE_DONE:
result = finalize(stmt) == SQLITE_OK result = finalize(stmt) == SQLITE_OK
proc Exec*(db: TDbConn, query: TSqlQuery, args: varargs[string]) = proc Exec*(db: TDbConn, query: TSqlQuery, args: varargs[string, `$`]) =
## executes the query and raises EDB if not successful. ## executes the query and raises EDB if not successful.
if not TryExec(db, query, args): dbError(db) if not TryExec(db, query, args): dbError(db)
@ -89,7 +89,7 @@ proc setRow(stmt: PStmt, r: var TRow, cols: cint) =
if not isNil(x): add(r[col], x) if not isNil(x): add(r[col], x)
iterator FastRows*(db: TDbConn, query: TSqlQuery, iterator FastRows*(db: TDbConn, query: TSqlQuery,
args: varargs[string]): TRow = args: varargs[string, `$`]): TRow =
## executes the query and iterates over the result dataset. This is very ## executes the query and iterates over the result dataset. This is very
## fast, but potenially dangerous: If the for-loop-body executes another ## fast, but potenially dangerous: If the for-loop-body executes another
## query, the results can be undefined. For Sqlite it is safe though. ## query, the results can be undefined. For Sqlite it is safe though.
@ -102,7 +102,7 @@ iterator FastRows*(db: TDbConn, query: TSqlQuery,
if finalize(stmt) != SQLITE_OK: dbError(db) if finalize(stmt) != SQLITE_OK: dbError(db)
proc getRow*(db: TDbConn, query: TSqlQuery, proc getRow*(db: TDbConn, query: TSqlQuery,
args: varargs[string]): TRow = args: varargs[string, `$`]): TRow =
## retrieves a single row. ## retrieves a single row.
var stmt = setupQuery(db, query, args) var stmt = setupQuery(db, query, args)
var L = (columnCount(stmt)) var L = (columnCount(stmt))
@ -112,19 +112,19 @@ proc getRow*(db: TDbConn, query: TSqlQuery,
if finalize(stmt) != SQLITE_OK: dbError(db) if finalize(stmt) != SQLITE_OK: dbError(db)
proc GetAllRows*(db: TDbConn, query: TSqlQuery, proc GetAllRows*(db: TDbConn, query: TSqlQuery,
args: varargs[string]): seq[TRow] = args: varargs[string, `$`]): seq[TRow] =
## executes the query and returns the whole result dataset. ## executes the query and returns the whole result dataset.
result = @[] result = @[]
for r in FastRows(db, query, args): for r in FastRows(db, query, args):
result.add(r) result.add(r)
iterator Rows*(db: TDbConn, query: TSqlQuery, iterator Rows*(db: TDbConn, query: TSqlQuery,
args: varargs[string]): TRow = args: varargs[string, `$`]): TRow =
## same as `FastRows`, but slower and safe. ## same as `FastRows`, but slower and safe.
for r in FastRows(db, query, args): yield r for r in FastRows(db, query, args): yield r
proc GetValue*(db: TDbConn, query: TSqlQuery, proc GetValue*(db: TDbConn, query: TSqlQuery,
args: varargs[string]): string = args: varargs[string, `$`]): string =
## executes the query and returns the result dataset's the first column ## executes the query and returns the result dataset's the first column
## of the first row. Returns "" if the dataset contains no rows. ## of the first row. Returns "" if the dataset contains no rows.
var stmt = setupQuery(db, query, args) var stmt = setupQuery(db, query, args)
@ -140,7 +140,7 @@ proc GetValue*(db: TDbConn, query: TSqlQuery,
result = "" result = ""
proc TryInsertID*(db: TDbConn, query: TSqlQuery, proc TryInsertID*(db: TDbConn, query: TSqlQuery,
args: varargs[string]): int64 = args: varargs[string, `$`]): int64 =
## 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 tryExec(db, query, args): if tryExec(db, query, args):
@ -149,7 +149,7 @@ proc TryInsertID*(db: TDbConn, query: TSqlQuery,
result = -1 result = -1
proc InsertID*(db: TDbConn, query: TSqlQuery, proc InsertID*(db: TDbConn, query: TSqlQuery,
args: varargs[string]): int64 = args: varargs[string, `$`]): int64 =
## executes the query (typically "INSERT") and returns the ## executes the query (typically "INSERT") and returns the
## generated ID for the row. For Postgre this adds ## generated ID for the row. For Postgre this adds
## ``RETURNING id`` to the query, so it only works if your primary key is ## ``RETURNING id`` to the query, so it only works if your primary key is
@ -158,7 +158,7 @@ proc InsertID*(db: TDbConn, query: TSqlQuery,
if result < 0: dbError(db) if result < 0: dbError(db)
proc ExecAffectedRows*(db: TDbConn, query: TSqlQuery, proc ExecAffectedRows*(db: TDbConn, query: TSqlQuery,
args: varargs[string]): int64 = args: varargs[string, `$`]): int64 =
## executes the query (typically "UPDATE") and returns the ## executes the query (typically "UPDATE") and returns the
## number of affected rows. ## number of affected rows.
Exec(db, query, args) Exec(db, query, args)