-
Notifications
You must be signed in to change notification settings - Fork 160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use std::string_view as argument instead of std::string on C++17 Compilers #167
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
64c40af
C++17
jimmyjxiao 2f2e7d8
Fix compilation on MSVC/Windows
jimmyjxiao 5515487
Fix Modern C++ detection on MSVC
jimmyjxiao fd3f0bf
String_view arguments instead of string
jimmyjxiao c69ffc1
Fallback to traditional string on pre-c++17 compilers
jimmyjxiao e0e3a22
Looks like String.h doesn't include string_view.h, maybe this will fi…
jimmyjxiao 6c95d0a
Fix preprocessor directives
jimmyjxiao 3b181fe
has_sqlite_type specifically for stringviews is unnecessary, it looks…
jimmyjxiao b9ddb6b
Original SQL should have string, not string_view return type.
jimmyjxiao b08f6b6
Reuse UTF-8 prepare function, and add length to prepare call
jimmyjxiao 080411a
Switch back to string parameters for database constructor
jimmyjxiao 64f5f66
Function definitions back to std::string
jimmyjxiao 9e56736
Sizes for string_view functions
jimmyjxiao a1849e2
Change preprocessor options for string_view to be more consistent wit…
jimmyjxiao fac3344
Switch to typedef instead of define
jimmyjxiao 8e09803
Pass string_view by value
jimmyjxiao 7d6ea22
Small Changes
jimmyjxiao c03e4e8
Add tests from string_view
jimmyjxiao b12f949
Update Readme
jimmyjxiao dffc090
oops
jimmyjxiao b0e3c8d
Sqlite takes bind text16 sizes in bytes
jimmyjxiao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -85,15 +85,15 @@ namespace sqlite { | |
return ++_inx; | ||
} | ||
|
||
sqlite3_stmt* _prepare(const std::u16string& sql) { | ||
sqlite3_stmt* _prepare(u16str_ref sql) { | ||
return _prepare(utility::utf16_to_utf8(sql)); | ||
} | ||
|
||
sqlite3_stmt* _prepare(const std::string& sql) { | ||
sqlite3_stmt* _prepare(str_ref sql) { | ||
int hresult; | ||
sqlite3_stmt* tmp = nullptr; | ||
const char *remaining; | ||
hresult = sqlite3_prepare_v2(_db.get(), sql.data(), -1, &tmp, &remaining); | ||
hresult = sqlite3_prepare_v2(_db.get(), sql.data(), sql.length(), &tmp, &remaining); | ||
if(hresult != SQLITE_OK) errors::throw_sqlite_error(hresult, sql); | ||
if(!std::all_of(remaining, sql.data() + sql.size(), [](char ch) {return std::isspace(ch);})) | ||
throw errors::more_statements("Multiple semicolon separated statements are unsupported", sql); | ||
|
@@ -105,13 +105,13 @@ namespace sqlite { | |
|
||
public: | ||
|
||
database_binder(std::shared_ptr<sqlite3> db, std::u16string const & sql): | ||
database_binder(std::shared_ptr<sqlite3> db, u16str_ref sql): | ||
_db(db), | ||
_stmt(_prepare(sql), sqlite3_finalize), | ||
_inx(0) { | ||
} | ||
|
||
database_binder(std::shared_ptr<sqlite3> db, std::string const & sql): | ||
database_binder(std::shared_ptr<sqlite3> db, str_ref sql): | ||
_db(db), | ||
_stmt(_prepare(sql), sqlite3_finalize), | ||
_inx(0) { | ||
|
@@ -372,36 +372,22 @@ namespace sqlite { | |
*this << R"(PRAGMA encoding = "UTF-16";)"; | ||
} | ||
|
||
database(const std::u16string &db_name, const sqlite_config &config = {}): _db(nullptr) { | ||
auto db_name_utf8 = utility::utf16_to_utf8(db_name); | ||
sqlite3* tmp = nullptr; | ||
auto ret = sqlite3_open_v2(db_name_utf8.data(), &tmp, static_cast<int>(config.flags), config.zVfs); | ||
_db = std::shared_ptr<sqlite3>(tmp, [=](sqlite3* ptr) { sqlite3_close_v2(ptr); }); // this will close the connection eventually when no longer needed. | ||
if(ret != SQLITE_OK) errors::throw_sqlite_error(_db ? sqlite3_extended_errcode(_db.get()) : ret); | ||
sqlite3_extended_result_codes(_db.get(), true); | ||
if(config.encoding != Encoding::UTF8) | ||
database(const std::u16string &db_name, const sqlite_config &config = {}): database(utility::utf16_to_utf8(db_name), config) { | ||
if (config.encoding == Encoding::ANY) | ||
*this << R"(PRAGMA encoding = "UTF-16";)"; | ||
} | ||
|
||
database(std::shared_ptr<sqlite3> db): | ||
_db(db) {} | ||
|
||
database_binder operator<<(const std::string& sql) { | ||
database_binder operator<<(str_ref sql) { | ||
return database_binder(_db, sql); | ||
} | ||
|
||
database_binder operator<<(const char* sql) { | ||
return *this << std::string(sql); | ||
} | ||
|
||
database_binder operator<<(const std::u16string& sql) { | ||
database_binder operator<<(u16str_ref sql) { | ||
return database_binder(_db, sql); | ||
} | ||
|
||
database_binder operator<<(const char16_t* sql) { | ||
return *this << std::u16string(sql); | ||
} | ||
|
||
connection_type connection() const { return _db; } | ||
|
||
sqlite3_int64 last_insert_rowid() const { | ||
|
@@ -418,7 +404,7 @@ namespace sqlite { | |
|
||
auto funcPtr = new auto(std::forward<Function>(func)); | ||
if(int result = sqlite3_create_function_v2( | ||
_db.get(), name.c_str(), traits::arity, SQLITE_UTF8, funcPtr, | ||
_db.get(), name.data(), traits::arity, SQLITE_UTF8, funcPtr, | ||
This comment was marked as resolved.
Sorry, something went wrong. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should stick to |
||
sql_function_binder::scalar<traits::arity, typename std::remove_reference<Function>::type>, | ||
nullptr, nullptr, [](void* ptr){ | ||
delete static_cast<decltype(funcPtr)>(ptr); | ||
|
@@ -652,3 +638,4 @@ namespace sqlite { | |
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#include <iostream> | ||
#include <cstdlib> | ||
#include <sqlite_modern_cpp.h> | ||
#include <catch.hpp> | ||
|
||
|
||
#ifdef MODERN_SQLITE_STRINGVIEW_SUPPORT | ||
#include <string_view> | ||
|
||
using namespace sqlite; | ||
using namespace std; | ||
TEST_CASE("std::string_view works", "[string_view]") { | ||
database db(":memory:");; | ||
db << "CREATE TABLE foo (a integer, b string);\n"; | ||
const std::string_view test1 = "null terminated string view"; | ||
db << "INSERT INTO foo VALUES (?, ?)" << 1 << test1; | ||
std::string str; | ||
db << "SELECT b from FOO where a=?;" << 1 >> str; | ||
REQUIRE(test1 == str); | ||
const char s[] = "hello world"; | ||
std::string_view test2(&s[0], 2); | ||
db << "INSERT INTO foo VALUES (?,?)" << 2 << test2; | ||
db << "SELECT b from FOO where a=?" << 2 >> str; | ||
REQUIRE(str == "he"); | ||
} | ||
#endif |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We still support C++14, so it would make sense to keep this set to 14. Especially since our Travis compiler is so old that it does not support most C++17 features anyway.