SlHelpers
Loading...
Searching...
No Matches
SlSqlite::SQLConn Class Reference

SQLite3 connection (the core class). More...

#include <SQLConn.h>

Classes

struct  TableEntry
 A table to be created by createTables(). More...

Public Member Functions

 SQLConn (SQLConn &&)=default
 Move constructor.
SQLConnoperator= (SQLConn &&)=default
 Move assignment.
bool open (const std::filesystem::path &dbFile, unsigned int flags=0)
 Open a database connection (openDB() + createDB() + prepDB()).
bool openDB (const std::filesystem::path &dbFile, unsigned int flags=0) noexcept
 Just open a database file.
virtual bool createDB ()
 Creates tables, views, triggers and such.
virtual bool prepDB ()
 Prepares statements.
bool exec (const std::string &sql) const noexcept
 Execute sql.
bool attach (const std::filesystem::path &dbFile, std::string_view dbName) const noexcept
 Attach another database using ATTACH.
bool begin (TransactionType type=TransactionType::DEFERRED) const noexcept
 Begin a transaction.
bool end () const noexcept
 End a transaction.
AutoTransaction beginAuto (TransactionType type=TransactionType::DEFERRED) const noexcept
 Begin a transaction which is automatically ended when the returned object dies.
std::string lastError () const
 Return the last error string if some.
int lastErrorCode () const
 Return the last error number.
int lastErrorCodeExt () const
 Return the last extended error number.

Protected Types

enum  TableFlags : unsigned { TABLE_TEMPORARY = 1u << 0 }
 Flags for TableEntry::flags.
using BindVal = std::variant<std::monostate, int, unsigned, std::string, std::string_view>
 Bind value (SQL's null, number, string).
using Binding = std::vector<std::pair<std::string, BindVal>>
 Bind name -> bind value.
using Column = std::variant<std::monostate, int, std::string>
 One column returned by SELECT.
using Row = std::vector<Column>
 One row returned by SELECT (ie. list of Columns).
using SelectResult = std::vector<Row>
 Complete SELECT result.
using Tables = std::vector<TableEntry>
 Tables to be created by createTables().
using Indices = std::vector<std::pair<std::string, std::string>>
 Indices to be created by createIndices().
using Triggers = Indices
 Triggers to be created by createTriggers().
using Views = Indices
 Views to be created by createViews().
using Statements = std::vector<std::pair<std::reference_wrapper<SQLStmtHolder>, std::string>>
 Statements to be prepared by prepareStatements().
using LastError = SlHelpers::LastErrorStream<int, int>
 Store a string + 2 ints.

Protected Member Functions

bool createTables (const Tables &tables) const noexcept
 Create tables in the DB as specified in tables.
bool createIndices (const Indices &indices) const noexcept
 Create indices in the DB as specified in indices.
bool createTriggers (const Triggers &triggers) const noexcept
 Create triggers in the DB as specified in triggers.
bool createViews (const Views &views) const noexcept
 Create views in the DB as specified in views.
bool prepareStatement (std::string_view sql, SQLStmtHolder &stmt) const noexcept
 Prepate one sql string into stmt.
bool prepareStatements (const Statements &stmts) const noexcept
 Prepate all statements as specified in stmts.
bool bind (const SQLStmtHolder &ins, const std::string &key, const BindVal &val, bool transient=false) const noexcept
 Bind one value val into key of the statement ins.
bool bind (const SQLStmtHolder &ins, const Binding &binding, bool transient=false) const noexcept
 Bind one binding for the statement ins.
bool step (const SQLStmtHolder &ins, uint64_t *affected=nullptr, bool *uniqueError=nullptr) const noexcept
 Do one step of the statement ins.
bool insert (const SQLStmtHolder &ins, const Binding &binding={}, uint64_t *affected=nullptr) const noexcept
 Bind, step, and reset the statement ins, using the passed binding.
std::optional< SQLConn::SelectResultselect (const SQLStmtHolder &sel, const Binding &binding) const noexcept
 Perform one SELECT (sel), using the passed binding.
LastErrorsetError (int ret, std::string_view error, bool errmsg=false) const
 Set last error to ret and error.

Static Protected Member Functions

static BindVal valueOrNull (bool cond, BindVal val)
 A helper to build a null BindVal if cond does not hold, val otherwise.

Protected Attributes

SQLHolder sqlHolder
 The DB connection.
unsigned int m_flags
 OpenFlags.
LastError m_lastError
 The last error + error code + extended error code.

Friends

class Select

Detailed Description

SQLite3 connection (the core class).

Member Function Documentation

◆ attach()

bool SlSqlite::SQLConn::attach ( const std::filesystem::path & dbFile,
std::string_view dbName ) const
noexcept

Attach another database using ATTACH.

Parameters
dbFilePath to another DB
dbNameName to use for this attached database
Returns
true on success.

◆ begin()

bool SlSqlite::SQLConn::begin ( TransactionType type = TransactionType::DEFERRED) const
noexcept

Begin a transaction.

Parameters
typeKind of transaction
Returns
true on success.

◆ beginAuto()

AutoTransaction SlSqlite::SQLConn::beginAuto ( TransactionType type = TransactionType::DEFERRED) const
inlinenoexcept

Begin a transaction which is automatically ended when the returned object dies.

Parameters
typeKind of transaction
Returns
AutoTransaction which ends the transation when destructed.

◆ createDB()

virtual bool SlSqlite::SQLConn::createDB ( )
inlinevirtual

Creates tables, views, triggers and such.

Returns
true on success.

Override this if you want to create some structures in the database.

◆ end()

bool SlSqlite::SQLConn::end ( ) const
inlinenoexcept

End a transaction.

Returns
true on success.

◆ exec()

bool SlSqlite::SQLConn::exec ( const std::string & sql) const
inlinenoexcept

Execute sql.

Parameters
sqlSQL to execute
Returns
true on success.

◆ open()

bool SlSqlite::SQLConn::open ( const std::filesystem::path & dbFile,
unsigned int flags = 0 )
inline

Open a database connection (openDB() + createDB() + prepDB()).

Parameters
dbFilePath to the database
flagsFlags to use (like OpenFlags::CREATE)
Returns
true on success.

◆ openDB()

bool SlSqlite::SQLConn::openDB ( const std::filesystem::path & dbFile,
unsigned int flags = 0 )
noexcept

Just open a database file.

Parameters
dbFilePath to the database
flagsFlags to use (like OpenFlags::CREATE)
Returns
true on success.

◆ prepDB()

virtual bool SlSqlite::SQLConn::prepDB ( )
inlinevirtual

Prepares statements.

Returns
true on success.

Override this if you want to use some statements.


The documentation for this class was generated from the following file:
  • /home/abuild/rpmbuild/BUILD/slhelpers-20260428.f233ce9-build/slhelpers-20260428.f233ce9/include/sqlite/SQLConn.h