Cleanup of line endings

This commit is contained in:
JamesP 2015-09-05 11:49:25 +10:00
commit 22d56fc494

View file

@ -7,7 +7,7 @@
# distribution, for details about the copyright. # distribution, for details about the copyright.
# #
## A higher level `mySQL`:idx: database wrapper. The same interface is ## A higher level `mySQL`:idx: database wrapper. The same interface is
## implemented for other databases too. ## implemented for other databases too.
## ##
## Example: ## Example:
@ -60,22 +60,22 @@ type
{.deprecated: [TRow: Row, TSqlQuery: SqlQuery, TDbConn: DbConn].} {.deprecated: [TRow: Row, TSqlQuery: SqlQuery, TDbConn: DbConn].}
proc sql*(query: string): SqlQuery {.noSideEffect, inline.} = proc sql*(query: string): SqlQuery {.noSideEffect, inline.} =
## constructs a SqlQuery from the string `query`. This is supposed to be ## constructs a SqlQuery from the string `query`. This is supposed to be
## used as a raw-string-literal modifier: ## used as a raw-string-literal modifier:
## ``sql"update user set counter = counter + 1"`` ## ``sql"update user set counter = counter + 1"``
## ##
## If assertions are turned off, it does nothing. If assertions are turned ## If assertions are turned off, it does nothing. If assertions are turned
## on, later versions will check the string for valid syntax. ## on, later versions will check the string for valid syntax.
result = SqlQuery(query) result = SqlQuery(query)
proc dbError(db: DbConn) {.noreturn.} = proc dbError(db: DbConn) {.noreturn.} =
## raises an EDb exception. ## raises an EDb exception.
var e: ref EDb var e: ref EDb
new(e) new(e)
e.msg = $mysql.error(db) e.msg = $mysql.error(db)
raise e raise e
proc dbError*(msg: string) {.noreturn.} = proc dbError*(msg: string) {.noreturn.} =
## raises an EDb exception with message `msg`. ## raises an EDb exception with message `msg`.
var e: ref EDb var e: ref EDb
new(e) new(e)
@ -86,9 +86,9 @@ when false:
proc dbQueryOpt*(db: DbConn, query: string, args: varargs[string, `$`]) = proc dbQueryOpt*(db: DbConn, 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:
dbError(db) dbError(db)
var var
binding: seq[MYSQL_BIND] binding: seq[MYSQL_BIND]
discard mysql_stmt_close(stmt) discard mysql_stmt_close(stmt)
@ -110,9 +110,9 @@ proc dbFormat(formatstr: SqlQuery, args: varargs[string]): string =
else: else:
add(result, dbQuote(args[a])) add(result, dbQuote(args[a]))
inc(a) inc(a)
else: else:
add(result, c) add(result, c)
proc tryExec*(db: DbConn, query: SqlQuery, args: varargs[string, `$`]): bool {. proc tryExec*(db: DbConn, query: SqlQuery, args: varargs[string, `$`]): bool {.
tags: [FReadDB, FWriteDb].} = tags: [FReadDB, FWriteDb].} =
## tries to execute the query and returns true if successful, false otherwise. ## tries to execute the query and returns true if successful, false otherwise.
@ -128,16 +128,16 @@ proc exec*(db: DbConn, query: SqlQuery, 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)
proc newRow(L: int): Row = proc newRow(L: int): Row =
newSeq(result, L) newSeq(result, L)
for i in 0..L-1: result[i] = "" for i in 0..L-1: result[i] = ""
proc properFreeResult(sqlres: mysql.PRES, row: cstringArray) = proc properFreeResult(sqlres: mysql.PRES, row: cstringArray) =
if row != nil: if row != nil:
while mysql.fetchRow(sqlres) != nil: discard while mysql.fetchRow(sqlres) != nil: discard
mysql.freeResult(sqlres) mysql.freeResult(sqlres)
iterator fastRows*(db: DbConn, query: SqlQuery, iterator fastRows*(db: DbConn, query: SqlQuery,
args: varargs[string, `$`]): Row {.tags: [FReadDB].} = args: varargs[string, `$`]): Row {.tags: [FReadDB].} =
## executes the query and iterates over the result dataset. ## executes the query and iterates over the result dataset.
@ -156,7 +156,7 @@ iterator fastRows*(db: DbConn, query: SqlQuery,
while true: while true:
row = mysql.fetchRow(sqlres) row = mysql.fetchRow(sqlres)
if row == nil: break if row == nil: break
for i in 0..L-1: for i in 0..L-1:
setLen(result[i], 0) setLen(result[i], 0)
if row[i] == nil: if row[i] == nil:
result[i] = nil result[i] = nil
@ -199,8 +199,8 @@ proc getRow*(db: DbConn, query: SqlQuery,
var L = int(mysql.numFields(sqlres)) var L = int(mysql.numFields(sqlres))
result = newRow(L) result = newRow(L)
var row = mysql.fetchRow(sqlres) var row = mysql.fetchRow(sqlres)
if row != nil: if row != nil:
for i in 0..L-1: for i in 0..L-1:
setLen(result[i], 0) setLen(result[i], 0)
if row[i] == nil: if row[i] == nil:
result[i] = nil result[i] = nil
@ -208,7 +208,7 @@ proc getRow*(db: DbConn, query: SqlQuery,
add(result[i], row[i]) add(result[i], row[i])
properFreeResult(sqlres, row) properFreeResult(sqlres, row)
proc getAllRows*(db: DbConn, query: SqlQuery, proc getAllRows*(db: DbConn, query: SqlQuery,
args: varargs[string, `$`]): seq[Row] {.tags: [FReadDB].} = args: varargs[string, `$`]): seq[Row] {.tags: [FReadDB].} =
## executes the query and returns the whole result dataset. ## executes the query and returns the whole result dataset.
result = @[] result = @[]
@ -231,44 +231,44 @@ proc getAllRows*(db: DbConn, query: SqlQuery,
inc(j) inc(j)
mysql.freeResult(sqlres) mysql.freeResult(sqlres)
iterator rows*(db: DbConn, query: SqlQuery, iterator rows*(db: DbConn, query: SqlQuery,
args: varargs[string, `$`]): Row {.tags: [FReadDB].} = args: varargs[string, `$`]): Row {.tags: [FReadDB].} =
## 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: DbConn, query: SqlQuery, proc getValue*(db: DbConn, query: SqlQuery,
args: varargs[string, `$`]): string {.tags: [FReadDB].} = args: varargs[string, `$`]): string {.tags: [FReadDB].} =
## 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.
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: [FWriteDb].} = args: varargs[string, `$`]): int64 {.tags: [FWriteDb].} =
## 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)
if mysql.realQuery(db, q, q.len) != 0'i32: if mysql.realQuery(db, q, q.len) != 0'i32:
result = -1'i64 result = -1'i64
else: else:
result = mysql.insertId(db) result = mysql.insertId(db)
proc insertId*(db: DbConn, query: SqlQuery, proc insertId*(db: DbConn, query: SqlQuery,
args: varargs[string, `$`]): int64 {.tags: [FWriteDb].} = args: varargs[string, `$`]): int64 {.tags: [FWriteDb].} =
## 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: DbConn, query: SqlQuery, proc execAffectedRows*(db: DbConn, query: SqlQuery,
args: varargs[string, `$`]): int64 {. args: varargs[string, `$`]): int64 {.
tags: [FReadDB, FWriteDb].} = tags: [FReadDB, FWriteDb].} =
## runs the query (typically "UPDATE") and returns the ## runs the query (typically "UPDATE") and returns the
## number of affected rows ## number of affected rows
rawExec(db, query, args) rawExec(db, query, args)
result = mysql.affectedRows(db) result = mysql.affectedRows(db)
proc close*(db: DbConn) {.tags: [FDb].} = proc close*(db: DbConn) {.tags: [FDb].} =
## closes the database connection. ## closes the database connection.
if db != nil: mysql.close(db) if db != nil: mysql.close(db)
@ -277,14 +277,14 @@ proc open*(connection, user, password, database: string): DbConn {.
## opens a database connection. Raises `EDb` if the connection could not ## opens a database connection. Raises `EDb` if the connection could not
## be established. ## be established.
result = mysql.init(nil) result = mysql.init(nil)
if result == nil: dbError("could not open database connection") if result == nil: dbError("could not open database connection")
let let
colonPos = connection.find(':') colonPos = connection.find(':')
host = if colonPos < 0: connection host = if colonPos < 0: connection
else: substr(connection, 0, colonPos-1) else: substr(connection, 0, colonPos-1)
port: int32 = if colonPos < 0: 0'i32 port: int32 = if colonPos < 0: 0'i32
else: substr(connection, colonPos+1).parseInt.int32 else: substr(connection, colonPos+1).parseInt.int32
if mysql.realConnect(result, host, user, password, database, if mysql.realConnect(result, host, user, password, database,
port, nil, 0) == nil: port, nil, 0) == nil:
var errmsg = $mysql.error(result) var errmsg = $mysql.error(result)
db_mysql.close(result) db_mysql.close(result)
@ -292,6 +292,6 @@ proc open*(connection, user, password, database: string): DbConn {.
proc setEncoding*(connection: DbConn, encoding: string): bool {. proc setEncoding*(connection: DbConn, encoding: string): bool {.
tags: [FDb].} = tags: [FDb].} =
## sets the encoding of a database connection, returns true for ## sets the encoding of a database connection, returns true for
## success, false for failure. ## success, false for failure.
result = mysql.set_character_set(connection, encoding) == 0 result = mysql.set_character_set(connection, encoding) == 0