qTox  Version: nightly | Commit: bc751c8e1cac455f9690654fcfe0f560d2d7dfdd
Classes | Public Types | Public Slots | Public Member Functions | Static Public Member Functions | Protected Slots | Static Protected Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
RawDatabase Class Reference

Implements a low level RAII interface to a SQLCipher (SQlite3) database. More...

#include <rawdatabase.h>

Inheritance diagram for RawDatabase:
Inheritance graph
Collaboration diagram for RawDatabase:
Collaboration graph

Classes

class  Query
 
struct  Transaction
 

Public Types

enum  SqlCipherParams { SqlCipherParams::p3_0, SqlCipherParams::halfUpgradedTo4, SqlCipherParams::p4_0 }
 

Public Slots

bool setPassword (const QString &password)
 Changes the database password, encrypting or decrypting if necessary. More...
 
bool rename (const QString &newPath)
 Moves the database file on disk to match the new path. More...
 
bool remove ()
 Deletes the on disk database file after closing it. More...
 

Public Member Functions

 RawDatabase (const QString &path, const QString &password, const QByteArray &salt)
 Tries to open a database. More...
 
 ~RawDatabase ()
 
bool isOpen ()
 Checks, that the database is open. More...
 
bool execNow (const QString &statement)
 Executes a SQL transaction synchronously. More...
 
bool execNow (const Query &statement)
 Executes a SQL transaction synchronously. More...
 
bool execNow (const QVector< Query > &statements)
 
void execLater (const QString &statement)
 Executes a SQL transaction asynchronously. More...
 
void execLater (const Query &statement)
 
void execLater (const QVector< Query > &statements)
 
void sync ()
 Waits until all the pending transactions are executed. More...
 

Static Public Member Functions

static QString toString (SqlCipherParams params)
 

Protected Slots

bool open (const QString &path, const QString &hexKey={})
 Tries to open the database with the given (possibly empty) key. More...
 
void close ()
 Close the database and free its associated resources. More...
 
void process ()
 Implements the actual processing of pending transactions. Unqueues, compiles, binds and executes queries, then notifies of results. More...
 

Static Protected Member Functions

static QString deriveKey (const QString &password, const QByteArray &salt)
 Derives a 256bit key from the password and returns it hex-encoded. More...
 
static QString deriveKey (const QString &password)
 Derives a 256bit key from the password and returns it hex-encoded. More...
 
static QVariant extractData (sqlite3_stmt *stmt, int col)
 Extracts a variant from one column of a result row depending on the column type. More...
 
static void regexpInsensitive (sqlite3_context *ctx, int argc, sqlite3_value **argv)
 Use for create function in db for search data use regular experessions without case sensitive. More...
 
static void regexpSensitive (sqlite3_context *ctx, int argc, sqlite3_value **argv)
 Use for create function in db for search data use regular experessions without case sensitive. More...
 

Private Member Functions

QString anonymizeQuery (const QByteArray &query)
 Hides public keys and timestamps in query. More...
 
bool openEncryptedDatabaseAtLatestSupportedVersion (const QString &hexKey)
 
bool updateSavedCipherParameters (const QString &hexKey, SqlCipherParams newParams)
 Changes stored db encryption from SQLCipher 3.x defaults to 4.x defaults. More...
 
bool setCipherParameters (SqlCipherParams params, const QString &database={})
 
SqlCipherParams highestSupportedParams ()
 
SqlCipherParams readSavedCipherParams (const QString &hexKey, SqlCipherParams newParams)
 
bool setKey (const QString &hexKey)
 
int getUserVersion ()
 
bool encryptDatabase (const QString &newHexKey)
 
bool decryptDatabase ()
 
bool commitDbSwap (const QString &hexKey)
 
bool testUsable ()
 

Static Private Member Functions

static void regexp (sqlite3_context *ctx, int argc, sqlite3_value **argv, const QRegularExpression::PatternOptions cs)
 

Private Attributes

sqlite3 * sqlite
 
std::unique_ptr< QThread > workerThread
 
QQueue< TransactionpendingTransactions
 
QMutex transactionsMutex
 Protects pendingTransactions. More...
 
QString path
 
QByteArray currentSalt
 
QString currentHexKey
 

Detailed Description

Implements a low level RAII interface to a SQLCipher (SQlite3) database.

Thread-safe, does all database operations on a worker thread. The queries must not contain transaction commands (BEGIN/COMMIT/...) or the behavior is undefined.

Definition at line 52 of file rawdatabase.h.

Member Enumeration Documentation

◆ SqlCipherParams

Enumerator
p3_0 
halfUpgradedTo4 
p4_0 

Definition at line 90 of file rawdatabase.h.

Constructor & Destructor Documentation

◆ RawDatabase()

RawDatabase::RawDatabase ( const QString &  path,
const QString &  password,
const QByteArray &  salt 
)

Tries to open a database.

Parameters
pathPath to database.
passwordIf empty, the database will be opened unencrypted. Otherwise we will use toxencryptsave to derive a key and encrypt the database.

Definition at line 87 of file rawdatabase.cpp.

◆ ~RawDatabase()

RawDatabase::~RawDatabase ( )

Definition at line 129 of file rawdatabase.cpp.

Here is the call graph for this function:

Member Function Documentation

◆ anonymizeQuery()

QString RawDatabase::anonymizeQuery ( const QByteArray &  query)
private

Hides public keys and timestamps in query.

Parameters
querySource query, which should be anonymized.
Returns
Query without timestamps and public keys.

Definition at line 877 of file rawdatabase.cpp.

Here is the caller graph for this function:

◆ close

void RawDatabase::close ( )
protectedslot

Close the database and free its associated resources.

Definition at line 371 of file rawdatabase.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ commitDbSwap()

bool RawDatabase::commitDbSwap ( const QString &  hexKey)
private

Definition at line 589 of file rawdatabase.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ decryptDatabase()

bool RawDatabase::decryptDatabase ( )
private

Definition at line 569 of file rawdatabase.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ deriveKey() [1/2]

QString RawDatabase::deriveKey ( const QString &  password)
staticprotected

Derives a 256bit key from the password and returns it hex-encoded.

Parameters
passwordPassword to decrypt database
Returns
String representation of key
Deprecated:
deprecated on 2016-11-06, kept for compatibility, replaced by the salted version

Definition at line 685 of file rawdatabase.cpp.

◆ deriveKey() [2/2]

QString RawDatabase::deriveKey ( const QString &  password,
const QByteArray &  salt 
)
staticprotected

Derives a 256bit key from the password and returns it hex-encoded.

Parameters
passwordPassword to decrypt database
saltSalt to improve password strength, must be TOX_PASS_SALT_LENGTH bytes
Returns
String representation of key

Definition at line 708 of file rawdatabase.cpp.

Here is the caller graph for this function:

◆ encryptDatabase()

bool RawDatabase::encryptDatabase ( const QString &  newHexKey)
private

Definition at line 543 of file rawdatabase.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ execLater() [1/3]

void RawDatabase::execLater ( const QString &  statement)

Executes a SQL transaction asynchronously.

Parameters
statementStatement to execute.

Definition at line 452 of file rawdatabase.cpp.

Here is the caller graph for this function:

◆ execLater() [2/3]

void RawDatabase::execLater ( const Query statement)

Definition at line 457 of file rawdatabase.cpp.

Here is the call graph for this function:

◆ execLater() [3/3]

void RawDatabase::execLater ( const QVector< Query > &  statements)

◆ execNow() [1/3]

bool RawDatabase::execNow ( const QString &  statement)

Executes a SQL transaction synchronously.

Parameters
statementStatement to execute.
Returns
Whether the transaction was successful.

Definition at line 400 of file rawdatabase.cpp.

Here is the caller graph for this function:

◆ execNow() [2/3]

bool RawDatabase::execNow ( const Query statement)

Executes a SQL transaction synchronously.

Parameters
statementStatement to execute.
Returns
Whether the transaction was successful.

Definition at line 410 of file rawdatabase.cpp.

Here is the call graph for this function:

◆ execNow() [3/3]

bool RawDatabase::execNow ( const QVector< Query > &  statements)

◆ extractData()

QVariant RawDatabase::extractData ( sqlite3_stmt *  stmt,
int  col 
)
staticprotected

Extracts a variant from one column of a result row depending on the column type.

Parameters
stmtStatement to execute.
colNumber of column to extract.
Returns
Extracted data.

Definition at line 894 of file rawdatabase.cpp.

Here is the caller graph for this function:

◆ getUserVersion()

int RawDatabase::getUserVersion ( )
private

Definition at line 356 of file rawdatabase.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ highestSupportedParams()

RawDatabase::SqlCipherParams RawDatabase::highestSupportedParams ( )
private

Definition at line 297 of file rawdatabase.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ isOpen()

bool RawDatabase::isOpen ( )

Checks, that the database is open.

Returns
True if the database was opened successfully.

Definition at line 389 of file rawdatabase.cpp.

◆ open

bool RawDatabase::open ( const QString &  path,
const QString &  hexKey = {} 
)
protectedslot

Tries to open the database with the given (possibly empty) key.

Parameters
pathPath to database.
hexKeyHex representation of the key in string.
Returns
True if success, false otherwise.

Definition at line 143 of file rawdatabase.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ openEncryptedDatabaseAtLatestSupportedVersion()

bool RawDatabase::openEncryptedDatabaseAtLatestSupportedVersion ( const QString &  hexKey)
private

Definition at line 186 of file rawdatabase.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ process

void RawDatabase::process ( )
protectedslot

Implements the actual processing of pending transactions. Unqueues, compiles, binds and executes queries, then notifies of results.

Warning
MUST only be called from the worker thread

Definition at line 736 of file rawdatabase.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ readSavedCipherParams()

RawDatabase::SqlCipherParams RawDatabase::readSavedCipherParams ( const QString &  hexKey,
SqlCipherParams  newParams 
)
private

Definition at line 326 of file rawdatabase.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ regexp()

void RawDatabase::regexp ( sqlite3_context *  ctx,
int  argc,
sqlite3_value **  argv,
const QRegularExpression::PatternOptions  cs 
)
staticprivate

Definition at line 934 of file rawdatabase.cpp.

Here is the caller graph for this function:

◆ regexpInsensitive()

void RawDatabase::regexpInsensitive ( sqlite3_context *  ctx,
int  argc,
sqlite3_value **  argv 
)
staticprotected

Use for create function in db for search data use regular experessions without case sensitive.

Parameters
ctxctx the context in which an SQL function executes
argcnumber of arguments
argvarguments

Definition at line 918 of file rawdatabase.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ regexpSensitive()

void RawDatabase::regexpSensitive ( sqlite3_context *  ctx,
int  argc,
sqlite3_value **  argv 
)
staticprotected

Use for create function in db for search data use regular experessions without case sensitive.

Parameters
ctxthe context in which an SQL function executes
argcnumber of arguments
argvarguments

Definition at line 929 of file rawdatabase.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ remove

bool RawDatabase::remove ( )
slot

Deletes the on disk database file after closing it.

Note
Will process all transactions before deletings.
Returns
True if success, false otherwise.

Definition at line 646 of file rawdatabase.cpp.

Here is the call graph for this function:

◆ rename

bool RawDatabase::rename ( const QString &  newPath)
slot

Moves the database file on disk to match the new path.

Parameters
newPathPath to move database file.
Returns
True if success, false otherwise.
Note
Will process all transactions before renaming

Definition at line 612 of file rawdatabase.cpp.

Here is the call graph for this function:

◆ setCipherParameters()

bool RawDatabase::setCipherParameters ( SqlCipherParams  params,
const QString &  database = {} 
)
private

Definition at line 254 of file rawdatabase.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setKey()

bool RawDatabase::setKey ( const QString &  hexKey)
private

Definition at line 346 of file rawdatabase.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setPassword

bool RawDatabase::setPassword ( const QString &  password)
slot

Changes the database password, encrypting or decrypting if necessary.

Parameters
passwordIf password is empty, the database will be decrypted.
Returns
True if success, false otherwise.
Note
Will process all transactions before changing the password.

Definition at line 493 of file rawdatabase.cpp.

Here is the call graph for this function:

◆ sync()

void RawDatabase::sync ( )

Waits until all the pending transactions are executed.

Definition at line 482 of file rawdatabase.cpp.

◆ testUsable()

bool RawDatabase::testUsable ( )
private

Definition at line 210 of file rawdatabase.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ toString()

static QString RawDatabase::toString ( SqlCipherParams  params)
inlinestatic

Definition at line 114 of file rawdatabase.h.

Here is the caller graph for this function:

◆ updateSavedCipherParameters()

bool RawDatabase::updateSavedCipherParameters ( const QString &  hexKey,
SqlCipherParams  newParams 
)
private

Changes stored db encryption from SQLCipher 3.x defaults to 4.x defaults.

Definition at line 219 of file rawdatabase.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ currentHexKey

QString RawDatabase::currentHexKey
private

Definition at line 177 of file rawdatabase.h.

◆ currentSalt

QByteArray RawDatabase::currentSalt
private

Definition at line 176 of file rawdatabase.h.

◆ path

QString RawDatabase::path
private

Definition at line 175 of file rawdatabase.h.

◆ pendingTransactions

QQueue<Transaction> RawDatabase::pendingTransactions
private

Definition at line 173 of file rawdatabase.h.

◆ sqlite

sqlite3* RawDatabase::sqlite
private

Definition at line 171 of file rawdatabase.h.

◆ transactionsMutex

QMutex RawDatabase::transactionsMutex
private

Protects pendingTransactions.

Definition at line 174 of file rawdatabase.h.

◆ workerThread

std::unique_ptr<QThread> RawDatabase::workerThread
private

Definition at line 172 of file rawdatabase.h.


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