- aligned coding style in sqlite plugin

This commit is contained in:
kunitoki 2011-10-22 14:50:24 +02:00
parent f1460b78cd
commit c0273234b6
5 changed files with 178 additions and 192 deletions

View file

@ -2,7 +2,7 @@
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2010 Artem Pavlenko
* Copyright (C) 2011 Artem Pavlenko
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@ -19,7 +19,6 @@
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*****************************************************************************/
// $Id$
#include "sqlite_datasource.hpp"
#include "sqlite_featureset.hpp"
@ -27,9 +26,7 @@
// mapnik
#include <mapnik/ptree_helpers.hpp>
#include <mapnik/sql_utils.hpp>
// to enable extent fallback hack
#include <mapnik/feature_factory.hpp>
#include <mapnik/feature_factory.hpp> // to enable extent fallback hack
// boost
#include <boost/algorithm/string.hpp>
@ -39,15 +36,9 @@
#include <boost/filesystem/operations.hpp>
#include <boost/make_shared.hpp>
using boost::lexical_cast;
using boost::bad_lexical_cast;
using mapnik::datasource;
using mapnik::parameters;
DATASOURCE_PLUGIN(sqlite_datasource)
using mapnik::box2d;
using mapnik::coord2d;
using mapnik::query;
@ -55,31 +46,34 @@ using mapnik::featureset_ptr;
using mapnik::layer_descriptor;
using mapnik::attribute_descriptor;
using mapnik::datasource_exception;
using mapnik::datasource;
using mapnik::parameters;
DATASOURCE_PLUGIN(sqlite_datasource)
sqlite_datasource::sqlite_datasource(parameters const& params, bool bind)
: datasource(params),
extent_(),
extent_initialized_(false),
type_(datasource::Vector),
table_(*params_.get<std::string>("table","")),
fields_(*params_.get<std::string>("fields","*")),
metadata_(*params_.get<std::string>("metadata","")),
geometry_table_(*params_.get<std::string>("geometry_table","")),
geometry_field_(*params_.get<std::string>("geometry_field","")),
index_table_(*params_.get<std::string>("index_table","")),
key_field_(*params_.get<std::string>("key_field","")),
row_offset_(*params_.get<int>("row_offset",0)),
row_limit_(*params_.get<int>("row_limit",0)),
desc_(*params_.get<std::string>("type"), *params_.get<std::string>("encoding","utf-8")),
format_(mapnik::wkbAuto)
: datasource(params),
extent_(),
extent_initialized_(false),
type_(datasource::Vector),
table_(*params_.get<std::string>("table", "")),
fields_(*params_.get<std::string>("fields", "*")),
metadata_(*params_.get<std::string>("metadata", "")),
geometry_table_(*params_.get<std::string>("geometry_table", "")),
geometry_field_(*params_.get<std::string>("geometry_field", "")),
index_table_(*params_.get<std::string>("index_table", "")),
key_field_(*params_.get<std::string>("key_field", "")),
row_offset_(*params_.get<int>("row_offset", 0)),
row_limit_(*params_.get<int>("row_limit", 0)),
desc_(*params_.get<std::string>("type"), *params_.get<std::string>("encoding", "utf-8")),
format_(mapnik::wkbAuto)
{
// TODO
// - change param from 'file' to 'dbname'
// - ensure that the supplied key_field is a valid "integer primary key"
boost::optional<std::string> file = params_.get<std::string>("file");
if (!file) throw datasource_exception("Sqlite Plugin: missing <file> parameter");
if (! file) throw datasource_exception("Sqlite Plugin: missing <file> parameter");
if (table_.empty())
{
@ -144,27 +138,27 @@ void sqlite_datasource::bind() const
boost::optional<std::string> attachdb = params_.get<std::string>("attachdb");
if (attachdb)
{
parse_attachdb(*attachdb);
parse_attachdb(*attachdb);
}
boost::optional<std::string> initdb = params_.get<std::string>("initdb");
if (initdb)
{
init_statements_.push_back(*initdb);
init_statements_.push_back(*initdb);
}
if (!boost::filesystem::exists(dataset_name_))
throw datasource_exception("Sqlite Plugin: " + dataset_name_ + " does not exist");
dataset_ = new sqlite_connection (dataset_name_);
dataset_ = new sqlite_connection(dataset_name_);
// Execute init_statements_
for (std::vector<std::string>::const_iterator iter = init_statements_.begin();
iter != init_statements_.end(); ++iter)
{
#ifdef MAPNIK_DEBUG
#ifdef MAPNIK_DEBUG
std::clog << "Sqlite Plugin: Execute init sql: " << *iter << std::endl;
#endif
#endif
dataset_->execute(*iter);
}
@ -197,10 +191,10 @@ void sqlite_datasource::bind() const
boost::scoped_ptr<sqlite_resultset> rs(dataset_->execute_query(s.str()));
if (rs->is_valid() && rs->step_next())
{
for (int i = 0; i < rs->column_count (); ++i)
for (int i = 0; i < rs->column_count(); ++i)
{
const int type_oid = rs->column_type (i);
const char* fld_name = rs->column_name (i);
const int type_oid = rs->column_type(i);
const char* fld_name = rs->column_name(i);
switch (type_oid)
{
case SQLITE_INTEGER:
@ -233,9 +227,9 @@ void sqlite_datasource::bind() const
break;
default:
#ifdef MAPNIK_DEBUG
#ifdef MAPNIK_DEBUG
std::clog << "Sqlite Plugin: unknown type_oid=" << type_oid << std::endl;
#endif
#endif
break;
}
}
@ -252,13 +246,15 @@ void sqlite_datasource::bind() const
if (key_field_.empty())
{
use_pragma_table_info = true;
use_pragma_table_info = true;
}
else
{
// TODO - we can't trust so much the rowid here
if (key_field_ == "rowid")
desc_.add_descriptor(attribute_descriptor("rowid", mapnik::Integer));
// TODO - we can't trust so much the rowid here
if (key_field_ == "rowid")
{
desc_.add_descriptor(attribute_descriptor("rowid", mapnik::Integer));
}
}
if (use_pragma_table_info)
@ -325,7 +321,7 @@ void sqlite_datasource::bind() const
desc_.add_descriptor(attribute_descriptor(fld_name, mapnik::String));
}
}
#ifdef MAPNIK_DEBUG
#ifdef MAPNIK_DEBUG
else
{
// "Column Affinity" says default to "Numeric" but for now we pass..
@ -338,7 +334,7 @@ void sqlite_datasource::bind() const
<< "' unhandled due to unknown type: "
<< fld_type << std::endl;
}
#endif
#endif
}
if (! found_table)
@ -377,12 +373,12 @@ void sqlite_datasource::bind() const
{
has_spatial_index_ = true;
}
#ifdef MAPNIK_DEBUG
#ifdef MAPNIK_DEBUG
else
{
std::clog << "SQLite Plugin: rtree index lookup did not succeed: '" << sqlite3_errmsg(*(*dataset_)) << "'\n";
}
#endif
#endif
}
if (! metadata_.empty() && ! extent_initialized_)
@ -399,7 +395,7 @@ void sqlite_datasource::bind() const
double xmax = rs->column_double (2);
double ymax = rs->column_double (3);
extent_.init (xmin,ymin,xmax,ymax);
extent_.init (xmin, ymin, xmax, ymax);
extent_initialized_ = true;
}
}
@ -421,10 +417,10 @@ void sqlite_datasource::bind() const
double ymin = lexical_cast<double>(rs->column_double(1));
double xmax = lexical_cast<double>(rs->column_double(2));
double ymax = lexical_cast<double>(rs->column_double(3));
extent_.init (xmin,ymin,xmax,ymax);
extent_.init(xmin, ymin, xmax, ymax);
extent_initialized_ = true;
}
catch (bad_lexical_cast &ex)
catch (bad_lexical_cast& ex)
{
std::clog << boost::format("SQLite Plugin: warning: could not determine extent from query: %s\nError was: '%s'\n") % s.str() % ex.what() << std::endl;
}
@ -503,7 +499,7 @@ void sqlite_datasource::bind() const
while (rs->is_valid() && rs->step_next())
{
int size;
const char* data = (const char *) rs->column_blob (0, size);
const char* data = (const char*) rs->column_blob(0, size);
if (data)
{
// create a tmp feature to be able to parse geometry
@ -534,8 +530,8 @@ void sqlite_datasource::bind() const
{
std::ostringstream type_error;
type_error << "Sqlite Plugin: invalid type for key field '"
<< key_field_ << "' when creating index '" << index_table_
<< "' type was: " << type_oid << "";
<< key_field_ << "' when creating index '" << index_table_
<< "' type was: " << type_oid << "";
throw datasource_exception(type_error.str());
}
@ -680,24 +676,24 @@ int sqlite_datasource::type() const
box2d<double> sqlite_datasource::envelope() const
{
if (!is_bound_) bind();
if (! is_bound_) bind();
return extent_;
}
layer_descriptor sqlite_datasource::get_descriptor() const
{
if (!is_bound_) bind();
if (! is_bound_) bind();
return desc_;
}
featureset_ptr sqlite_datasource::features(query const& q) const
{
if (!is_bound_) bind();
if (! is_bound_) bind();
if (dataset_)
{
if (dataset_)
{
mapnik::box2d<double> const& e = q.get_bbox();
std::ostringstream s;
@ -752,10 +748,10 @@ featureset_ptr sqlite_datasource::features(query const& q) const
s << " OFFSET " << row_offset_;
}
#ifdef MAPNIK_DEBUG
#ifdef MAPNIK_DEBUG
std::clog << "Sqlite Plugin: table: " << table_ << "\n\n";
std::clog << "Sqlite Plugin: query:" << s.str() << "\n\n";
#endif
#endif
boost::shared_ptr<sqlite_resultset> rs(dataset_->execute_query(s.str()));
@ -771,12 +767,12 @@ featureset_ptr sqlite_datasource::features(query const& q) const
featureset_ptr sqlite_datasource::features_at_point(coord2d const& pt) const
{
if (!is_bound_) bind();
if (! is_bound_) bind();
if (dataset_)
{
if (dataset_)
{
// TODO - need tolerance
mapnik::box2d<double> const e(pt.x,pt.y,pt.x,pt.y);
mapnik::box2d<double> const e(pt.x, pt.y, pt.x, pt.y);
std::ostringstream s;
s << "SELECT " << geometry_field_ << "," << key_field_;
@ -795,7 +791,7 @@ featureset_ptr sqlite_datasource::features_at_point(coord2d const& pt) const
s << " FROM ";
std::string query (table_);
std::string query(table_);
if (has_spatial_index_)
{
@ -826,9 +822,9 @@ featureset_ptr sqlite_datasource::features_at_point(coord2d const& pt) const
s << " OFFSET " << row_offset_;
}
#ifdef MAPNIK_DEBUG
#ifdef MAPNIK_DEBUG
std::clog << "Sqlite Plugin: " << s.str() << std::endl;
#endif
#endif
boost::shared_ptr<sqlite_resultset> rs(dataset_->execute_query(s.str()));

View file

@ -2,7 +2,7 @@
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2007 Artem Pavlenko
* Copyright (C) 2011 Artem Pavlenko
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@ -19,7 +19,6 @@
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*****************************************************************************/
//$Id$
#ifndef SQLITE_DATASOURCE_HPP
#define SQLITE_DATASOURCE_HPP
@ -38,47 +37,45 @@
#include "sqlite_types.hpp"
//==============================================================================
class sqlite_datasource : public mapnik::datasource
{
public:
sqlite_datasource(mapnik::parameters const& params, bool bind=true);
virtual ~sqlite_datasource ();
int type() const;
static std::string name();
mapnik::featureset_ptr features(mapnik::query const& q) const;
mapnik::featureset_ptr features_at_point(mapnik::coord2d const& pt) const;
mapnik::box2d<double> envelope() const;
mapnik::layer_descriptor get_descriptor() const;
void bind() const;
private:
mutable mapnik::box2d<double> extent_;
mutable bool extent_initialized_;
int type_;
mutable std::string dataset_name_;
mutable sqlite_connection* dataset_;
std::string table_;
std::string fields_;
std::string metadata_;
mutable std::string geometry_table_;
mutable std::string geometry_field_;
mutable std::string index_table_;
mutable std::string key_field_;
mutable int row_offset_;
mutable int row_limit_;
mutable mapnik::layer_descriptor desc_;
mutable mapnik::wkbFormat format_;
mutable bool multiple_geometries_;
mutable bool use_spatial_index_;
mutable bool has_spatial_index_;
mutable bool using_subquery_;
mutable std::vector<std::string> init_statements_;
// Fill init_statements with any statements
// needed to attach auxillary databases
void parse_attachdb(std::string const& attachdb) const;
public:
sqlite_datasource(mapnik::parameters const& params, bool bind = true);
virtual ~sqlite_datasource ();
int type() const;
static std::string name();
mapnik::featureset_ptr features(mapnik::query const& q) const;
mapnik::featureset_ptr features_at_point(mapnik::coord2d const& pt) const;
mapnik::box2d<double> envelope() const;
mapnik::layer_descriptor get_descriptor() const;
void bind() const;
private:
mutable mapnik::box2d<double> extent_;
mutable bool extent_initialized_;
int type_;
mutable std::string dataset_name_;
mutable sqlite_connection* dataset_;
std::string table_;
std::string fields_;
std::string metadata_;
mutable std::string geometry_table_;
mutable std::string geometry_field_;
mutable std::string index_table_;
mutable std::string key_field_;
mutable int row_offset_;
mutable int row_limit_;
mutable mapnik::layer_descriptor desc_;
mutable mapnik::wkbFormat format_;
mutable bool multiple_geometries_;
mutable bool use_spatial_index_;
mutable bool has_spatial_index_;
mutable bool using_subquery_;
mutable std::vector<std::string> init_statements_;
// Fill init_statements with any statements
// needed to attach auxillary databases
void parse_attachdb(std::string const& attachdb) const;
};
#endif // SQLITE_DATASOURCE_HPP

View file

@ -2,7 +2,7 @@
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2007 Artem Pavlenko
* Copyright (C) 2011 Artem Pavlenko
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@ -19,7 +19,6 @@
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*****************************************************************************/
//$Id$
// mapnik
#include <mapnik/global.hpp>
@ -49,35 +48,37 @@ sqlite_featureset::sqlite_featureset(boost::shared_ptr<sqlite_resultset> rs,
mapnik::wkbFormat format,
bool multiple_geometries,
bool using_subquery)
: rs_(rs),
tr_(new transcoder(encoding)),
format_(format),
multiple_geometries_(multiple_geometries),
using_subquery_(using_subquery)
: rs_(rs),
tr_(new transcoder(encoding)),
format_(format),
multiple_geometries_(multiple_geometries),
using_subquery_(using_subquery)
{
}
sqlite_featureset::~sqlite_featureset() {}
sqlite_featureset::~sqlite_featureset()
{
}
feature_ptr sqlite_featureset::next()
{
if (rs_->is_valid () && rs_->step_next ())
{
int size;
const char* data = (const char *) rs_->column_blob (0, size);
const char* data = (const char*) rs_->column_blob(0, size);
if (! data)
{
return feature_ptr();
}
int feature_id = rs_->column_integer (1);
int feature_id = rs_->column_integer(1);
feature_ptr feature(feature_factory::create(feature_id));
geometry_utils::from_wkb(feature->paths(), data, size, multiple_geometries_, format_);
for (int i = 2; i < rs_->column_count (); ++i)
for (int i = 2; i < rs_->column_count(); ++i)
{
const int type_oid = rs_->column_type (i);
const int type_oid = rs_->column_type(i);
const char* fld_name = rs_->column_name(i);
if (! fld_name)
@ -95,13 +96,13 @@ feature_ptr sqlite_featureset::next()
{
case SQLITE_INTEGER:
{
boost::put(*feature, fld_name_str, rs_->column_integer (i));
boost::put(*feature, fld_name_str, rs_->column_integer(i));
break;
}
case SQLITE_FLOAT:
{
boost::put(*feature, fld_name_str, rs_->column_double (i));
boost::put(*feature, fld_name_str, rs_->column_double(i));
break;
}

View file

@ -2,7 +2,7 @@
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2007 Artem Pavlenko
* Copyright (C) 2011 Artem Pavlenko
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@ -19,7 +19,6 @@
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*****************************************************************************/
//$Id$
#ifndef SQLITE_FEATURESET_HPP
#define SQLITE_FEATURESET_HPP
@ -36,26 +35,24 @@
// sqlite
#include "sqlite_types.hpp"
//==============================================================================
class sqlite_featureset : public mapnik::Featureset
{
public:
sqlite_featureset(boost::shared_ptr<sqlite_resultset> rs,
std::string const& encoding,
mapnik::wkbFormat format,
bool multiple_geometries,
bool using_subquery);
virtual ~sqlite_featureset();
mapnik::feature_ptr next();
private:
boost::shared_ptr<sqlite_resultset> rs_;
boost::scoped_ptr<mapnik::transcoder> tr_;
mapnik::wkbFormat format_;
bool multiple_geometries_;
bool using_subquery_;
public:
sqlite_featureset(boost::shared_ptr<sqlite_resultset> rs,
std::string const& encoding,
mapnik::wkbFormat format,
bool multiple_geometries,
bool using_subquery);
virtual ~sqlite_featureset();
mapnik::feature_ptr next();
private:
boost::shared_ptr<sqlite_resultset> rs_;
boost::scoped_ptr<mapnik::transcoder> tr_;
mapnik::wkbFormat format_;
bool multiple_geometries_;
bool using_subquery_;
};
#endif // SQLITE_FEATURESET_HPP

View file

@ -2,7 +2,7 @@
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2007 Artem Pavlenko
* Copyright (C) 2011 Artem Pavlenko
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@ -19,13 +19,12 @@
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*****************************************************************************/
//$Id$
#ifndef SQLITE_TYPES_HPP
#define SQLITE_TYPES_HPP
// stdc++
#include <string.h>
// stl
#include <cstring>
// mapnik
#include <mapnik/datasource.hpp>
@ -40,32 +39,25 @@ extern "C" {
}
//==============================================================================
class sqlite_utils
{
public:
static void dequote(std::string & z)
static void dequote(std::string& z)
{
boost::algorithm::trim_if(z,boost::algorithm::is_any_of("[]'\"`"));
boost::algorithm::trim_if(z, boost::algorithm::is_any_of("[]'\"`"));
}
};
//==============================================================================
class sqlite_resultset
{
public:
sqlite_resultset (sqlite3_stmt* stmt)
sqlite_resultset(sqlite3_stmt* stmt)
: stmt_(stmt)
{
}
~sqlite_resultset ()
virtual ~sqlite_resultset()
{
if (stmt_)
{
@ -73,14 +65,14 @@ public:
}
}
bool is_valid ()
bool is_valid()
{
return stmt_ != 0;
}
bool step_next ()
bool step_next()
{
const int status = sqlite3_step (stmt_);
const int status = sqlite3_step(stmt_);
if (status != SQLITE_ROW && status != SQLITE_DONE)
{
std::ostringstream s;
@ -97,56 +89,56 @@ public:
return status == SQLITE_ROW;
}
int column_count ()
int column_count()
{
return sqlite3_column_count (stmt_);
return sqlite3_column_count(stmt_);
}
int column_type (int col)
int column_type(int col)
{
return sqlite3_column_type (stmt_, col);
return sqlite3_column_type(stmt_, col);
}
const char* column_name (int col)
const char* column_name(int col)
{
return sqlite3_column_name (stmt_, col);
return sqlite3_column_name(stmt_, col);
}
bool column_isnull (int col)
bool column_isnull(int col)
{
return sqlite3_column_type (stmt_, col) == SQLITE_NULL;
return sqlite3_column_type(stmt_, col) == SQLITE_NULL;
}
int column_integer (int col)
int column_integer(int col)
{
return sqlite3_column_int (stmt_, col);
return sqlite3_column_int(stmt_, col);
}
int column_integer64 (int col)
int column_integer64(int col)
{
return sqlite3_column_int64 (stmt_, col);
return sqlite3_column_int64(stmt_, col);
}
double column_double (int col)
double column_double(int col)
{
return sqlite3_column_double (stmt_, col);
return sqlite3_column_double(stmt_, col);
}
const char* column_text (int col, int& len)
const char* column_text(int col, int& len)
{
len = sqlite3_column_bytes (stmt_, col);
return (const char*) sqlite3_column_text (stmt_, col);
len = sqlite3_column_bytes(stmt_, col);
return (const char*) sqlite3_column_text(stmt_, col);
}
const char* column_text (int col)
const char* column_text(int col)
{
return (const char*) sqlite3_column_text (stmt_, col);
return (const char*) sqlite3_column_text(stmt_, col);
}
const void* column_blob (int col, int& bytes)
const void* column_blob(int col, int& bytes)
{
bytes = sqlite3_column_bytes (stmt_, col);
return (const char*) sqlite3_column_blob (stmt_, col);
bytes = sqlite3_column_bytes(stmt_, col);
return (const char*) sqlite3_column_blob(stmt_, col);
}
sqlite3_stmt* get_statement()
@ -160,13 +152,11 @@ private:
};
//==============================================================================
class sqlite_connection
{
public:
sqlite_connection (const std::string& file)
sqlite_connection(const std::string& file)
: db_(0)
{
// sqlite3_open_v2 is available earlier but
@ -175,28 +165,28 @@ public:
const int rc = sqlite3_enable_shared_cache(1);
if (rc != SQLITE_OK)
{
throw mapnik::datasource_exception (sqlite3_errmsg (db_));
throw mapnik::datasource_exception(sqlite3_errmsg (db_));
}
int mode = SQLITE_OPEN_READWRITE | SQLITE_OPEN_NOMUTEX | SQLITE_OPEN_SHAREDCACHE;
if (sqlite3_open_v2 (file.c_str(), &db_, mode, NULL))
if (sqlite3_open_v2(file.c_str(), &db_, mode, NULL))
#else
#warning "Mapnik's sqlite plugin is compiling against a version of sqlite older than 3.6.18 which may make rendering slow..."
if (sqlite3_open (file.c_str(), &db_))
if (sqlite3_open(file.c_str(), &db_))
#endif
{
std::ostringstream s;
s << "Sqlite Plugin: " << sqlite3_errmsg (db_);
s << "Sqlite Plugin: " << sqlite3_errmsg(db_);
throw mapnik::datasource_exception (s.str());
throw mapnik::datasource_exception(s.str());
}
}
virtual ~sqlite_connection ()
virtual ~sqlite_connection()
{
if (db_)
{
sqlite3_close (db_);
sqlite3_close(db_);
}
}
@ -204,10 +194,16 @@ public:
{
std::ostringstream s;
s << "Sqlite Plugin: ";
if (db_)
{
s << "'" << sqlite3_errmsg(db_) << "'";
}
else
{
s << "unknown error, lost connection";
}
s << "\nFull sql was: '" << sql << "'\n";
throw mapnik::datasource_exception (s.str());
@ -217,7 +213,7 @@ public:
{
sqlite3_stmt* stmt = 0;
const int rc = sqlite3_prepare_v2 (db_, sql.c_str(), -1, &stmt, 0);
const int rc = sqlite3_prepare_v2(db_, sql.c_str(), -1, &stmt, 0);
if (rc != SQLITE_OK)
{
throw_sqlite_error(sql);
@ -251,5 +247,4 @@ private:
sqlite3* db_;
};
#endif //SQLITE_TYPES_HPP
#endif // SQLITE_TYPES_HPP