diff --git a/plugins/input/sqlite/sqlite_datasource.cpp b/plugins/input/sqlite/sqlite_datasource.cpp index ce3f18aef..5c90bd75b 100644 --- a/plugins/input/sqlite/sqlite_datasource.cpp +++ b/plugins/input/sqlite/sqlite_datasource.cpp @@ -79,7 +79,6 @@ sqlite_datasource::sqlite_datasource(parameters const& params, bool bind) // TODO // - change param from 'file' to 'dbname' // - ensure that the supplied key_field is a valid "integer primary key" - // - move all intialization code to bind() boost::optional file = params_.get("file"); if (!file) throw datasource_exception("Sqlite Plugin: missing parameter"); @@ -88,65 +87,14 @@ sqlite_datasource::sqlite_datasource(parameters const& params, bool bind) throw mapnik::datasource_exception("Sqlite Plugin: missing parameter"); } - boost::optional key_field_name = params_.get("key_field"); - if (key_field_name) { - std::string const& key_field_string = *key_field_name; - if (key_field_string.empty()) { - key_field_ = "rowid"; - } else { - key_field_ = key_field_string; - } - } - else - { - key_field_ = "rowid"; - } - - boost::optional wkb = params_.get("wkb_format"); - if (wkb) - { - if (*wkb == "spatialite") - format_ = mapnik::wkbSpatiaLite; - } - - multiple_geometries_ = *params_.get("multiple_geometries",false); - use_spatial_index_ = *params_.get("use_spatial_index",true); - has_spatial_index_ = false; - using_subquery_ = false; - - boost::optional ext = params_.get("extent"); - if (ext) extent_initialized_ = extent_.from_string(*ext); - - boost::optional base = params_.get("base"); - if (base) - dataset_name_ = *base + "/" + *file; - else - dataset_name_ = *file; - - // Populate init_statements_ - // 1. Build attach database statements from the "attachdb" parameter - // 2. Add explicit init statements from "initdb" parameter - // Note that we do some extra work to make sure that any attached - // databases are relative to directory containing dataset_name_. Sqlite - // will default to attaching from cwd. Typicaly usage means that the - // map loader will produce full paths here. - boost::optional attachdb = params_.get("attachdb"); - if (attachdb) { - parse_attachdb(*attachdb); - } - - boost::optional initdb = params_.get("initdb"); - if (initdb) { - init_statements_.push_back(*initdb); - } - if (bind) { this->bind(); } } -void sqlite_datasource::parse_attachdb(std::string const& attachdb) { +void sqlite_datasource::parse_attachdb(std::string const& attachdb) const +{ boost::char_separator sep(","); boost::tokenizer > tok(attachdb, sep); @@ -207,6 +155,61 @@ void sqlite_datasource::bind() const { if (is_bound_) return; + boost::optional file = params_.get("file"); + if (!file) throw datasource_exception("Sqlite Plugin: missing parameter"); + + boost::optional key_field_name = params_.get("key_field"); + if (key_field_name) { + std::string const& key_field_string = *key_field_name; + if (key_field_string.empty()) { + key_field_ = "rowid"; + } else { + key_field_ = key_field_string; + } + } + else + { + key_field_ = "rowid"; + } + + boost::optional wkb = params_.get("wkb_format"); + if (wkb) + { + if (*wkb == "spatialite") + format_ = mapnik::wkbSpatiaLite; + } + + multiple_geometries_ = *params_.get("multiple_geometries",false); + use_spatial_index_ = *params_.get("use_spatial_index",true); + has_spatial_index_ = false; + using_subquery_ = false; + + boost::optional ext = params_.get("extent"); + if (ext) extent_initialized_ = extent_.from_string(*ext); + + boost::optional base = params_.get("base"); + if (base) + dataset_name_ = *base + "/" + *file; + else + dataset_name_ = *file; + + // Populate init_statements_ + // 1. Build attach database statements from the "attachdb" parameter + // 2. Add explicit init statements from "initdb" parameter + // Note that we do some extra work to make sure that any attached + // databases are relative to directory containing dataset_name_. Sqlite + // will default to attaching from cwd. Typicaly usage means that the + // map loader will produce full paths here. + boost::optional attachdb = params_.get("attachdb"); + if (attachdb) { + parse_attachdb(*attachdb); + } + + boost::optional initdb = params_.get("initdb"); + if (initdb) { + init_statements_.push_back(*initdb); + } + if (!boost::filesystem::exists(dataset_name_)) throw datasource_exception("Sqlite Plugin: " + dataset_name_ + " does not exist"); diff --git a/plugins/input/sqlite/sqlite_datasource.hpp b/plugins/input/sqlite/sqlite_datasource.hpp index 746f81e74..4f75d4560 100644 --- a/plugins/input/sqlite/sqlite_datasource.hpp +++ b/plugins/input/sqlite/sqlite_datasource.hpp @@ -53,7 +53,7 @@ class sqlite_datasource : public mapnik::datasource mutable mapnik::box2d extent_; mutable bool extent_initialized_; int type_; - std::string dataset_name_; + mutable std::string dataset_name_; mutable sqlite_connection* dataset_; std::string table_; std::string fields_; @@ -61,20 +61,20 @@ class sqlite_datasource : public mapnik::datasource mutable std::string geometry_table_; mutable std::string geometry_field_; mutable std::string index_table_; - std::string key_field_; - const int row_offset_; - const int row_limit_; + mutable std::string key_field_; + mutable int row_offset_; + mutable int row_limit_; mutable mapnik::layer_descriptor desc_; - mapnik::wkbFormat format_; - bool multiple_geometries_; + mutable mapnik::wkbFormat format_; + mutable bool multiple_geometries_; mutable bool use_spatial_index_; mutable bool has_spatial_index_; mutable bool using_subquery_; - std::vector init_statements_; + mutable std::vector init_statements_; // Fill init_statements with any statements // needed to attach auxillary databases - void parse_attachdb(std::string const& attachdb); + void parse_attachdb(std::string const& attachdb) const; };