44#include < string>
55#include < memory>
66#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
812#ifdef __has_include
913#if __cplusplus > 201402 && __has_include(<optional>)
1014#define MODERN_SQLITE_STD_OPTIONAL_SUPPORT
3135#ifdef MODERN_SQLITE_STD_VARIANT_SUPPORT
3236#include < variant>
3337#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
3552#include < sqlite3.h>
3653#include " errors.h"
3754
@@ -150,16 +167,17 @@ namespace sqlite {
150167 sqlite3_result_null (db);
151168 }
152169
153- // std::string
170+ // str_ref
154171 template <>
155172 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);
159175 }
160176
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+ }
163181
164182 inline std::string get_col_from_db (sqlite3_stmt* stmt, int inx, result_type<std::string>) {
165183 return sqlite3_column_type (stmt, inx) == SQLITE_NULL ? std::string () :
@@ -170,31 +188,32 @@ namespace sqlite {
170188 std::string (reinterpret_cast <char const *>(sqlite3_value_text (value)), sqlite3_value_bytes (value));
171189 }
172190
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);
175193 }
176- // std::u16string
194+ // u16str_ref
177195 template <>
178196 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);
182199 }
183200
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+ }
186205
187206 inline std::u16string get_col_from_db (sqlite3_stmt* stmt, int inx, result_type<std::u16string>) {
188207 return sqlite3_column_type (stmt, inx) == SQLITE_NULL ? std::u16string () :
189208 std::u16string (reinterpret_cast <char16_t const *>(sqlite3_column_text16 (stmt, inx)), sqlite3_column_bytes16 (stmt, inx));
190209 }
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>) {
192211 return sqlite3_value_type (value) == SQLITE_NULL ? std::u16string () :
193212 std::u16string (reinterpret_cast <char16_t const *>(sqlite3_value_text16 (value)), sqlite3_value_bytes16 (value));
194213 }
195214
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);
198217 }
199218
200219 // Other integer types
0 commit comments