ATLAS Offline Software
Statement.h
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 #ifndef SQLITEDBSVC_STATEMENT_H
6 #define SQLITEDBSVC_STATEMENT_H
7 
8 #include <concepts>
9 #include <mutex>
10 #include <source_location> // Improves error messages
11 #include <string>
12 #include <string_view>
13 #include <tuple>
14 #include <vector>
15 
16 #include "sqlite3.h"
17 
18 namespace SQLite {
20 template <typename T>
22  requires std::convertible_to<T, std::int64_t> ||
23  std::convertible_to<T, double> ||
24  std::convertible_to<T, std::string_view>;
25 };
26 
28 template <typename T>
30  requires std::same_as<std::remove_cvref_t<T>, T> &&
31  (std::is_arithmetic_v<T> || std::same_as<T, std::string>);
32 };
33 
35 template <ValidColumnType... Args>
36  requires(sizeof...(Args) >= 1)
37 using ResultType = std::vector<std::tuple<Args...>>;
38 
42  using type = ResultType<Args...>;
43 };
44 template <>
46  using type = void;
47 };
48 
50 class Statement {
51  public:
55  Statement(sqlite3* db, std::string_view sql, std::source_location call);
56  ~Statement();
57  Statement() = default;
58  Statement(const Statement&) = delete;
59 
62  Statement& operator=(Statement&& rhs) noexcept;
63 
65  template <ValidColumnType... ReturnArgs, ValidParamType... ParamArgs>
66  ResultTypeWrapper<ReturnArgs...>::type run(ParamArgs... params);
67 
68  private:
70  void reset();
72  bool step();
73 
75  void bind(int index, std::int64_t value);
76  // template to force the correct overload to be called for ints
77  template <typename I>
78  requires std::integral<I>
79  void bind(int index, I value);
81  void bind(int index, double value);
83  void bind(int index, std::string_view value);
84 
86  template <ValidColumnType T, std::size_t I>
87  T column();
89  template <ValidColumnType... Ts, std::size_t... Is>
90  std::tuple<Ts...> columns(std::index_sequence<Is...>);
91 
92  std::recursive_mutex m_stmtMutex;
93  sqlite3_stmt* m_stmt = nullptr;
94  sqlite3* m_db = nullptr;
95  std::source_location m_creationPoint;
96 };
97 } // namespace SQLite
98 
99 #include "Statement.icc"
100 #endif // SQLITEDBSVC_STATEMENT_H
SQLite::Statement::bind
requires std::integral< I > void bind(int index, I value)
SQLite::Statement
SQLite prepared statement.
Definition: Statement.h:50
index
Definition: index.py:1
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
taskman.template
dictionary template
Definition: taskman.py:316
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
Args
Definition: test_lwtnn_fastgraph.cxx:12
Statement.icc
python.CaloAddPedShiftConfig.type
type
Definition: CaloAddPedShiftConfig.py:42
SQLite::Statement::column
T column()
Retrieve a column.
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
SQLite::Statement::m_creationPoint
std::source_location m_creationPoint
Definition: Statement.h:95
SQLite::ResultTypeWrapper::type
ResultType< Args... > type
Definition: Statement.h:42
vector
Definition: MultiHisto.h:13
SQLite::ValidParamType
concept ValidParamType
Test if a type is a valid SQLite parameter type.
Definition: Statement.h:21
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::run
ResultTypeWrapper< ReturnArgs... >::type run(ParamArgs... params)
Run the statement.
SQLite::Statement::m_stmtMutex
std::recursive_mutex m_stmtMutex
Definition: Statement.h:92
SQLite::Statement::Statement
Statement(const Statement &)=delete
SQLite::Statement::m_db
sqlite3 * m_db
Definition: Statement.h:94
SQLite::Statement::columns
std::tuple< Ts... > columns(std::index_sequence< Is... >)
Retrieve columns.
python.Dumpers.typename
def typename(t)
Definition: Dumpers.py:193
SQLite::Statement::step
bool step()
Step through the prepared statement.
Definition: Statement.cxx:54
SQLite::requires
requires(sizeof...(Args) >=1) using ResultType
Return type for result of an SQL statement that returns data.
SQLite::ResultTypeWrapper
Helper to return void if Args is empty or void.
Definition: Statement.h:41
SQLite::Statement::Statement
Statement()=default
SQLite::ValidColumnType
concept ValidColumnType
Test if a type is a valid placeholder for some SQLite column type.
Definition: Statement.h:29
SQLite::ResultTypeWrapper<>::type
void type
Definition: Statement.h:46
PowhegControl_ttFCNC_NLO.params
params
Definition: PowhegControl_ttFCNC_NLO.py:226
SQLite
Definition: Statement.h:18