Skip to content

Commit 8e09803

Browse files
committed
Pass string_view by value
1 parent fac3344 commit 8e09803

File tree

2 files changed

+21
-26
lines changed

2 files changed

+21
-26
lines changed

hdr/sqlite_modern_cpp.h

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,11 @@ namespace sqlite {
8585
return ++_inx;
8686
}
8787

88-
sqlite3_stmt* _prepare(const U16STR_REF& sql) {
88+
sqlite3_stmt* _prepare(U16STR_REF sql) {
8989
return _prepare(utility::utf16_to_utf8(sql));
9090
}
9191

92-
sqlite3_stmt* _prepare(const STR_REF& sql) {
92+
sqlite3_stmt* _prepare(STR_REF sql) {
9393
int hresult;
9494
sqlite3_stmt* tmp = nullptr;
9595
const char *remaining;
@@ -105,13 +105,13 @@ namespace sqlite {
105105

106106
public:
107107

108-
database_binder(std::shared_ptr<sqlite3> db, U16STR_REF const & sql):
108+
database_binder(std::shared_ptr<sqlite3> db, U16STR_REF sql):
109109
_db(db),
110110
_stmt(_prepare(sql), sqlite3_finalize),
111111
_inx(0) {
112112
}
113113

114-
database_binder(std::shared_ptr<sqlite3> db, STR_REF const & sql):
114+
database_binder(std::shared_ptr<sqlite3> db, STR_REF sql):
115115
_db(db),
116116
_stmt(_prepare(sql), sqlite3_finalize),
117117
_inx(0) {
@@ -380,22 +380,14 @@ namespace sqlite {
380380
database(std::shared_ptr<sqlite3> db):
381381
_db(db) {}
382382

383-
database_binder operator<<(const STR_REF& sql) {
383+
database_binder operator<<(STR_REF sql) {
384384
return database_binder(_db, sql);
385385
}
386386

387-
database_binder operator<<(const char* sql) {
388-
return *this << STR_REF(sql);
389-
}
390-
391-
database_binder operator<<(const U16STR_REF& sql) {
387+
database_binder operator<<(U16STR_REF sql) {
392388
return database_binder(_db, sql);
393389
}
394390

395-
database_binder operator<<(const char16_t* sql) {
396-
return *this << U16STR_REF(sql);
397-
}
398-
399391
connection_type connection() const { return _db; }
400392

401393
sqlite3_int64 last_insert_rowid() const {
@@ -646,5 +638,4 @@ namespace sqlite {
646638
}
647639
}
648640
}
649-
#undef STR_REF
650-
#undef U16STR_REF
641+

hdr/sqlite_modern_cpp/type_wrapper.h

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@
3737
#endif
3838
#ifdef MODERN_SQLITE_STRINGVIEW_SUPPORT
3939
#include <string_view>
40-
typedef std::string_view STR_REF;
41-
typedef std::u16string_view U16STR_REF;
40+
typedef const std::string_view STR_REF;
41+
typedef const std::u16string_view U16STR_REF;
4242
#else
43-
typedef std::string STR_REF;
44-
typedef std::u16string U16STR_REF;
43+
typedef const std::string& STR_REF;
44+
typedef const std::u16string& U16STR_REF;
4545
#endif
4646
#include <sqlite3.h>
4747
#include "errors.h"
@@ -164,12 +164,14 @@ namespace sqlite {
164164
// STR_REF
165165
template<>
166166
struct has_sqlite_type<std::string, SQLITE3_TEXT, void> : std::true_type {};
167-
inline int bind_col_in_db(sqlite3_stmt* stmt, int inx, const STR_REF& val) {
167+
inline int bind_col_in_db(sqlite3_stmt* stmt, int inx, STR_REF val) {
168168
return sqlite3_bind_text(stmt, inx, val.data(), val.length(), SQLITE_TRANSIENT);
169169
}
170170

171171
// Convert char* to string_view to trigger op<<(..., const STR_REF )
172-
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)); }
172+
template<std::size_t N> inline int bind_col_in_db(sqlite3_stmt* stmt, int inx, const char(&STR)[N]) {
173+
return sqlite3_bind_text(stmt, inx, &STR[0], N-1, SQLITE_TRANSIENT);
174+
}
173175

174176
inline std::string get_col_from_db(sqlite3_stmt* stmt, int inx, result_type<std::string>) {
175177
return sqlite3_column_type(stmt, inx) == SQLITE_NULL ? std::string() :
@@ -180,18 +182,20 @@ namespace sqlite {
180182
std::string(reinterpret_cast<char const *>(sqlite3_value_text(value)), sqlite3_value_bytes(value));
181183
}
182184

183-
inline void store_result_in_db(sqlite3_context* db, const STR_REF& val) {
185+
inline void store_result_in_db(sqlite3_context* db, STR_REF val) {
184186
sqlite3_result_text(db, val.data(), val.length(), SQLITE_TRANSIENT);
185187
}
186188
// U16STR_REF
187189
template<>
188190
struct has_sqlite_type<std::u16string, SQLITE3_TEXT, void> : std::true_type {};
189-
inline int bind_col_in_db(sqlite3_stmt* stmt, int inx, const U16STR_REF& val) {
191+
inline int bind_col_in_db(sqlite3_stmt* stmt, int inx, U16STR_REF val) {
190192
return sqlite3_bind_text16(stmt, inx, val.data(), sizeof(char16_t) * val.length(), SQLITE_TRANSIENT);
191193
}
192194

193195
// Convert char* to string_view to trigger op<<(..., const STR_REF )
194-
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)); }
196+
template<std::size_t N> inline int bind_col_in_db(sqlite3_stmt* stmt, int inx, const char16_t(&STR)[N]) {
197+
return sqlite3_bind_text16(stmt, inx, &STR[0], N-1, SQLITE_TRANSIENT);
198+
}
195199

196200
inline std::u16string get_col_from_db(sqlite3_stmt* stmt, int inx, result_type<std::u16string>) {
197201
return sqlite3_column_type(stmt, inx) == SQLITE_NULL ? std::u16string() :
@@ -202,7 +206,7 @@ namespace sqlite {
202206
std::u16string(reinterpret_cast<char16_t const *>(sqlite3_value_text16(value)), sqlite3_value_bytes16(value));
203207
}
204208

205-
inline void store_result_in_db(sqlite3_context* db, const U16STR_REF& val) {
209+
inline void store_result_in_db(sqlite3_context* db, U16STR_REF val) {
206210
sqlite3_result_text16(db, val.data(), sizeof(char16_t) * val.length(), SQLITE_TRANSIENT);
207211
}
208212

0 commit comments

Comments
 (0)