-
-
Notifications
You must be signed in to change notification settings - Fork 325
/
Copy pathstatement_binder.h
318 lines (262 loc) · 11.3 KB
/
statement_binder.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#pragma once
#include <sqlite3.h>
#include <type_traits> // std::enable_if_t, std::is_arithmetic, std::is_same, std::true_type, std::false_type
#include <memory> // std::default_delete
#include <string> // std::string, std::wstring
#ifndef SQLITE_ORM_OMITS_CODECVT
#include <codecvt> // std::codecvt_utf8_utf16
#endif // SQLITE_ORM_OMITS_CODECVT
#include <vector> // std::vector
#include <cstddef> // std::nullptr_t
#include <utility> // std::declval
#include <locale> // std::wstring_convert
#include <cstring> // ::strncpy, ::strlen
#include "is_std_ptr.h"
#include "arithmetic_tag.h"
#include "xdestroy_handling.h"
#include "pointer_value.h"
namespace sqlite_orm {
/**
* Helper class used for binding fields to sqlite3 statements.
*/
template<class V, typename Enable = void>
struct statement_binder : std::false_type {};
/**
* Specialization for 'pointer-passing interface'.
*/
template<class P, class T, class D>
struct statement_binder<pointer_binding<P, T, D>, void> {
using V = pointer_binding<P, T, D>;
// ownership of pointed-to-object is left untouched and remains at prepared statement's AST expression
int bind(sqlite3_stmt* stmt, int index, const V& value) const {
// note: C-casting `P* -> void*`, internal::xdestroy_proxy() does the inverse
return sqlite3_bind_pointer(stmt, index, (void*)value.ptr(), T::value, null_xdestroy_f);
}
// ownership of pointed-to-object is transferred to sqlite
void result(sqlite3_context* context, V& value) const {
// note: C-casting `P* -> void*`,
// row_extractor<pointer_arg<P, T>>::extract() and internal::xdestroy_proxy() do the inverse
sqlite3_result_pointer(context, (void*)value.take_ptr(), T::value, value.get_xdestroy());
}
};
/**
* Specialization for arithmetic types.
*/
template<class V>
struct statement_binder<V, std::enable_if_t<std::is_arithmetic<V>::value>> {
int bind(sqlite3_stmt* stmt, int index, const V& value) const {
return this->bind(stmt, index, value, tag());
}
void result(sqlite3_context* context, const V& value) const {
this->result(context, value, tag());
}
private:
using tag = arithmetic_tag_t<V>;
int bind(sqlite3_stmt* stmt, int index, const V& value, const int_or_smaller_tag&) const {
return sqlite3_bind_int(stmt, index, static_cast<int>(value));
}
void result(sqlite3_context* context, const V& value, const int_or_smaller_tag&) const {
sqlite3_result_int(context, static_cast<int>(value));
}
int bind(sqlite3_stmt* stmt, int index, const V& value, const bigint_tag&) const {
return sqlite3_bind_int64(stmt, index, static_cast<sqlite3_int64>(value));
}
void result(sqlite3_context* context, const V& value, const bigint_tag&) const {
sqlite3_result_int64(context, static_cast<sqlite3_int64>(value));
}
int bind(sqlite3_stmt* stmt, int index, const V& value, const real_tag&) const {
return sqlite3_bind_double(stmt, index, static_cast<double>(value));
}
void result(sqlite3_context* context, const V& value, const real_tag&) const {
sqlite3_result_double(context, static_cast<double>(value));
}
};
/**
* Specialization for std::string and C-string.
*/
template<class V>
struct statement_binder<V,
std::enable_if_t<std::is_same<V, std::string>::value || std::is_same<V, const char*>::value
#ifdef SQLITE_ORM_STRING_VIEW_SUPPORTED
|| std::is_same<V, std::string_view>::value
#endif
>> {
int bind(sqlite3_stmt* stmt, int index, const V& value) const {
auto stringData = this->string_data(value);
return sqlite3_bind_text(stmt, index, std::get<0>(stringData), std::get<1>(stringData), SQLITE_TRANSIENT);
}
void result(sqlite3_context* context, const V& value) const {
auto stringData = this->string_data(value);
auto stringDataLength = std::get<1>(stringData);
auto dataCopy = new char[stringDataLength + 1];
constexpr auto deleter = std::default_delete<char[]>{};
auto stringChars = std::get<0>(stringData);
::strncpy(dataCopy, stringChars, stringDataLength + 1);
sqlite3_result_text(context, dataCopy, stringDataLength, obtain_xdestroy_for(deleter, dataCopy));
}
private:
std::tuple<const char*, int> string_data(const std::string& s) const {
return {s.c_str(), int(s.size())};
}
std::tuple<const char*, int> string_data(const char* s) const {
auto length = int(::strlen(s));
return {s, length};
}
#ifdef SQLITE_ORM_STRING_VIEW_SUPPORTED
std::tuple<const char*, int> string_data(const std::string_view& s) const {
return {s.data(), int(s.size())};
}
#endif
};
#ifndef SQLITE_ORM_OMITS_CODECVT
/**
* Specialization for std::wstring and C-wstring.
*/
template<class V>
struct statement_binder<
V,
std::enable_if_t<std::is_same<V, std::wstring>::value || std::is_same<V, const wchar_t*>::value>> {
int bind(sqlite3_stmt* stmt, int index, const V& value) const {
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
std::string utf8Str = converter.to_bytes(value);
return statement_binder<decltype(utf8Str)>().bind(stmt, index, utf8Str);
}
void result(sqlite3_context* context, const V& value) const {
sqlite3_result_text16(context, (const void*)value.data(), int(value.length()), nullptr);
}
};
#endif // SQLITE_ORM_OMITS_CODECVT
/**
* Specialization for std::nullptr_t.
*/
template<>
struct statement_binder<std::nullptr_t, void> {
int bind(sqlite3_stmt* stmt, int index, const std::nullptr_t&) const {
return sqlite3_bind_null(stmt, index);
}
void result(sqlite3_context* context, const std::nullptr_t&) const {
sqlite3_result_null(context);
}
};
#ifdef SQLITE_ORM_OPTIONAL_SUPPORTED
/**
* Specialization for std::nullopt_t.
*/
template<>
struct statement_binder<std::nullopt_t, void> {
int bind(sqlite3_stmt* stmt, int index, const std::nullopt_t&) const {
return sqlite3_bind_null(stmt, index);
}
void result(sqlite3_context* context, const std::nullopt_t&) const {
sqlite3_result_null(context);
}
};
#endif // SQLITE_ORM_OPTIONAL_SUPPORTED
template<class V>
struct statement_binder<V, std::enable_if_t<is_std_ptr<V>::value>> {
using value_type = typename is_std_ptr<V>::element_type;
int bind(sqlite3_stmt* stmt, int index, const V& value) const {
if(value) {
return statement_binder<value_type>().bind(stmt, index, *value);
} else {
return statement_binder<std::nullptr_t>().bind(stmt, index, nullptr);
}
}
void result(sqlite3_context* context, const V& value) const {
if(value) {
statement_binder<value_type>().result(context, value);
} else {
statement_binder<std::nullptr_t>().result(context, nullptr);
}
}
};
/**
* Specialization for optional type (std::vector<char>).
*/
template<>
struct statement_binder<std::vector<char>, void> {
int bind(sqlite3_stmt* stmt, int index, const std::vector<char>& value) const {
if(value.size()) {
return sqlite3_bind_blob(stmt, index, (const void*)&value.front(), int(value.size()), SQLITE_TRANSIENT);
} else {
return sqlite3_bind_blob(stmt, index, "", 0, SQLITE_TRANSIENT);
}
}
void result(sqlite3_context* context, const std::vector<char>& value) const {
if(value.size()) {
sqlite3_result_blob(context, (const void*)&value.front(), int(value.size()), nullptr);
} else {
sqlite3_result_blob(context, "", 0, nullptr);
}
}
};
#ifdef SQLITE_ORM_OPTIONAL_SUPPORTED
template<class T>
struct statement_binder<std::optional<T>, void> {
using value_type = T;
int bind(sqlite3_stmt* stmt, int index, const std::optional<T>& value) const {
if(value) {
return statement_binder<value_type>().bind(stmt, index, *value);
} else {
return statement_binder<std::nullopt_t>().bind(stmt, index, std::nullopt);
}
}
void result(sqlite3_context* context, const std::optional<T>& value) const {
if(value) {
statement_binder<value_type>().result(context, value);
} else {
statement_binder<std::nullopt_t>().result(context, std::nullopt);
}
}
};
#endif // SQLITE_ORM_OPTIONAL_SUPPORTED
namespace internal {
template<class T>
using is_bindable = std::integral_constant<bool, !std::is_base_of<std::false_type, statement_binder<T>>::value>;
struct conditional_binder_base {
sqlite3_stmt* stmt = nullptr;
int& index;
conditional_binder_base(decltype(stmt) stmt_, decltype(index) index_) : stmt(stmt_), index(index_) {}
};
template<class T, class C>
struct conditional_binder;
template<class T>
struct conditional_binder<T, std::true_type> : conditional_binder_base {
using conditional_binder_base::conditional_binder_base;
int operator()(const T& t) const {
return statement_binder<T>().bind(this->stmt, this->index++, t);
}
};
template<class T>
struct conditional_binder<T, std::false_type> : conditional_binder_base {
using conditional_binder_base::conditional_binder_base;
int operator()(const T&) const {
return SQLITE_OK;
}
};
template<class T, template<class C> class F, class SFINAE = void>
struct tuple_filter_single;
template<class T, template<class C> class F>
struct tuple_filter_single<T, F, typename std::enable_if<F<T>::value>::type> {
using type = std::tuple<T>;
};
template<class T, template<class C> class F>
struct tuple_filter_single<T, F, typename std::enable_if<!F<T>::value>::type> {
using type = std::tuple<>;
};
template<class T, template<class C> class F>
struct tuple_filter;
template<class... Args, template<class C> class F>
struct tuple_filter<std::tuple<Args...>, F> {
using type = typename conc_tuple<typename tuple_filter_single<Args, F>::type...>::type;
};
template<class T>
struct bindable_filter_single : tuple_filter_single<T, is_bindable> {};
template<class T>
struct bindable_filter;
template<class... Args>
struct bindable_filter<std::tuple<Args...>> {
using type = typename conc_tuple<typename bindable_filter_single<Args>::type...>::type;
};
}
}