#include <database.h>
Abstract interface to a SQL database
Definition at line 26 of file database.h.
◆ Column_Type
The supported column types
| Enumerator |
|---|
| Blob | |
| String | |
| Integer | |
Definition at line 196 of file database.h.
196 : uint8_t {
197 Blob,
198 String,
200 };
◆ ~SQL_Database()
| virtual Botan::SQL_Database::~SQL_Database |
( |
| ) |
|
|
virtualdefault |
◆ create_table()
| virtual void Botan::SQL_Database::create_table |
( |
const Table_Schema & | schema | ) |
|
|
pure virtual |
Create a table
- Parameters
-
| schema | the name and columns of the table to create |
Implemented in Botan::Sqlite3_Database.
◆ exec()
| virtual size_t Botan::SQL_Database::exec |
( |
std::string_view | sql | ) |
|
|
inlinevirtual |
Prepare and run a statement to completion
- Parameters
-
| sql | the SQL text to execute |
- Returns
- the number of result rows which were stepped over
Definition at line 342 of file database.h.
virtual std::shared_ptr< Statement > new_statement(std::string_view base_sql) const =0
References new_statement().
◆ is_threadsafe()
| virtual bool Botan::SQL_Database::is_threadsafe |
( |
| ) |
const |
|
inlinevirtual |
Query whether this database may be used from multiple threads
- Returns
- true if the implementation is threadsafe
Reimplemented in Botan::Sqlite3_Database.
Definition at line 348 of file database.h.
◆ is_valid_table_name()
| bool Botan::SQL_Database::is_valid_table_name |
( |
std::string_view | table | ) |
const |
|
virtual |
Return true if the given name seems to be valid as the name for a table
Default implementation accepts non-empty [a-zA-Z0-9_]
- Parameters
-
- Returns
- true if the name is acceptable as a table name
Definition at line 14 of file database.cpp.
14 {
15 if(table.empty()) {
16 return false;
17 }
18
20 for(const char c : table) {
21 if(!valid_table_name_char(c)) {
22 return false;
23 }
24 }
25 return true;
26}
static constexpr CharacterValidityTable alpha_numeric_plus(std::string_view extras)
References Botan::CharacterValidityTable::alpha_numeric_plus().
◆ new_statement()
| virtual std::shared_ptr< Statement > Botan::SQL_Database::new_statement |
( |
std::string_view | base_sql | ) |
const |
|
pure virtual |
Create a new statement for execution. Use ?1, ?2, ?3, etc for parameters to set later with bind
- Parameters
-
| base_sql | the SQL text of the statement |
- Returns
- the prepared statement
Implemented in Botan::Sqlite3_Database.
References select().
Referenced by exec(), and select().
◆ row_count()
| virtual size_t Botan::SQL_Database::row_count |
( |
std::string_view | table_name | ) |
|
|
pure virtual |
Count the rows of a table
- Parameters
-
| table_name | the table to count |
- Returns
- the number of rows in the table
Implemented in Botan::Sqlite3_Database.
◆ rows_changed_by_last_statement()
| virtual size_t Botan::SQL_Database::rows_changed_by_last_statement |
( |
| ) |
|
|
pure virtual |
Count the rows modified by the most recently executed statement
- Returns
- the number of rows inserted, updated or deleted
Implemented in Botan::Sqlite3_Database.
◆ select()
| std::shared_ptr< SQL_Database::Statement > Botan::SQL_Database::select |
( |
std::string_view | columns, |
|
|
std::string_view | table, |
|
|
std::string_view | where = {}, |
|
|
std::optional< size_t > | limit = std::nullopt ) const |
|
virtual |
Prepare a "SELECT <columns> FROM <table> [WHERE <where>] [LIMIT <limit>]" statement. where is the body of the WHERE clause (e.g. "id = ?1 AND name = ?2"); pass an empty string for no WHERE clause. Use ?1, ?2, ... for bound parameters. Virtual so backends can override if helpful.
- Parameters
-
| columns | the columns to select |
| table | the table to select from |
| where | the body of the WHERE clause, or empty for no WHERE clause |
| limit | the maximum number of rows, or nullopt for no limit |
- Returns
- the prepared statement
Definition at line 28 of file database.cpp.
31 {
32 std::string sql = "SELECT ";
33 sql += columns;
34 sql += " FROM ";
35 sql += table;
36 if(!where.empty()) {
37 sql += " WHERE ";
38 sql += where;
39 }
40 if(limit.has_value()) {
41 sql += " LIMIT ";
42 sql += std::to_string(*limit);
43 }
45}
References new_statement().
Referenced by new_statement().
◆ upsert()
| virtual std::shared_ptr< Statement > Botan::SQL_Database::upsert |
( |
std::string_view | table, |
|
|
std::initializer_list< std::string_view > | columns ) const |
|
pure virtual |
Prepare an upsert (insert-or-replace) statement for the given columns of the given table. The returned statement expects placeholders ?1..?N bound in the order the columns were given. The list must include every column of the table's primary key; backends that need the key/value distinction (e.g. Postgres ON CONFLICT) derive it by introspecting the schema.
- Parameters
-
| table | the table to upsert into |
| columns | the columns to write, in placeholder order |
- Returns
- the prepared statement
Implemented in Botan::Sqlite3_Database.
The documentation for this class was generated from the following files: