Avoid exceptions for flow control during database creation

Unfortuntely, it's difficult to avoid all exceptions, because sqlite3_open_v2 does not reliably return SQLITE_NOTADB if the file is not a database. However, this should avoid cases where developers misinterpret the SQLITE_CANTOPEN exception as a crash, which is the common case.
This commit is contained in:
John Firebaugh
2018-05-08 10:40:35 -07:00
parent d1dff1d6e7
commit a1195f6a9d
7 changed files with 137 additions and 102 deletions

View File

@@ -6,7 +6,7 @@
TEST(SQLite, Statement) {
using namespace mbgl;
mapbox::sqlite::Database db(":memory:", mapbox::sqlite::Create | mapbox::sqlite::ReadWrite);
mapbox::sqlite::Database db = mapbox::sqlite::Database::open(":memory:", mapbox::sqlite::Create | mapbox::sqlite::ReadWrite);
db.exec("CREATE TABLE test (id INTEGER);");
mapbox::sqlite::Statement stmt1{ db, "INSERT INTO test (id) VALUES (?1);" };
@@ -28,13 +28,10 @@ TEST(SQLite, Statement) {
ASSERT_EQ(query2.changes(), 1u);
}
TEST(SQLite, TEST_REQUIRES_WRITE(CantOpenException)) {
try {
// Should throw a CANTOPEN when the database doesn't exist,
// make sure all the backends behave the same way.
mapbox::sqlite::Database("test/fixtures/offline_database/foobar123.db", mapbox::sqlite::ReadOnly);
FAIL();
} catch (mapbox::sqlite::Exception& ex) {
ASSERT_EQ(ex.code, mapbox::sqlite::ResultCode::CantOpen);
}
TEST(SQLite, TEST_REQUIRES_WRITE(TryOpen)) {
// Should return a CANTOPEN exception when the database doesn't exist,
// make sure all the backends behave the same way.
auto result = mapbox::sqlite::Database::tryOpen("test/fixtures/offline_database/foobar123.db", mapbox::sqlite::ReadOnly);
ASSERT_TRUE(result.is<mapbox::sqlite::Exception>());
ASSERT_EQ(result.get<mapbox::sqlite::Exception>().code, mapbox::sqlite::ResultCode::CantOpen);
}