RhythmGame 1.4.3
Loading...
Searching...
No Matches
SqliteCppDb.h
Go to the documentation of this file.
1//
2// Created by bobini on 11.07.2022.
3//
4
5#ifndef RHYTHMGAME_SQLITECPPDB_H
6#define RHYTHMGAME_SQLITECPPDB_H
7#include <SQLiteCpp/SQLiteCpp.h>
8#include <optional>
9#include <vector>
10#include <type_traits>
11#include <chrono>
12#include <cstdint>
13#include <stop_token>
14#include "support/get.h"
15#include "support/TupleSize.h"
16
17struct sqlite3_mutex;
18
22namespace db {
23
30{
31 SQLite::Database db;
32 uint64_t nextSavepoint = 0;
33 unsigned transactionDepth = 0;
34
35 void checkTransaction() const;
36
37 class ConnectionLock
38 {
39 sqlite3_mutex* mutex;
40
41 public:
42 explicit ConnectionLock(const SQLite::Database* database);
43 ~ConnectionLock();
44 ConnectionLock(const ConnectionLock&) = delete;
45 auto operator=(const ConnectionLock&) -> ConnectionLock& = delete;
46 void unlock();
47 };
48
49 class StatementExecution
50 {
51 ConnectionLock lock;
52 SQLite::Statement* statement;
53 const SQLite::Database* database;
54 std::stop_token stopToken;
55 bool finished = false;
56
57 public:
58 StatementExecution(SQLite::Statement* statement,
59 const SqliteCppDb* database,
60 std::stop_token stopToken);
61 ~StatementExecution();
62 void checkCancelled() const;
63 void finish();
64 };
65
66 public:
67 enum class Durability
68 {
69 Normal,
70 Full
71 };
72
81 {
82 ConnectionLock lock;
83 SqliteCppDb* owner;
84 SQLite::Database* database;
85 std::string commitQuery;
86 std::string rollbackQuery;
87 bool nested;
88 unsigned depth;
89 bool committed = false;
90
91 public:
92 explicit Transaction(SqliteCppDb* database);
94 void commit();
95 };
96
97 [[nodiscard]] auto transaction() -> Transaction;
98
108 {
109 SQLite::Statement statement;
111
112 public:
113 Statement(SQLite::Statement statement, SqliteCppDb* db);
114 template<typename... T>
115 auto bind(int index, T&&... values) -> void
116 {
117 const ConnectionLock lock(&db->db);
118 statement.bind(index, std::forward<T>(values)...);
119 }
120 template<typename... T>
121 auto bind(const std::string& name, T&&... values) -> void
122 {
123 const ConnectionLock lock(&db->db);
124 statement.bind(name, std::forward<T>(values)...);
125 }
126 void reset();
127
128 void execute(std::stop_token stopToken = {});
129
139 template<std::default_initializable Ret>
140 [[nodiscard]] auto executeAndGet(std::stop_token stopToken = {})
141 -> std::optional<Ret>
142 {
143 StatementExecution execution(&statement, db, stopToken);
144 std::optional<Ret> result;
145 if (statement.executeStep()) {
146 result.emplace();
147 writeRow(statement, *result);
148 }
149 execution.finish();
150 return result;
151 }
160 template<std::default_initializable Ret>
161 [[nodiscard]] auto executeAndGetAll(std::stop_token stopToken = {})
162 -> std::vector<Ret>
163 {
164 StatementExecution execution(&statement, db, stopToken);
165 std::vector<Ret> result;
166
167 while (statement.executeStep()) {
168 execution.checkCancelled();
169 result.emplace_back();
170 writeRow(statement, result.back());
171 }
172
173 execution.finish();
174 return result;
175 }
176
177 private:
178 template<typename ElemType>
179 static auto getElem(SQLite::Statement& statement, int index) -> ElemType
180 {
181 // This if is necessary for MSVC (don't ask me why)
182 if constexpr (std::is_same_v<ElemType, std::string>) {
183 return statement.getColumn(index).getString();
184 } else {
185 return static_cast<ElemType>(statement.getColumn(index));
186 }
187 }
188
189 template<std::default_initializable Ret>
190 void writeRow(SQLite::Statement& statement, Ret& ret) const
191 {
192 int index = 0;
193 writeRow(statement, ret, index);
194 }
195
196 template<std::default_initializable Ret>
197 void writeRow(SQLite::Statement& stmt, Ret& ret, int& index) const
198 {
199 if constexpr (std::convertible_to<SQLite::Column, Ret> || std::is_same_v<Ret, std::string>) {
200 ret = getElem<std::remove_cvref_t<Ret>>(stmt, index++);
201 } else {
202 constexpr size_t tupleSize = support::tupleSizeV<Ret>;
203 constexpr auto indices =
204 std::make_integer_sequence<int, static_cast<int>(tupleSize)>();
205 [this, &stmt, &ret, &index]<int... N>(std::integer_sequence<int, N...>) {
206 (writeRow(stmt, support::get<N>(ret), index), ...);
207 }(indices);
208 }
209 }
210 };
211
220 explicit SqliteCppDb(
221 const std::filesystem::path& dbPath,
222 std::chrono::milliseconds busyTimeout = std::chrono::milliseconds{ 0 },
223 Durability durability = Durability::Normal);
225 static auto openReadOnly(const std::filesystem::path& dbPath,
226 std::chrono::milliseconds busyTimeout =
227 std::chrono::milliseconds{ 0 }) -> SqliteCppDb;
233 void execute(const std::string& query);
234 auto createStatement(const std::string& query) -> Statement;
241 [[nodiscard]] auto hasTable(const std::string& table) const -> bool;
242
243 private:
244 SqliteCppDb(const std::filesystem::path& dbPath,
245 std::chrono::milliseconds busyTimeout,
246 Durability durability,
247 bool readOnly);
248};
249} // namespace db
250
251#endif // RHYTHMGAME_SQLITECPPDB_H
bool finished
Definition ArenaOpponentTarget.cpp:19
gameplay_logic::BmsResult::DTO result
Definition ScoreDb.cpp:86
zip_uint64_t index
Definition SongAssetStore.cpp:436
Wrapper for SQLiteCpp::Statement.
Definition SqliteCppDb.h:108
auto bind(const std::string &name, T &&... values) -> void
Definition SqliteCppDb.h:121
auto bind(int index, T &&... values) -> void
Definition SqliteCppDb.h:115
void execute(std::stop_token stopToken={})
Definition SqliteCppDb.cpp:75
auto executeAndGet(std::stop_token stopToken={}) -> std::optional< Ret >
Executes a query that returns a single row.
Definition SqliteCppDb.h:140
auto executeAndGetAll(std::stop_token stopToken={}) -> std::vector< Ret >
Executes a query that returns any number of rows.
Definition SqliteCppDb.h:161
void reset()
Definition SqliteCppDb.cpp:82
Isolates a transaction from other users of this connection.
Definition SqliteCppDb.h:81
~Transaction()
Definition SqliteCppDb.cpp:189
void commit()
Definition SqliteCppDb.cpp:211
Database wrapper for SQLiteCpp.
Definition SqliteCppDb.h:30
SqliteCppDb(const std::filesystem::path &dbPath, std::chrono::milliseconds busyTimeout=std::chrono::milliseconds{ 0 }, Durability durability=Durability::Normal)
Constructs a database wrapper.
Definition SqliteCppDb.cpp:14
auto hasTable(const std::string &table) const -> bool
Queries the database to inspect whether the table with the provided name exists.
Definition SqliteCppDb.cpp:54
auto createStatement(const std::string &query) -> Statement
Definition SqliteCppDb.cpp:68
auto transaction() -> Transaction
Definition SqliteCppDb.cpp:228
static auto openReadOnly(const std::filesystem::path &dbPath, std::chrono::milliseconds busyTimeout=std::chrono::milliseconds{ 0 }) -> SqliteCppDb
Definition SqliteCppDb.cpp:22
void execute(const std::string &query)
Executes a query.
Definition SqliteCppDb.cpp:61
Durability
Definition SqliteCppDb.h:68
Namespace for database related classes and functions.
Definition QtArenaRoundLoader.h:15