8#include <botan/sqlite3.h>
10#include <botan/exceptn.h>
11#include <botan/mem_ops.h>
12#include <botan/internal/fmt.h>
20 const int open_flags =
21 sqlite_open_flags.value_or(SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX);
22 int rc = ::sqlite3_open_v2(std::string(db_filename).c_str(), &m_db, open_flags,
nullptr);
25 const std::string err_msg = ::sqlite3_errmsg(m_db);
26 ::sqlite3_close(m_db);
34 ::sqlite3_close(m_db);
40 return std::make_shared<Sqlite3_Statement>(m_db, base_sql);
47 return stmt->get_size_t(0);
49 throw SQL_DB_Error(
fmt(
"Querying size of table '{}' failed", table_name));
54 char* errmsg =
nullptr;
55 int rc = ::sqlite3_exec(m_db, std::string(table_schema).c_str(),
nullptr,
nullptr, &errmsg);
58 const std::string err_msg = errmsg;
59 ::sqlite3_free(errmsg);
60 ::sqlite3_close(m_db);
62 throw SQL_DB_Error(
"sqlite3_exec for table failed - " + err_msg);
67 const auto result = ::sqlite3_changes64(m_db);
69 return static_cast<size_t>(result);
73 const int flag = sqlite3_threadsafe();
90Sqlite3_Database::Sqlite3_Statement::Sqlite3_Statement(sqlite3* db, std::string_view base_sql) {
91 int rc = ::sqlite3_prepare_v2(db, base_sql.data(),
static_cast<int>(base_sql.size()), &m_stmt,
nullptr);
94 throw SQL_DB_Error(
fmt(
"sqlite3_prepare failed on '{}' with err {}", base_sql, rc), rc);
98void Sqlite3_Database::Sqlite3_Statement::bind(
int column, std::string_view val) {
99 int rc = ::sqlite3_bind_text64(m_stmt, column, val.data(), val.size(), SQLITE_TRANSIENT, SQLITE_UTF8);
100 if(rc != SQLITE_OK) {
101 throw SQL_DB_Error(
"sqlite3_bind_text failed", rc);
105void Sqlite3_Database::Sqlite3_Statement::bind(
int column,
size_t val) {
106 int rc = ::sqlite3_bind_int64(m_stmt, column, val);
107 if(rc != SQLITE_OK) {
108 throw SQL_DB_Error(
"sqlite3_bind_int failed", rc);
112void Sqlite3_Database::Sqlite3_Statement::bind(
int column, std::chrono::system_clock::time_point time) {
113 const uint64_t timeval = std::chrono::duration_cast<std::chrono::seconds>(time.time_since_epoch()).count();
114 bind(column,
static_cast<size_t>(timeval));
117void Sqlite3_Database::Sqlite3_Statement::bind(
int column,
const std::vector<uint8_t>& val) {
118 int rc = ::sqlite3_bind_blob64(m_stmt, column, val.data(), val.size(), SQLITE_TRANSIENT);
119 if(rc != SQLITE_OK) {
120 throw SQL_DB_Error(
"sqlite3_bind_text failed", rc);
124void Sqlite3_Database::Sqlite3_Statement::bind(
int column,
const uint8_t* p,
size_t len) {
125 int rc = ::sqlite3_bind_blob64(m_stmt, column, p, len, SQLITE_TRANSIENT);
126 if(rc != SQLITE_OK) {
127 throw SQL_DB_Error(
"sqlite3_bind_text failed", rc);
131std::pair<const uint8_t*, size_t> Sqlite3_Database::Sqlite3_Statement::get_blob(
int column) {
132 const auto column_type = ::sqlite3_column_type(m_stmt, column);
133 if(column_type == SQLITE_NULL) {
137 BOTAN_ASSERT(column_type == SQLITE_BLOB,
"Return value is a blob");
139 const void* session_blob = ::sqlite3_column_blob(m_stmt, column);
140 const int session_blob_size = ::sqlite3_column_bytes(m_stmt, column);
142 BOTAN_ASSERT(session_blob_size >= 0,
"Blob size is non-negative");
144 return std::make_pair(
static_cast<const uint8_t*
>(session_blob),
static_cast<size_t>(session_blob_size));
147std::string Sqlite3_Database::Sqlite3_Statement::get_str(
int column) {
148 BOTAN_ASSERT(::sqlite3_column_type(m_stmt, column) == SQLITE_TEXT,
"Return value is text");
150 const unsigned char* str = ::sqlite3_column_text(m_stmt, column);
155size_t Sqlite3_Database::Sqlite3_Statement::get_size_t(
int column) {
156 BOTAN_ASSERT(::sqlite3_column_type(m_stmt, column) == SQLITE_INTEGER,
"Return count is an integer");
158 const size_t sessions_int = ::sqlite3_column_int64(m_stmt, column);
163size_t Sqlite3_Database::Sqlite3_Statement::spin() {
172bool Sqlite3_Database::Sqlite3_Statement::step() {
173 return (::sqlite3_step(m_stmt) == SQLITE_ROW);
176Sqlite3_Database::Sqlite3_Statement::~Sqlite3_Statement() {
177 ::sqlite3_finalize(m_stmt);
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_ASSERT(expr, assertion_made)
~Sqlite3_Database() override
std::shared_ptr< Statement > new_statement(std::string_view sql) const override
size_t rows_changed_by_last_statement() override
bool is_threadsafe() const override
void create_table(std::string_view table_schema) override
Sqlite3_Database(std::string_view file, std::optional< int > sqlite_open_flags=std::nullopt)
size_t row_count(std::string_view table_name) override
std::string fmt(std::string_view format, const T &... args)
const char * cast_uint8_ptr_to_char(const uint8_t *b)