ATLAS Offline Software
Loading...
Searching...
No Matches
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
18namespace SQLite {
20template <typename T>
21concept ValidParamType = requires {
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
28template <typename T>
29concept ValidColumnType = requires {
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
35template <ValidColumnType... Args>
36 requires(sizeof...(Args) >= 1)
37using ResultType = std::vector<std::tuple<Args...>>;
38
40template <typename... Args>
42 using type = ResultType<Args...>;
43};
44template <>
46 using type = void;
47};
48
50class 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
#define I(x, y, z)
Definition MD5.cxx:116
Statement()=default
std::tuple< Ts... > columns(std::index_sequence< Is... >)
Retrieve columns.
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
Statement(const Statement &)=delete
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
T column()
Retrieve a column.
void bind(int index, I value)
std::recursive_mutex m_stmtMutex
Definition Statement.h:92
ResultTypeWrapper< ReturnArgs... >::type run(ParamArgs... params)
Run the statement.
std::source_location m_creationPoint
Definition Statement.h:95
bool step()
Step through the prepared statement.
Definition Statement.cxx:54
Test if a type is a valid placeholder for some SQLite column type.
Definition Statement.h:29
Test if a type is a valid SQLite parameter type.
Definition Statement.h:21
std::vector< std::tuple< Args... > > ResultType
Return type for result of an SQL statement that returns data.
Definition Statement.h:37
Definition index.py:1
Helper to return void if Args is empty or void.
Definition Statement.h:41
ResultType< Args... > type
Definition Statement.h:42