-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueries.h
More file actions
187 lines (155 loc) · 6.06 KB
/
Copy pathqueries.h
File metadata and controls
187 lines (155 loc) · 6.06 KB
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
// MIT License
//
// Copyright (c) 2023 Ioannis Kaliakatsos
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#pragma once
#include <string>
#include <vector>
#include "reflection.h"
#include "query_predicates.h"
struct sqlite3;
struct sqlite3_stmt;
namespace sqlite_reflection {
/// A wrapper of all SQLite queries, encapsulating the preparation and
/// execution of queries against the SQLite database
class REFLECTION_EXPORT Query
{
public:
virtual ~Query() = default;
protected:
Query(sqlite3* db, const Reflection& record);
/// A textual representation of the given query, in SQL language
virtual std::string PrepareSql() const = 0;
/// Returns a comma-separated string of all struct member names
std::string JoinedRecordColumnNames() const;
/// Returns all struct member names in textual format
std::vector<std::string> GetRecordColumnNames() const;
/// Hook for customization of a given table column name, used primarily
/// for marking the column corresponding to id as PRIMARY KEY
virtual std::string CustomizedColumnName(size_t index) const;
sqlite3* db_;
const Reflection& record_;
};
/// A query for which no results are expected, such as
/// CREATE TABLE
/// UPDATE
/// INSERT
/// DELETE
class REFLECTION_EXPORT ExecutionQuery : public Query
{
public:
~ExecutionQuery() override = default;
explicit ExecutionQuery(sqlite3* db, const Reflection& record);
void Execute() const;
protected:
virtual std::vector<SqlValue> Bindings() const;
std::vector<SqlValue> GetValues(void* p) const;
};
/// A query for which direct SQL prompts are used
class REFLECTION_EXPORT SqlQuery : public ExecutionQuery
{
public:
~SqlQuery() override = default;
explicit SqlQuery(sqlite3* db, const std::string& sql);
protected:
std::string PrepareSql() const override;
std::string sql_;
};
/// A query for creating a table for a given reflectable struct. When the database
/// is initialized, a table for every registered reflectable struct is created
/// This maps to CREATE TABLE IF NOT EXISTS in SQL
class REFLECTION_EXPORT CreateTableQuery final : public ExecutionQuery
{
public:
~CreateTableQuery() override = default;
explicit CreateTableQuery(sqlite3* db, const Reflection& record);
protected:
std::string PrepareSql() const override;
std::string CustomizedColumnName(size_t index) const override;
};
/// A query to delete a given record from the database, by means of its id
/// This maps to DELETE in SQL
class REFLECTION_EXPORT DeleteQuery final : public ExecutionQuery
{
public:
~DeleteQuery() override = default;
explicit DeleteQuery(sqlite3* db, const Reflection& record, const QueryPredicateBase* predicate);
protected:
std::string PrepareSql() const override;
std::vector<SqlValue> Bindings() const override;
const QueryPredicateBase* predicate_;
};
/// A query to insert a given record to the database, by supplying a given type-erased struct instance
/// This maps to INSERT INTO in SQL
class REFLECTION_EXPORT InsertQuery final : public ExecutionQuery
{
public:
~InsertQuery() override = default;
explicit InsertQuery(sqlite3* db, const Reflection& record, void* p);
protected:
std::string PrepareSql() const override;
std::vector<SqlValue> Bindings() const override;
void* p_;
};
/// A query to update a given record to the database, by supplying a given type-erased struct instance
/// This maps to UPDATE in SQL
class REFLECTION_EXPORT UpdateQuery final : public ExecutionQuery
{
public:
~UpdateQuery() override = default;
explicit UpdateQuery(sqlite3* db, const Reflection& record, void* p);
protected:
std::string PrepareSql() const override;
std::vector<SqlValue> Bindings() const override;
void* p_;
};
/// A query for retrieving the max id of a given record from the database
class REFLECTION_EXPORT FetchMaxIdQuery final : public Query
{
public:
explicit FetchMaxIdQuery(sqlite3* db, const Reflection& record);
~FetchMaxIdQuery() override;
/// Retrieve the max id currently used for the given record type
int64_t GetMaxId();
protected:
std::string PrepareSql() const override;
sqlite3_stmt* stmt_;
};
struct FetchQueryResults;
/// A query for retrieving all records from the database, which match a given predicate condition
/// This maps to SELECT * in SQL
class REFLECTION_EXPORT FetchRecordsQuery final : public Query
{
public:
explicit FetchRecordsQuery(sqlite3* db, const Reflection& record, const QueryPredicateBase* predicate);
~FetchRecordsQuery() override;
/// Returns a textual representation of the results of the query
FetchQueryResults GetResults();
/// Reconstructs all record member values based on their concrete type,
/// based on the textual representation of the corresponding row result
/// of the fetch query
static void Hydrate(void* p, const FetchQueryResults& query_results, const Reflection& record, size_t i);
protected:
std::string PrepareSql() const override;
std::wstring GetColumnValue(int col) const;
sqlite3_stmt* stmt_;
const QueryPredicateBase* predicate_;
};
}