From 3b90dfdcd1c589917346a6d81f7acafbc6deaedb Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Thu, 18 Apr 2013 19:56:28 -0700 Subject: [PATCH] Don't hold sqlite locks across invocations. --- src/cpp/database/SqliteDatabase.cpp | 31 +++++++++++++++-------------- src/cpp/database/SqliteDatabase.h | 4 ++-- src/cpp/database/database.h | 4 ++-- 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/cpp/database/SqliteDatabase.cpp b/src/cpp/database/SqliteDatabase.cpp index 6e78dfb1e5..7ea6ff4f71 100644 --- a/src/cpp/database/SqliteDatabase.cpp +++ b/src/cpp/database/SqliteDatabase.cpp @@ -80,6 +80,7 @@ bool SqliteDatabase::executeSQL(const char* sql, bool fail_ok) #endif } return false; + endIterRows(); } rc = sqlite3_step(mCurrentStmt); if (rc == SQLITE_ROW) @@ -88,6 +89,7 @@ bool SqliteDatabase::executeSQL(const char* sql, bool fail_ok) } else if (rc == SQLITE_DONE) { + endIterRows(); mMoreRows = false; } else @@ -107,6 +109,7 @@ bool SqliteDatabase::executeSQL(const char* sql, bool fail_ok) cLog(lsWARNING) << "Error: " << sqlite3_errmsg(mConnection); #endif } + endIterRows(); return false; } @@ -126,7 +129,7 @@ int SqliteDatabase::getLastInsertID() } // returns false if there are no results -bool SqliteDatabase::startIterRows() +bool SqliteDatabase::startIterRows(bool finalize) { mColNameTable.clear(); mColNameTable.resize(sqlite3_column_count(mCurrentStmt)); @@ -135,6 +138,9 @@ bool SqliteDatabase::startIterRows() mColNameTable[n]=sqlite3_column_name(mCurrentStmt,n); } + if (!mMoreRows && finalize) + endIterRows(); + return(mMoreRows); } @@ -146,25 +152,20 @@ void SqliteDatabase::endIterRows() // call this after you executeSQL // will return false if there are no more rows -bool SqliteDatabase::getNextRow() +bool SqliteDatabase::getNextRow(bool finalize) { - if (!mMoreRows) return(false); - - int rc=sqlite3_step(mCurrentStmt); - if (rc==SQLITE_ROW) - { - return(true); - } - else if (rc==SQLITE_DONE) - { - return(false); - } - else + if (mMoreRows) { + int rc=sqlite3_step(mCurrentStmt); + if (rc==SQLITE_ROW) + return(true); assert((rc != SQLITE_BUSY) && (rc != SQLITE_LOCKED)); cLog(lsWARNING) << "Rerror: " << mHost << ": " << rc; - return(false); } + + if (finalize) + endIterRows(); + return false; } bool SqliteDatabase::getNull(int colIndex) diff --git a/src/cpp/database/SqliteDatabase.h b/src/cpp/database/SqliteDatabase.h index 1e675a0587..0d9f248e4d 100644 --- a/src/cpp/database/SqliteDatabase.h +++ b/src/cpp/database/SqliteDatabase.h @@ -34,12 +34,12 @@ public: int getLastInsertID(); // returns false if there are no results - bool startIterRows(); + bool startIterRows(bool finalize); void endIterRows(); // call this after you executeSQL // will return false if there are no more rows - bool getNextRow(); + bool getNextRow(bool finalize); bool getNull(int colIndex); char* getStr(int colIndex,std::string& retStr); diff --git a/src/cpp/database/database.h b/src/cpp/database/database.h index dca77106e5..5b69056fca 100644 --- a/src/cpp/database/database.h +++ b/src/cpp/database/database.h @@ -53,12 +53,12 @@ public: virtual int getLastInsertID()=0; // returns false if there are no results - virtual bool startIterRows()=0; + virtual bool startIterRows(bool finalize = true)=0; virtual void endIterRows()=0; // call this after you executeSQL // will return false if there are no more rows - virtual bool getNextRow()=0; + virtual bool getNextRow(bool finalize = true)=0; // get Data from the current row bool getNull(const char* colName);