Belle II Software light-2406-ragdoll
ObjectStatement< ObjectType, Columns > Class Template Reference

SQLite prepared statement wrapper. More...

#include <sqlite.h>

Classes

class  iterator
 Iterator class to allow iterating over the rows. More...
 

Public Types

using value_type = ObjectType
 each row gets converted to a instance of this type
 

Public Member Functions

iterator begin ()
 Iterator to the beginning.
 
iterator end () const
 Iterator to the end.
 
 ObjectStatement (sqlite3 *db, const std::string &query, bool persistent)
 Create a statement for an existing database object.
 
 ~ObjectStatement ()
 Clean up the statement.
 
template<class ... Parameters>
ObjectStatementexecute (Parameters... parameters)
 Execute the statement, providing all necessary parameters.
 
bool step ()
 Step to the next row in the result.
 
value_type getRow () const
 Return the current row.
 

Private Attributes

sqlite3_stmt * m_statement {nullptr}
 pointer to the statement object
 

Detailed Description

template<class ObjectType, class ... Columns>
class sqlite::ObjectStatement< ObjectType, Columns >

SQLite prepared statement wrapper.

This class is meant to prepare a SQLite statement where each row of the result is supposed to fill one object of type ObjectType.

Columns is the type of each of the columns in the returned data. When calling getRow() the selected columns from the current result row will be converted to the typesspecified by Columns and passed to the constructor of ObjectType and the resulting object is returned.

After calling execute one can iterate over the statement to get all rows.

ObjectStatement<TVector3, double, double, double> vectors(connection, "SELECT x,y,z from vectors");
for(auto&& tvec3: vectors.execute()) {
tvec3.Print();
}
SQLite prepared statement wrapper.
Definition: sqlite.h:194
Warning
Be aware that you cannot iterate over this statement multiple times and should not have multiple independent iterators as the underlying sqlite statement object will change its state. One iteration over the rows per execute() call.

Definition at line 194 of file sqlite.h.

Member Typedef Documentation

◆ value_type

using value_type = ObjectType

each row gets converted to a instance of this type

Definition at line 197 of file sqlite.h.

Constructor & Destructor Documentation

◆ ObjectStatement()

ObjectStatement ( sqlite3 *  db,
const std::string &  query,
bool  persistent 
)
inline

Create a statement for an existing database object.

Definition at line 238 of file sqlite.h.

239 {
240 detail::checkSQLiteError(sqlite3_prepare_v3(db, query.data(), query.size(), persistent ? SQLITE_PREPARE_PERSISTENT : 0,
241 &m_statement, nullptr), "Cannot prepare database statement: ");
242 if (auto cols = sqlite3_column_count(m_statement); cols != sizeof...(Columns)) {
243 throw std::runtime_error("Number of column (" + std::to_string(cols) + ") doesn't match number of template parameters (" +
244 std::to_string(sizeof...(Columns)) + ')');
245 }
246 }
sqlite3_stmt * m_statement
pointer to the statement object
Definition: sqlite.h:285

◆ ~ObjectStatement()

~ObjectStatement ( )
inline

Clean up the statement.

Definition at line 248 of file sqlite.h.

248{ if (m_statement) sqlite3_finalize(m_statement); }

Member Function Documentation

◆ begin()

iterator begin ( )
inline

Iterator to the beginning.

Definition at line 233 of file sqlite.h.

233{ return iterator(this); }

◆ end()

iterator end ( ) const
inline

Iterator to the end.

Definition at line 235 of file sqlite.h.

235{ return iterator(); }

◆ execute()

ObjectStatement & execute ( Parameters...  parameters)
inline

Execute the statement, providing all necessary parameters.

Definition at line 252 of file sqlite.h.

253 {
254 if (auto params = sqlite3_bind_parameter_count(m_statement); params != sizeof...(Parameters)) {
255 throw std::runtime_error("Number of arguments (" + std::to_string(sizeof...(Parameters)) +
256 ") doesn't match number of statement parameters (" + std::to_string(params) + ")");
257 }
258 sqlite3_reset(m_statement);
259 auto parameter_tuple = std::make_tuple(parameters...);
260 detail::visitTupleWithIndex(parameter_tuple, detail::ParameterBinder{m_statement});
261 return *this;
262 }

◆ getRow()

value_type getRow ( ) const
inline

Return the current row.

Definition at line 277 of file sqlite.h.

278 {
279 std::tuple<Columns...> result;
280 detail::visitTupleWithIndex(result, detail::ColumnFiller{m_statement});
281 return std::make_from_tuple<value_type>(std::move(result));
282 }

◆ step()

bool step ( )
inline

Step to the next row in the result.

Returns
true if a new row is loaded, false if there a no more rows.

Definition at line 267 of file sqlite.h.

268 {
269 if (auto ret = sqlite3_step(m_statement); ret != SQLITE_ROW) {
270 if (ret == SQLITE_DONE) return false;
271 detail::checkSQLiteError(ret);
272 }
273 return true;
274 }

Member Data Documentation

◆ m_statement

sqlite3_stmt* m_statement {nullptr}
private

pointer to the statement object

Definition at line 285 of file sqlite.h.


The documentation for this class was generated from the following file: