Botan 3.13.0
Crypto and TLS for C&
sqlite3.h
Go to the documentation of this file.
1/*
2* SQLite3 wrapper
3* (C) 2012,2014 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#ifndef BOTAN_UTILS_SQLITE3_H_
9#define BOTAN_UTILS_SQLITE3_H_
10
11#include <botan/database.h>
12
13#include <memory>
14#include <optional>
15
16struct sqlite3;
17struct sqlite3_stmt;
18
19namespace Botan {
20
21/**
22* An SQL_Database implementation backed by SQLite3
23*/
25 public:
26 /**
27 * Create a new SQLite database handle from a file.
28 *
29 * @param file path to the database file be opened and/or created
30 * @param sqlite_open_flags flags that will be passed to sqlite3_open_v2()
31 * (default: SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX)
32 */
33 BOTAN_FUTURE_EXPLICIT Sqlite3_Database(std::string_view file,
34 std::optional<int> sqlite_open_flags = std::nullopt);
35
37
38 // Database handles are not copyable or moveable
39 Sqlite3_Database(const Sqlite3_Database& other) = delete;
43
44 /**
45 * Count the rows of a table
46 * @param table_name the table to count
47 * @return the number of rows in the table
48 */
49 size_t row_count(std::string_view table_name) override;
50
51 /**
52 * Create a table
53 * @param schema the name and columns of the table to create
54 */
55 void create_table(const Table_Schema& schema) override;
56
57 /**
58 * Count the rows modified by the most recently executed statement
59 * @return the number of rows inserted, updated or deleted
60 */
61 size_t rows_changed_by_last_statement() override;
62
63 /**
64 * Create a new statement for execution
65 * @param sql the SQL text of the statement
66 * @return the prepared statement
67 */
68 std::shared_ptr<Statement> new_statement(std::string_view sql) const override;
69
70 /**
71 * Prepare an insert-or-replace statement
72 * @param table the table to upsert into
73 * @param columns the columns to write, in placeholder order
74 * @return the prepared statement
75 */
76 std::shared_ptr<Statement> upsert(std::string_view table,
77 std::initializer_list<std::string_view> columns) const override;
78
79 /**
80 * Query whether this database may be used from multiple threads
81 * @return true if SQLite3 was compiled with threading support
82 */
83 bool is_threadsafe() const override;
84
85 private:
86 class Sqlite3_Statement final : public Statement {
87 public:
88 void bind(int column, std::string_view val) override;
89 void bind(int column, size_t val) override;
90 void bind(int column, std::chrono::system_clock::time_point time) override;
91 void bind(int column, const std::vector<uint8_t>& val) override;
92 void bind(int column, const uint8_t* data, size_t len) override;
93 void bind_null(int column) override;
94
95 std::span<const uint8_t> get_blob(int column) override;
96 std::optional<std::string> get_str(int column) override;
97 size_t get_size_t(int column) override;
98
99 size_t spin() override;
100 bool step() override;
101
102 Sqlite3_Statement(std::shared_ptr<sqlite3> db, std::string_view base_sql);
103 ~Sqlite3_Statement() override;
104
105 Sqlite3_Statement(const Sqlite3_Statement& other) = delete;
106 Sqlite3_Statement(Sqlite3_Statement&& other) = delete;
107 Sqlite3_Statement& operator=(const Sqlite3_Statement& other) = delete;
108 Sqlite3_Statement& operator=(Sqlite3_Statement&& other) = delete;
109
110 private:
111 // m_db is declared before m_stmt so the prepared statement is
112 // finalized before the connection's refcount is released.
113 std::shared_ptr<sqlite3> m_db;
114 sqlite3_stmt* m_stmt;
115 };
116
117 std::shared_ptr<sqlite3> m_db;
118};
119
120} // namespace Botan
121
122#endif
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
#define BOTAN_FUTURE_EXPLICIT
Definition api.h:52
Sqlite3_Database & operator=(const Sqlite3_Database &other)=delete
Sqlite3_Database & operator=(Sqlite3_Database &&other)=delete
Sqlite3_Database(Sqlite3_Database &&other)=delete
std::shared_ptr< Statement > upsert(std::string_view table, std::initializer_list< std::string_view > columns) const override
Definition sqlite3.cpp:41
~Sqlite3_Database() override
std::shared_ptr< Statement > new_statement(std::string_view sql) const override
Definition sqlite3.cpp:37
size_t rows_changed_by_last_statement() override
Definition sqlite3.cpp:129
bool is_threadsafe() const override
Definition sqlite3.cpp:135
Sqlite3_Database(const Sqlite3_Database &other)=delete
BOTAN_FUTURE_EXPLICIT Sqlite3_Database(std::string_view file, std::optional< int > sqlite_open_flags=std::nullopt)
Definition sqlite3.cpp:18
size_t row_count(std::string_view table_name) override
Definition sqlite3.cpp:68
void create_table(const Table_Schema &schema) override
Definition sqlite3.cpp:78