Belle II Software development
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 254 of file sqlite.h.

255 {
256 detail::checkSQLiteError(sqlite3_prepare_v3(db, query.data(), query.size(), persistent ? SQLITE_PREPARE_PERSISTENT : 0,
257 &m_statement, nullptr), "Cannot prepare database statement: ");
258 if (auto cols = sqlite3_column_count(m_statement); cols != sizeof...(Columns)) {
259 throw std::runtime_error("Number of column (" + std::to_string(cols) + ") doesn't match number of template parameters (" +
260 std::to_string(sizeof...(Columns)) + ')');
261 }
262 }
sqlite3_stmt * m_statement
pointer to the statement object
Definition: sqlite.h:301

◆ ~ObjectStatement()

~ObjectStatement ( )
inline

Clean up the statement.

Definition at line 264 of file sqlite.h.

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

Member Function Documentation

◆ begin()

iterator begin ( )
inline

Iterator to the beginning.

Definition at line 249 of file sqlite.h.

249{ return iterator(this); }

◆ end()

iterator end ( ) const
inline

Iterator to the end.

Definition at line 251 of file sqlite.h.

251{ return iterator(); }

◆ execute()

ObjectStatement & execute ( Parameters...  parameters)
inline

Execute the statement, providing all necessary parameters.

Definition at line 268 of file sqlite.h.

269 {
270 if (auto params = sqlite3_bind_parameter_count(m_statement); params != sizeof...(Parameters)) {
271 throw std::runtime_error("Number of arguments (" + std::to_string(sizeof...(Parameters)) +
272 ") doesn't match number of statement parameters (" + std::to_string(params) + ")");
273 }
274 sqlite3_reset(m_statement);
275 auto parameter_tuple = std::make_tuple(parameters...);
276 detail::visitTupleWithIndex(parameter_tuple, detail::ParameterBinder{m_statement});
277 return *this;
278 }

◆ getRow()

value_type getRow ( ) const
inline

Return the current row.

Definition at line 293 of file sqlite.h.

294 {
295 std::tuple<Columns...> result;
296 detail::visitTupleWithIndex(result, detail::ColumnFiller{m_statement});
297 return std::make_from_tuple<value_type>(std::move(result));
298 }

◆ 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 283 of file sqlite.h.

284 {
285 if (auto ret = sqlite3_step(m_statement); ret != SQLITE_ROW) {
286 if (ret == SQLITE_DONE) return false;
287 detail::checkSQLiteError(ret);
288 }
289 return true;
290 }

Member Data Documentation

◆ m_statement

sqlite3_stmt* m_statement {nullptr}
private

pointer to the statement object

Definition at line 301 of file sqlite.h.


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