Skip to content

Commit

Permalink
Open SQLite files read-only, improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Nakaner committed Mar 25, 2024
1 parent f220e17 commit 728da2e
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/mbtiles_vector_datasource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,11 @@ void mbtiles_vector_datasource::init(mapnik::parameters const& params)
throw mapnik::datasource_exception("MBTiles Plugin: parameter 'layer' is missing.");
}
layer_ = layer.get();
dataset_ = std::make_shared<sqlite_connection>(database_path_);
int sqlite_mode = SQLITE_OPEN_READONLY | SQLITE_OPEN_NOMUTEX | SQLITE_OPEN_SHAREDCACHE;
dataset_ = std::make_shared<sqlite_connection>(database_path_, sqlite_mode);
// Ensure that the tileset contains vector tiles
std::shared_ptr<sqlite_resultset> result (dataset_->execute_query("SELECT value FROM metadata WHERE name = 'format';"));
if (result->is_valid() && result->step_next() && result->column_type(0) == SQLITE_TEXT && strcmp(result->column_text(0), "pbf"))
if (!result->is_valid() || !result->step_next() || result->column_type(0) != SQLITE_TEXT || strcmp(result->column_text(0), "pbf"))
{
throw mapnik::datasource_exception("MBTiles Plugin: " + database_path_ + " has unsupported vector tile format, expected 'pbf'.");
}
Expand All @@ -130,7 +131,11 @@ void mbtiles_vector_datasource::init(mapnik::parameters const& params)
result = dataset_->execute_query("SELECT value FROM metadata WHERE name = 'bounds';");
if (result->is_valid() && result->step_next() && result->column_type(0) == SQLITE_TEXT)
{
extent_.from_string(result->column_text(0));
const std::string extent_str = result->column_text(0);
if (extent_str.empty()) {
throw mapnik::datasource_exception("MBTiles Plugin: " + database_path_ + " has invalid extent.");
}
extent_.from_string(extent_str);
}
}
if (!extent_.valid())
Expand Down

0 comments on commit 728da2e

Please sign in to comment.