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

SqliteTools: Cache all compiled sqlite statements

parent 56d6b513
......@@ -85,6 +85,7 @@ list(APPEND SRC_LIST ${HEADERS_LIST}
database/SqliteConnection.cpp
database/SqliteTransaction.cpp
database/SqliteTools.cpp
logging/IostreamLogger.cpp
logging/Logger.cpp
......
/*****************************************************************************
* Media Library
*****************************************************************************
* Copyright (C) 2015 Hugo Beauzée-Luyssen, Videolabs
*
* Authors: Hugo Beauzée-Luyssen<hugo@beauzee.fr>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#include "SqliteTools.h"
namespace sqlite
{
thread_local std::unordered_map<std::string, Statement::CachedStmtPtr> Statement::StatementsCache;
}
......@@ -29,6 +29,7 @@
#include <memory>
#include <sqlite3.h>
#include <string>
#include <unordered_map>
#include <vector>
#include "Types.h"
......@@ -101,19 +102,28 @@ class Statement
{
public:
Statement( DBConnection dbConnection, const std::string& req )
: m_stmt( nullptr, &sqlite3_finalize )
: m_stmt( nullptr, &sqlite3_reset )
, m_dbConn( dbConnection )
, m_req( req )
, m_bindIdx( 0 )
{
sqlite3_stmt* stmt;
int res = sqlite3_prepare_v2( dbConnection->getConn(), req.c_str(), -1, &stmt, NULL );
if ( res != SQLITE_OK )
auto it = StatementsCache.find( req );
if ( it == end( StatementsCache ) )
{
sqlite3_stmt* stmt;
int res = sqlite3_prepare_v2( dbConnection->getConn(), 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() ) );
}
m_stmt.reset( stmt );
StatementsCache.emplace( req, CachedStmtPtr( stmt, &sqlite3_finalize ) );
}
else
{
throw std::runtime_error( std::string( "Failed to execute request: " ) + req + " " +
sqlite3_errmsg( dbConnection->getConn() ) );
m_stmt.reset( it->second.get() );
}
m_stmt.reset( stmt );
}
template <typename... Args>
......@@ -155,10 +165,13 @@ private:
}
private:
std::unique_ptr<sqlite3_stmt, int (*)(sqlite3_stmt*)> m_stmt;
using CachedStmtPtr = std::unique_ptr<sqlite3_stmt, int (*)(sqlite3_stmt*)>;
using StmtPtr = CachedStmtPtr;
StmtPtr m_stmt;
DBConnection m_dbConn;
std::string m_req;
unsigned int m_bindIdx;
static thread_local std::unordered_map<std::string, CachedStmtPtr> StatementsCache;
};
class Tools
......
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