ATLAS Offline Software
Loading...
Searching...
No Matches
Statement.cxx
Go to the documentation of this file.
1/* -*- C++ -*- */
2/*
3 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
4*/
5
7
8#include <format>
9#include <stdexcept>
10
11namespace SQLite {
12Statement::Statement(sqlite3* db, std::string_view sql,
13 std::source_location call)
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}
25
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}
35
37 if (m_stmt == nullptr) {
38 return; // Empty statement (unfilled or moved-from)
39 }
40 sqlite3_finalize(m_stmt);
41 m_stmt = nullptr;
42}
43
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}
53
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}
70
71// These functions just throw a tuple of the parameter index and error code on
72// error This is reinterpreted in the run function
73void Statement::bind(int index, std::int64_t value) {
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}
79
80void Statement::bind(int index, double value) {
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}
86
87void Statement::bind(int index, std::string_view value) {
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}
94} // namespace SQLite
Statement()=default
void reset()
Reset prepared statement for re-execution and clear bindings.
Definition Statement.cxx:44
sqlite3 * m_db
Definition Statement.h:94
Statement & operator=(Statement &&rhs) noexcept
Move assignment operator allows Statement member variables to be filled from createStatement.
Definition Statement.cxx:26
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 cre...
Definition Statement.cxx:12
sqlite3_stmt * m_stmt
Definition Statement.h:93
void bind(int index, std::int64_t value)
Bind an integer to a parameter.
Definition Statement.cxx:73
std::recursive_mutex m_stmtMutex
Definition Statement.h:92
std::source_location m_creationPoint
Definition Statement.h:95
bool step()
Step through the prepared statement.
Definition Statement.cxx:54
Definition index.py:1