|
| 1 | +#include "sqlite3.hpp" |
| 2 | +#include "log.hpp" |
| 3 | + |
| 4 | +#include <cassert> |
| 5 | +#include <sqlite3.h> |
| 6 | + |
| 7 | +namespace acppsrv::sqlite { |
| 8 | + |
| 9 | +/*** connection::impl ********************************************************/ |
| 10 | + |
| 11 | +class connection::impl { |
| 12 | +public: |
| 13 | + explicit impl(connection& conn); |
| 14 | + impl(const impl&) = delete; |
| 15 | + impl(impl&&) = delete; |
| 16 | + ~impl(); |
| 17 | + impl& operator=(const impl&) = delete; |
| 18 | + impl& operator=(impl&&) = delete; |
| 19 | + connection& conn; |
| 20 | + sqlite3* db = nullptr; |
| 21 | +}; |
| 22 | + |
| 23 | +connection::impl::impl(connection& conn): conn(conn) |
| 24 | +{ |
| 25 | + if (int status = sqlite3_open_v2(conn._file.c_str(), &db, |
| 26 | + SQLITE_OPEN_READWRITE | |
| 27 | + SQLITE_OPEN_URI | |
| 28 | + SQLITE_OPEN_NOMUTEX | |
| 29 | + SQLITE_OPEN_EXRESCODE, |
| 30 | + nullptr); |
| 31 | + status != SQLITE_OK) |
| 32 | + { |
| 33 | + if (!db) |
| 34 | + throw error(conn._file); |
| 35 | + throw error(conn, *this); |
| 36 | + } |
| 37 | + assert(db); |
| 38 | +} |
| 39 | + |
| 40 | +connection::impl::~impl() |
| 41 | +{ |
| 42 | + if (sqlite3_close_v2(db) != SQLITE_OK) { |
| 43 | + assert(db); |
| 44 | + log_msg(log_level::emerg) << "Cannot close database \"" << conn._file << |
| 45 | + "\" handle: " << sqlite3_errmsg(db); |
| 46 | + } else |
| 47 | + log_msg(log_level::debug) << "Closed database \"" << conn._file << '"'; |
| 48 | +} |
| 49 | + |
| 50 | +/*** connection **************************************************************/ |
| 51 | + |
| 52 | +connection::connection(std::string file): |
| 53 | + _file(std::move(file)), _impl(std::make_unique<impl>(*this)) |
| 54 | +{ |
| 55 | +} |
| 56 | + |
| 57 | +connection::~connection() = default; |
| 58 | + |
| 59 | +/*** query::impl *************************************************************/ |
| 60 | + |
| 61 | +class query::impl { |
| 62 | +public: |
| 63 | + explicit impl(query& q); |
| 64 | + impl(const impl&) = delete; |
| 65 | + impl(impl&&) = delete; |
| 66 | + ~impl(); |
| 67 | + impl& operator=(const impl&) = delete; |
| 68 | + impl& operator=(impl&&) = delete; |
| 69 | +private: |
| 70 | + query& q; |
| 71 | + sqlite3_stmt* stmt = nullptr; |
| 72 | +}; |
| 73 | + |
| 74 | +query::impl::impl(query& q): q(q) |
| 75 | +{ |
| 76 | + // sqlite3 allows passing size incl. terminating NUL |
| 77 | + if (sqlite3_prepare_v3(q._db._impl->db, q._sql.c_str(), |
| 78 | + int(q._sql.size() + 1), |
| 79 | + SQLITE_PREPARE_PERSISTENT, &stmt, |
| 80 | + nullptr) != SQLITE_OK) |
| 81 | + { |
| 82 | + assert(!stmt); |
| 83 | + // this->q to silence compiler warning about unused q |
| 84 | + throw error(this->q._db, this->q._sql_id); |
| 85 | + } |
| 86 | + assert(stmt); |
| 87 | +} |
| 88 | + |
| 89 | +query::impl::~impl() |
| 90 | +{ |
| 91 | + sqlite3_finalize(stmt); |
| 92 | +} |
| 93 | + |
| 94 | +/*** query *******************************************************************/ |
| 95 | + |
| 96 | +query::query(connection& db, std::string sql, std::string sql_id): |
| 97 | + _db(db), _sql(std::move(sql)), _sql_id(std::move(sql_id)), |
| 98 | + _impl(std::make_unique<impl>(*this)) |
| 99 | +{ |
| 100 | +} |
| 101 | + |
| 102 | +query::~query() = default; |
| 103 | + |
| 104 | +/*** error *******************************************************************/ |
| 105 | + |
| 106 | +error::error(const std::string& file): |
| 107 | + runtime_error("sqlite3 error in db \"" + file + "\"" + |
| 108 | + ": Cannot allocate database handle") |
| 109 | +{ |
| 110 | +} |
| 111 | + |
| 112 | +error::error(connection& db, const std::string& sql_id): |
| 113 | + runtime_error("sqlite3 error in db \"" + db._file + "\"" + |
| 114 | + (sql_id.empty() ? "" : (" (" + sql_id + ")")) + |
| 115 | + ": " + sqlite3_errmsg(db._impl->db)) |
| 116 | +{ |
| 117 | +} |
| 118 | + |
| 119 | +error::error(connection& db, connection::impl& impl): |
| 120 | + runtime_error("sqlite3 error in db \"" + db._file + "\"" + |
| 121 | + ": " + sqlite3_errmsg(impl.db)) |
| 122 | +{ |
| 123 | +} |
| 124 | + |
| 125 | +} // namespace acppsrv::sqlite |
0 commit comments