1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
/*****************************************************************************
*
* 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/debug.hpp>
#include <mapnik/datasource.hpp>
#include <mapnik/params.hpp>
#include <mapnik/box2d.hpp>
#include <mapnik/noncopyable.hpp>
// boost
#include <boost/shared_ptr.hpp>
// stl
#include <string.h>
#include "sqlite_connection.hpp"
// sqlite
extern "C" {
#include <sqlite3.h>
}
class prepared_index_statement : mapnik::noncopyable
{
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_),
sql.c_str(),
-1,
&stmt_,
0);
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());
}
}
~prepared_index_statement()
{
if (stmt_)
{
int res = sqlite3_finalize(stmt_);
if (res != SQLITE_OK)
{
if (*(*ds_))
{
MAPNIK_LOG_ERROR(sqlite) << "~prepared_index_statement:"
<< sqlite3_errmsg(*(*ds_));
}
else
{
MAPNIK_LOG_ERROR(sqlite) << "~prepared_index_statement:"
<< res;
}
}
}
}
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");
}
}
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");
}
}
bool step_next ()
{
const int status = sqlite3_step(stmt_);
if (status != SQLITE_DONE)
{
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());
}
sqlite3_clear_bindings(stmt_);
if (sqlite3_reset(stmt_) != SQLITE_OK)
{
throw mapnik::datasource_exception("sqlite3_reset failed");
}
return true;
}
private:
boost::shared_ptr<sqlite_connection> ds_;
sqlite3_stmt * stmt_;
};
#endif // MAPNIK_SQLITE_PREPARED_HPP
|