Commit 4bf9ddaf authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

sqlite: Statement: Pass a native sqlite handle to the constructor

parent eb61249b
......@@ -34,7 +34,7 @@ Settings::Settings()
bool Settings::load( DBConnection dbConn )
{
m_dbConn = dbConn;
sqlite::Statement s( m_dbConn, "SELECT * FROM Settings" );
sqlite::Statement s( m_dbConn->getConn(), "SELECT * FROM Settings" );
auto row = s.row();
// First launch: no settings
if ( row == nullptr )
......
......@@ -35,7 +35,7 @@ SqliteConnection::SqliteConnection( const std::string &dbPath )
throw std::runtime_error( "Failed to enable sqlite multithreaded mode" );
}
sqlite3 *SqliteConnection::getConn()
SqliteConnection::Handle SqliteConnection::getConn()
{
std::unique_lock<std::mutex> lock( m_connMutex );
sqlite3* dbConnection;
......
......@@ -42,11 +42,12 @@ class SqliteConnection
public:
using ReadContext = std::unique_lock<utils::ReadLocker>;
using WriteContext = std::unique_lock<utils::WriteLocker>;
using Handle = sqlite3*;
explicit SqliteConnection( const std::string& dbPath );
// Returns the current thread's connection
// This will initiate a connection if required
sqlite3* getConn();
Handle getConn();
// Release the current thread's connection
void release();
std::unique_ptr<sqlite::Transaction> newTransaction();
......
......@@ -101,7 +101,7 @@ private:
class Statement
{
public:
Statement( DBConnection dbConnection, const std::string& req )
Statement( SqliteConnection::Handle dbConnection, const std::string& req )
: m_stmt( nullptr, &sqlite3_reset )
, m_dbConn( dbConnection )
, m_req( req )
......@@ -111,11 +111,11 @@ public:
if ( it == end( StatementsCache ) )
{
sqlite3_stmt* stmt;
int res = sqlite3_prepare_v2( dbConnection->getConn(), req.c_str(), -1, &stmt, NULL );
int res = sqlite3_prepare_v2( dbConnection, req.c_str(), -1, &stmt, NULL );
if ( res != SQLITE_OK )
{
throw std::runtime_error( std::string( "Failed to execute request: " ) + req + " " +
sqlite3_errmsg( dbConnection->getConn() ) );
sqlite3_errmsg( dbConnection ) );
}
m_stmt.reset( stmt );
StatementsCache.emplace( req, CachedStmtPtr( stmt, &sqlite3_finalize ) );
......@@ -142,7 +142,7 @@ public:
return Row();
else
{
std::string errMsg = sqlite3_errmsg( m_dbConn->getConn() );
std::string errMsg = sqlite3_errmsg( m_dbConn );
switch ( res )
{
case SQLITE_CONSTRAINT:
......@@ -168,7 +168,7 @@ private:
using CachedStmtPtr = std::unique_ptr<sqlite3_stmt, int (*)(sqlite3_stmt*)>;
using StmtPtr = CachedStmtPtr;
StmtPtr m_stmt;
DBConnection m_dbConn;
SqliteConnection::Handle m_dbConn;
std::string m_req;
unsigned int m_bindIdx;
static thread_local std::unordered_map<std::string, CachedStmtPtr> StatementsCache;
......@@ -193,7 +193,7 @@ class Tools
auto chrono = std::chrono::steady_clock::now();
std::vector<std::shared_ptr<INTF>> results;
auto stmt = Statement( dbConnection, req );
auto stmt = Statement( dbConnection->getConn(), req );
stmt.execute( std::forward<Args>( args )... );
Row sqliteRow;
while ( ( sqliteRow = stmt.row() ) != nullptr )
......@@ -215,7 +215,7 @@ class Tools
ctx = dbConnection->acquireReadContext();
auto chrono = std::chrono::steady_clock::now();
auto stmt = Statement( dbConnection, req );
auto stmt = Statement( dbConnection->getConn(), req );
stmt.execute( std::forward<Args>( args )... );
auto row = stmt.row();
if ( row == nullptr )
......@@ -274,7 +274,7 @@ class Tools
static bool executeRequestLocked( DBConnection dbConnection, const std::string& req, Args&&... args )
{
auto chrono = std::chrono::steady_clock::now();
auto stmt = Statement( dbConnection, req );
auto stmt = Statement( dbConnection->getConn(), req );
stmt.execute( std::forward<Args>( args )... );
while ( stmt.row() != nullptr )
;
......
......@@ -35,7 +35,7 @@ Transaction::Transaction(DBConnection dbConn)
{
assert( CurrentTransaction == nullptr );
LOG_DEBUG( "Starting SQLite transaction" );
Statement s( dbConn, "BEGIN" );
Statement s( dbConn->getConn(), "BEGIN" );
s.execute();
while ( s.row() != nullptr )
;
......@@ -45,7 +45,7 @@ Transaction::Transaction(DBConnection dbConn)
void Transaction::commit()
{
auto chrono = std::chrono::steady_clock::now();
Statement s( m_dbConn, "COMMIT" );
Statement s( m_dbConn->getConn(), "COMMIT" );
s.execute();
while ( s.row() != nullptr )
;
......@@ -65,7 +65,7 @@ Transaction::~Transaction()
{
if ( CurrentTransaction != nullptr )
{
Statement s( m_dbConn, "ROLLBACK" );
Statement s( m_dbConn->getConn(), "ROLLBACK" );
s.execute();
while ( s.row() != nullptr )
;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment