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