diff options
author | Bruno de Oliveira Abinader <bruno@mapbox.com> | 2017-01-18 16:13:06 +0100 |
---|---|---|
committer | Bruno de Oliveira Abinader <bruno@mapbox.com> | 2017-01-23 19:06:53 +0200 |
commit | 00b4baff5f14e37703f893473bd1c78778b9f63f (patch) | |
tree | 92f7bf152377aa80b3a0b21a57b44301c63b8c51 /platform/qt | |
parent | 1c759cfa09480058243c1896e1340ee06b3f2e14 (diff) | |
download | qtlocation-mapboxgl-00b4baff5f14e37703f893473bd1c78778b9f63f.tar.gz |
[Qt] Check for Qt SQLite database driver
Diffstat (limited to 'platform/qt')
-rw-r--r-- | platform/qt/src/sqlite3.cpp | 41 |
1 files changed, 21 insertions, 20 deletions
diff --git a/platform/qt/src/sqlite3.cpp b/platform/qt/src/sqlite3.cpp index 00ad178fdf..367b51811d 100644 --- a/platform/qt/src/sqlite3.cpp +++ b/platform/qt/src/sqlite3.cpp @@ -17,8 +17,6 @@ #include <mbgl/util/string.hpp> #include <mbgl/util/traits.hpp> -static uint32_t count = 0; - namespace mapbox { namespace sqlite { @@ -53,9 +51,12 @@ void checkDatabaseError(const QSqlDatabase &db) { class DatabaseImpl { public: - DatabaseImpl(const char* filename, int flags) - : db(QSqlDatabase::addDatabase("QSQLITE", QString::fromStdString(mbgl::util::toString(count++)))) { - QString connectOptions = db.connectOptions(); + DatabaseImpl(const char* filename, int flags) { + if (!QSqlDatabase::drivers().contains("QSQLITE")) { + throw Exception { Exception::Code::CANTOPEN, "SQLite driver not found." }; + } + db.reset(new QSqlDatabase(QSqlDatabase::addDatabase("QSQLITE", QString::number(qrand())))); + QString connectOptions = db->connectOptions(); if (flags & OpenFlag::ReadOnly) { if (!connectOptions.isEmpty()) connectOptions.append(';'); connectOptions.append("QSQLITE_OPEN_READONLY"); @@ -65,20 +66,20 @@ public: connectOptions.append("QSQLITE_ENABLE_SHARED_CACHE"); } - db.setConnectOptions(connectOptions); - db.setDatabaseName(QString(filename)); + db->setConnectOptions(connectOptions); + db->setDatabaseName(QString(filename)); - if (!db.open()) { - checkDatabaseError(db); + if (!db->open()) { + checkDatabaseError(*db); } } ~DatabaseImpl() { - db.close(); - checkDatabaseError(db); + db->close(); + checkDatabaseError(*db); } - QSqlDatabase db; + QScopedPointer<QSqlDatabase> db; }; class StatementImpl { @@ -129,17 +130,17 @@ Database::operator bool() const { void Database::setBusyTimeout(std::chrono::milliseconds timeout) { assert(impl); std::string timeoutStr = mbgl::util::toString(timeout.count()); - QString connectOptions = impl->db.connectOptions(); + QString connectOptions = impl->db->connectOptions(); if (connectOptions.isEmpty()) { if (!connectOptions.isEmpty()) connectOptions.append(';'); connectOptions.append("QSQLITE_BUSY_TIMEOUT=").append(QString::fromStdString(timeoutStr)); } - if (impl->db.isOpen()) { - impl->db.close(); + if (impl->db->isOpen()) { + impl->db->close(); } - impl->db.setConnectOptions(connectOptions); - if (!impl->db.open()) { - checkDatabaseError(impl->db); + impl->db->setConnectOptions(connectOptions); + if (!impl->db->open()) { + checkDatabaseError(*impl->db); } } @@ -151,7 +152,7 @@ void Database::exec(const std::string &sql) { if (!statement.endsWith(';')) { statement.append(';'); } - QSqlQuery query(impl->db); + QSqlQuery query(*impl->db); query.setForwardOnly(true); query.prepare(statement); if (!query.exec()) { @@ -165,7 +166,7 @@ Statement Database::prepare(const char *query) { } Statement::Statement(Database *db, const char *sql) - : impl(std::make_unique<StatementImpl>(QString(sql), db->impl->db)) { + : impl(std::make_unique<StatementImpl>(QString(sql), *db->impl->db)) { assert(impl); } |