sqlite_modern_cpp
sqlite_modern_cpp copied to clipboard
std::string_view + other non null-terminated input support for insertion
Right now there is such thing in the implementation:
// Convert char* to string to trigger op<<(..., const std::string )
template<std::size_t N> inline database_binder& operator <<(database_binder& db, const char(&STR)[N]) { return db << std::string(STR); }
template<std::size_t N> inline database_binder& operator <<(database_binder& db, const char16_t(&STR)[N]) { return db << std::u16string(STR); }
inline database_binder& operator <<(database_binder& db, const std::string& txt) {
int hresult;
if((hresult = sqlite3_bind_text(db._stmt.get(), db._next_index(), txt.data(), -1, SQLITE_TRANSIENT)) != SQLITE_OK) {
errors::throw_sqlite_error(hresult, db.sql());
}
return db;
}
The implementation builds temporary strings (unneded allocation) and loses the information on their length by passing -1
.
This looks like a simple thing to fix - while also giving the opportunity to add support for non-null-terminated strings such as std::string_view
. I propose database_binder::bind_next(T* ptr, int length)
.