2009-02-20 16:58:33 +01:00
|
|
|
/*****************************************************************************
|
2011-11-14 04:54:32 +01:00
|
|
|
*
|
2009-02-20 16:58:33 +01:00
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2015-06-16 12:49:16 +02:00
|
|
|
* Copyright (C) 2015 Artem Pavlenko
|
2009-02-20 16:58:33 +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
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2015-01-07 13:11:09 +01:00
|
|
|
#include <mapnik/util/noncopyable.hpp>
|
2014-08-12 14:40:45 +02:00
|
|
|
#include <mapnik/util/variant.hpp>
|
2009-02-20 16:58:33 +01:00
|
|
|
// boost
|
2013-09-20 15:13:23 +02:00
|
|
|
#include <memory>
|
2009-02-20 16:58:33 +01:00
|
|
|
//sqlite3
|
|
|
|
#include <sqlite3.h>
|
|
|
|
|
|
|
|
//stl
|
2014-08-12 20:10:32 +02:00
|
|
|
#ifdef MAPNIK_DEBUG
|
|
|
|
#include <cassert>
|
|
|
|
#endif
|
2014-08-12 21:08:52 +02:00
|
|
|
|
|
|
|
#include <iostream>
|
2009-02-20 16:58:33 +01:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace mapnik { namespace sqlite {
|
|
|
|
|
2015-01-07 13:11:09 +01:00
|
|
|
class database : private util::noncopyable
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
|
|
|
friend class prepared_statement;
|
2011-11-14 04:54:32 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
struct database_closer
|
|
|
|
{
|
2009-02-20 16:58:33 +01:00
|
|
|
void operator () (sqlite3 * db)
|
|
|
|
{
|
|
|
|
#ifdef MAPNIK_DEBUG
|
2010-06-02 13:03:30 +02:00
|
|
|
std::cerr << "close database " << db << "\n";
|
2009-02-20 16:58:33 +01:00
|
|
|
#endif
|
2010-06-02 13:03:30 +02:00
|
|
|
sqlite3_close(db);
|
2009-02-20 16:58:33 +01:00
|
|
|
}
|
2010-06-02 13:03:30 +02:00
|
|
|
};
|
2011-11-14 04:54:32 +01:00
|
|
|
|
2014-07-07 19:23:15 +02:00
|
|
|
using sqlite_db = std::shared_ptr<sqlite3>;
|
2010-06-02 13:03:30 +02:00
|
|
|
sqlite_db db_;
|
2011-11-14 04:54:32 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
public:
|
|
|
|
database(std::string const& name);
|
|
|
|
~database();
|
|
|
|
bool execute(std::string const& sql);
|
|
|
|
};
|
2009-02-20 16:58:33 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
struct null_type {};
|
2011-11-14 04:54:32 +01:00
|
|
|
struct blob
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
|
|
|
blob(const char* buf, unsigned size)
|
2009-02-21 12:22:17 +01:00
|
|
|
: buf_(buf), size_(size) {}
|
2011-11-14 04:54:32 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
const char * buf_;
|
|
|
|
unsigned size_;
|
|
|
|
};
|
2009-02-21 12:22:17 +01:00
|
|
|
|
2014-08-12 14:40:45 +02:00
|
|
|
using value_type = mapnik::util::variant<int,double,std::string, blob,null_type>;
|
2014-07-07 19:23:15 +02:00
|
|
|
using record_type = std::vector<value_type>;
|
2011-11-14 04:54:32 +01:00
|
|
|
|
2015-01-07 13:11:09 +01:00
|
|
|
class prepared_statement : util::noncopyable
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2015-01-07 11:35:21 +01:00
|
|
|
struct binder
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2009-02-20 16:58:33 +01:00
|
|
|
binder(sqlite3_stmt * stmt, unsigned index)
|
2010-06-02 13:03:30 +02:00
|
|
|
: stmt_(stmt), index_(index) {}
|
2011-11-14 04:54:32 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
bool operator() (null_type )
|
|
|
|
{
|
|
|
|
if (sqlite3_bind_null(stmt_, index_) != SQLITE_OK)
|
|
|
|
{
|
2013-11-27 16:52:47 +01:00
|
|
|
std::cerr << "cannot bind nullptr\n";
|
2010-06-02 13:03:30 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2011-11-14 04:54:32 +01:00
|
|
|
|
2009-02-20 16:58:33 +01:00
|
|
|
bool operator() (int val)
|
|
|
|
{
|
2010-06-02 13:03:30 +02:00
|
|
|
if (sqlite3_bind_int(stmt_, index_ , val ) != SQLITE_OK)
|
|
|
|
{
|
|
|
|
std::cerr << "cannot bind " << val << "\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2009-02-20 16:58:33 +01:00
|
|
|
}
|
2011-11-14 04:54:32 +01:00
|
|
|
|
2009-02-20 16:58:33 +01:00
|
|
|
bool operator() (double val)
|
|
|
|
{
|
2010-06-02 13:03:30 +02:00
|
|
|
if (sqlite3_bind_double(stmt_, index_ , val ) != SQLITE_OK)
|
|
|
|
{
|
|
|
|
std::cerr << "cannot bind " << val << "\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2009-02-20 16:58:33 +01:00
|
|
|
}
|
2011-11-14 04:54:32 +01:00
|
|
|
|
2009-02-20 16:58:33 +01:00
|
|
|
bool operator() (std::string const& val)
|
|
|
|
{
|
2010-06-02 13:03:30 +02:00
|
|
|
if (sqlite3_bind_text(stmt_, index_, val.c_str(), val.length(), SQLITE_STATIC) != SQLITE_OK)
|
|
|
|
{
|
|
|
|
std::cerr << "cannot bind " << val << "\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2009-02-20 16:58:33 +01:00
|
|
|
}
|
2011-11-14 04:54:32 +01:00
|
|
|
|
2009-02-21 12:22:17 +01:00
|
|
|
bool operator() (blob const& val)
|
|
|
|
{
|
2010-06-02 13:03:30 +02:00
|
|
|
if (sqlite3_bind_blob(stmt_, index_, val.buf_, val.size_, SQLITE_STATIC) != SQLITE_OK)
|
|
|
|
{
|
|
|
|
std::cerr << "cannot bind BLOB\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2009-02-21 12:22:17 +01:00
|
|
|
}
|
2011-11-14 04:54:32 +01:00
|
|
|
|
2009-02-20 16:58:33 +01:00
|
|
|
sqlite3_stmt * stmt_;
|
|
|
|
unsigned index_;
|
2010-06-02 13:03:30 +02:00
|
|
|
};
|
|
|
|
public:
|
|
|
|
prepared_statement(database & db, std::string const& sql)
|
2009-02-20 16:58:33 +01:00
|
|
|
: db_(db.db_.get()), stmt_(0)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2009-02-20 16:58:33 +01:00
|
|
|
const char * tail;
|
2009-02-21 00:04:30 +01:00
|
|
|
//char * err_msg;
|
2009-02-20 16:58:33 +01:00
|
|
|
int res = sqlite3_prepare_v2(db_, sql.c_str(),-1, &stmt_,&tail);
|
|
|
|
if (res != SQLITE_OK)
|
|
|
|
{
|
2011-11-14 04:54:32 +01:00
|
|
|
std::cerr << "ERR:"<< res << "\n";
|
2010-06-02 13:03:30 +02:00
|
|
|
throw;
|
2009-02-20 16:58:33 +01:00
|
|
|
}
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2011-11-14 04:54:32 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
~prepared_statement()
|
|
|
|
{
|
|
|
|
int res = sqlite3_finalize(stmt_);
|
2009-02-20 16:58:33 +01:00
|
|
|
if (res != SQLITE_OK)
|
|
|
|
{
|
2011-11-14 04:54:32 +01:00
|
|
|
std::cerr << "ERR:" << res << "\n";
|
2009-02-20 16:58:33 +01:00
|
|
|
}
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2011-11-14 04:54:32 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
bool insert_record(record_type const& rec) const
|
2011-11-14 04:54:32 +01:00
|
|
|
{
|
2009-02-21 01:23:41 +01:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2009-02-21 12:22:17 +01:00
|
|
|
assert( unsigned(sqlite3_bind_parameter_count(stmt_)) == rec.size());
|
2009-02-21 01:23:41 +01:00
|
|
|
#endif
|
2009-02-20 16:58:33 +01:00
|
|
|
record_type::const_iterator itr = rec.begin();
|
|
|
|
record_type::const_iterator end = rec.end();
|
|
|
|
int count = 1;
|
|
|
|
for (; itr!=end;++itr)
|
|
|
|
{
|
2010-06-02 13:03:30 +02:00
|
|
|
binder op(stmt_,count++);
|
2014-08-12 14:40:45 +02:00
|
|
|
if (!util::apply_visitor(op,*itr))
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2009-02-20 16:58:33 +01:00
|
|
|
}
|
2011-11-14 04:54:32 +01:00
|
|
|
|
2009-02-20 16:58:33 +01:00
|
|
|
sqlite3_step(stmt_);
|
|
|
|
sqlite3_reset(stmt_);
|
|
|
|
|
|
|
|
return true;
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2009-02-20 16:58:33 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
private:
|
|
|
|
sqlite3 * db_;
|
|
|
|
sqlite3_stmt * stmt_;
|
|
|
|
};
|
|
|
|
}
|
2009-02-20 16:58:33 +01:00
|
|
|
}
|