Skip to content

Commit

Permalink
fix db_sqlite.tryInsertID does raise exceptions in 1.6.0 #19743
Browse files Browse the repository at this point in the history
  • Loading branch information
ringabout committed Apr 24, 2022
1 parent 02e8aa9 commit b29e54a
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions lib/impure/db_sqlite.nim
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ proc getValue*(db: DbConn, stmtName: SqlPrepared): string

proc tryInsertID*(db: DbConn, query: SqlQuery,
args: varargs[string, `$`]): int64
{.tags: [WriteDbEffect], raises: [DbError].} =
{.tags: [WriteDbEffect], raises: [].} =
## Executes the query (typically "INSERT") and returns the
## generated ID for the row or -1 in case of an error.
##
Expand All @@ -650,16 +650,19 @@ proc tryInsertID*(db: DbConn, query: SqlQuery,
## 1, "item#1") == -1
## db.close()
assert(not db.isNil, "Database not connected.")
var q = dbFormat(query, args)
var stmt: sqlite3.PStmt
result = -1
if prepare_v2(db, q, q.len.cint, stmt, nil) == SQLITE_OK:
if step(stmt) == SQLITE_DONE:
result = last_insert_rowid(db)
if finalize(stmt) != SQLITE_OK:
result = -1
else:
discard finalize(stmt)
try:
var q = dbFormat(query, args)
var stmt: sqlite3.PStmt
if prepare_v2(db, q, q.len.cint, stmt, nil) == SQLITE_OK:
if step(stmt) == SQLITE_DONE:
result = last_insert_rowid(db)
if finalize(stmt) != SQLITE_OK:
result = -1
else:
discard finalize(stmt)
except DbError:
discard

proc insertID*(db: DbConn, query: SqlQuery,
args: varargs[string, `$`]): int64 {.tags: [WriteDbEffect].} =
Expand Down Expand Up @@ -692,7 +695,7 @@ proc insertID*(db: DbConn, query: SqlQuery,

proc tryInsert*(db: DbConn, query: SqlQuery, pkName: string,
args: varargs[string, `$`]): int64
{.tags: [WriteDbEffect], raises: [DbError], since: (1, 3).} =
{.tags: [WriteDbEffect], raises: [], since: (1, 3).} =
## same as tryInsertID
tryInsertID(db, query, args)

Expand Down

0 comments on commit b29e54a

Please sign in to comment.