11#ifndef OPENSWMM_GPKG_UTILS_HPP
12#define OPENSWMM_GPKG_UTILS_HPP
28 explicit GpkgError(
const std::string& msg) : std::runtime_error(msg) {}
30 : std::runtime_error(msg +
" (sqlite rc=" + std::to_string(rc) +
")") {}
39 if (db) sqlite3_close_v2(db);
42using DbPtr = std::unique_ptr<sqlite3, DbDeleter>;
44inline DbPtr open_database(
const std::string& path,
int flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE) {
45 sqlite3* raw =
nullptr;
46 int rc = sqlite3_open_v2(path.c_str(), &raw, flags,
nullptr);
48 if (rc != SQLITE_OK) {
49 std::string msg = raw ? sqlite3_errmsg(raw) :
"unknown error";
50 throw GpkgError(
"Failed to open database '" + path +
"': " + msg, rc);
61 if (stmt) sqlite3_finalize(stmt);
64using StmtPtr = std::unique_ptr<sqlite3_stmt, StmtDeleter>;
67 sqlite3_stmt* raw =
nullptr;
68 int rc = sqlite3_prepare_v2(db, sql.c_str(),
static_cast<int>(sql.size()), &raw,
nullptr);
70 if (rc != SQLITE_OK) {
71 throw GpkgError(
"Failed to prepare: " + sql +
" — " + sqlite3_errmsg(db), rc);
80inline void exec(sqlite3* db,
const std::string& sql) {
81 char* errmsg =
nullptr;
82 int rc = sqlite3_exec(db, sql.c_str(),
nullptr,
nullptr, &errmsg);
83 if (rc != SQLITE_OK) {
84 std::string msg = errmsg ? errmsg :
"unknown error";
86 throw GpkgError(
"exec failed: " + msg +
" — SQL: " + sql, rc);
90inline void bind_text(sqlite3_stmt* stmt,
int col,
const std::string& val) {
91 sqlite3_bind_text(stmt, col, val.c_str(),
static_cast<int>(val.size()), SQLITE_TRANSIENT);
94inline void bind_double(sqlite3_stmt* stmt,
int col,
double val) {
95 sqlite3_bind_double(stmt, col, val);
98inline void bind_int(sqlite3_stmt* stmt,
int col,
int val) {
99 sqlite3_bind_int(stmt, col, val);
103 sqlite3_bind_null(stmt, col);
107 sqlite3_bind_blob(stmt, col,
data,
size, SQLITE_TRANSIENT);
111 const unsigned char* txt = sqlite3_column_text(stmt, col);
112 return txt ? std::string(
reinterpret_cast<const char*
>(txt)) : std::string{};
116 return sqlite3_column_double(stmt, col);
120 return sqlite3_column_int(stmt, col);
124 return sqlite3_column_type(stmt, col) == SQLITE_NULL;
127inline std::vector<uint8_t>
column_blob(sqlite3_stmt* stmt,
int col) {
128 int size = sqlite3_column_bytes(stmt, col);
129 const uint8_t*
data =
static_cast<const uint8_t*
>(sqlite3_column_blob(stmt, col));
138 exec(db_,
"BEGIN IMMEDIATE");
148 sqlite3_exec(db_,
"ROLLBACK",
nullptr,
nullptr,
nullptr);
155 bool committed_ =
false;
int size
Definition XSectBatch.cpp:567
const double * data
Definition XSectBatch.cpp:567
Definition GpkgUtils.hpp:26
GpkgError(const std::string &msg, int rc)
Definition GpkgUtils.hpp:29
GpkgError(const std::string &msg)
Definition GpkgUtils.hpp:28
Definition GpkgUtils.hpp:135
Transaction(sqlite3 *db)
Definition GpkgUtils.hpp:137
void commit()
Definition GpkgUtils.hpp:140
~Transaction()
Definition GpkgUtils.hpp:146
Transaction & operator=(const Transaction &)=delete
Transaction(const Transaction &)=delete
Definition GeoPackageInputPlugin.cpp:15
std::unique_ptr< sqlite3_stmt, StmtDeleter > StmtPtr
Definition GpkgUtils.hpp:64
void exec(sqlite3 *db, const std::string &sql)
Definition GpkgUtils.hpp:80
void bind_blob(sqlite3_stmt *stmt, int col, const void *data, int size)
Definition GpkgUtils.hpp:106
DbPtr open_database(const std::string &path, int flags=SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE)
Definition GpkgUtils.hpp:44
void bind_int(sqlite3_stmt *stmt, int col, int val)
Definition GpkgUtils.hpp:98
void bind_double(sqlite3_stmt *stmt, int col, double val)
Definition GpkgUtils.hpp:94
StmtPtr prepare(sqlite3 *db, const std::string &sql)
Definition GpkgUtils.hpp:66
void bind_null(sqlite3_stmt *stmt, int col)
Definition GpkgUtils.hpp:102
std::string column_text(sqlite3_stmt *stmt, int col)
Definition GpkgUtils.hpp:110
int column_int(sqlite3_stmt *stmt, int col)
Definition GpkgUtils.hpp:119
bool column_is_null(sqlite3_stmt *stmt, int col)
Definition GpkgUtils.hpp:123
std::unique_ptr< sqlite3, DbDeleter > DbPtr
Definition GpkgUtils.hpp:42
void bind_text(sqlite3_stmt *stmt, int col, const std::string &val)
Definition GpkgUtils.hpp:90
double column_double(sqlite3_stmt *stmt, int col)
Definition GpkgUtils.hpp:115
std::vector< uint8_t > column_blob(sqlite3_stmt *stmt, int col)
Definition GpkgUtils.hpp:127
Definition GpkgUtils.hpp:37
void operator()(sqlite3 *db) const noexcept
Definition GpkgUtils.hpp:38
Definition GpkgUtils.hpp:59
void operator()(sqlite3_stmt *stmt) const noexcept
Definition GpkgUtils.hpp:60