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

A sqlite (sqlcipher) database. More...

#include <sqldatabase.h>

Public Member Functions

 SqlDatabase (const std::filesystem::path &path, int flags)
 Constructs a SqlDatabase.
 
 SqlDatabase (const SqlDatabase &database)
 Copies a SqlDatabase object.
 
 SqlDatabase (SqlDatabase &&database) noexcept
 Moves a SqlDatabase object.
 
const std::filesystem::path & getPath () const
 Gets the path of the database file.
 
bool isEncrypted () const
 Gets whether or not the database is encrypted.
 
sqlite3 * c_obj ()
 Returns the underlying sqlite3 object pointer for the database.
 
bool unlock (const std::string &password)
 Unlocks the database.
 
bool changePassword (const std::string &password)
 Changes the database's password.
 
bool exec (const std::string &command)
 Executes an sql command on the database.
 
SqlStatement createStatement (const std::string &command)
 Creates an new SqlStatement for the database.
 
bool registerFunction (const std::string &name, const SqliteCustomFunction &func, int expectedArgs=-1)
 Registers a custom sql function to the database.
 
SqlDatabaseoperator= (const SqlDatabase &database)
 Copies a SqlDatabase object.
 
SqlDatabaseoperator= (SqlDatabase &&database) noexcept
 Moves a SqlDatabase object.
 
 operator bool () const
 Gets whether or not the object is valid or not.
 

Detailed Description

A sqlite (sqlcipher) database.

Constructor & Destructor Documentation

◆ SqlDatabase() [1/3]

Nickvision::Database::SqlDatabase::SqlDatabase ( const std::filesystem::path & path,
int flags )

Constructs a SqlDatabase.

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

◆ SqlDatabase() [2/3]

Nickvision::Database::SqlDatabase::SqlDatabase ( const SqlDatabase & database)

Copies a SqlDatabase object.

Parameters
databaseThe object to move

◆ SqlDatabase() [3/3]

Nickvision::Database::SqlDatabase::SqlDatabase ( SqlDatabase && database)
noexcept

Moves a SqlDatabase object.

Parameters
databaseThe object to move

Member Function Documentation

◆ c_obj()

sqlite3 * Nickvision::Database::SqlDatabase::c_obj ( )

Returns the underlying sqlite3 object pointer for the database.

Using this method is strongly discouraged, as you can break the state of this object.

Returns
sqlite3*

◆ changePassword()

bool Nickvision::Database::SqlDatabase::changePassword ( const std::string & password)

Changes the database's password.

Passing an empty string will unencrypt the database.

Changing the password on an encrypted database requires that the database be unlocked first, even if removing the password.

Parameters
passwordThe new database password
Returns
True if password changed, else false
Exceptions
std::runtime_errorThrown if error in executing sql statement

◆ createStatement()

SqlStatement Nickvision::Database::SqlDatabase::createStatement ( const std::string & command)

Creates an new SqlStatement for the database.

Parameters
commandThe command to bind to the statement.
Returns
The new SqlStatement
Exceptions
std::runtime_errorThrown if error in executing sql statement

◆ exec()

bool Nickvision::Database::SqlDatabase::exec ( const std::string & command)

Executes an 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::SqlDatabase::getPath ( ) const

Gets the path of the database file.

Returns
The database file path

◆ isEncrypted()

bool Nickvision::Database::SqlDatabase::isEncrypted ( ) const

Gets whether or not the database is encrypted.

Returns
True if encrypted, else false

◆ operator bool()

Nickvision::Database::SqlDatabase::operator bool ( ) const

Gets whether or not the object is valid or not.

Returns
True if valid (if isEncrypted ? isUnlocked : true), else false

◆ operator=() [1/2]

SqlDatabase & Nickvision::Database::SqlDatabase::operator= ( const SqlDatabase & database)

Copies a SqlDatabase object.

Parameters
databaseThe SqlDatabase to copy
Returns
this

◆ operator=() [2/2]

SqlDatabase & Nickvision::Database::SqlDatabase::operator= ( SqlDatabase && database)
noexcept

Moves a SqlDatabase object.

Parameters
databaseThe SqlDatabase to move
Returns
this

◆ registerFunction()

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

Registers a custom sql function to the database.

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

◆ unlock()

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

Unlocks the database.

If the database is not encrypted and is newly created, this will encrypt and set its password.

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

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