2011-11-03 23:51:37 +00:00
|
|
|
/*****************************************************************************
|
2011-11-14 03:37:50 +00:00
|
|
|
*
|
2011-11-03 23:51:37 +00:00
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* 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
|
|
|
|
#include <mapnik/datasource.hpp>
|
2012-07-25 02:21:55 +00:00
|
|
|
#include <mapnik/params.hpp>
|
2011-11-03 23:51:37 +00:00
|
|
|
#include <mapnik/box2d.hpp>
|
2012-12-17 02:19:52 +00:00
|
|
|
#include <mapnik/noncopyable.hpp>
|
2011-11-03 23:51:37 +00:00
|
|
|
|
|
|
|
// boost
|
|
|
|
#include <boost/shared_ptr.hpp>
|
|
|
|
|
|
|
|
// stl
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "sqlite_connection.hpp"
|
|
|
|
|
|
|
|
// sqlite
|
|
|
|
extern "C" {
|
2011-11-14 03:37:50 +00:00
|
|
|
#include <sqlite3.h>
|
2011-11-03 23:51:37 +00:00
|
|
|
}
|
|
|
|
|
2012-12-17 02:19:52 +00:00
|
|
|
class prepared_index_statement : mapnik::noncopyable
|
2011-11-03 23:51:37 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
public:
|
|
|
|
prepared_index_statement(boost::shared_ptr<sqlite_connection> ds, std::string const& sql)
|
|
|
|
: ds_(ds),
|
|
|
|
stmt_(0)
|
|
|
|
{
|
|
|
|
|
|
|
|
const int rc = sqlite3_prepare_v2(*(*ds_),
|
2011-11-14 03:37:50 +00:00
|
|
|
sql.c_str(),
|
|
|
|
-1,
|
|
|
|
&stmt_,
|
|
|
|
0);
|
2011-11-03 23:51:37 +00: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 03:37:50 +00:00
|
|
|
|
2011-11-03 23:51:37 +00:00
|
|
|
~prepared_index_statement()
|
|
|
|
{
|
|
|
|
if (stmt_)
|
|
|
|
{
|
|
|
|
int res = sqlite3_finalize(stmt_);
|
|
|
|
if (res != SQLITE_OK)
|
|
|
|
{
|
|
|
|
if (*(*ds_))
|
|
|
|
{
|
|
|
|
std::cerr << "ERR:" << sqlite3_errmsg(*(*ds_)) << "\n";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << "SQLite Plugin: " << res << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2011-11-03 23:51:37 +00: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 03:37:50 +00:00
|
|
|
|
2011-11-03 23:51:37 +00: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 03:37:50 +00:00
|
|
|
|
2011-11-03 23:51:37 +00:00
|
|
|
bool step_next ()
|
|
|
|
{
|
|
|
|
const int status = sqlite3_step(stmt_);
|
2011-11-14 00:41:34 +00:00
|
|
|
if (status != SQLITE_DONE)
|
2011-11-03 23:51:37 +00: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 00:41:34 +00:00
|
|
|
sqlite3_clear_bindings(stmt_);
|
|
|
|
if (sqlite3_reset(stmt_) != SQLITE_OK)
|
|
|
|
{
|
|
|
|
throw mapnik::datasource_exception("sqlite3_reset failed");
|
|
|
|
}
|
|
|
|
return true;
|
2011-11-03 23:51:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
boost::shared_ptr<sqlite_connection> ds_;
|
|
|
|
sqlite3_stmt * stmt_;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // MAPNIK_SQLITE_PREPARED_HPP
|