Skip to content

Commit f9cf0fd

Browse files
authored
Merge pull request #83 from zauguin/utf8
Fix UTF-8 encoding of filenames and SQL statements
2 parents d008389 + 42dc385 commit f9cf0fd

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,12 @@ The usual way works for installing:
321321

322322
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.
323323

324+
Breaking Changes
325+
----
326+
327+
- Databases with non-ASCII characters in their names created with versions up to 2.4 are not found by the current master.
328+
You have to manually rename them to their actual (UTF-8 encoded) name.
329+
324330
Package managers
325331
----
326332
Pull requests are welcome :wink:

hdr/sqlite_modern_cpp.h

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ namespace sqlite {
133133

134134
database_binder(database_binder&& other) :
135135
_db(std::move(other._db)),
136-
_sql(std::move(other._sql)),
137136
_stmt(std::move(other._stmt)),
138137
_inx(other._inx), execution_started(other.execution_started) { }
139138

@@ -160,7 +159,6 @@ namespace sqlite {
160159

161160
private:
162161
std::shared_ptr<sqlite3> _db;
163-
std::u16string _sql;
164162
std::unique_ptr<sqlite3_stmt, decltype(&sqlite3_finalize)> _stmt;
165163

166164
int _inx;
@@ -209,6 +207,14 @@ namespace sqlite {
209207
return tmp;
210208
}
211209

210+
sqlite3_stmt* _prepare(const std::string& sql) {
211+
int hresult;
212+
sqlite3_stmt* tmp = nullptr;
213+
hresult = sqlite3_prepare_v2(_db.get(), sql.data(), -1, &tmp, nullptr);
214+
if((hresult) != SQLITE_OK) exceptions::throw_sqlite_error(hresult);
215+
return tmp;
216+
}
217+
212218
template <typename Type>
213219
struct is_sqlite_value : public std::integral_constant<
214220
bool,
@@ -266,13 +272,15 @@ namespace sqlite {
266272

267273
database_binder(std::shared_ptr<sqlite3> db, std::u16string const & sql):
268274
_db(db),
269-
_sql(sql),
270275
_stmt(_prepare(sql), sqlite3_finalize),
271276
_inx(1) {
272277
}
273278

274279
database_binder(std::shared_ptr<sqlite3> db, std::string const & sql):
275-
database_binder(db, std::u16string(sql.begin(), sql.end())) {}
280+
_db(db),
281+
_stmt(_prepare(sql), sqlite3_finalize),
282+
_inx(1) {
283+
}
276284

277285
~database_binder() noexcept(false) {
278286
/* Will be executed if no >>op is found, but not if an exception
@@ -318,13 +326,16 @@ namespace sqlite {
318326
auto ret = sqlite3_open16(db_name.data(), &tmp);
319327
_db = std::shared_ptr<sqlite3>(tmp, [=](sqlite3* ptr) { sqlite3_close_v2(ptr); }); // this will close the connection eventually when no longer needed.
320328
if(ret != SQLITE_OK) exceptions::throw_sqlite_error(ret);
321-
322-
323329
//_db.reset(tmp, sqlite3_close); // alternative close. (faster?)
324330
}
325331

326-
database(std::string const & db_name):
327-
database(std::u16string(db_name.begin(), db_name.end())) {}
332+
database(std::string const & db_name): _db(nullptr) {
333+
sqlite3* tmp = nullptr;
334+
auto ret = sqlite3_open(db_name.data(), &tmp);
335+
_db = std::shared_ptr<sqlite3>(tmp, [=](sqlite3* ptr) { sqlite3_close_v2(ptr); }); // this will close the connection eventually when no longer needed.
336+
if(ret != SQLITE_OK) exceptions::throw_sqlite_error(ret);
337+
//_db.reset(tmp, sqlite3_close); // alternative close. (faster?)
338+
}
328339

329340
database(std::shared_ptr<sqlite3> db):
330341
_db(db) {}

0 commit comments

Comments
 (0)