From 0e15d016b19357f5521ae17d71db8c74f07476cf Mon Sep 17 00:00:00 2001 From: Alex MacCaw Date: Thu, 6 Feb 2025 08:56:54 -0600 Subject: [PATCH] Call sqlLite_finalize to free up memory. --- ios/Plugin/Utils/UtilsSQLCipher.swift | 98 +++++++++++++++------------ 1 file changed, 56 insertions(+), 42 deletions(-) diff --git a/ios/Plugin/Utils/UtilsSQLCipher.swift b/ios/Plugin/Utils/UtilsSQLCipher.swift index 772bf438..bb2a006f 100644 --- a/ios/Plugin/Utils/UtilsSQLCipher.swift +++ b/ios/Plugin/Utils/UtilsSQLCipher.swift @@ -709,53 +709,67 @@ class UtilsSQLCipher { // MARK: - querySQL - class func querySQL(mDB: Database, sql: String, - values: [Any]) throws -> [[String: Any]] { - var msg: String = "Error querySQL: " - if !mDB.isDBOpen() { - msg.append("Database not opened") - throw UtilsSQLCipherError.querySQL(message: msg) + class func querySQL(mDB: Database, sql: String, values: [Any]) throws -> [[String: Any]] { + var results: [[String: Any]] = [] + var stmt: OpaquePointer? = nil + + // Prepare the SQL statement. + if sqlite3_prepare_v2(mDB.mDb, sql, -1, &stmt, nil) != SQLITE_OK { + let errorMessage = String(cString: sqlite3_errmsg(mDB.mDb)) + throw UtilsSQLCipherError.querySQL(message: "Error preparing query: \(errorMessage)") } - var selectSQLStatement: OpaquePointer? - var result: [[String: Any]] = [] - var message: String = "" - var returnCode: Int32 = - sqlite3_prepare_v2(mDB.mDb, sql, -1, &selectSQLStatement, - nil) - if returnCode == SQLITE_OK { - if !values.isEmpty { - // do the binding of values - message = UtilsBinding.bindValues( - handle: selectSQLStatement, values: values) + + // Bind provided values to the statement. + if values.count > 0 { + let bindError = UtilsBinding.bindValues(handle: stmt, values: values) + if bindError.count > 0 { + sqlite3_finalize(stmt) + throw UtilsSQLCipherError.querySQL(message: "Error binding values: \(bindError)") } - if message.count == 0 { - do { - result = try UtilsSQLCipher.fetchColumnInfo( - handle: selectSQLStatement, returnMode: "all") - } catch UtilsSQLCipherError - .fetchColumnInfo(let message) { - throw UtilsSQLCipherError - .querySQL(message: message) + } + + // Execute the statement and build the results array. + while sqlite3_step(stmt) == SQLITE_ROW { + var row: [String: Any] = [:] + let columnCount = sqlite3_column_count(stmt) + for i in 0.. 0 { - throw UtilsSQLCipherError.querySQL(message: message) - } else { - return result + results.append(row) } + + // Finalize the statement to free its memory. + sqlite3_finalize(stmt) + return results } // MARK: - FetchColumnInfo