Botan 3.13.0
Crypto and TLS for C&
Botan::SQL_Database Class Referenceabstract

#include <database.h>

Inheritance diagram for Botan::SQL_Database:
Botan::Sqlite3_Database

Classes

class  Column
class  SQL_DB_Error
class  Statement
class  Table_Schema

Public Types

enum class  Column_Type : uint8_t { Blob , String , Integer }

Public Member Functions

virtual void create_table (const Table_Schema &schema)=0
virtual size_t exec (std::string_view sql)
virtual bool is_threadsafe () const
virtual bool is_valid_table_name (std::string_view table) const
virtual std::shared_ptr< Statementnew_statement (std::string_view base_sql) const =0
virtual size_t row_count (std::string_view table_name)=0
virtual size_t rows_changed_by_last_statement ()=0
virtual std::shared_ptr< Statementselect (std::string_view columns, std::string_view table, std::string_view where={}, std::optional< size_t > limit=std::nullopt) const
virtual std::shared_ptr< Statementupsert (std::string_view table, std::initializer_list< std::string_view > columns) const =0
virtual ~SQL_Database ()=default

Detailed Description

Abstract interface to a SQL database

Definition at line 26 of file database.h.

Member Enumeration Documentation

◆ Column_Type

enum class Botan::SQL_Database::Column_Type : uint8_t
strong

The supported column types

Enumerator
Blob 
String 
Integer 

Definition at line 196 of file database.h.

196 : uint8_t {
197 Blob,
198 String,
199 Integer,
200 };

Constructor & Destructor Documentation

◆ ~SQL_Database()

virtual Botan::SQL_Database::~SQL_Database ( )
virtualdefault

Member Function Documentation

◆ create_table()

virtual void Botan::SQL_Database::create_table ( const Table_Schema & schema)
pure virtual

Create a table

Parameters
schemathe 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
sqlthe SQL text to execute
Returns
the number of result rows which were stepped over

Definition at line 342 of file database.h.

342{ return new_statement(sql)->spin(); }
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.

348{ return false; }

◆ 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
tablethe name to check
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
19 constexpr auto valid_table_name_char = CharacterValidityTable::alpha_numeric_plus("_");
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)
Definition charset.h:114

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_sqlthe 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_namethe 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
columnsthe columns to select
tablethe table to select from
wherethe body of the WHERE clause, or empty for no WHERE clause
limitthe 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 }
44 return new_statement(sql);
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
tablethe table to upsert into
columnsthe 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: