Audacity 3.2.0
Statement.h
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-or-later
3 * SPDX-FileName: Statement.h
4 * SPDX-FileContributor: Dmitry Vedenko
5 */
6
7#pragma once
8
9#include <memory>
10#include <optional>
11#include <string>
12#include <string_view>
13#include <vector>
14
15#include "Error.h"
16
17
18struct sqlite3_stmt;
19
20namespace audacity::sqlite
21{
22struct StatementHandle;
23using StatementHandlePtr = std::shared_ptr<StatementHandle>;
24
25class RowIterator;
26
28
31class SQLITE_HELPERS_API Row final
32{
33 friend class RowIterator;
34 explicit Row(
35 StatementHandlePtr statement, std::vector<Error>& errors) noexcept;
36 Row() = default;
37
38public:
39 bool Get(int columnIndex, bool& value) const;
40 bool Get(int columnIndex, int& value) const;
41 bool Get(int columnIndex, long& value) const;
42 bool Get(int columnIndex, long long& value) const;
43 bool Get(int columnIndex, float& value) const;
44 bool Get(int columnIndex, double& value) const;
45 bool Get(int columnIndex, std::string& value) const;
46
47 template <typename T> T GetOr(int columnIndex, T defaultValue = T()) const
48 {
49 T value;
50 return Get(columnIndex, value) ? value : defaultValue;
51 }
52
53 int GetColumnCount() const;
54
55 int64_t GetColumnBytes(int columnIndex) const;
56 int64_t ReadData(int columnIndex, void* buffer, int64_t maxSize) const;
57
58private:
59 template <typename Reader>
60 bool DoGet(Reader reader, int columnIndex) const;
61
62 StatementHandlePtr mStatement {};
63 std::vector<Error>* mErrors {};
64 int mColumnsCount { 0 };
65};
66
68class SQLITE_HELPERS_API RowIterator final
69{
70 friend class RunResult;
72 StatementHandlePtr statement, std::vector<Error>& errors) noexcept;
73
74 RowIterator() noexcept;
75
76public:
77 RowIterator(const RowIterator&) = delete;
78 RowIterator(RowIterator&&) noexcept;
79 RowIterator& operator=(const RowIterator&) = delete;
80 RowIterator& operator=(RowIterator&&) noexcept;
81
82 RowIterator& operator++() noexcept;
83
84 bool operator==(const RowIterator& other) const noexcept;
85 bool operator!=(const RowIterator& other) const noexcept;
86
87 Row operator*() const noexcept;
88
89private:
90 StatementHandlePtr mStatement {};
91 std::vector<Error>* mErrors {};
92
93 int mRowIndex { 0 };
94 bool mDone { false };
95};
96
98class SQLITE_HELPERS_API RunResult final
99{
100 friend class RunContext;
101 RunResult(StatementHandlePtr statement, std::vector<Error> errors) noexcept;
102
103public:
104 RunResult(const RunResult&) = delete;
105 RunResult(RunResult&&) noexcept;
106 RunResult& operator=(const RunResult&) = delete;
107 RunResult& operator=(RunResult&&) noexcept;
108
109 ~RunResult();
110
111 bool IsOk() const noexcept;
112 bool HasRows() const noexcept;
113
114 int GetModifiedRowsCount() const noexcept;
115
116 const std::vector<Error>& GetErrors() const noexcept;
117
118 RowIterator begin() noexcept;
119 RowIterator end() noexcept;
120
121private:
123 std::vector<Error> mErrors {};
124 int mModifiedRowsCount { 0 };
125 bool mHasRows { false };
126};
127
129
132class SQLITE_HELPERS_API RunContext final
133{
134 friend class Statement;
135 explicit RunContext(StatementHandlePtr statement) noexcept;
136
137public:
138 RunContext(const RunContext&) = delete;
139 RunContext(RunContext&&) noexcept;
140 RunContext& operator=(const RunContext&) = delete;
141 RunContext& operator=(RunContext&&) noexcept;
142
144 Bind(int index, const void* data, int64_t size, bool makeCopy = true);
145
146 RunContext& Bind(int index, const std::string& value, bool makeCopy = true);
147 RunContext& Bind(int index, std::string_view value, bool makeCopy = true);
148 RunContext& Bind(int index, const char* value, bool makeCopy = true);
149
150 RunContext& Bind(int index, bool value);
151 RunContext& Bind(int index, int value);
152 RunContext& Bind(int index, long value);
153 RunContext& Bind(int index, long long value);
154 RunContext& Bind(int index, std::size_t value);
155 RunContext& Bind(int index, float value);
156 RunContext& Bind(int index, double value);
157
158 RunContext& Bind(int index, std::nullptr_t);
159
160 RunContext& BindZeroBlob(int index, int64_t size);
161
162 template <typename T> RunContext& Bind(const std::string& name, const T& value)
163 {
164 return Bind(GetParameterIndex(name), value);
165 }
166
167 template <typename T>
168 RunContext& Bind(const std::string& name, const T& value, bool make_copy)
169 {
170 return Bind(GetParameterIndex(name), value, make_copy);
171 }
172
173 template <typename... Args> RunContext& BindAll(Args&&... args)
174 {
175 int index = 0;
176 (Bind(++index, std::forward<Args>(args)), ...);
177 return *this;
178 }
179
180 int GetParameterIndex(const std::string& name) const noexcept;
181
182 RunResult Run();
183
184private:
185 template <typename Binder> RunContext& DoBind(Binder binder);
186
187 StatementHandlePtr mStatement {};
188 std::vector<Error> mErrors {};
189 bool mNeedsReset { false };
190};
191
193
201class SQLITE_HELPERS_API Statement final
202{
203 explicit Statement(sqlite3_stmt* stmt);
204
205public:
206
207 Statement(const Statement&) = delete;
208 Statement(Statement&&) noexcept;
209
210 Statement& operator=(const Statement&) = delete;
211 Statement& operator=(Statement&&) noexcept;
212
213 RunContext& Prepare() noexcept;
214
215 template<typename... Args>
216 RunContext& Prepare(Args&&... args)
217 {
218 return Prepare().BindAll(std::forward<Args>(args)...);
219 }
220
221private:
222 StatementHandlePtr mStatement {};
223 std::optional<RunContext> mRunContext {};
224
225 friend class Connection;
226};
227
228} // namespace audacity::sqlite
wxString name
Definition: TagsEditor.cpp:166
A class representing a connection to a SQLite database.
Definition: Connection.h:48
A class representing an error in SQLite.
Definition: Error.h:17
A class representing a row in a result set.
Definition: Statement.h:32
T GetOr(int columnIndex, T defaultValue=T()) const
Definition: Statement.h:47
A class representing an iterator over a result set.
Definition: Statement.h:69
A class representing a context of a run operation.
Definition: Statement.h:133
RunContext & BindAll(Args &&... args)
Definition: Statement.h:173
RunContext & Bind(const std::string &name, const T &value, bool make_copy)
Definition: Statement.h:168
RunContext(const RunContext &)=delete
A class representing a result of a run operation.
Definition: Statement.h:99
RunResult(const RunResult &)=delete
A class representing a compiled statement.
Definition: Statement.h:202
Statement(const Statement &)=delete
Services * Get()
Fetch the global instance, or nullptr if none is yet installed.
Definition: BasicUI.cpp:202
std::shared_ptr< StatementHandle > StatementHandlePtr
Definition: Statement.h:23
const char * end(const char *str) noexcept
Definition: StringUtils.h:106
const char * begin(const char *str) noexcept
Definition: StringUtils.h:101
STL namespace.