Botan 3.6.1
Crypto and TLS for C&
sqlite3.cpp
Go to the documentation of this file.
1/*
2* SQLite wrapper
3* (C) 2012 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/sqlite3.h>
9
10#include <botan/exceptn.h>
11#include <botan/mem_ops.h>
12#include <botan/internal/fmt.h>
13#include <sqlite3.h>
14
15namespace Botan {
16
17Sqlite3_Database::Sqlite3_Database(std::string_view db_filename, std::optional<int> sqlite_open_flags) {
18 // SQLITE_OPEN_FULLMUTEX ensures that the database object can be used
19 // concurrently from multiple threads.
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);
23
24 if(rc) [[unlikely]] {
25 const std::string err_msg = ::sqlite3_errmsg(m_db);
26 ::sqlite3_close(m_db);
27 m_db = nullptr;
28 throw SQL_DB_Error("sqlite3_open failed - " + err_msg);
29 }
30}
31
33 if(m_db) [[likely]] {
34 ::sqlite3_close(m_db);
35 }
36 m_db = nullptr;
37}
38
39std::shared_ptr<SQL_Database::Statement> Sqlite3_Database::new_statement(std::string_view base_sql) const {
40 return std::make_shared<Sqlite3_Statement>(m_db, base_sql);
41}
42
43size_t Sqlite3_Database::row_count(std::string_view table_name) {
44 auto stmt = new_statement(fmt("select count(*) from {}", table_name));
45
46 if(stmt->step()) {
47 return stmt->get_size_t(0);
48 } else {
49 throw SQL_DB_Error(fmt("Querying size of table '{}' failed", table_name));
50 }
51}
52
53void Sqlite3_Database::create_table(std::string_view table_schema) {
54 char* errmsg = nullptr;
55 int rc = ::sqlite3_exec(m_db, std::string(table_schema).c_str(), nullptr, nullptr, &errmsg);
56
57 if(rc != SQLITE_OK) {
58 const std::string err_msg = errmsg;
59 ::sqlite3_free(errmsg);
60 ::sqlite3_close(m_db);
61 m_db = nullptr;
62 throw SQL_DB_Error("sqlite3_exec for table failed - " + err_msg);
63 }
64}
65
67 const auto result = ::sqlite3_changes64(m_db);
68 BOTAN_ASSERT_NOMSG(result >= 0);
69 return static_cast<size_t>(result);
70}
71
73 const int flag = sqlite3_threadsafe();
74
75 // `flag` can have three values:
76 //
77 // 0 - single threaded: no locking is done inside the SQLite code
78 // 1 - serialized: all SQLite database features can be used safely
79 // from multiple threads
80 // 2 - reduced locking: application must ensure not to use a single
81 // database connection across threads
82 //
83 // https://www.sqlite.org/c3ref/threadsafe.html
84
85 // When opening the database connection we explicitly request
86 // SQLITE_OPEN_FULLMUTEX to ensure restrictive locking in SQLite.
87 return flag >= 1;
88}
89
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);
92
93 if(rc != SQLITE_OK) {
94 throw SQL_DB_Error(fmt("sqlite3_prepare failed on '{}' with err {}", base_sql, rc), rc);
95 }
96}
97
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);
102 }
103}
104
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);
109 }
110}
111
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));
115}
116
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);
121 }
122}
123
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);
128 }
129}
130
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) {
134 return {nullptr, 0};
135 }
136
137 BOTAN_ASSERT(column_type == SQLITE_BLOB, "Return value is a blob");
138
139 const void* session_blob = ::sqlite3_column_blob(m_stmt, column);
140 const int session_blob_size = ::sqlite3_column_bytes(m_stmt, column);
141
142 BOTAN_ASSERT(session_blob_size >= 0, "Blob size is non-negative");
143
144 return std::make_pair(static_cast<const uint8_t*>(session_blob), static_cast<size_t>(session_blob_size));
145}
146
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");
149
150 const unsigned char* str = ::sqlite3_column_text(m_stmt, column);
151
152 return std::string(cast_uint8_ptr_to_char(str));
153}
154
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");
157
158 const size_t sessions_int = ::sqlite3_column_int64(m_stmt, column);
159
160 return sessions_int;
161}
162
163size_t Sqlite3_Database::Sqlite3_Statement::spin() {
164 size_t steps = 0;
165 while(step()) {
166 ++steps;
167 }
168
169 return steps;
170}
171
172bool Sqlite3_Database::Sqlite3_Statement::step() {
173 return (::sqlite3_step(m_stmt) == SQLITE_ROW);
174}
175
176Sqlite3_Database::Sqlite3_Statement::~Sqlite3_Statement() {
177 ::sqlite3_finalize(m_stmt);
178}
179
180} // namespace Botan
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:59
#define BOTAN_ASSERT(expr, assertion_made)
Definition assert.h:50
~Sqlite3_Database() override
Definition sqlite3.cpp:32
std::shared_ptr< Statement > new_statement(std::string_view sql) const override
Definition sqlite3.cpp:39
size_t rows_changed_by_last_statement() override
Definition sqlite3.cpp:66
bool is_threadsafe() const override
Definition sqlite3.cpp:72
void create_table(std::string_view table_schema) override
Definition sqlite3.cpp:53
Sqlite3_Database(std::string_view file, std::optional< int > sqlite_open_flags=std::nullopt)
Definition sqlite3.cpp:17
size_t row_count(std::string_view table_name) override
Definition sqlite3.cpp:43
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
const char * cast_uint8_ptr_to_char(const uint8_t *b)
Definition mem_ops.h:277
Public Header.