2009-02-09 19:43:57 +00:00
|
|
|
/*****************************************************************************
|
2011-11-14 03:37:50 +00:00
|
|
|
*
|
2009-02-09 19:43:57 +00:00
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2021-01-05 14:39:07 +00:00
|
|
|
* Copyright (C) 2021 Artem Pavlenko
|
2009-02-09 19:43:57 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2011-11-03 23:51:37 +00:00
|
|
|
#ifndef MAPNIK_SQLITE_UTILS_HPP
|
|
|
|
#define MAPNIK_SQLITE_UTILS_HPP
|
2009-02-09 19:43:57 +00:00
|
|
|
|
2011-10-22 12:50:24 +00:00
|
|
|
// stl
|
2011-11-19 19:36:35 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2013-01-04 03:27:53 +00:00
|
|
|
#include <algorithm>
|
2014-10-21 23:57:44 +00:00
|
|
|
#include <memory>
|
2011-10-18 21:32:25 +00:00
|
|
|
|
2009-02-09 19:43:57 +00:00
|
|
|
// mapnik
|
2013-03-27 08:02:40 +00:00
|
|
|
#include <mapnik/debug.hpp>
|
2009-02-09 19:43:57 +00:00
|
|
|
#include <mapnik/datasource.hpp>
|
2012-07-25 02:21:55 +00:00
|
|
|
#include <mapnik/params.hpp>
|
2011-10-29 04:24:47 +00:00
|
|
|
#include <mapnik/sql_utils.hpp>
|
2013-06-03 02:28:24 +00:00
|
|
|
#include <mapnik/util/fs.hpp>
|
2016-08-02 13:43:02 +00:00
|
|
|
#include <mapnik/geometry/is_empty.hpp>
|
|
|
|
#include <mapnik/geometry/envelope.hpp>
|
2009-02-09 19:43:57 +00:00
|
|
|
|
2020-11-19 14:30:30 +00:00
|
|
|
#include <mapnik/warning.hpp>
|
|
|
|
MAPNIK_DISABLE_WARNING_PUSH
|
2015-11-08 01:53:09 +00:00
|
|
|
#include <mapnik/warning_ignore.hpp>
|
2011-10-21 10:41:20 +00:00
|
|
|
#include <boost/algorithm/string.hpp>
|
2020-11-19 14:30:30 +00:00
|
|
|
MAPNIK_DISABLE_WARNING_POP
|
2009-02-09 19:43:57 +00:00
|
|
|
|
|
|
|
// sqlite
|
|
|
|
extern "C" {
|
2011-11-14 03:37:50 +00:00
|
|
|
#include <sqlite3.h>
|
2009-02-09 19:43:57 +00:00
|
|
|
}
|
|
|
|
|
2011-11-03 23:51:37 +00:00
|
|
|
#include "sqlite_resultset.hpp"
|
|
|
|
#include "sqlite_prepared.hpp"
|
|
|
|
#include "sqlite_connection.hpp"
|
2009-02-09 19:43:57 +00:00
|
|
|
|
2011-10-29 04:24:47 +00:00
|
|
|
//==============================================================================
|
2011-10-18 21:32:25 +00:00
|
|
|
|
2011-11-03 23:51:37 +00:00
|
|
|
class sqlite_utils
|
2009-02-09 19:43:57 +00:00
|
|
|
{
|
2022-01-26 09:43:31 +00:00
|
|
|
public:
|
2011-10-29 04:24:47 +00:00
|
|
|
|
2011-11-10 17:42:04 +00:00
|
|
|
static bool needs_quoting(std::string const& name)
|
|
|
|
{
|
|
|
|
if (name.size() > 0)
|
|
|
|
{
|
|
|
|
const char first = name[0];
|
|
|
|
if (is_quote_char(first))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2022-01-26 09:43:31 +00:00
|
|
|
if ((first >= '0' && first <= '9') || (name.find("-") != std::string::npos))
|
2011-11-10 17:42:04 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool is_quote_char(const char z)
|
|
|
|
{
|
|
|
|
if (z == '"' || z == '\'' || z == '[' || z == '`')
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-01-26 09:43:31 +00:00
|
|
|
static void dequote(std::string& z) { boost::algorithm::trim_if(z, boost::algorithm::is_any_of("[]'\"`")); }
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2011-11-03 23:51:37 +00:00
|
|
|
static std::string index_for_table(std::string const& table, std::string const& field)
|
2009-02-09 19:43:57 +00:00
|
|
|
{
|
2011-11-14 00:41:54 +00:00
|
|
|
std::string table_trimmed = table;
|
|
|
|
dequote(table_trimmed);
|
|
|
|
return "\"idx_" + table_trimmed + "_" + field + "\"";
|
2009-02-09 19:43:57 +00:00
|
|
|
}
|
|
|
|
|
2011-11-03 23:51:37 +00:00
|
|
|
static std::string index_for_db(std::string const& file)
|
2009-02-09 19:43:57 +00:00
|
|
|
{
|
2022-01-26 09:43:31 +00:00
|
|
|
// std::size_t idx = file.find_last_of(".");
|
|
|
|
// if(idx != std::string::npos) {
|
|
|
|
// return file.substr(0,idx) + ".index" + file.substr(idx);
|
|
|
|
// }
|
|
|
|
// else
|
2011-11-03 23:51:37 +00:00
|
|
|
//{
|
2011-11-14 03:37:50 +00:00
|
|
|
return file + ".index";
|
2011-11-03 23:51:37 +00:00
|
|
|
//}
|
2011-11-10 03:18:12 +00:00
|
|
|
}
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2022-01-26 09:43:31 +00:00
|
|
|
static bool apply_spatial_filter(std::string& query,
|
2011-12-16 16:14:08 +00:00
|
|
|
mapnik::box2d<double> const& e,
|
|
|
|
std::string const& table,
|
|
|
|
std::string const& key_field,
|
|
|
|
std::string const& index_table,
|
2011-12-16 18:05:54 +00:00
|
|
|
std::string const& geometry_table,
|
|
|
|
std::string const& intersects_token)
|
2011-12-16 16:14:08 +00:00
|
|
|
{
|
|
|
|
std::ostringstream spatial_sql;
|
|
|
|
spatial_sql << std::setprecision(16);
|
2011-12-16 18:05:54 +00:00
|
|
|
spatial_sql << key_field << " IN (SELECT pkid FROM " << index_table;
|
2022-01-26 09:43:31 +00:00
|
|
|
spatial_sql << " WHERE xmax>=" << e.minx() << " AND xmin<=" << e.maxx();
|
2011-12-16 16:14:08 +00:00
|
|
|
spatial_sql << " AND ymax>=" << e.miny() << " AND ymin<=" << e.maxy() << ")";
|
2022-01-26 09:43:31 +00:00
|
|
|
if (boost::algorithm::ifind_first(query, intersects_token))
|
2011-12-16 16:14:08 +00:00
|
|
|
{
|
2011-12-16 18:05:54 +00:00
|
|
|
boost::algorithm::ireplace_all(query, intersects_token, spatial_sql.str());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// substitute first WHERE found if not using JOIN
|
|
|
|
// (because we can't know the WHERE is on the right table)
|
2022-01-26 09:43:31 +00:00
|
|
|
else if (boost::algorithm::ifind_first(query, "WHERE") && !boost::algorithm::ifind_first(query, "JOIN"))
|
2011-12-16 18:05:54 +00:00
|
|
|
{
|
|
|
|
std::string replace(" WHERE " + spatial_sql.str() + " AND ");
|
|
|
|
boost::algorithm::ireplace_first(query, "WHERE", replace);
|
2011-12-16 16:14:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// fallback to appending spatial filter at end of query
|
|
|
|
else if (boost::algorithm::ifind_first(query, geometry_table))
|
|
|
|
{
|
2011-12-16 18:05:54 +00:00
|
|
|
query = table + " WHERE " + spatial_sql.str();
|
2011-12-16 16:14:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-01-26 09:43:31 +00:00
|
|
|
static void get_tables(std::shared_ptr<sqlite_connection> ds, std::vector<std::string>& tables)
|
2011-11-10 03:18:12 +00:00
|
|
|
{
|
|
|
|
std::ostringstream sql;
|
|
|
|
// todo handle finding tables from attached db's
|
|
|
|
sql << " SELECT name FROM sqlite_master"
|
|
|
|
<< " WHERE type IN ('table','view')"
|
|
|
|
<< " AND name NOT LIKE 'sqlite_%'"
|
|
|
|
<< " AND name NOT LIKE 'idx_%'"
|
|
|
|
<< " AND name NOT LIKE '%geometry_columns%'"
|
|
|
|
<< " AND name NOT LIKE '%ref_sys%'"
|
|
|
|
<< " UNION ALL"
|
|
|
|
<< " SELECT name FROM sqlite_temp_master"
|
|
|
|
<< " WHERE type IN ('table','view')"
|
|
|
|
<< " ORDER BY 1";
|
|
|
|
sqlite3_stmt* stmt = 0;
|
2022-01-26 09:43:31 +00:00
|
|
|
const int rc = sqlite3_prepare_v2(*(*ds), sql.str().c_str(), -1, &stmt, 0);
|
2011-11-10 03:18:12 +00:00
|
|
|
if (rc == SQLITE_OK)
|
|
|
|
{
|
2013-09-20 13:00:11 +00:00
|
|
|
std::shared_ptr<sqlite_resultset> rs = std::make_shared<sqlite_resultset>(stmt);
|
2011-11-10 03:18:12 +00:00
|
|
|
while (rs->is_valid() && rs->step_next())
|
|
|
|
{
|
|
|
|
const int type_oid = rs->column_type(0);
|
|
|
|
if (type_oid == SQLITE_TEXT)
|
|
|
|
{
|
2022-01-26 09:43:31 +00:00
|
|
|
const char* data = rs->column_text(0);
|
2011-11-10 03:18:12 +00:00
|
|
|
if (data)
|
|
|
|
{
|
|
|
|
tables.push_back(std::string(data));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-11-14 03:37:50 +00:00
|
|
|
}
|
|
|
|
|
2022-01-26 09:43:31 +00:00
|
|
|
static void query_extent(std::shared_ptr<sqlite_resultset> rs, mapnik::box2d<double>& extent)
|
2009-02-09 19:43:57 +00:00
|
|
|
{
|
2011-11-03 23:51:37 +00:00
|
|
|
bool first = true;
|
|
|
|
while (rs->is_valid() && rs->step_next())
|
2011-10-18 20:19:03 +00:00
|
|
|
{
|
2011-11-03 23:51:37 +00:00
|
|
|
int size;
|
2011-12-06 12:53:16 +00:00
|
|
|
const char* data = static_cast<const char*>(rs->column_blob(0, size));
|
2011-11-03 23:51:37 +00:00
|
|
|
if (data)
|
2011-10-18 20:19:03 +00:00
|
|
|
{
|
2015-04-09 20:22:51 +00:00
|
|
|
mapnik::geometry::geometry<double> geom = mapnik::geometry_utils::from_wkb(data, size, mapnik::wkbAuto);
|
2015-03-24 21:16:58 +00:00
|
|
|
if (!mapnik::geometry::is_empty(geom))
|
2011-11-03 23:51:37 +00:00
|
|
|
{
|
2015-03-24 13:21:28 +00:00
|
|
|
mapnik::box2d<double> bbox = mapnik::geometry::envelope(geom);
|
2015-03-22 21:58:14 +00:00
|
|
|
if (bbox.valid())
|
2011-11-03 23:51:37 +00:00
|
|
|
{
|
2015-03-22 21:58:14 +00:00
|
|
|
if (first)
|
2011-11-03 23:51:37 +00:00
|
|
|
{
|
2015-03-22 21:58:14 +00:00
|
|
|
first = false;
|
|
|
|
extent = bbox;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
extent.expand_to_include(bbox);
|
2011-11-03 23:51:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-10-18 20:19:03 +00:00
|
|
|
}
|
2011-09-15 21:58:20 +00:00
|
|
|
}
|
2009-02-09 19:43:57 +00:00
|
|
|
}
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2011-11-10 03:18:12 +00:00
|
|
|
static bool create_spatial_index(std::string const& index_db,
|
2011-11-03 23:51:37 +00:00
|
|
|
std::string const& index_table,
|
2013-09-20 13:00:11 +00:00
|
|
|
std::shared_ptr<sqlite_resultset> rs)
|
2009-02-09 19:43:57 +00:00
|
|
|
{
|
2011-11-03 23:51:37 +00:00
|
|
|
/* TODO
|
2011-11-14 03:37:50 +00:00
|
|
|
- speedups
|
|
|
|
- return early/recover from failure
|
|
|
|
- allow either in db or in separate
|
2011-11-03 23:51:37 +00:00
|
|
|
*/
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2011-11-03 23:51:37 +00:00
|
|
|
if (!rs->is_valid())
|
2011-11-10 03:18:12 +00:00
|
|
|
return false;
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2011-10-29 04:24:47 +00:00
|
|
|
#if SQLITE_VERSION_NUMBER >= 3005000
|
2011-11-03 23:51:37 +00:00
|
|
|
int flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX;
|
2011-10-29 04:24:47 +00:00
|
|
|
#else
|
2011-11-03 23:51:37 +00:00
|
|
|
int flags;
|
2011-10-29 04:24:47 +00:00
|
|
|
#endif
|
|
|
|
|
2013-06-03 02:28:24 +00:00
|
|
|
bool existed = mapnik::util::exists(index_db);
|
2022-01-26 09:43:31 +00:00
|
|
|
std::shared_ptr<sqlite_connection> ds = std::make_shared<sqlite_connection>(index_db, flags);
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2011-11-10 01:44:50 +00:00
|
|
|
bool one_success = false;
|
|
|
|
try
|
2011-04-01 01:20:34 +00:00
|
|
|
{
|
2011-11-14 00:02:27 +00:00
|
|
|
ds->execute("PRAGMA synchronous=OFF");
|
|
|
|
ds->execute("BEGIN IMMEDIATE TRANSACTION");
|
2011-11-14 03:19:22 +00:00
|
|
|
|
|
|
|
/*
|
2011-11-14 03:37:50 +00:00
|
|
|
ds->execute("PRAGMA journal_mode=WAL");
|
|
|
|
ds->execute("PRAGMA fullfsync=1");
|
|
|
|
ds->execute("PRAGMA locking_mode = EXCLUSIVE");
|
|
|
|
ds->execute("BEGIN EXCLUSIVE TRANSACTION");
|
2011-11-14 03:19:22 +00:00
|
|
|
*/
|
|
|
|
|
2011-11-14 00:02:27 +00:00
|
|
|
// first drop the index if it already exists
|
|
|
|
std::ostringstream spatial_index_drop_sql;
|
|
|
|
spatial_index_drop_sql << "DROP TABLE IF EXISTS " << index_table;
|
|
|
|
ds->execute(spatial_index_drop_sql.str());
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2011-11-14 00:02:27 +00:00
|
|
|
// create the spatial index
|
|
|
|
std::ostringstream create_idx;
|
2022-01-26 09:43:31 +00:00
|
|
|
create_idx << "create virtual table " << index_table << " using rtree(pkid, xmin, xmax, ymin, ymax)";
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2011-11-14 00:02:27 +00:00
|
|
|
// insert for prepared statement
|
|
|
|
std::ostringstream insert_idx;
|
2022-01-26 09:43:31 +00:00
|
|
|
insert_idx << "insert into " << index_table << " values (?,?,?,?,?)";
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2011-11-14 00:02:27 +00:00
|
|
|
ds->execute(create_idx.str());
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2022-01-26 09:43:31 +00:00
|
|
|
prepared_index_statement ps(ds, insert_idx.str());
|
2011-11-14 00:02:27 +00:00
|
|
|
|
2011-11-10 01:44:50 +00:00
|
|
|
while (rs->is_valid() && rs->step_next())
|
2011-11-03 23:51:37 +00:00
|
|
|
{
|
2011-11-10 01:44:50 +00:00
|
|
|
int size;
|
2022-01-26 09:43:31 +00:00
|
|
|
const char* data = (const char*)rs->column_blob(0, size);
|
2011-11-10 01:44:50 +00:00
|
|
|
if (data)
|
2011-11-03 23:51:37 +00:00
|
|
|
{
|
2022-01-26 09:43:31 +00:00
|
|
|
mapnik::geometry::geometry<double> geom =
|
|
|
|
mapnik::geometry_utils::from_wkb(data, size, mapnik::wkbAuto);
|
2015-03-24 21:16:58 +00:00
|
|
|
if (!mapnik::geometry::is_empty(geom))
|
2011-11-03 23:51:37 +00:00
|
|
|
{
|
2015-03-24 13:21:28 +00:00
|
|
|
mapnik::box2d<double> bbox = mapnik::geometry::envelope(geom);
|
2015-03-22 21:58:14 +00:00
|
|
|
if (bbox.valid())
|
2011-12-12 16:30:46 +00:00
|
|
|
{
|
2015-03-22 21:58:14 +00:00
|
|
|
ps.bind(bbox);
|
|
|
|
const int type_oid = rs->column_type(1);
|
|
|
|
if (type_oid != SQLITE_INTEGER)
|
2012-07-20 22:09:01 +00:00
|
|
|
{
|
2015-03-22 21:58:14 +00:00
|
|
|
std::ostringstream error_msg;
|
2022-01-26 09:43:31 +00:00
|
|
|
error_msg << "Sqlite Plugin: invalid type for key field '" << rs->column_name(1)
|
|
|
|
<< "' when creating index '" << index_table << "' type was: " << type_oid
|
|
|
|
<< "";
|
2015-03-22 21:58:14 +00:00
|
|
|
throw mapnik::datasource_exception(error_msg.str());
|
2012-07-20 22:09:01 +00:00
|
|
|
}
|
2015-03-22 21:58:14 +00:00
|
|
|
const sqlite_int64 pkid = rs->column_integer64(1);
|
|
|
|
ps.bind(pkid);
|
2011-12-12 16:30:46 +00:00
|
|
|
}
|
2015-03-22 21:58:14 +00:00
|
|
|
else
|
2011-11-03 23:51:37 +00:00
|
|
|
{
|
|
|
|
std::ostringstream error_msg;
|
2022-01-26 09:43:31 +00:00
|
|
|
error_msg << "SQLite Plugin: encountered invalid bbox at '" << rs->column_name(1)
|
|
|
|
<< "' == " << rs->column_integer64(1);
|
2011-11-03 23:51:37 +00:00
|
|
|
throw mapnik::datasource_exception(error_msg.str());
|
|
|
|
}
|
|
|
|
}
|
2011-12-12 16:30:46 +00:00
|
|
|
ps.step_next();
|
|
|
|
one_success = true;
|
2011-11-03 23:51:37 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-10 15:57:38 +00:00
|
|
|
}
|
|
|
|
catch (mapnik::datasource_exception const& ex)
|
2011-11-10 01:44:50 +00:00
|
|
|
{
|
|
|
|
if (!existed)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2013-06-03 02:28:24 +00:00
|
|
|
mapnik::util::remove(index_db);
|
2022-11-10 15:57:38 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
2022-01-26 09:43:31 +00:00
|
|
|
{};
|
2011-11-10 01:44:50 +00:00
|
|
|
}
|
|
|
|
throw mapnik::datasource_exception(ex.what());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (one_success)
|
|
|
|
{
|
|
|
|
ds->execute("COMMIT");
|
2011-11-10 03:18:12 +00:00
|
|
|
return true;
|
2011-11-10 01:44:50 +00:00
|
|
|
}
|
|
|
|
else if (!existed)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2013-06-03 02:28:24 +00:00
|
|
|
mapnik::util::remove(index_db);
|
2022-11-10 15:57:38 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
2022-01-26 09:43:31 +00:00
|
|
|
{};
|
2011-11-10 01:44:50 +00:00
|
|
|
}
|
2011-11-10 03:18:12 +00:00
|
|
|
return false;
|
2009-02-09 19:43:57 +00:00
|
|
|
}
|
|
|
|
|
2022-01-26 09:43:31 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
2011-11-19 19:36:35 +00:00
|
|
|
sqlite_int64 pkid;
|
|
|
|
mapnik::box2d<double> bbox;
|
|
|
|
} rtree_type;
|
2012-02-02 01:37:35 +00:00
|
|
|
|
2022-01-26 09:43:31 +00:00
|
|
|
static void build_tree(std::shared_ptr<sqlite_resultset> rs, std::vector<sqlite_utils::rtree_type>& rtree_list)
|
2011-11-19 19:36:35 +00:00
|
|
|
{
|
|
|
|
while (rs->is_valid() && rs->step_next())
|
|
|
|
{
|
|
|
|
int size;
|
2011-12-06 12:53:16 +00:00
|
|
|
const char* data = static_cast<const char*>(rs->column_blob(0, size));
|
2011-11-19 19:36:35 +00:00
|
|
|
if (data)
|
|
|
|
{
|
2015-04-09 20:22:51 +00:00
|
|
|
mapnik::geometry::geometry<double> geom = mapnik::geometry_utils::from_wkb(data, size, mapnik::wkbAuto);
|
2015-03-24 21:16:58 +00:00
|
|
|
if (!mapnik::geometry::is_empty(geom))
|
2011-11-19 19:36:35 +00:00
|
|
|
{
|
2015-03-24 13:21:28 +00:00
|
|
|
mapnik::box2d<double> bbox = mapnik::geometry::envelope(geom);
|
2015-03-22 21:58:14 +00:00
|
|
|
if (bbox.valid())
|
2011-11-19 19:36:35 +00:00
|
|
|
{
|
2015-03-22 21:58:14 +00:00
|
|
|
const int type_oid = rs->column_type(1);
|
|
|
|
if (type_oid != SQLITE_INTEGER)
|
2011-11-19 19:36:35 +00:00
|
|
|
{
|
|
|
|
std::ostringstream error_msg;
|
2022-01-26 09:43:31 +00:00
|
|
|
error_msg << "Sqlite Plugin: invalid type for key field '" << rs->column_name(1)
|
|
|
|
<< "' when creating index "
|
2015-03-22 21:58:14 +00:00
|
|
|
<< "type was: " << type_oid << "";
|
2011-11-19 19:36:35 +00:00
|
|
|
throw mapnik::datasource_exception(error_msg.str());
|
|
|
|
}
|
2015-03-22 21:58:14 +00:00
|
|
|
const sqlite_int64 pkid = rs->column_integer64(1);
|
|
|
|
rtree_type entry = rtree_type();
|
|
|
|
entry.pkid = pkid;
|
|
|
|
entry.bbox = bbox;
|
|
|
|
rtree_list.push_back(entry);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::ostringstream error_msg;
|
2022-01-26 09:43:31 +00:00
|
|
|
error_msg << "SQLite Plugin: encountered invalid bbox at '" << rs->column_name(1)
|
|
|
|
<< "' == " << rs->column_integer64(1);
|
2015-03-22 21:58:14 +00:00
|
|
|
throw mapnik::datasource_exception(error_msg.str());
|
2011-11-19 19:36:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool create_spatial_index2(std::string const& index_db,
|
2012-02-02 01:37:35 +00:00
|
|
|
std::string const& index_table,
|
|
|
|
std::vector<rtree_type> const& rtree_list)
|
2011-11-19 19:36:35 +00:00
|
|
|
{
|
|
|
|
#if SQLITE_VERSION_NUMBER >= 3005000
|
|
|
|
int flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX;
|
|
|
|
#else
|
|
|
|
int flags;
|
|
|
|
#endif
|
|
|
|
|
2022-01-26 09:43:31 +00:00
|
|
|
bool existed = mapnik::util::exists(index_db);
|
|
|
|
;
|
2013-09-20 13:00:11 +00:00
|
|
|
|
2022-01-26 09:43:31 +00:00
|
|
|
std::shared_ptr<sqlite_connection> ds = std::make_shared<sqlite_connection>(index_db, flags);
|
2011-11-19 19:36:35 +00:00
|
|
|
|
|
|
|
bool one_success = false;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
ds->execute("PRAGMA synchronous=OFF");
|
|
|
|
ds->execute("BEGIN IMMEDIATE TRANSACTION");
|
|
|
|
|
|
|
|
// first drop the index if it already exists
|
|
|
|
std::ostringstream spatial_index_drop_sql;
|
|
|
|
spatial_index_drop_sql << "DROP TABLE IF EXISTS " << index_table;
|
|
|
|
ds->execute(spatial_index_drop_sql.str());
|
|
|
|
|
|
|
|
// create the spatial index
|
|
|
|
std::ostringstream create_idx;
|
2022-01-26 09:43:31 +00:00
|
|
|
create_idx << "create virtual table " << index_table << " using rtree(pkid, xmin, xmax, ymin, ymax)";
|
2011-11-19 19:36:35 +00:00
|
|
|
|
|
|
|
// insert for prepared statement
|
|
|
|
std::ostringstream insert_idx;
|
2022-01-26 09:43:31 +00:00
|
|
|
insert_idx << "insert into " << index_table << " values (?,?,?,?,?)";
|
2011-11-19 19:36:35 +00:00
|
|
|
|
|
|
|
ds->execute(create_idx.str());
|
|
|
|
|
2022-01-26 09:43:31 +00:00
|
|
|
prepared_index_statement ps(ds, insert_idx.str());
|
2011-11-19 19:36:35 +00:00
|
|
|
|
|
|
|
std::vector<rtree_type>::const_iterator it = rtree_list.begin();
|
|
|
|
std::vector<rtree_type>::const_iterator end = rtree_list.end();
|
|
|
|
for (; it != end; ++it)
|
|
|
|
{
|
2012-02-02 01:37:35 +00:00
|
|
|
ps.bind(it->bbox);
|
|
|
|
ps.bind(it->pkid);
|
|
|
|
ps.step_next();
|
|
|
|
one_success = true;
|
2011-11-19 19:36:35 +00:00
|
|
|
}
|
2022-11-10 15:57:38 +00:00
|
|
|
}
|
|
|
|
catch (mapnik::datasource_exception const& ex)
|
2011-11-19 19:36:35 +00:00
|
|
|
{
|
|
|
|
if (!existed)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2013-06-03 02:28:24 +00:00
|
|
|
mapnik::util::remove(index_db);
|
2022-11-10 15:57:38 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
2022-01-26 09:43:31 +00:00
|
|
|
{};
|
2011-11-19 19:36:35 +00:00
|
|
|
}
|
|
|
|
throw mapnik::datasource_exception(ex.what());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (one_success)
|
|
|
|
{
|
|
|
|
ds->execute("COMMIT");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (!existed)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2013-06-03 02:28:24 +00:00
|
|
|
mapnik::util::remove(index_db);
|
2022-11-10 15:57:38 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
2022-01-26 09:43:31 +00:00
|
|
|
{};
|
2011-11-19 19:36:35 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2012-02-02 01:37:35 +00:00
|
|
|
|
2013-09-20 13:00:11 +00:00
|
|
|
static bool detect_extent(std::shared_ptr<sqlite_connection> ds,
|
2011-11-03 23:51:37 +00:00
|
|
|
bool has_spatial_index,
|
2022-01-26 09:43:31 +00:00
|
|
|
mapnik::box2d<double>& extent,
|
2011-11-03 23:51:37 +00:00
|
|
|
std::string const& index_table,
|
|
|
|
std::string const& metadata,
|
|
|
|
std::string const& geometry_field,
|
|
|
|
std::string const& geometry_table,
|
|
|
|
std::string const& key_field,
|
2022-01-26 09:43:31 +00:00
|
|
|
std::string const& table)
|
2009-02-09 19:43:57 +00:00
|
|
|
{
|
2022-01-26 09:43:31 +00:00
|
|
|
if (!metadata.empty())
|
2013-01-03 15:44:05 +00:00
|
|
|
{
|
|
|
|
std::ostringstream s;
|
|
|
|
s << "SELECT xmin, ymin, xmax, ymax FROM " << metadata;
|
|
|
|
s << " WHERE LOWER(f_table_name) = LOWER('" << geometry_table << "')";
|
2013-07-03 14:54:42 +00:00
|
|
|
MAPNIK_LOG_DEBUG(sqlite) << "sqlite_datasource: executing: '" << s.str() << "'";
|
2013-09-20 13:00:11 +00:00
|
|
|
std::shared_ptr<sqlite_resultset> rs(ds->execute_query(s.str()));
|
2013-01-03 15:44:05 +00:00
|
|
|
if (rs->is_valid() && rs->step_next())
|
|
|
|
{
|
|
|
|
double xmin = rs->column_double(0);
|
|
|
|
double ymin = rs->column_double(1);
|
|
|
|
double xmax = rs->column_double(2);
|
|
|
|
double ymax = rs->column_double(3);
|
2022-01-26 09:43:31 +00:00
|
|
|
extent.init(xmin, ymin, xmax, ymax);
|
2013-01-03 15:44:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (has_spatial_index)
|
2011-10-18 20:19:03 +00:00
|
|
|
{
|
2011-11-03 23:51:37 +00:00
|
|
|
std::ostringstream s;
|
2022-01-26 09:43:31 +00:00
|
|
|
s << "SELECT MIN(xmin), MIN(ymin), MAX(xmax), MAX(ymax) FROM " << index_table;
|
2013-07-03 14:54:42 +00:00
|
|
|
MAPNIK_LOG_DEBUG(sqlite) << "sqlite_datasource: executing: '" << s.str() << "'";
|
2013-09-20 13:00:11 +00:00
|
|
|
std::shared_ptr<sqlite_resultset> rs(ds->execute_query(s.str()));
|
2011-11-03 23:51:37 +00:00
|
|
|
if (rs->is_valid() && rs->step_next())
|
|
|
|
{
|
2022-01-26 09:43:31 +00:00
|
|
|
if (!rs->column_isnull(0))
|
2011-11-03 23:51:37 +00:00
|
|
|
{
|
2012-06-22 20:49:53 +00:00
|
|
|
double xmin = rs->column_double(0);
|
|
|
|
double ymin = rs->column_double(1);
|
|
|
|
double xmax = rs->column_double(2);
|
|
|
|
double ymax = rs->column_double(3);
|
2022-01-26 09:43:31 +00:00
|
|
|
extent.init(xmin, ymin, xmax, ymax);
|
2012-06-22 20:49:53 +00:00
|
|
|
return true;
|
2011-11-03 23:51:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-01-03 15:44:05 +00:00
|
|
|
|
2022-01-26 09:43:31 +00:00
|
|
|
else if (!key_field.empty())
|
2011-07-11 17:46:53 +00:00
|
|
|
{
|
2011-11-03 23:51:37 +00:00
|
|
|
std::ostringstream s;
|
2022-01-26 09:43:31 +00:00
|
|
|
s << "SELECT " << geometry_field << "," << key_field << " FROM (" << table << ")";
|
2013-07-03 14:54:42 +00:00
|
|
|
MAPNIK_LOG_DEBUG(sqlite) << "sqlite_datasource: executing: '" << s.str() << "'";
|
2013-09-20 13:00:11 +00:00
|
|
|
std::shared_ptr<sqlite_resultset> rs(ds->execute_query(s.str()));
|
2022-01-26 09:43:31 +00:00
|
|
|
sqlite_utils::query_extent(rs, extent);
|
2011-11-03 23:51:37 +00:00
|
|
|
return true;
|
2011-07-11 17:46:53 +00:00
|
|
|
}
|
2011-11-03 23:51:37 +00:00
|
|
|
return false;
|
2011-07-11 17:46:53 +00:00
|
|
|
}
|
2009-02-09 19:43:57 +00:00
|
|
|
|
2022-01-26 09:43:31 +00:00
|
|
|
static bool has_rtree(std::string const& index_table, std::shared_ptr<sqlite_connection> ds)
|
2011-10-29 04:24:47 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
std::ostringstream s;
|
|
|
|
s << "SELECT pkid,xmin,xmax,ymin,ymax FROM " << index_table << " LIMIT 1";
|
2013-09-20 13:00:11 +00:00
|
|
|
std::shared_ptr<sqlite_resultset> rs = ds->execute_query(s.str());
|
2011-10-29 04:24:47 +00:00
|
|
|
if (rs->is_valid() && rs->step_next())
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2022-11-10 15:57:38 +00:00
|
|
|
}
|
|
|
|
catch (std::exception const& ex)
|
2011-10-29 04:24:47 +00:00
|
|
|
{
|
2022-01-26 09:43:31 +00:00
|
|
|
MAPNIK_LOG_DEBUG(sqlite) << "has_rtree returned:" << ex.what();
|
2011-10-29 04:24:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-11-03 23:51:37 +00:00
|
|
|
static bool detect_types_from_subquery(std::string const& query,
|
2022-01-26 09:43:31 +00:00
|
|
|
std::string& geometry_field,
|
|
|
|
mapnik::layer_descriptor& desc,
|
2013-09-20 13:00:11 +00:00
|
|
|
std::shared_ptr<sqlite_connection> ds)
|
2011-10-29 04:24:47 +00:00
|
|
|
{
|
|
|
|
bool found = false;
|
2013-09-20 13:00:11 +00:00
|
|
|
std::shared_ptr<sqlite_resultset> rs(ds->execute_query(query));
|
2011-10-29 04:24:47 +00:00
|
|
|
if (rs->is_valid() && rs->step_next())
|
|
|
|
{
|
|
|
|
for (int i = 0; i < rs->column_count(); ++i)
|
|
|
|
{
|
|
|
|
found = true;
|
|
|
|
const int type_oid = rs->column_type(i);
|
|
|
|
const char* fld_name = rs->column_name(i);
|
|
|
|
switch (type_oid)
|
|
|
|
{
|
2022-01-26 09:43:31 +00:00
|
|
|
case SQLITE_INTEGER:
|
|
|
|
desc.add_descriptor(mapnik::attribute_descriptor(fld_name, mapnik::Integer));
|
|
|
|
break;
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2022-01-26 09:43:31 +00:00
|
|
|
case SQLITE_FLOAT:
|
|
|
|
desc.add_descriptor(mapnik::attribute_descriptor(fld_name, mapnik::Double));
|
|
|
|
break;
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2022-01-26 09:43:31 +00:00
|
|
|
case SQLITE_TEXT:
|
|
|
|
desc.add_descriptor(mapnik::attribute_descriptor(fld_name, mapnik::String));
|
|
|
|
break;
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2022-01-26 09:43:31 +00:00
|
|
|
case SQLITE_NULL:
|
|
|
|
// sqlite reports based on value, not actual column type unless
|
|
|
|
// PRAGMA table_info is used so here we assume the column is a string
|
|
|
|
// which is a lesser evil than altogether dropping the column
|
|
|
|
desc.add_descriptor(mapnik::attribute_descriptor(fld_name, mapnik::String));
|
|
|
|
break;
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2022-01-26 09:43:31 +00:00
|
|
|
case SQLITE_BLOB:
|
|
|
|
if (geometry_field.empty() && (boost::algorithm::icontains(fld_name, "geom") ||
|
|
|
|
boost::algorithm::icontains(fld_name, "point") ||
|
|
|
|
boost::algorithm::icontains(fld_name, "linestring") ||
|
|
|
|
boost::algorithm::icontains(fld_name, "polygon")))
|
|
|
|
{
|
|
|
|
geometry_field = std::string(fld_name);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
MAPNIK_LOG_DEBUG(sqlite) << "detect_types_from_subquery: unknown type_oid=" << type_oid;
|
|
|
|
break;
|
2011-10-29 04:24:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2011-10-29 04:24:47 +00:00
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2022-01-26 09:43:31 +00:00
|
|
|
static bool table_info(std::string& key_field,
|
2011-11-14 03:37:50 +00:00
|
|
|
bool detected_types,
|
2022-01-26 09:43:31 +00:00
|
|
|
std::string& field,
|
|
|
|
std::string& table,
|
|
|
|
mapnik::layer_descriptor& desc,
|
2013-09-20 13:00:11 +00:00
|
|
|
std::shared_ptr<sqlite_connection> ds)
|
2011-10-29 04:24:47 +00:00
|
|
|
{
|
|
|
|
// http://www.sqlite.org/pragma.html#pragma_table_info
|
|
|
|
// use pragma table_info to detect primary key
|
2011-11-14 03:37:50 +00:00
|
|
|
// and to detect types if no subquery is used or
|
2011-10-29 04:24:47 +00:00
|
|
|
// if the subquery-based type detection failed
|
|
|
|
std::ostringstream s;
|
|
|
|
s << "PRAGMA table_info(" << table << ")";
|
2013-09-20 13:00:11 +00:00
|
|
|
std::shared_ptr<sqlite_resultset> rs(ds->execute_query(s.str()));
|
2011-10-29 04:24:47 +00:00
|
|
|
bool found_table = false;
|
|
|
|
bool found_pk = false;
|
|
|
|
while (rs->is_valid() && rs->step_next())
|
|
|
|
{
|
|
|
|
found_table = true;
|
|
|
|
const char* fld_name = rs->column_text(1);
|
|
|
|
std::string fld_type(rs->column_text(2));
|
2012-12-20 03:24:52 +00:00
|
|
|
sqlite_int64 fld_pk = rs->column_integer64(5);
|
2013-01-04 03:27:53 +00:00
|
|
|
std::transform(fld_type.begin(), fld_type.end(), fld_type.begin(), ::tolower);
|
2011-10-29 04:24:47 +00:00
|
|
|
// TODO - how to handle primary keys on multiple columns ?
|
2022-01-26 09:43:31 +00:00
|
|
|
if (key_field.empty() && !found_pk && fld_pk != 0)
|
2011-10-29 04:24:47 +00:00
|
|
|
{
|
|
|
|
key_field = fld_name;
|
|
|
|
found_pk = true;
|
|
|
|
}
|
2022-01-26 09:43:31 +00:00
|
|
|
if (!detected_types)
|
2011-10-29 04:24:47 +00:00
|
|
|
{
|
|
|
|
// see 2.1 "Column Affinity" at http://www.sqlite.org/datatype3.html
|
|
|
|
// TODO - refactor this somehow ?
|
2022-01-26 09:43:31 +00:00
|
|
|
if (field.empty() &&
|
|
|
|
((boost::algorithm::contains(fld_type, "geom") || boost::algorithm::contains(fld_type, "point") ||
|
|
|
|
boost::algorithm::contains(fld_type, "linestring") ||
|
|
|
|
boost::algorithm::contains(fld_type, "polygon")) ||
|
|
|
|
(boost::algorithm::icontains(fld_name, "geom") || boost::algorithm::icontains(fld_name, "point") ||
|
|
|
|
boost::algorithm::icontains(fld_name, "linestring") ||
|
|
|
|
boost::algorithm::icontains(fld_name, "polygon"))))
|
2011-10-29 04:24:47 +00:00
|
|
|
{
|
|
|
|
field = std::string(fld_name);
|
|
|
|
}
|
|
|
|
else if (boost::algorithm::contains(fld_type, "int"))
|
|
|
|
{
|
|
|
|
desc.add_descriptor(mapnik::attribute_descriptor(fld_name, mapnik::Integer));
|
|
|
|
}
|
2022-01-26 09:43:31 +00:00
|
|
|
else if (boost::algorithm::contains(fld_type, "text") || boost::algorithm::contains(fld_type, "char") ||
|
2011-10-29 04:24:47 +00:00
|
|
|
boost::algorithm::contains(fld_type, "clob"))
|
|
|
|
{
|
|
|
|
desc.add_descriptor(mapnik::attribute_descriptor(fld_name, mapnik::String));
|
|
|
|
}
|
2022-01-26 09:43:31 +00:00
|
|
|
else if (boost::algorithm::contains(fld_type, "real") || boost::algorithm::contains(fld_type, "floa") ||
|
2014-11-25 10:09:42 +00:00
|
|
|
boost::algorithm::contains(fld_type, "doub"))
|
2011-10-29 04:24:47 +00:00
|
|
|
{
|
|
|
|
desc.add_descriptor(mapnik::attribute_descriptor(fld_name, mapnik::Double));
|
|
|
|
}
|
|
|
|
else if (boost::algorithm::contains(fld_type, "blob"))
|
|
|
|
{
|
2022-01-26 09:43:31 +00:00
|
|
|
if (!field.empty())
|
2011-10-29 04:24:47 +00:00
|
|
|
{
|
|
|
|
desc.add_descriptor(mapnik::attribute_descriptor(fld_name, mapnik::String));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// "Column Affinity" says default to "Numeric" but for now we pass..
|
2022-01-26 09:43:31 +00:00
|
|
|
// desc_.add_descriptor(attribute_descriptor(fld_name,mapnik::Double));
|
2014-11-25 10:09:42 +00:00
|
|
|
|
2014-11-20 17:48:31 +00:00
|
|
|
desc.add_descriptor(mapnik::attribute_descriptor(fld_name, mapnik::String));
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2013-03-27 08:02:40 +00:00
|
|
|
#ifdef MAPNIK_LOG
|
|
|
|
// Do not fail when we specify geometry_field in XML file
|
|
|
|
if (field.empty())
|
|
|
|
{
|
2022-01-26 09:43:31 +00:00
|
|
|
MAPNIK_LOG_DEBUG(sqlite)
|
|
|
|
<< "Column '" << std::string(fld_name) << "' unhandled due to unknown type: '" << fld_type
|
|
|
|
<< "', using String";
|
2013-03-27 08:02:40 +00:00
|
|
|
}
|
2011-11-14 03:37:50 +00:00
|
|
|
#endif
|
2013-03-27 08:02:40 +00:00
|
|
|
}
|
2011-10-29 04:24:47 +00:00
|
|
|
}
|
|
|
|
}
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2011-10-29 04:24:47 +00:00
|
|
|
return found_table;
|
|
|
|
}
|
2009-02-09 19:43:57 +00:00
|
|
|
};
|
|
|
|
|
2011-11-03 23:51:37 +00:00
|
|
|
#endif // MAPNIK_SQLITE_UTILS_HPP
|