4
4
#include < string>
5
5
#include < memory>
6
6
#include < vector>
7
-
7
+ #ifdef __has_include
8
+ #if __cplusplus >= 201703 && __has_include(<string_view>)
9
+ #define MODERN_SQLITE_STRINGVIEW_SUPPORT
10
+ #endif
11
+ #endif
8
12
#ifdef __has_include
9
13
#if __cplusplus > 201402 && __has_include(<optional>)
10
14
#define MODERN_SQLITE_STD_OPTIONAL_SUPPORT
31
35
#ifdef MODERN_SQLITE_STD_VARIANT_SUPPORT
32
36
#include < variant>
33
37
#endif
34
-
38
+ #ifdef MODERN_SQLITE_STRINGVIEW_SUPPORT
39
+ #include < string_view>
40
+ namespace sqlite
41
+ {
42
+ typedef const std::string_view str_ref;
43
+ typedef const std::u16string_view u16str_ref;
44
+ }
45
+ #else
46
+ namespace sqlite
47
+ {
48
+ typedef const std::string& str_ref;
49
+ typedef const std::u16string& u16str_ref;
50
+ }
51
+ #endif
35
52
#include < sqlite3.h>
36
53
#include " errors.h"
37
54
@@ -150,16 +167,17 @@ namespace sqlite {
150
167
sqlite3_result_null (db);
151
168
}
152
169
153
- // std::string
170
+ // str_ref
154
171
template <>
155
172
struct has_sqlite_type <std::string, SQLITE3_TEXT, void > : std::true_type {};
156
-
157
- inline int bind_col_in_db (sqlite3_stmt* stmt, int inx, const std::string& val) {
158
- return sqlite3_bind_text (stmt, inx, val.data (), -1 , SQLITE_TRANSIENT);
173
+ inline int bind_col_in_db (sqlite3_stmt* stmt, int inx, str_ref val) {
174
+ return sqlite3_bind_text (stmt, inx, val.data (), val.length (), SQLITE_TRANSIENT);
159
175
}
160
176
161
- // Convert char* to string to trigger op<<(..., const std::string )
162
- template <std::size_t N> inline int bind_col_in_db (sqlite3_stmt* stmt, int inx, const char (&STR)[N]) { return bind_col_in_db (stmt, inx, std::string (STR, N-1 )); }
177
+ // Convert char* to string_view to trigger op<<(..., const str_ref )
178
+ template <std::size_t N> inline int bind_col_in_db (sqlite3_stmt* stmt, int inx, const char (&STR)[N]) {
179
+ return sqlite3_bind_text (stmt, inx, &STR[0 ], N-1 , SQLITE_TRANSIENT);
180
+ }
163
181
164
182
inline std::string get_col_from_db (sqlite3_stmt* stmt, int inx, result_type<std::string>) {
165
183
return sqlite3_column_type (stmt, inx) == SQLITE_NULL ? std::string () :
@@ -170,31 +188,32 @@ namespace sqlite {
170
188
std::string (reinterpret_cast <char const *>(sqlite3_value_text (value)), sqlite3_value_bytes (value));
171
189
}
172
190
173
- inline void store_result_in_db (sqlite3_context* db, const std::string& val) {
174
- sqlite3_result_text (db, val.data (), - 1 , SQLITE_TRANSIENT);
191
+ inline void store_result_in_db (sqlite3_context* db, str_ref val) {
192
+ sqlite3_result_text (db, val.data (), val. length () , SQLITE_TRANSIENT);
175
193
}
176
- // std::u16string
194
+ // u16str_ref
177
195
template <>
178
196
struct has_sqlite_type <std::u16string, SQLITE3_TEXT, void > : std::true_type {};
179
-
180
- inline int bind_col_in_db (sqlite3_stmt* stmt, int inx, const std::u16string& val) {
181
- return sqlite3_bind_text16 (stmt, inx, val.data (), -1 , SQLITE_TRANSIENT);
197
+ inline int bind_col_in_db (sqlite3_stmt* stmt, int inx, u16str_ref val) {
198
+ return sqlite3_bind_text16 (stmt, inx, val.data (), sizeof (char16_t ) * val.length (), SQLITE_TRANSIENT);
182
199
}
183
200
184
- // Convert char* to string to trigger op<<(..., const std::string )
185
- template <std::size_t N> inline int bind_col_in_db (sqlite3_stmt* stmt, int inx, const char16_t (&STR)[N]) { return bind_col_in_db (stmt, inx, std::u16string (STR, N-1 )); }
201
+ // Convert char* to string_view to trigger op<<(..., const str_ref )
202
+ template <std::size_t N> inline int bind_col_in_db (sqlite3_stmt* stmt, int inx, const char16_t (&STR)[N]) {
203
+ return sqlite3_bind_text16 (stmt, inx, &STR[0 ], sizeof (char16_t ) * (N-1 ), SQLITE_TRANSIENT);
204
+ }
186
205
187
206
inline std::u16string get_col_from_db (sqlite3_stmt* stmt, int inx, result_type<std::u16string>) {
188
207
return sqlite3_column_type (stmt, inx) == SQLITE_NULL ? std::u16string () :
189
208
std::u16string (reinterpret_cast <char16_t const *>(sqlite3_column_text16 (stmt, inx)), sqlite3_column_bytes16 (stmt, inx));
190
209
}
191
- inline std::u16string get_val_from_db (sqlite3_value *value, result_type<std::u16string >) {
210
+ inline std::u16string get_val_from_db (sqlite3_value *value, result_type<std::u16string>) {
192
211
return sqlite3_value_type (value) == SQLITE_NULL ? std::u16string () :
193
212
std::u16string (reinterpret_cast <char16_t const *>(sqlite3_value_text16 (value)), sqlite3_value_bytes16 (value));
194
213
}
195
214
196
- inline void store_result_in_db (sqlite3_context* db, const std::u16string& val) {
197
- sqlite3_result_text16 (db, val.data (), - 1 , SQLITE_TRANSIENT);
215
+ inline void store_result_in_db (sqlite3_context* db, u16str_ref val) {
216
+ sqlite3_result_text16 (db, val.data (), sizeof ( char16_t ) * val. length () , SQLITE_TRANSIENT);
198
217
}
199
218
200
219
// Other integer types
0 commit comments