2011-11-04 00:51:37 +01:00
|
|
|
/*****************************************************************************
|
2011-11-14 04:37:50 +01:00
|
|
|
*
|
2011-11-04 00:51:37 +01:00
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2016-12-20 17:57:22 +01:00
|
|
|
* Copyright (C) 2016 Artem Pavlenko
|
2011-11-04 00:51:37 +01: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
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef MAPNIK_SQLITE_PREPARED_HPP
|
|
|
|
#define MAPNIK_SQLITE_PREPARED_HPP
|
|
|
|
|
|
|
|
// mapnik
|
2013-03-27 09:02:40 +01:00
|
|
|
#include <mapnik/debug.hpp>
|
2011-11-04 00:51:37 +01:00
|
|
|
#include <mapnik/datasource.hpp>
|
2012-07-25 04:21:55 +02:00
|
|
|
#include <mapnik/params.hpp>
|
2011-11-04 00:51:37 +01:00
|
|
|
#include <mapnik/box2d.hpp>
|
2015-01-07 13:11:09 +01:00
|
|
|
#include <mapnik/util/noncopyable.hpp>
|
2011-11-04 00:51:37 +01:00
|
|
|
|
|
|
|
// boost
|
2013-09-20 15:13:23 +02:00
|
|
|
#include <memory>
|
2011-11-04 00:51:37 +01:00
|
|
|
|
|
|
|
// stl
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "sqlite_connection.hpp"
|
|
|
|
|
|
|
|
// sqlite
|
|
|
|
extern "C" {
|
2011-11-14 04:37:50 +01:00
|
|
|
#include <sqlite3.h>
|
2011-11-04 00:51:37 +01:00
|
|
|
}
|
|
|
|
|
2015-01-07 13:11:09 +01:00
|
|
|
class prepared_index_statement : mapnik::util::noncopyable
|
2011-11-04 00:51:37 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
public:
|
2013-09-20 15:00:11 +02:00
|
|
|
prepared_index_statement(std::shared_ptr<sqlite_connection> ds, std::string const& sql)
|
2011-11-04 00:51:37 +01:00
|
|
|
: ds_(ds),
|
|
|
|
stmt_(0)
|
|
|
|
{
|
|
|
|
|
|
|
|
const int rc = sqlite3_prepare_v2(*(*ds_),
|
2011-11-14 04:37:50 +01:00
|
|
|
sql.c_str(),
|
|
|
|
-1,
|
|
|
|
&stmt_,
|
|
|
|
0);
|
2011-11-04 00:51:37 +01:00
|
|
|
|
|
|
|
if (rc != SQLITE_OK)
|
|
|
|
{
|
|
|
|
std::ostringstream index_error;
|
|
|
|
index_error << "Sqlite Plugin: auto-index table creation failed: '"
|
|
|
|
<< sqlite3_errmsg(*(*ds_)) << "' query was: "
|
|
|
|
<< sql;
|
|
|
|
|
|
|
|
throw mapnik::datasource_exception(index_error.str());
|
|
|
|
}
|
|
|
|
}
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2011-11-04 00:51:37 +01:00
|
|
|
~prepared_index_statement()
|
|
|
|
{
|
|
|
|
if (stmt_)
|
|
|
|
{
|
|
|
|
int res = sqlite3_finalize(stmt_);
|
|
|
|
if (res != SQLITE_OK)
|
|
|
|
{
|
|
|
|
if (*(*ds_))
|
|
|
|
{
|
2013-03-27 09:02:40 +01:00
|
|
|
MAPNIK_LOG_ERROR(sqlite) << "~prepared_index_statement:"
|
|
|
|
<< sqlite3_errmsg(*(*ds_));
|
2011-11-04 00:51:37 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-03-27 09:02:40 +01:00
|
|
|
MAPNIK_LOG_ERROR(sqlite) << "~prepared_index_statement:"
|
|
|
|
<< res;
|
2011-11-04 00:51:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2011-11-04 00:51:37 +01:00
|
|
|
void bind(sqlite_int64 const pkid)
|
|
|
|
{
|
|
|
|
if (sqlite3_bind_int64(stmt_, 1, pkid) != SQLITE_OK)
|
|
|
|
{
|
|
|
|
throw mapnik::datasource_exception("SQLite Plugin: invalid value for for key field while generating index");
|
|
|
|
}
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2011-11-04 00:51:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void bind(mapnik::box2d<double> const& bbox)
|
|
|
|
{
|
|
|
|
if ((sqlite3_bind_double(stmt_, 2, bbox.minx()) != SQLITE_OK) ||
|
|
|
|
(sqlite3_bind_double(stmt_, 3, bbox.maxx()) != SQLITE_OK) ||
|
|
|
|
(sqlite3_bind_double(stmt_, 4, bbox.miny()) != SQLITE_OK) ||
|
|
|
|
(sqlite3_bind_double(stmt_, 5, bbox.maxy()) != SQLITE_OK))
|
|
|
|
{
|
|
|
|
throw mapnik::datasource_exception("SQLite Plugin: invalid value for for extent while generating index");
|
|
|
|
}
|
|
|
|
}
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2011-11-04 00:51:37 +01:00
|
|
|
bool step_next ()
|
|
|
|
{
|
|
|
|
const int status = sqlite3_step(stmt_);
|
2011-11-14 01:41:34 +01:00
|
|
|
if (status != SQLITE_DONE)
|
2011-11-04 00:51:37 +01:00
|
|
|
{
|
|
|
|
std::ostringstream s;
|
|
|
|
s << "SQLite Plugin: inserting bbox into rtree index failed";
|
|
|
|
std::string msg(sqlite3_errmsg(sqlite3_db_handle(stmt_)));
|
|
|
|
if (msg != "unknown error")
|
|
|
|
{
|
|
|
|
s << ": " << msg;
|
|
|
|
}
|
|
|
|
throw mapnik::datasource_exception(s.str());
|
|
|
|
}
|
2011-11-14 01:41:34 +01:00
|
|
|
sqlite3_clear_bindings(stmt_);
|
|
|
|
if (sqlite3_reset(stmt_) != SQLITE_OK)
|
|
|
|
{
|
|
|
|
throw mapnik::datasource_exception("sqlite3_reset failed");
|
|
|
|
}
|
|
|
|
return true;
|
2011-11-04 00:51:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-09-20 15:00:11 +02:00
|
|
|
std::shared_ptr<sqlite_connection> ds_;
|
2011-11-04 00:51:37 +01:00
|
|
|
sqlite3_stmt * stmt_;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // MAPNIK_SQLITE_PREPARED_HPP
|