Botan 3.3.0
Crypto and TLS for C&
Public Member Functions | List of all members
Botan::Sqlite3_Database Class Referencefinal

#include <sqlite3.h>

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

Public Member Functions

void create_table (std::string_view table_schema) override
 
virtual size_t exec (std::string_view sql)
 
bool is_threadsafe () const override
 
std::shared_ptr< Statementnew_statement (std::string_view sql) const override
 
size_t row_count (std::string_view table_name) override
 
size_t rows_changed_by_last_statement () override
 
 Sqlite3_Database (std::string_view file, std::optional< int > sqlite_open_flags=std::nullopt)
 
 ~Sqlite3_Database () override
 

Detailed Description

Definition at line 20 of file sqlite3.h.

Constructor & Destructor Documentation

◆ Sqlite3_Database()

Botan::Sqlite3_Database::Sqlite3_Database ( std::string_view file,
std::optional< int > sqlite_open_flags = std::nullopt )

Create a new SQLite database handle from a file.

Parameters
filepath to the database file be opened and/or created
sqlite_open_flagsflags that will be passed to sqlite3_open_v2() (default: SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX)

Definition at line 17 of file sqlite3.cpp.

17 {
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}

◆ ~Sqlite3_Database()

Botan::Sqlite3_Database::~Sqlite3_Database ( )
override

Definition at line 32 of file sqlite3.cpp.

32 {
33 if(m_db) [[likely]] {
34 ::sqlite3_close(m_db);
35 }
36 m_db = nullptr;
37}

Member Function Documentation

◆ create_table()

void Botan::Sqlite3_Database::create_table ( std::string_view table_schema)
overridevirtual

Implements Botan::SQL_Database.

Definition at line 53 of file sqlite3.cpp.

53 {
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}

◆ exec()

virtual size_t Botan::SQL_Database::exec ( std::string_view sql)
inlinevirtualinherited

Definition at line 76 of file database.h.

76{ return new_statement(sql)->spin(); }
virtual std::shared_ptr< Statement > new_statement(std::string_view base_sql) const =0

◆ is_threadsafe()

bool Botan::Sqlite3_Database::is_threadsafe ( ) const
overridevirtual

Reimplemented from Botan::SQL_Database.

Definition at line 72 of file sqlite3.cpp.

72 {
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}

◆ new_statement()

std::shared_ptr< SQL_Database::Statement > Botan::Sqlite3_Database::new_statement ( std::string_view sql) const
overridevirtual

Implements Botan::SQL_Database.

Definition at line 39 of file sqlite3.cpp.

39 {
40 return std::make_shared<Sqlite3_Statement>(m_db, base_sql);
41}

Referenced by row_count().

◆ row_count()

size_t Botan::Sqlite3_Database::row_count ( std::string_view table_name)
overridevirtual

Implements Botan::SQL_Database.

Definition at line 43 of file sqlite3.cpp.

43 {
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}
std::shared_ptr< Statement > new_statement(std::string_view sql) const override
Definition sqlite3.cpp:39
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53

References Botan::fmt(), and new_statement().

◆ rows_changed_by_last_statement()

size_t Botan::Sqlite3_Database::rows_changed_by_last_statement ( )
overridevirtual

Implements Botan::SQL_Database.

Definition at line 66 of file sqlite3.cpp.

66 {
67 const auto result = ::sqlite3_changes64(m_db);
68 BOTAN_ASSERT_NOMSG(result >= 0);
69 return static_cast<size_t>(result);
70}
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:59

References BOTAN_ASSERT_NOMSG.


The documentation for this class was generated from the following files: