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:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user