ATLAS Offline Software
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 
11 namespace SQLite {
12 Statement::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
73 void 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 
80 void 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 
87 void 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
vtune_athena.format
format
Definition: vtune_athena.py:14
SQLite::Statement
SQLite prepared statement.
Definition: Statement.h:50
index
Definition: index.py:1
Statement.h
SQLite::Statement::bind
void bind(int index, std::int64_t value)
Bind an integer to a parameter.
Definition: Statement.cxx:73
CaloCondBlobAlgs_fillNoiseFromASCII.db
db
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:42
SQLite::Statement::reset
void reset()
Reset prepared statement for re-execution and clear bindings.
Definition: Statement.cxx:44
SQLite::Statement::m_stmt
sqlite3_stmt * m_stmt
Definition: Statement.h:93
athena.value
value
Definition: athena.py:124
beamspotman.sql
sql
Definition: beamspotman.py:672
python.trfUtils.call
def call(args, bufsize=0, executable=None, stdin=None, preexec_fn=None, close_fds=False, shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0, message="", logger=msg, loglevel=None, timeout=None, retry=2, timefactor=1.5, sleeptime=10)
Definition: trfUtils.py:155
SQLite::Statement::~Statement
~Statement()
Definition: Statement.cxx:36
dqt_zlumi_pandas.err
err
Definition: dqt_zlumi_pandas.py:183
SQLite::Statement::m_creationPoint
std::source_location m_creationPoint
Definition: Statement.h:95
SQLite::Statement::operator=
Statement & operator=(Statement &&rhs) noexcept
Move assignment operator allows Statement member variables to be filled from createStatement.
Definition: Statement.cxx:26
SQLite::Statement::m_db
sqlite3 * m_db
Definition: Statement.h:94
SQLite::Statement::step
bool step()
Step through the prepared statement.
Definition: Statement.cxx:54
SQLite::Statement::Statement
Statement()=default
SQLite
Definition: Statement.h:18