/usr/include/osmium/utils/sqlite.hpp is in libosmium-dev 0.0~20111213-g7f3500a-1build2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | #ifndef OSMIUM_UTILS_SQLITE_HPP
#define OSMIUM_UTILS_SQLITE_HPP
/*
Copyright 2011 Jochen Topf <jochen@topf.org> and others (see README).
This file is part of Osmium (https://github.com/joto/osmium).
Osmium is free software: you can redistribute it and/or modify it under the
terms of the GNU Lesser General Public License or (at your option) the GNU
General Public License as published by the Free Software Foundation, either
version 3 of the Licenses, or (at your option) any later version.
Osmium 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 and the GNU
General Public License for more details.
You should have received a copy of the Licenses along with Osmium. If not, see
<http://www.gnu.org/licenses/>.
*/
#include <stdexcept>
#include <string>
#include <iostream>
#include <sqlite3.h>
namespace Osmium {
/**
* @brief The %Sqlite classes wrap the %Sqlite C library.
*/
namespace Sqlite {
/**
* Exception returned by Sqlite wrapper classes when there are errors in the Sqlite3 lib
*/
class Exception : public std::runtime_error {
public:
Exception(const std::string &msg, const std::string &error) : std::runtime_error(msg + ": " + error + '\n') {
}
};
class Statement;
/**
* Wrapper class for Sqlite database
*/
class Database {
private:
sqlite3* db;
public:
Database(const char* filename) {
if (sqlite3_open_v2(filename, &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0)) {
sqlite3_close(db);
throw Sqlite::Exception("Can't open database", errmsg());
}
}
~Database() {
sqlite3_close(db);
}
const std::string &errmsg() const {
static std::string error = std::string(sqlite3_errmsg(db));
return error;
}
sqlite3* get_sqlite3() {
return db;
}
void begin_transaction() {
if (SQLITE_OK != sqlite3_exec(db, "BEGIN TRANSACTION;", 0, 0, 0)) {
std::cerr << "Database error: " << sqlite3_errmsg(db) << "\n";
sqlite3_close(db);
exit(1);
}
}
void commit() {
if (SQLITE_OK != sqlite3_exec(db, "COMMIT;", 0, 0, 0)) {
std::cerr << "Database error: " << sqlite3_errmsg(db) << "\n";
sqlite3_close(db);
exit(1);
}
}
Statement* prepare(const char* sql);
}; // class Database
/**
* Wrapper class for Sqlite prepared statement.
*/
class Statement {
private:
Database* db_;
sqlite3_stmt* statement;
int bindnum;
public:
Statement(Database* db, const char* sql) : db_(db), statement(0), bindnum(1) {
sqlite3_prepare_v2(db->get_sqlite3(), sql, -1, &statement, 0);
if (statement == 0) {
throw Sqlite::Exception("Can't prepare statement", db_->errmsg());
}
}
~Statement() {
sqlite3_finalize(statement);
}
Statement* bind_null() {
if (SQLITE_OK != sqlite3_bind_null(statement, bindnum++)) {
throw Sqlite::Exception("Can't bind null value", db_->errmsg());
}
return this;
}
Statement* bind_text(const char* value) {
if (SQLITE_OK != sqlite3_bind_text(statement, bindnum++, value, -1, SQLITE_STATIC)) {
throw Sqlite::Exception("Can't bind text value", db_->errmsg());
}
return this;
}
Statement* bind_text(const std::string& value) {
if (SQLITE_OK != sqlite3_bind_text(statement, bindnum++, value.c_str(), -1, SQLITE_STATIC)) {
throw Sqlite::Exception("Can't bind text value", db_->errmsg());
}
return this;
}
Statement* bind_int(int value) {
if (SQLITE_OK != sqlite3_bind_int(statement, bindnum++, value)) {
throw Sqlite::Exception("Can't bind int value", db_->errmsg());
}
return this;
}
Statement* bind_int64(int64_t value) {
if (SQLITE_OK != sqlite3_bind_int64(statement, bindnum++, value)) {
throw Sqlite::Exception("Can't bind int64 value", db_->errmsg());
}
return this;
}
Statement* bind_double(double value) {
if (SQLITE_OK != sqlite3_bind_double(statement, bindnum++, value)) {
throw Sqlite::Exception("Can't bind double value", db_->errmsg());
}
return this;
}
Statement* bind_blob(const void* value, int length) {
if (SQLITE_OK != sqlite3_bind_blob(statement, bindnum++, value, length, 0)) {
throw Sqlite::Exception("Can't bind blob value", db_->errmsg());
}
return this;
}
void execute() {
sqlite3_step(statement);
if (SQLITE_OK != sqlite3_reset(statement)) {
throw Sqlite::Exception("Can't execute statement", db_->errmsg());
}
bindnum = 1;
}
}; // class Statement
inline Statement* Database::prepare(const char* sql) {
return new Statement(this, sql);
}
} // namespace Sqlite
} // namespace Osmium
#endif // OSMIUM_UTILS_SQLITE_HPP
|