This repository has been archived by the owner on Jan 3, 2024. It is now read-only.
forked from fnc12/sqlite_orm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpragma.h
227 lines (195 loc) · 8.66 KB
/
pragma.h
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
#pragma once
#include <sqlite3.h>
#include <string> // std::string
#include <functional> // std::function
#include <memory> // std::shared_ptr
#include <vector> // std::vector
#include <sstream>
#include "error_code.h"
#include "row_extractor.h"
#include "journal_mode.h"
#include "connection_holder.h"
#include "util.h"
#include "serializing_util.h"
namespace sqlite_orm {
namespace internal {
struct storage_base;
template<class T>
int getPragmaCallback(void* data, int argc, char** argv, char** x) {
return extract_single_value<T>(data, argc, argv, x);
}
template<>
inline int getPragmaCallback<std::vector<std::string>>(void* data, int argc, char** argv, char**) {
auto& res = *(std::vector<std::string>*)data;
res.reserve(argc);
for(decltype(argc) i = 0; i < argc; ++i) {
auto rowString = row_extractor<std::string>().extract(argv[i]);
res.push_back(std::move(rowString));
}
return 0;
}
struct pragma_t {
using get_connection_t = std::function<internal::connection_ref()>;
pragma_t(get_connection_t get_connection_) : get_connection(std::move(get_connection_)) {}
void busy_timeout(int value) {
this->set_pragma("busy_timeout", value);
}
int busy_timeout() {
return this->get_pragma<int>("busy_timeout");
}
sqlite_orm::journal_mode journal_mode() {
return this->get_pragma<sqlite_orm::journal_mode>("journal_mode");
}
void journal_mode(sqlite_orm::journal_mode value) {
this->_journal_mode = -1;
this->set_pragma("journal_mode", value);
this->_journal_mode = static_cast<decltype(this->_journal_mode)>(value);
}
/**
* https://www.sqlite.org/pragma.html#pragma_application_id
*/
int application_id() {
return this->get_pragma<int>("application_id");
}
/**
* https://www.sqlite.org/pragma.html#pragma_application_id
*/
void application_id(int value) {
this->set_pragma("application_id", value);
}
int synchronous() {
return this->get_pragma<int>("synchronous");
}
void synchronous(int value) {
this->_synchronous = -1;
this->set_pragma("synchronous", value);
this->_synchronous = value;
}
int user_version() {
return this->get_pragma<int>("user_version");
}
void user_version(int value) {
this->set_pragma("user_version", value);
}
int auto_vacuum() {
return this->get_pragma<int>("auto_vacuum");
}
void auto_vacuum(int value) {
this->set_pragma("auto_vacuum", value);
}
std::vector<std::string> integrity_check() {
return this->get_pragma<std::vector<std::string>>("integrity_check");
}
template<class T>
std::vector<std::string> integrity_check(T table_name) {
std::ostringstream ss;
ss << "integrity_check(" << table_name << ")" << std::flush;
return this->get_pragma<std::vector<std::string>>(ss.str());
}
std::vector<std::string> integrity_check(int n) {
std::ostringstream ss;
ss << "integrity_check(" << n << ")" << std::flush;
return this->get_pragma<std::vector<std::string>>(ss.str());
}
// will include generated columns in response as opposed to table_info
std::vector<sqlite_orm::table_xinfo> table_xinfo(const std::string& tableName) const {
auto connection = this->get_connection();
std::vector<sqlite_orm::table_xinfo> result;
std::ostringstream ss;
ss << "PRAGMA "
"table_xinfo("
<< streaming_identifier(tableName) << ")" << std::flush;
perform_exec(
connection.get(),
ss.str(),
[](void* data, int argc, char** argv, char**) -> int {
auto& res = *(std::vector<sqlite_orm::table_xinfo>*)data;
if(argc) {
auto index = 0;
auto cid = std::atoi(argv[index++]);
std::string name = argv[index++];
std::string type = argv[index++];
bool notnull = !!std::atoi(argv[index++]);
std::string dflt_value = argv[index] ? argv[index] : "";
++index;
auto pk = std::atoi(argv[index++]);
auto hidden = std::atoi(argv[index++]);
res.emplace_back(cid,
std::move(name),
std::move(type),
notnull,
std::move(dflt_value),
pk,
hidden);
}
return 0;
},
&result);
return result;
}
std::vector<sqlite_orm::table_info> table_info(const std::string& tableName) const {
auto connection = this->get_connection();
std::ostringstream ss;
ss << "PRAGMA "
"table_info("
<< streaming_identifier(tableName) << ")" << std::flush;
std::vector<sqlite_orm::table_info> result;
perform_exec(
connection.get(),
ss.str(),
[](void* data, int argc, char** argv, char**) -> int {
auto& res = *(std::vector<sqlite_orm::table_info>*)data;
if(argc) {
auto index = 0;
auto cid = std::atoi(argv[index++]);
std::string name = argv[index++];
std::string type = argv[index++];
bool notnull = !!std::atoi(argv[index++]);
std::string dflt_value = argv[index] ? argv[index] : "";
++index;
auto pk = std::atoi(argv[index++]);
res.emplace_back(cid, std::move(name), std::move(type), notnull, std::move(dflt_value), pk);
}
return 0;
},
&result);
return result;
}
private:
friend struct storage_base;
int _synchronous = -1;
signed char _journal_mode = -1; // if != -1 stores static_cast<sqlite_orm::journal_mode>(journal_mode)
get_connection_t get_connection;
template<class T>
T get_pragma(const std::string& name) {
auto connection = this->get_connection();
T result;
perform_exec(connection.get(), "PRAGMA " + name, getPragmaCallback<T>, &result);
return result;
}
/**
* Yevgeniy Zakharov: I wanted to refactor this function with statements and value bindings
* but it turns out that bindings in pragma statements are not supported.
*/
template<class T>
void set_pragma(const std::string& name, const T& value, sqlite3* db = nullptr) {
auto con = this->get_connection();
if(!db) {
db = con.get();
}
std::stringstream ss;
ss << "PRAGMA " << name << " = " << value << std::flush;
perform_void_exec(db, ss.str());
}
void set_pragma(const std::string& name, const sqlite_orm::journal_mode& value, sqlite3* db = nullptr) {
auto con = this->get_connection();
if(!db) {
db = con.get();
}
std::stringstream ss;
ss << "PRAGMA " << name << " = " << to_string(value) << std::flush;
perform_void_exec(db, ss.str());
}
};
}
}