From af8aef8c77f369cdcf9c0161fa0265d7c30da622 Mon Sep 17 00:00:00 2001 From: Nicholas Fette Date: Sat, 8 Jun 2024 19:36:00 -0700 Subject: [PATCH 1/3] Encapsulate raw pointer sqlite3 m_connection --- src/EnergyPlus/SQLiteProcedures.cc | 7 ++++--- src/EnergyPlus/SQLiteProcedures.hh | 1 - 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/EnergyPlus/SQLiteProcedures.cc b/src/EnergyPlus/SQLiteProcedures.cc index d78be82c969..5b566dbfb7c 100644 --- a/src/EnergyPlus/SQLiteProcedures.cc +++ b/src/EnergyPlus/SQLiteProcedures.cc @@ -2597,7 +2597,7 @@ SQLite::SQLiteData::SQLiteData(std::shared_ptr const &errorStream, } SQLiteProcedures::SQLiteProcedures(std::shared_ptr const &errorStream, std::shared_ptr const &db) - : m_writeOutputToSQLite(true), m_errorStream(errorStream), m_connection(nullptr), m_db(db) + : m_writeOutputToSQLite(true), m_errorStream(errorStream), m_db(db) { } @@ -2605,8 +2605,9 @@ SQLiteProcedures::SQLiteProcedures(std::shared_ptr const &errorStr bool writeOutputToSQLite, fs::path const &dbName, fs::path const &errorFilePath) - : m_writeOutputToSQLite(writeOutputToSQLite), m_errorStream(errorStream), m_connection(nullptr) + : m_writeOutputToSQLite(writeOutputToSQLite), m_errorStream(errorStream) { + sqlite3 *m_connection = nullptr; if (m_writeOutputToSQLite) { int rc = -1; bool ok = true; @@ -2803,7 +2804,7 @@ int SQLiteProcedures::sqliteResetCommand(sqlite3_stmt *stmt) bool SQLiteProcedures::sqliteWithinTransaction() { - return (sqlite3_get_autocommit(m_connection) == 0); + return (sqlite3_get_autocommit(m_db.get()) == 0); } // int SQLiteProcedures::sqliteClearBindings(sqlite3_stmt * stmt) diff --git a/src/EnergyPlus/SQLiteProcedures.hh b/src/EnergyPlus/SQLiteProcedures.hh index eb6e677b808..a641e830fe8 100644 --- a/src/EnergyPlus/SQLiteProcedures.hh +++ b/src/EnergyPlus/SQLiteProcedures.hh @@ -104,7 +104,6 @@ protected: bool m_writeOutputToSQLite; std::shared_ptr m_errorStream; - sqlite3 *m_connection; std::shared_ptr m_db; }; From bedc5154b529f43a415e766fa88276c4bd6fb6eb Mon Sep 17 00:00:00 2001 From: Nicholas Fette Date: Sat, 8 Jun 2024 19:37:55 -0700 Subject: [PATCH 2/3] Avoid accessing sqlite3 connection after sqlite3_close() After calling sqlite3_close(), the call to sqlite3_errmsg() is guaranteed to yield useless message "bad parameter or other API misuse" --- src/EnergyPlus/SQLiteProcedures.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/EnergyPlus/SQLiteProcedures.cc b/src/EnergyPlus/SQLiteProcedures.cc index 5b566dbfb7c..28570d56198 100644 --- a/src/EnergyPlus/SQLiteProcedures.cc +++ b/src/EnergyPlus/SQLiteProcedures.cc @@ -2645,7 +2645,7 @@ SQLiteProcedures::SQLiteProcedures(std::shared_ptr const &errorStr rc = sqlite3_exec(m_connection, "CREATE TABLE Test(x INTEGER PRIMARY KEY)", nullptr, 0, &zErrMsg); sqlite3_close(m_connection); if (rc) { - *m_errorStream << "SQLite3 message, can't get exclusive lock to edit database: " << sqlite3_errmsg(m_connection) << std::endl; + *m_errorStream << "SQLite3 message, can't get exclusive lock to edit database: " << zErrMsg << std::endl; ok = false; } else { if (dbName != ":memory:") { From 8fae2defe85e0ceab15ba934965c323ea19ae5ee Mon Sep 17 00:00:00 2001 From: Nicholas Fette Date: Sat, 8 Jun 2024 19:48:47 -0700 Subject: [PATCH 3/3] Set journal_mode OFF to avoid creating sqlite3 journal file Creating a journal file could decrease the character limit for folder paths on Windows. Currently, the only inheriting class uses journal_mode = OFF, so it is not necessary to test for access to create and lock a journal file. Confer SQLite::SQLite(). --- src/EnergyPlus/SQLiteProcedures.cc | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/EnergyPlus/SQLiteProcedures.cc b/src/EnergyPlus/SQLiteProcedures.cc index 28570d56198..523f8bb3093 100644 --- a/src/EnergyPlus/SQLiteProcedures.cc +++ b/src/EnergyPlus/SQLiteProcedures.cc @@ -2642,7 +2642,11 @@ SQLiteProcedures::SQLiteProcedures(std::shared_ptr const &errorStr } if (ok) { char *zErrMsg = nullptr; - rc = sqlite3_exec(m_connection, "CREATE TABLE Test(x INTEGER PRIMARY KEY)", nullptr, 0, &zErrMsg); + // Set journal_mode OFF to avoid creating the file dbName + "-journal" (when dbName is a regular file) + rc = sqlite3_exec(m_connection, "PRAGMA journal_mode = OFF;", nullptr, 0, &zErrMsg); + if (!rc) { + rc = sqlite3_exec(m_connection, "CREATE TABLE Test(x INTEGER PRIMARY KEY)", nullptr, 0, &zErrMsg); + } sqlite3_close(m_connection); if (rc) { *m_errorStream << "SQLite3 message, can't get exclusive lock to edit database: " << zErrMsg << std::endl;