Skip to content

Commit

Permalink
chore: sqlite make sure code is always run (#2891)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivansete-status authored Jul 10, 2024
1 parent e1518cf commit 4ac4ab2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
12 changes: 7 additions & 5 deletions waku/common/databases/db_sqlite.nim
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,13 @@ proc query*(
return err($sqlite3_errstr(v))
return ok gotResults
except Exception, CatchableError:
# release implicit transaction
discard sqlite3_reset(s) # same return information as step
discard sqlite3_clear_bindings(s) # no errors possible
discard sqlite3_finalize(s)
# NB: dispose of the prepared query statement and free associated memory
error "exception in query", query = query, error = getCurrentExceptionMsg()

# release implicit transaction
discard sqlite3_reset(s) # same return information as step
discard sqlite3_clear_bindings(s) # no errors possible
discard sqlite3_finalize(s)
# NB: dispose of the prepared query statement and free associated memory

proc prepareStmt*(
db: SqliteDatabase, stmt: string, Params: type, Res: type
Expand Down
27 changes: 18 additions & 9 deletions waku/waku_archive/driver/sqlite_driver/queries.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{.push raises: [].}

import std/[options, sequtils], stew/byteutils, sqlite3_abi, results
import chronicles
import
../../../common/databases/db_sqlite,
../../../common/databases/common,
Expand Down Expand Up @@ -427,9 +428,12 @@ proc execSelectMessagesV2WithLimitStmt(
else:
return err($sqlite3_errstr(v))
except Exception, CatchableError:
# release implicit transaction
discard sqlite3_reset(s) # same return information as step
discard sqlite3_clear_bindings(s) # no errors possible
error "exception in execSelectMessagesV2WithLimitStmt",
error = getCurrentExceptionMsg()

# release implicit transaction
discard sqlite3_reset(s) # same return information as step
discard sqlite3_clear_bindings(s) # no errors possible

proc selectMessagesByHistoryQueryWithLimit*(
db: SqliteDatabase,
Expand Down Expand Up @@ -498,9 +502,11 @@ proc execSelectMessageByHash(
else:
return err($sqlite3_errstr(v))
except Exception, CatchableError:
# release implicit transaction
discard sqlite3_reset(s) # same return information as step
discard sqlite3_clear_bindings(s) # no errors possible
error "exception in execSelectMessageByHash", error = getCurrentExceptionMsg()

# release implicit transaction
discard sqlite3_reset(s) # same return information as step
discard sqlite3_clear_bindings(s) # no errors possible

proc selectMessageByHashQuery(): SqlQueryStr =
var query: string
Expand Down Expand Up @@ -629,9 +635,12 @@ proc execSelectMessagesWithLimitStmt(
else:
return err($sqlite3_errstr(v))
except Exception, CatchableError:
# release implicit transaction
discard sqlite3_reset(s) # same return information as step
discard sqlite3_clear_bindings(s) # no errors possible
error "exception in execSelectMessagesWithLimitStmt",
error = getCurrentExceptionMsg()

# release implicit transaction
discard sqlite3_reset(s) # same return information as step
discard sqlite3_clear_bindings(s) # no errors possible

proc selectMessagesWithLimitQuery(
table: string, where: Option[string], limit: uint, ascending = true, v3 = false
Expand Down

0 comments on commit 4ac4ab2

Please sign in to comment.