libnick 2025.9.1
A cross-platform base for native Nickvision applications.
Loading...
Searching...
No Matches
Nickvision::Database::SqliteDatabase Class Reference

A thread-safe sqlite (sqlcipher) database. More...

#include <sqlitedatabase.h>

Public Member Functions

 SqliteDatabase (const std::filesystem::path &path, int flags=SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE)
 Constructs a SqliteDatabase.
 
 SqliteDatabase (const SqliteDatabase &)=delete
 
 SqliteDatabase (SqliteDatabase &&other) noexcept
 Constructs a SqliteDatabase via move.
 
 ~SqliteDatabase () noexcept
 Destructs a SqliteDatabase.
 
const std::filesystem::path & getPath () const noexcept
 Gets the path of the database file.
 
bool isEncrypted () const noexcept
 Gets whether or not the database is encrypted.
 
bool isUnlocked () const noexcept
 Gets whether or not the database is unlocked.
 
bool unlock (const std::string &password) noexcept
 Unlocks the database.
 
bool setPassword (const std::string &password)
 Sets the database's password.
 
bool registerFunction (const std::string &name, const SqliteCustomFunction &func, int expectedArgs=-1) noexcept
 Registers a custom sql function to the database.
 
SqliteStatement createStatement (const std::string &command)
 Creates a new SqlStatement for the database.
 
bool execute (const std::string &command) noexcept
 Executes a sql command on the database.
 
SqliteDatabaseoperator= (const SqliteDatabase &)=delete
 
SqliteDatabaseoperator= (SqliteDatabase &&other) noexcept
 Assigns a SqliteDatabase via move.
 

Detailed Description

A thread-safe sqlite (sqlcipher) database.

Constructor & Destructor Documentation

◆ SqliteDatabase() [1/3]

Nickvision::Database::SqliteDatabase::SqliteDatabase ( const std::filesystem::path & path,
int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE )

Constructs a SqliteDatabase.

If the database if not encrypted, it will be unlocked automatically.

Parameters
pathThe path to the database file
flagsThe flags for opening the database
Exceptions
std::runtime_errorThrown if error in executing sql statement

◆ SqliteDatabase() [2/3]

Nickvision::Database::SqliteDatabase::SqliteDatabase ( const SqliteDatabase & )
delete

◆ SqliteDatabase() [3/3]

Nickvision::Database::SqliteDatabase::SqliteDatabase ( SqliteDatabase && other)
noexcept

Constructs a SqliteDatabase via move.

Parameters
otherThe SqliteDatabase to move

◆ ~SqliteDatabase()

Nickvision::Database::SqliteDatabase::~SqliteDatabase ( )
noexcept

Destructs a SqliteDatabase.

Member Function Documentation

◆ createStatement()

SqliteStatement Nickvision::Database::SqliteDatabase::createStatement ( const std::string & command)

Creates a new SqlStatement for the database.

Executing the statement is controlled by calling step() on the statement object.

Parameters
commandThe command to bind to the statement.
Returns
The new SqlStatement if successful
An empty SqlStatement if failed (can be checked with operator bool())
Exceptions
std::runtime_errorThrown if error in executing sql statement

◆ execute()

bool Nickvision::Database::SqliteDatabase::execute ( const std::string & command)
noexcept

Executes a sql command on the database.

Parameters
commandThe command to execute
Returns
True if command returned SQLITE_OK, else false

◆ getPath()

const std::filesystem::path & Nickvision::Database::SqliteDatabase::getPath ( ) const
noexcept

Gets the path of the database file.

Returns
The database file path

◆ isEncrypted()

bool Nickvision::Database::SqliteDatabase::isEncrypted ( ) const
noexcept

Gets whether or not the database is encrypted.

Returns
True if encrypted, else false

◆ isUnlocked()

bool Nickvision::Database::SqliteDatabase::isUnlocked ( ) const
noexcept

Gets whether or not the database is unlocked.

Returns
True if unlocked, else false

◆ operator=() [1/2]

SqliteDatabase & Nickvision::Database::SqliteDatabase::operator= ( const SqliteDatabase & )
delete

◆ operator=() [2/2]

SqliteDatabase & Nickvision::Database::SqliteDatabase::operator= ( SqliteDatabase && other)
noexcept

Assigns a SqliteDatabase via move.

Parameters
otherThe SqliteDatabase to move
Returns
Reference to this SqliteDatabase

◆ registerFunction()

bool Nickvision::Database::SqliteDatabase::registerFunction ( const std::string & name,
const SqliteCustomFunction & func,
int expectedArgs = -1 )
noexcept

Registers a custom sql function to the database.

Parameters
nameThe name of the sql function
funcThe custom sql function
expectedArgsThe number of args the sql function expects to receive (specify -1 for unlimited number of args)
Returns
True if function registered, else false

◆ setPassword()

bool Nickvision::Database::SqliteDatabase::setPassword ( const std::string & password)

Sets the database's password.

If the database is not encrypted, it will be encrypted with the password.

If the database is encrypted and locked, this method will return false.

If the database is encrypted and unlocked and the password is empty, it will be decrypted.

If the database is encrypted and unlocked and the password is not empty, it will be reencrypted with the new password.

Parameters
passwordThe new database password
Returns
True if successful, else false
Exceptions
std::runtime_errorThrown if database cannot be opened

◆ unlock()

bool Nickvision::Database::SqliteDatabase::unlock ( const std::string & password)
noexcept

Unlocks the database.

If the database is not encrypted, this method will have no effect and return true.

If the database is encrypted, this method must be called before any other operations.

Parameters
passwordThe password of the database
Returns
True if unlocked, else false

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