2009-02-09 20:43:57 +01:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
|
|
|
* Copyright (C) 2007 Artem Pavlenko
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
//$Id$
|
|
|
|
|
2011-05-17 01:41:34 +02:00
|
|
|
// mapnik
|
2009-02-09 20:43:57 +01:00
|
|
|
#include <mapnik/global.hpp>
|
|
|
|
#include <mapnik/datasource.hpp>
|
2009-12-16 21:02:06 +01:00
|
|
|
#include <mapnik/box2d.hpp>
|
2009-02-09 20:43:57 +01:00
|
|
|
#include <mapnik/geometry.hpp>
|
|
|
|
#include <mapnik/feature.hpp>
|
|
|
|
#include <mapnik/feature_layer_desc.hpp>
|
|
|
|
#include <mapnik/wkb.hpp>
|
|
|
|
#include <mapnik/unicode.hpp>
|
2011-05-17 01:41:34 +02:00
|
|
|
#include <mapnik/feature_factory.hpp>
|
2011-08-24 02:32:00 +02:00
|
|
|
#include <string.h>
|
2009-02-09 20:43:57 +01:00
|
|
|
|
2010-05-30 05:23:59 +02:00
|
|
|
// ogr
|
2009-02-09 20:43:57 +01:00
|
|
|
#include "sqlite_featureset.hpp"
|
|
|
|
|
|
|
|
using mapnik::query;
|
2009-12-16 21:02:06 +01:00
|
|
|
using mapnik::box2d;
|
2009-02-09 20:43:57 +01:00
|
|
|
using mapnik::CoordTransform;
|
|
|
|
using mapnik::Feature;
|
|
|
|
using mapnik::feature_ptr;
|
|
|
|
using mapnik::geometry_utils;
|
|
|
|
using mapnik::transcoder;
|
2011-05-17 01:41:34 +02:00
|
|
|
using mapnik::feature_factory;
|
2009-02-09 20:43:57 +01:00
|
|
|
|
|
|
|
sqlite_featureset::sqlite_featureset(boost::shared_ptr<sqlite_resultset> rs,
|
|
|
|
std::string const& encoding,
|
2009-02-23 16:00:25 +01:00
|
|
|
mapnik::wkbFormat format,
|
2011-08-24 02:32:00 +02:00
|
|
|
bool multiple_geometries,
|
|
|
|
bool using_subquery)
|
2009-02-09 20:43:57 +01:00
|
|
|
: rs_(rs),
|
|
|
|
tr_(new transcoder(encoding)),
|
2009-02-23 16:00:25 +01:00
|
|
|
format_(format),
|
2011-08-24 02:32:00 +02:00
|
|
|
multiple_geometries_(multiple_geometries),
|
|
|
|
using_subquery_(using_subquery)
|
2009-02-09 20:43:57 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
sqlite_featureset::~sqlite_featureset() {}
|
|
|
|
|
2011-10-18 22:19:03 +02:00
|
|
|
// TODO - refactor, make a static member using std::string or better UnicodeString
|
|
|
|
|
|
|
|
void sqlite_dequote(char *z)
|
|
|
|
{
|
|
|
|
char quote = z[0];
|
|
|
|
|
|
|
|
if (quote=='[' || quote=='\'' || quote=='"' || quote=='`')
|
|
|
|
{
|
|
|
|
int iIn = 1; // Index of next byte to read from input
|
|
|
|
int iOut = 0; // Index of next byte to write to output
|
2011-08-24 02:32:00 +02:00
|
|
|
|
2011-10-18 22:19:03 +02:00
|
|
|
// If the first byte was a '[', then the close-quote character is a ']'
|
|
|
|
if (quote == '[')
|
|
|
|
{
|
|
|
|
quote = ']';
|
|
|
|
}
|
|
|
|
|
|
|
|
while (z[iIn])
|
|
|
|
{
|
|
|
|
if (z[iIn] == quote)
|
|
|
|
{
|
|
|
|
if (z[iIn+1] != quote) break;
|
|
|
|
z[iOut++] = quote;
|
|
|
|
iIn += 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
z[iOut++] = z[iIn++];
|
|
|
|
}
|
|
|
|
}
|
2011-08-24 02:32:00 +02:00
|
|
|
|
2011-10-18 22:19:03 +02:00
|
|
|
z[iOut] = '\0';
|
|
|
|
}
|
|
|
|
}
|
2011-08-24 02:32:00 +02:00
|
|
|
|
2009-02-09 20:43:57 +01:00
|
|
|
feature_ptr sqlite_featureset::next()
|
|
|
|
{
|
|
|
|
if (rs_->is_valid () && rs_->step_next ())
|
|
|
|
{
|
2009-02-10 20:09:16 +01:00
|
|
|
int size;
|
|
|
|
const char* data = (const char *) rs_->column_blob (0, size);
|
2011-10-18 22:19:03 +02:00
|
|
|
if (! data)
|
|
|
|
{
|
2010-10-01 22:16:49 +02:00
|
|
|
return feature_ptr();
|
2011-10-18 22:19:03 +02:00
|
|
|
}
|
|
|
|
|
2009-02-10 20:09:16 +01:00
|
|
|
int feature_id = rs_->column_integer (1);
|
2009-02-09 20:43:57 +01:00
|
|
|
|
2011-05-17 01:41:34 +02:00
|
|
|
feature_ptr feature(feature_factory::create(feature_id));
|
2011-10-18 22:19:03 +02:00
|
|
|
geometry_utils::from_wkb(feature->paths(), data, size, multiple_geometries_, format_);
|
2009-02-22 21:53:45 +01:00
|
|
|
|
2009-02-10 20:09:16 +01:00
|
|
|
for (int i = 2; i < rs_->column_count (); ++i)
|
2009-02-09 20:43:57 +01:00
|
|
|
{
|
2011-08-24 02:32:00 +02:00
|
|
|
const int type_oid = rs_->column_type (i);
|
|
|
|
const char* fld_name = rs_->column_name(i);
|
|
|
|
|
2011-10-18 22:19:03 +02:00
|
|
|
if (! fld_name)
|
2011-09-02 02:56:42 +02:00
|
|
|
continue;
|
|
|
|
|
2011-10-18 22:19:03 +02:00
|
|
|
if (! using_subquery_)
|
2011-08-24 02:32:00 +02:00
|
|
|
{
|
|
|
|
switch (type_oid)
|
|
|
|
{
|
2011-10-18 22:19:03 +02:00
|
|
|
case SQLITE_INTEGER:
|
|
|
|
{
|
|
|
|
boost::put(*feature, fld_name, rs_->column_integer (i));
|
|
|
|
break;
|
|
|
|
}
|
2011-08-24 02:32:00 +02:00
|
|
|
|
2011-10-18 22:19:03 +02:00
|
|
|
case SQLITE_FLOAT:
|
|
|
|
{
|
|
|
|
boost::put(*feature, fld_name, rs_->column_double (i));
|
|
|
|
break;
|
|
|
|
}
|
2011-08-24 02:32:00 +02:00
|
|
|
|
2011-10-18 22:19:03 +02:00
|
|
|
case SQLITE_TEXT:
|
|
|
|
{
|
|
|
|
int text_size;
|
|
|
|
const char * data = rs_->column_text(i, text_size);
|
|
|
|
UnicodeString ustr = tr_->transcode(data, text_size);
|
|
|
|
boost::put(*feature, fld_name, ustr);
|
|
|
|
break;
|
|
|
|
}
|
2011-08-24 02:32:00 +02:00
|
|
|
|
2011-10-18 22:19:03 +02:00
|
|
|
case SQLITE_NULL:
|
|
|
|
{
|
|
|
|
boost::put(*feature,fld_name,mapnik::value_null());
|
|
|
|
break;
|
|
|
|
}
|
2011-08-24 02:32:00 +02:00
|
|
|
|
2011-10-18 22:19:03 +02:00
|
|
|
case SQLITE_BLOB:
|
|
|
|
break;
|
2011-08-24 02:32:00 +02:00
|
|
|
|
2011-10-18 22:19:03 +02:00
|
|
|
default:
|
2011-08-24 02:32:00 +02:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2011-10-18 22:19:03 +02:00
|
|
|
std::clog << "Sqlite Plugin: unhandled type_oid=" << type_oid << std::endl;
|
2011-08-24 02:32:00 +02:00
|
|
|
#endif
|
2011-10-18 22:19:03 +02:00
|
|
|
break;
|
2011-08-24 02:32:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-10-18 22:19:03 +02:00
|
|
|
// TODO - refactor this code, it is C99 but not valid in C++ (even if GCC allows this)
|
|
|
|
|
2011-08-24 02:32:00 +02:00
|
|
|
// subqueries in sqlite lead to field double quoting which we need to strip
|
|
|
|
char fld_name2[strlen(fld_name)];
|
|
|
|
strcpy(fld_name2,fld_name);
|
|
|
|
sqlite_dequote(fld_name2);
|
2011-10-18 22:19:03 +02:00
|
|
|
|
2011-08-24 02:32:00 +02:00
|
|
|
switch (type_oid)
|
|
|
|
{
|
2011-10-18 22:19:03 +02:00
|
|
|
case SQLITE_INTEGER:
|
|
|
|
{
|
|
|
|
boost::put(*feature,fld_name2,rs_->column_integer (i));
|
|
|
|
break;
|
|
|
|
}
|
2011-08-24 02:32:00 +02:00
|
|
|
|
2011-10-18 22:19:03 +02:00
|
|
|
case SQLITE_FLOAT:
|
|
|
|
{
|
|
|
|
boost::put(*feature,fld_name2,rs_->column_double (i));
|
|
|
|
break;
|
|
|
|
}
|
2011-08-24 02:32:00 +02:00
|
|
|
|
2011-10-18 22:19:03 +02:00
|
|
|
case SQLITE_TEXT:
|
|
|
|
{
|
|
|
|
int text_size;
|
|
|
|
const char * data = rs_->column_text(i,text_size);
|
|
|
|
UnicodeString ustr = tr_->transcode(data,text_size);
|
|
|
|
boost::put(*feature,fld_name2,ustr);
|
|
|
|
break;
|
|
|
|
}
|
2011-08-24 02:32:00 +02:00
|
|
|
|
2011-10-18 22:19:03 +02:00
|
|
|
case SQLITE_NULL:
|
|
|
|
{
|
|
|
|
boost::put(*feature,fld_name2,mapnik::value_null());
|
|
|
|
break;
|
|
|
|
}
|
2011-08-24 02:32:00 +02:00
|
|
|
|
2011-10-18 22:19:03 +02:00
|
|
|
case SQLITE_BLOB:
|
|
|
|
break;
|
2011-08-24 02:32:00 +02:00
|
|
|
|
2011-10-18 22:19:03 +02:00
|
|
|
default:
|
2011-08-24 02:32:00 +02:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2011-10-18 22:19:03 +02:00
|
|
|
std::clog << "Sqlite Plugin: unhandled type_oid=" << type_oid << std::endl;
|
2011-08-24 02:32:00 +02:00
|
|
|
#endif
|
2011-10-18 22:19:03 +02:00
|
|
|
break;
|
2011-08-24 02:32:00 +02:00
|
|
|
}
|
|
|
|
}
|
2009-02-09 20:43:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return feature;
|
|
|
|
}
|
|
|
|
|
|
|
|
return feature_ptr();
|
|
|
|
}
|