Skip to content

Commit

Permalink
Merge pull request #83 from zauguin/utf8
Browse files Browse the repository at this point in the history
Fix UTF-8 encoding of filenames and SQL statements
  • Loading branch information
zauguin authored Feb 13, 2017
2 parents d008389 + 42dc385 commit f9cf0fd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,12 @@ The usual way works for installing:

Note, there's nothing to make, so you there's no need to run configure and you can simply point your compiler at the hdr/ directory.

Breaking Changes
----

- Databases with non-ASCII characters in their names created with versions up to 2.4 are not found by the current master.
You have to manually rename them to their actual (UTF-8 encoded) name.

Package managers
----
Pull requests are welcome :wink:
Expand Down
27 changes: 19 additions & 8 deletions hdr/sqlite_modern_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ namespace sqlite {

database_binder(database_binder&& other) :
_db(std::move(other._db)),
_sql(std::move(other._sql)),
_stmt(std::move(other._stmt)),
_inx(other._inx), execution_started(other.execution_started) { }

Expand All @@ -160,7 +159,6 @@ namespace sqlite {

private:
std::shared_ptr<sqlite3> _db;
std::u16string _sql;
std::unique_ptr<sqlite3_stmt, decltype(&sqlite3_finalize)> _stmt;

int _inx;
Expand Down Expand Up @@ -209,6 +207,14 @@ namespace sqlite {
return tmp;
}

sqlite3_stmt* _prepare(const std::string& sql) {
int hresult;
sqlite3_stmt* tmp = nullptr;
hresult = sqlite3_prepare_v2(_db.get(), sql.data(), -1, &tmp, nullptr);
if((hresult) != SQLITE_OK) exceptions::throw_sqlite_error(hresult);
return tmp;
}

template <typename Type>
struct is_sqlite_value : public std::integral_constant<
bool,
Expand Down Expand Up @@ -266,13 +272,15 @@ namespace sqlite {

database_binder(std::shared_ptr<sqlite3> db, std::u16string const & sql):
_db(db),
_sql(sql),
_stmt(_prepare(sql), sqlite3_finalize),
_inx(1) {
}

database_binder(std::shared_ptr<sqlite3> db, std::string const & sql):
database_binder(db, std::u16string(sql.begin(), sql.end())) {}
_db(db),
_stmt(_prepare(sql), sqlite3_finalize),
_inx(1) {
}

~database_binder() noexcept(false) {
/* Will be executed if no >>op is found, but not if an exception
Expand Down Expand Up @@ -318,13 +326,16 @@ namespace sqlite {
auto ret = sqlite3_open16(db_name.data(), &tmp);
_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) exceptions::throw_sqlite_error(ret);


//_db.reset(tmp, sqlite3_close); // alternative close. (faster?)
}

database(std::string const & db_name):
database(std::u16string(db_name.begin(), db_name.end())) {}
database(std::string const & db_name): _db(nullptr) {
sqlite3* tmp = nullptr;
auto ret = sqlite3_open(db_name.data(), &tmp);
_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) exceptions::throw_sqlite_error(ret);
//_db.reset(tmp, sqlite3_close); // alternative close. (faster?)
}

database(std::shared_ptr<sqlite3> db):
_db(db) {}
Expand Down

0 comments on commit f9cf0fd

Please sign in to comment.