Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convenience procs for db_mysql [#3217] #3318

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions lib/impure/db_mysql.nim
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ proc tryExec*(db: DbConn, query: SqlQuery, args: varargs[string, `$`]): bool {.
var q = dbFormat(query, args)
return mysql.realQuery(db, q, q.len) == 0'i32

proc rawExec(db: DbConn, query: SqlQuery, args: varargs[string, `$`]) =
proc rawExec*(db: DbConn, query: SqlQuery, args: varargs[string, `$`]) =
var q = dbFormat(query, args)
if mysql.realQuery(db, q, q.len) != 0'i32: dbError(db)

Expand All @@ -133,7 +133,7 @@ proc newRow(L: int): Row =
newSeq(result, L)
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:
while mysql.fetchRow(sqlres) != nil: discard
mysql.freeResult(sqlres)
Expand Down Expand Up @@ -189,6 +189,26 @@ proc len*(row: InstantRow): int {.inline.} =
## returns number of columns in the row
row.len

proc hasData*(rows: seq[Row]): bool =
result = false
for row in rows:
for item in row:
if item != "" and item != nil:
result = true
break

proc hasData*(row: Row): bool =
result = false
for item in row:
if item != "" and item != nil:
result = true
break

proc hasData*(value: string): bool =
result = false
if value != "" and value != nil:
result = true

proc getRow*(db: DbConn, query: SqlQuery,
args: varargs[string, `$`]): Row {.tags: [FReadDB].} =
## retrieves a single row. If the query doesn't return any rows, this proc
Expand Down
24 changes: 22 additions & 2 deletions lib/impure/db_postgres.nim
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,15 @@ proc newRow(L: int): Row =
newSeq(result, L)
for i in 0..L-1: result[i] = ""

proc setupQuery(db: DbConn, query: SqlQuery,
proc setupQuery*(db: DbConn, query: SqlQuery,
args: varargs[string]): PPGresult =
var arr = allocCStringArray(args)
result = pqexecParams(db, query.string, int32(args.len), nil, arr,
nil, nil, 0)
deallocCStringArray(arr)
if pqResultStatus(result) != PGRES_TUPLES_OK: dbError(db)

proc setupQuery(db: DbConn, stmtName: SqlPrepared,
proc setupQuery*(db: DbConn, stmtName: SqlPrepared,
args: varargs[string]): PPGresult =
var arr = allocCStringArray(args)
result = pqexecPrepared(db, stmtName.string, int32(args.len), arr,
Expand Down Expand Up @@ -180,6 +180,26 @@ proc len*(row: InstantRow): int32 {.inline.} =
## returns number of columns in the row
pqNfields(row.res)

proc hasData*(rows: seq[Row]): bool =
result = false
for row in rows:
for item in row:
if item != "" and item != nil:
result = true
break

proc hasData*(row: Row): bool =
result = false
for item in row:
if item != "" and item != nil:
result = true
break

proc hasData*(value: string): bool =
result = false
if value != "" and value != nil:
result = true

proc getRow*(db: DbConn, query: SqlQuery,
args: varargs[string, `$`]): Row {.tags: [FReadDB].} =
## retrieves a single row. If the query doesn't return any rows, this proc
Expand Down
24 changes: 22 additions & 2 deletions lib/impure/db_sqlite.nim
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ proc sql*(query: string): SqlQuery {.noSideEffect, inline.} =
## on, later versions will check the string for valid syntax.
result = SqlQuery(query)

proc dbError(db: DbConn) {.noreturn.} =
proc dbError*(db: DbConn) {.noreturn.} =
## raises an EDb exception.
var e: ref EDb
new(e)
Expand Down Expand Up @@ -116,7 +116,7 @@ proc newRow(L: int): Row =
newSeq(result, L)
for i in 0..L-1: result[i] = ""

proc setupQuery(db: DbConn, query: SqlQuery,
proc setupQuery*(db: DbConn, query: SqlQuery,
args: varargs[string]): Pstmt =
var q = dbFormat(query, args)
if prepare_v2(db, q, q.len.cint, result, nil) != SQLITE_OK: dbError(db)
Expand Down Expand Up @@ -163,6 +163,26 @@ proc len*(row: InstantRow): int32 {.inline.} =
## returns number of columns in the row
column_count(row)

proc hasData*(rows: seq[Row]): bool =
result = false
for row in rows:
for item in row:
if item != "" and item != nil:
result = true
break

proc hasData*(row: Row): bool =
result = false
for item in row:
if item != "" and item != nil:
result = true
break

proc hasData*(value: string): bool =
result = false
if value != "" and value != nil:
result = true

proc getRow*(db: DbConn, query: SqlQuery,
args: varargs[string, `$`]): Row {.tags: [FReadDb].} =
## retrieves a single row. If the query doesn't return any rows, this proc
Expand Down