Skip to content

Commit c69ffc1

Browse files
committed
Fallback to traditional string on pre-c++17 compilers
1 parent fd3f0bf commit c69ffc1

File tree

3 files changed

+39
-31
lines changed

3 files changed

+39
-31
lines changed

hdr/sqlite_modern_cpp.h

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ namespace sqlite {
4242

4343
void execute();
4444

45-
std::string_view sql() {
45+
STR_REF sql() {
4646
#if SQLITE_VERSION_NUMBER >= 3014000
4747
auto sqlite_deleter = [](void *ptr) {sqlite3_free(ptr);};
4848
std::unique_ptr<char, decltype(sqlite_deleter)> str(sqlite3_expanded_sql(_stmt.get()), sqlite_deleter);
@@ -52,7 +52,7 @@ namespace sqlite {
5252
#endif
5353
}
5454

55-
std::string_view original_sql() {
55+
STR_REF original_sql() {
5656
return sqlite3_sql(_stmt.get());
5757
}
5858

@@ -85,7 +85,7 @@ namespace sqlite {
8585
return ++_inx;
8686
}
8787

88-
sqlite3_stmt* _prepare(const std::u16string_view& sql) {
88+
sqlite3_stmt* _prepare(const U16STR_REF& sql) {
8989
//return _prepare(utility::utf16_to_utf8(sql));
9090
int hresult;
9191
sqlite3_stmt* tmp = nullptr;
@@ -97,7 +97,7 @@ namespace sqlite {
9797
return tmp;
9898
}
9999

100-
sqlite3_stmt* _prepare(const std::string_view& sql) {
100+
sqlite3_stmt* _prepare(const STR_REF& sql) {
101101
int hresult;
102102
sqlite3_stmt* tmp = nullptr;
103103
const char *remaining;
@@ -113,13 +113,13 @@ namespace sqlite {
113113

114114
public:
115115

116-
database_binder(std::shared_ptr<sqlite3> db, std::u16string_view const & sql):
116+
database_binder(std::shared_ptr<sqlite3> db, U16STR_REF const & sql):
117117
_db(db),
118118
_stmt(_prepare(sql), sqlite3_finalize),
119119
_inx(0) {
120120
}
121121

122-
database_binder(std::shared_ptr<sqlite3> db, std::string_view const & sql):
122+
database_binder(std::shared_ptr<sqlite3> db, STR_REF const & sql):
123123
_db(db),
124124
_stmt(_prepare(sql), sqlite3_finalize),
125125
_inx(0) {
@@ -370,7 +370,7 @@ namespace sqlite {
370370
std::shared_ptr<sqlite3> _db;
371371

372372
public:
373-
database(const std::string_view &db_name, const sqlite_config &config = {}): _db(nullptr) {
373+
database(const STR_REF &db_name, const sqlite_config &config = {}): _db(nullptr) {
374374
sqlite3* tmp = nullptr;
375375
auto ret = sqlite3_open_v2(db_name.data(), &tmp, static_cast<int>(config.flags), config.zVfs);
376376
_db = std::shared_ptr<sqlite3>(tmp, [=](sqlite3* ptr) { sqlite3_close_v2(ptr); }); // this will close the connection eventually when no longer needed.
@@ -380,7 +380,7 @@ namespace sqlite {
380380
*this << R"(PRAGMA encoding = "UTF-16";)";
381381
}
382382

383-
database(const std::u16string_view &db_name, const sqlite_config &config = {}): _db(nullptr) {
383+
database(const U16STR_REF &db_name, const sqlite_config &config = {}): _db(nullptr) {
384384
auto db_name_utf8 = utility::utf16_to_utf8(db_name.data());
385385
sqlite3* tmp = nullptr;
386386
auto ret = sqlite3_open_v2(db_name_utf8.data(), &tmp, static_cast<int>(config.flags), config.zVfs);
@@ -394,20 +394,20 @@ namespace sqlite {
394394
database(std::shared_ptr<sqlite3> db):
395395
_db(db) {}
396396

397-
database_binder operator<<(const std::string_view& sql) {
397+
database_binder operator<<(const STR_REF& sql) {
398398
return database_binder(_db, sql);
399399
}
400400

401401
database_binder operator<<(const char* sql) {
402-
return *this << std::string_view(sql);
402+
return *this << STR_REF(sql);
403403
}
404404

405-
database_binder operator<<(const std::u16string_view& sql) {
405+
database_binder operator<<(const U16STR_REF& sql) {
406406
return database_binder(_db, sql);
407407
}
408408

409409
database_binder operator<<(const char16_t* sql) {
410-
return *this << std::u16string_view(sql);
410+
return *this << U16STR_REF(sql);
411411
}
412412

413413
connection_type connection() const { return _db; }
@@ -421,7 +421,7 @@ namespace sqlite {
421421
}
422422

423423
template <typename Function>
424-
void define(const std::string_view &name, Function&& func) {
424+
void define(const STR_REF &name, Function&& func) {
425425
typedef utility::function_traits<Function> traits;
426426

427427
auto funcPtr = new auto(std::forward<Function>(func));
@@ -435,7 +435,7 @@ namespace sqlite {
435435
}
436436

437437
template <typename StepFunction, typename FinalFunction>
438-
void define(const std::string_view &name, StepFunction&& step, FinalFunction&& final) {
438+
void define(const STR_REF &name, StepFunction&& step, FinalFunction&& final) {
439439
typedef utility::function_traits<StepFunction> traits;
440440
using ContextType = typename std::remove_reference<typename traits::template argument<0>>::type;
441441

@@ -660,3 +660,5 @@ namespace sqlite {
660660
}
661661
}
662662
}
663+
#undef STR_REF
664+
#undef U16STR_REF

hdr/sqlite_modern_cpp/errors.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ namespace sqlite {
99

1010
class sqlite_exception: public std::runtime_error {
1111
public:
12-
sqlite_exception(const char* msg, std::string_view sql, int code = -1): runtime_error(msg), code(code), sql(sql) {}
13-
sqlite_exception(int code, std::string_view sql): runtime_error(sqlite3_errstr(code)), code(code), sql(sql) {}
12+
sqlite_exception(const char* msg, STR_REF sql, int code = -1): runtime_error(msg), code(code), sql(sql) {}
13+
sqlite_exception(int code, STR_REF sql): runtime_error(sqlite3_errstr(code)), code(code), sql(sql) {}
1414
int get_code() const {return code & 0xFF;}
1515
int get_extended_code() const {return code;}
16-
std::string_view get_sql() const {return sql;}
16+
STR_REF get_sql() const {return sql;}
1717
private:
1818
int code;
1919
std::string sql;
@@ -40,7 +40,7 @@ namespace sqlite {
4040
class more_statements: public sqlite_exception { using sqlite_exception::sqlite_exception; }; // Prepared statements can only contain one statement
4141
class invalid_utf16: public sqlite_exception { using sqlite_exception::sqlite_exception; };
4242

43-
static void throw_sqlite_error(const int& error_code, const std::string_view &sql = "") {
43+
static void throw_sqlite_error(const int& error_code, const STR_REF &sql = "") {
4444
switch(error_code & 0xFF) {
4545
#define SQLITE_MODERN_CPP_ERROR_CODE(NAME,name,derived) \
4646
case SQLITE_ ## NAME: switch(error_code) { \

hdr/sqlite_modern_cpp/type_wrapper.h

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@
44
#include <string>
55
#include <memory>
66
#include <vector>
7-
7+
#ifdef __cplusplus >= 201703 && __has_include(<string_view>)
8+
#define STR_REF std::string_view
9+
#define U16STR_REF std::u16string_view
10+
#else
11+
#define STR_REF std::string
12+
#define U16STR_REF std::u16string
13+
#endif
814
#ifdef __has_include
915
#if __cplusplus > 201402 && __has_include(<optional>)
1016
#define MODERN_SQLITE_STD_OPTIONAL_SUPPORT
@@ -150,17 +156,17 @@ namespace sqlite {
150156
sqlite3_result_null(db);
151157
}
152158

153-
// std::string_view
159+
// STR_REF
154160
template<>
155-
struct has_sqlite_type<std::string_view, SQLITE3_TEXT, void> : std::true_type {};
161+
struct has_sqlite_type<STR_REF, SQLITE3_TEXT, void> : std::true_type {};
156162
template<>
157163
struct has_sqlite_type<std::string, SQLITE3_TEXT, void> : std::true_type {};
158-
inline int bind_col_in_db(sqlite3_stmt* stmt, int inx, const std::string_view& val) {
164+
inline int bind_col_in_db(sqlite3_stmt* stmt, int inx, const STR_REF& val) {
159165
return sqlite3_bind_text(stmt, inx, val.data(), -1, SQLITE_TRANSIENT);
160166
}
161167

162-
// Convert char* to string_view to trigger op<<(..., const std::string_view )
163-
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_view(STR, N-1)); }
168+
// Convert char* to string_view to trigger op<<(..., const STR_REF )
169+
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, STR_REF(STR, N-1)); }
164170

165171
inline std::string get_col_from_db(sqlite3_stmt* stmt, int inx, result_type<std::string>) {
166172
return sqlite3_column_type(stmt, inx) == SQLITE_NULL ? std::string() :
@@ -171,20 +177,20 @@ namespace sqlite {
171177
std::string(reinterpret_cast<char const *>(sqlite3_value_text(value)), sqlite3_value_bytes(value));
172178
}
173179

174-
inline void store_result_in_db(sqlite3_context* db, const std::string_view& val) {
180+
inline void store_result_in_db(sqlite3_context* db, const STR_REF& val) {
175181
sqlite3_result_text(db, val.data(), -1, SQLITE_TRANSIENT);
176182
}
177-
// std::u16string_view
183+
// U16STR_REF
178184
template<>
179-
struct has_sqlite_type<std::u16string_view, SQLITE3_TEXT, void> : std::true_type {};
185+
struct has_sqlite_type<U16STR_REF, SQLITE3_TEXT, void> : std::true_type {};
180186
template<>
181187
struct has_sqlite_type<std::u16string, SQLITE3_TEXT, void> : std::true_type {};
182-
inline int bind_col_in_db(sqlite3_stmt* stmt, int inx, const std::u16string_view& val) {
188+
inline int bind_col_in_db(sqlite3_stmt* stmt, int inx, const U16STR_REF& val) {
183189
return sqlite3_bind_text16(stmt, inx, val.data(), -1, SQLITE_TRANSIENT);
184190
}
185191

186-
// Convert char* to string_view to trigger op<<(..., const std::string_view )
187-
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_view(STR, N-1)); }
192+
// Convert char* to string_view to trigger op<<(..., const STR_REF )
193+
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, U16STR_REF(STR, N-1)); }
188194

189195
inline std::u16string get_col_from_db(sqlite3_stmt* stmt, int inx, result_type<std::u16string>) {
190196
return sqlite3_column_type(stmt, inx) == SQLITE_NULL ? std::u16string() :
@@ -195,7 +201,7 @@ namespace sqlite {
195201
std::u16string(reinterpret_cast<char16_t const *>(sqlite3_value_text16(value)), sqlite3_value_bytes16(value));
196202
}
197203

198-
inline void store_result_in_db(sqlite3_context* db, const std::u16string_view& val) {
204+
inline void store_result_in_db(sqlite3_context* db, const U16STR_REF& val) {
199205
sqlite3_result_text16(db, val.data(), -1, SQLITE_TRANSIENT);
200206
}
201207

0 commit comments

Comments
 (0)