@@ -42,7 +42,7 @@ namespace sqlite {
4242
4343 void execute ();
4444
45- std::string sql () {
45+ std::string_view 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 original_sql () {
55+ std::string_view original_sql () {
5656 return sqlite3_sql (_stmt.get ());
5757 }
5858
@@ -85,11 +85,19 @@ namespace sqlite {
8585 return ++_inx;
8686 }
8787
88- sqlite3_stmt* _prepare (const std::u16string& sql) {
89- return _prepare (utility::utf16_to_utf8 (sql));
88+ sqlite3_stmt* _prepare (const std::u16string_view& sql) {
89+ // return _prepare(utility::utf16_to_utf8(sql));
90+ int hresult;
91+ sqlite3_stmt* tmp = nullptr ;
92+ const void *remaining;
93+ hresult = sqlite3_prepare16_v2 (_db.get (), sql.data (), -1 , &tmp, &remaining);
94+ if (hresult != SQLITE_OK) errors::throw_sqlite_error (hresult, utility::utf16_to_utf8 (sql.data ()));
95+ if (!std::all_of (static_cast <const char16_t *>(remaining), sql.data () + sql.size (), [](char16_t ch) {return std::isspace (ch); }))
96+ throw errors::more_statements (" Multiple semicolon separated statements are unsupported" , utility::utf16_to_utf8 (sql.data ()));
97+ return tmp;
9098 }
9199
92- sqlite3_stmt* _prepare (const std::string & sql) {
100+ sqlite3_stmt* _prepare (const std::string_view & sql) {
93101 int hresult;
94102 sqlite3_stmt* tmp = nullptr ;
95103 const char *remaining;
@@ -105,13 +113,13 @@ namespace sqlite {
105113
106114 public:
107115
108- database_binder (std::shared_ptr<sqlite3> db, std::u16string const & sql):
116+ database_binder (std::shared_ptr<sqlite3> db, std::u16string_view const & sql):
109117 _db (db),
110118 _stmt (_prepare(sql), sqlite3_finalize),
111119 _inx (0 ) {
112120 }
113121
114- database_binder (std::shared_ptr<sqlite3> db, std::string const & sql):
122+ database_binder (std::shared_ptr<sqlite3> db, std::string_view const & sql):
115123 _db (db),
116124 _stmt (_prepare(sql), sqlite3_finalize),
117125 _inx (0 ) {
@@ -362,7 +370,7 @@ namespace sqlite {
362370 std::shared_ptr<sqlite3> _db;
363371
364372 public:
365- database (const std::string &db_name, const sqlite_config &config = {}): _db(nullptr ) {
373+ database (const std::string_view &db_name, const sqlite_config &config = {}): _db(nullptr ) {
366374 sqlite3* tmp = nullptr ;
367375 auto ret = sqlite3_open_v2 (db_name.data (), &tmp, static_cast <int >(config.flags ), config.zVfs );
368376 _db = std::shared_ptr<sqlite3>(tmp, [=](sqlite3* ptr) { sqlite3_close_v2 (ptr); }); // this will close the connection eventually when no longer needed.
@@ -372,8 +380,8 @@ namespace sqlite {
372380 *this << R"( PRAGMA encoding = "UTF-16";)" ;
373381 }
374382
375- database (const std::u16string &db_name, const sqlite_config &config = {}): _db(nullptr ) {
376- auto db_name_utf8 = utility::utf16_to_utf8 (db_name);
383+ database (const std::u16string_view &db_name, const sqlite_config &config = {}): _db(nullptr ) {
384+ auto db_name_utf8 = utility::utf16_to_utf8 (db_name. data () );
377385 sqlite3* tmp = nullptr ;
378386 auto ret = sqlite3_open_v2 (db_name_utf8.data (), &tmp, static_cast <int >(config.flags ), config.zVfs );
379387 _db = std::shared_ptr<sqlite3>(tmp, [=](sqlite3* ptr) { sqlite3_close_v2 (ptr); }); // this will close the connection eventually when no longer needed.
@@ -386,20 +394,20 @@ namespace sqlite {
386394 database (std::shared_ptr<sqlite3> db):
387395 _db (db) {}
388396
389- database_binder operator <<(const std::string & sql) {
397+ database_binder operator <<(const std::string_view & sql) {
390398 return database_binder (_db, sql);
391399 }
392400
393401 database_binder operator <<(const char * sql) {
394- return *this << std::string (sql);
402+ return *this << std::string_view (sql);
395403 }
396404
397- database_binder operator <<(const std::u16string & sql) {
405+ database_binder operator <<(const std::u16string_view & sql) {
398406 return database_binder (_db, sql);
399407 }
400408
401409 database_binder operator <<(const char16_t * sql) {
402- return *this << std::u16string (sql);
410+ return *this << std::u16string_view (sql);
403411 }
404412
405413 connection_type connection () const { return _db; }
@@ -413,12 +421,12 @@ namespace sqlite {
413421 }
414422
415423 template <typename Function>
416- void define (const std::string &name, Function&& func) {
424+ void define (const std::string_view &name, Function&& func) {
417425 typedef utility::function_traits<Function> traits;
418426
419427 auto funcPtr = new auto (std::forward<Function>(func));
420428 if (int result = sqlite3_create_function_v2 (
421- _db.get (), name.c_str (), traits::arity, SQLITE_UTF8, funcPtr,
429+ _db.get (), name.data (), traits::arity, SQLITE_UTF8, funcPtr,
422430 sql_function_binder::scalar<traits::arity, typename std::remove_reference<Function>::type>,
423431 nullptr , nullptr , [](void * ptr){
424432 delete static_cast <decltype (funcPtr)>(ptr);
@@ -427,13 +435,13 @@ namespace sqlite {
427435 }
428436
429437 template <typename StepFunction, typename FinalFunction>
430- void define (const std::string &name, StepFunction&& step, FinalFunction&& final ) {
438+ void define (const std::string_view &name, StepFunction&& step, FinalFunction&& final ) {
431439 typedef utility::function_traits<StepFunction> traits;
432440 using ContextType = typename std::remove_reference<typename traits::template argument<0 >>::type;
433441
434442 auto funcPtr = new auto (std::make_pair (std::forward<StepFunction>(step), std::forward<FinalFunction>(final )));
435443 if (int result = sqlite3_create_function_v2 (
436- _db.get (), name.c_str (), traits::arity - 1 , SQLITE_UTF8, funcPtr, nullptr ,
444+ _db.get (), name.data (), traits::arity - 1 , SQLITE_UTF8, funcPtr, nullptr ,
437445 sql_function_binder::step<ContextType, traits::arity, typename std::remove_reference<decltype (*funcPtr)>::type>,
438446 sql_function_binder::final <ContextType, typename std::remove_reference<decltype (*funcPtr)>::type>,
439447 [](void * ptr){
0 commit comments