diff options
author | Florian Bruhin <me@the-compiler.org> | 2018-09-12 10:04:39 +0200 |
---|---|---|
committer | Florian Bruhin <qt-project.org@the-compiler.org> | 2018-09-13 18:18:47 +0000 |
commit | 49efea26a5fae8c2275999c36c7c8d24cf4125de (patch) | |
tree | 88a829c298dc72a5938e0ec7c51b62877375dd22 /tests | |
parent | 45c1473847ad65c4d43f9a605a86439867442883 (diff) |
sqlite: Fix QSqlError handling when opening/closing database
Both sqlite3_open_v2 and sqlite3_close are documented to return an error code:
https://www.sqlite.org/c3ref/open.html https://sqlite.org/c3ref/close.html
However, those were ignored (other than checking whether the operation
succeeded), causing QSqlError::nativeErrorCode() to always be "-1" when there
was an error while opening/closing the database.
Additionally, the error string needs to be read (via sqlite3_errmsg16) in
qMakeError *before* d->access is set to 0, or the databaseText() will always be
"out of memory" no matter what error actually happened.
Task-number: QTBUG-70506
Change-Id: I75cbf178c9711442e640afd26c4502214d20c598
Reviewed-by: Andy Shaw <andy.shaw@qt.io>
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/sql/kernel/qsqldatabase/tst_qsqldatabase.cpp | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/tests/auto/sql/kernel/qsqldatabase/tst_qsqldatabase.cpp b/tests/auto/sql/kernel/qsqldatabase/tst_qsqldatabase.cpp index 1f055e9c33..8cf43e243b 100644 --- a/tests/auto/sql/kernel/qsqldatabase/tst_qsqldatabase.cpp +++ b/tests/auto/sql/kernel/qsqldatabase/tst_qsqldatabase.cpp @@ -197,6 +197,8 @@ private slots: void sqlite_enableRegexp_data() { generic_data("QSQLITE"); } void sqlite_enableRegexp(); + void sqlite_openError(); + private: void createTestTables(QSqlDatabase db); void dropTestTables(QSqlDatabase db); @@ -2332,6 +2334,22 @@ void tst_QSqlDatabase::sqlite_enableRegexp() QFAIL_SQL(q, next()); } +void tst_QSqlDatabase::sqlite_openError() +{ + // see QTBUG-70506 + if (!QSqlDatabase::drivers().contains("QSQLITE")) + QSKIP("Database driver QSQLITE not available"); + + QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "sqlite_openError"); + db.setDatabaseName("/doesnotexist/foo.sqlite"); + QVERIFY(db.isValid()); + + QVERIFY(!db.open()); + QSqlError error = db.lastError(); + QCOMPARE(error.nativeErrorCode(), "14"); // SQLITE_CANTOPEN + QCOMPARE(error.databaseText(), "unable to open database file"); +} + void tst_QSqlDatabase::cloneDatabase() { QFETCH(QString, dbName); |