Commit d3e0d257 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen
Browse files

Sqlite: Remove variadic template recursion in favor of parameter pack expension

parent 35d40d9c
...@@ -216,6 +216,7 @@ public: ...@@ -216,6 +216,7 @@ public:
Statement( DBConnection dbConnection, const std::string& req ) Statement( DBConnection dbConnection, const std::string& req )
: m_stmt( nullptr, &sqlite3_finalize ) : m_stmt( nullptr, &sqlite3_finalize )
, m_req( req ) , m_req( req )
, m_bindIdx( 0 )
{ {
sqlite3_stmt* stmt; sqlite3_stmt* stmt;
int res = sqlite3_prepare_v2( dbConnection->getConn(), req.c_str(), -1, &stmt, NULL ); int res = sqlite3_prepare_v2( dbConnection->getConn(), req.c_str(), -1, &stmt, NULL );
...@@ -230,7 +231,8 @@ public: ...@@ -230,7 +231,8 @@ public:
template <typename... Args> template <typename... Args>
void execute(Args&&... args) void execute(Args&&... args)
{ {
_bind<1>( std::forward<Args>( args )... ); m_bindIdx = 1;
(void)std::initializer_list<bool>{ _bind( std::forward<Args>( args ) )... };
} }
Row row() Row row()
...@@ -257,22 +259,20 @@ public: ...@@ -257,22 +259,20 @@ public:
} }
private: private:
// variadic recursion termination template <typename T>
template <int> bool _bind( T&& value )
void _bind() {}
template <int Idx, typename T, typename... Args>
void _bind( T&& value, Args&&... args )
{ {
auto res = Traits<T>::Bind( m_stmt.get(), Idx, std::forward<T>( value ) ); auto res = Traits<T>::Bind( m_stmt.get(), m_bindIdx, std::forward<T>( value ) );
if ( res != SQLITE_OK ) if ( res != SQLITE_OK )
throw std::runtime_error( "Failed to bind parameter" ); throw std::runtime_error( "Failed to bind parameter" );
_bind<Idx + 1, Args...>( std::forward<Args>( args )... ); m_bindIdx++;
return true;
} }
private: private:
std::unique_ptr<sqlite3_stmt, int (*)(sqlite3_stmt*)> m_stmt; std::unique_ptr<sqlite3_stmt, int (*)(sqlite3_stmt*)> m_stmt;
std::string m_req; std::string m_req;
unsigned int m_bindIdx;
}; };
class Tools 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