ATLAS Offline Software
Loading...
Searching...
No Matches
SQLite::Statement Class Reference

SQLite prepared statement. More...

#include <Statement.h>

Collaboration diagram for SQLite::Statement:

Public Member Functions

 Statement (sqlite3 *db, std::string_view sql, std::source_location call)
 Create a prepared statement attached to an SQLiteDBSvc This class should be constructed using the createStatement member function of SQLiteDBSvc.
 ~Statement ()
 Statement ()=default
 Statement (const Statement &)=delete
Statementoperator= (Statement &&rhs) noexcept
 Move assignment operator allows Statement member variables to be filled from createStatement.
template<ValidColumnType... ReturnArgs, ValidParamType... ParamArgs>
ResultTypeWrapper< ReturnArgs... >::type run (ParamArgs... params)
 Run the statement.

Private Member Functions

void reset ()
 Reset prepared statement for re-execution and clear bindings.
bool step ()
 Step through the prepared statement.
void bind (int index, std::int64_t value)
 Bind an integer to a parameter.
template<typename I>
requires std::integral<I>
void bind (int index, I value)
void bind (int index, double value)
 Bind a double to a parameter.
void bind (int index, std::string_view value)
 Bind a string to a parameter.
template<ValidColumnType T, std::size_t I>
column ()
 Retrieve a column.
template<ValidColumnType... Ts, std::size_t... Is>
std::tuple< Ts... > columns (std::index_sequence< Is... >)
 Retrieve columns.

Private Attributes

std::recursive_mutex m_stmtMutex
sqlite3_stmt * m_stmt = nullptr
sqlite3 * m_db = nullptr
std::source_location m_creationPoint

Detailed Description

SQLite prepared statement.

Definition at line 50 of file Statement.h.

Constructor & Destructor Documentation

◆ Statement() [1/3]

SQLite::Statement::Statement ( sqlite3 * db,
std::string_view sql,
std::source_location call )

Create a prepared statement attached to an SQLiteDBSvc This class should be constructed using the createStatement member function of SQLiteDBSvc.

Definition at line 12 of file Statement.cxx.

14 : m_db(db), m_creationPoint(call) {
15 const int err =
16 sqlite3_prepare_v2(db, sql.data(), int(sql.size()), &m_stmt, nullptr);
17 if (err != 0) {
18 throw std::logic_error(
19 std::format("ERROR preparing SQLite statement: {} ({}) at {} [{}:{}]",
20 sqlite3_errstr(err), sqlite3_errmsg(m_db),
21 m_creationPoint.function_name(),
22 m_creationPoint.file_name(), m_creationPoint.line()));
23 }
24}
sqlite3 * m_db
Definition Statement.h:94
sqlite3_stmt * m_stmt
Definition Statement.h:93
std::source_location m_creationPoint
Definition Statement.h:95

◆ ~Statement()

SQLite::Statement::~Statement ( )

Definition at line 36 of file Statement.cxx.

36 {
37 if (m_stmt == nullptr) {
38 return; // Empty statement (unfilled or moved-from)
39 }
40 sqlite3_finalize(m_stmt);
41 m_stmt = nullptr;
42}

◆ Statement() [2/3]

SQLite::Statement::Statement ( )
default

◆ Statement() [3/3]

SQLite::Statement::Statement ( const Statement & )
delete

Member Function Documentation

◆ bind() [1/4]

void SQLite::Statement::bind ( int index,
double value )
private

Bind a double to a parameter.

Definition at line 80 of file Statement.cxx.

80 {
81 const int err = sqlite3_bind_double(m_stmt, index, value);
82 if (err != 0) {
83 throw std::tuple<int, int>{err, index};
84 }
85}
str index
Definition DeMoScan.py:362

◆ bind() [2/4]

template<typename I>
requires std::integral<I>
void SQLite::Statement::bind ( int index,
I value )
private

◆ bind() [3/4]

void SQLite::Statement::bind ( int index,
std::int64_t value )
private

Bind an integer to a parameter.

Definition at line 73 of file Statement.cxx.

73 {
74 const int err = sqlite3_bind_int64(m_stmt, index, value);
75 if (err != 0) {
76 throw std::tuple<int, int>{err, index};
77 }
78}

◆ bind() [4/4]

void SQLite::Statement::bind ( int index,
std::string_view value )
private

Bind a string to a parameter.

Definition at line 87 of file Statement.cxx.

87 {
88 const int err = sqlite3_bind_text(m_stmt, index, value.data(),
89 int(value.size()), SQLITE_TRANSIENT);
90 if (err != 0) {
91 throw std::tuple<int, int>{err, index};
92 }
93}

◆ column()

template<ValidColumnType T, std::size_t I>
T SQLite::Statement::column ( )
private

Retrieve a column.

◆ columns()

template<ValidColumnType... Ts, std::size_t... Is>
std::tuple< Ts... > SQLite::Statement::columns ( std::index_sequence< Is... > )
private

Retrieve columns.

◆ operator=()

Statement & SQLite::Statement::operator= ( Statement && rhs)
noexcept

Move assignment operator allows Statement member variables to be filled from createStatement.

Definition at line 26 of file Statement.cxx.

26 {
27 std::scoped_lock lck(rhs.m_stmtMutex, m_stmtMutex);
28 m_stmt = rhs.m_stmt;
29 m_db = rhs.m_db;
30 m_creationPoint = rhs.m_creationPoint;
31 rhs.m_stmt = nullptr;
32 rhs.m_db = nullptr;
33 return *this;
34}
std::recursive_mutex m_stmtMutex
Definition Statement.h:92

◆ reset()

void SQLite::Statement::reset ( )
private

Reset prepared statement for re-execution and clear bindings.

Definition at line 44 of file Statement.cxx.

44 {
45 const int err = sqlite3_reset(m_stmt);
46 if (err != 0) {
47 throw std::runtime_error(
48 std::format("ERROR in SQLite statement reset: {} ({})",
49 sqlite3_errstr(err), sqlite3_errmsg(m_db)));
50 }
51 sqlite3_clear_bindings(m_stmt);
52}

◆ run()

template<ValidColumnType... ReturnArgs, ValidParamType... ParamArgs>
ResultTypeWrapper< ReturnArgs... >::type SQLite::Statement::run ( ParamArgs... params)

Run the statement.

◆ step()

bool SQLite::Statement::step ( )
private

Step through the prepared statement.

Definition at line 54 of file Statement.cxx.

54 {
55 int err = SQLITE_BUSY;
56 while (err == SQLITE_BUSY) {
57 err = sqlite3_step(m_stmt);
58 }
59 switch (err) {
60 case SQLITE_DONE:
61 return false;
62 case SQLITE_ROW:
63 return true;
64 default:
65 throw std::runtime_error(
66 std::format("ERROR in SQLite statement step: {} ({})",
67 sqlite3_errstr(err), sqlite3_errmsg(m_db)));
68 }
69}

Member Data Documentation

◆ m_creationPoint

std::source_location SQLite::Statement::m_creationPoint
private

Definition at line 95 of file Statement.h.

◆ m_db

sqlite3* SQLite::Statement::m_db = nullptr
private

Definition at line 94 of file Statement.h.

◆ m_stmt

sqlite3_stmt* SQLite::Statement::m_stmt = nullptr
private

Definition at line 93 of file Statement.h.

◆ m_stmtMutex

std::recursive_mutex SQLite::Statement::m_stmtMutex
private

Definition at line 92 of file Statement.h.


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