Torc  0.1
Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
TorcDB Class Referenceabstract

Base Sql database access class. More...

#include <torcdb.h>

Inheritance diagram for TorcDB:
[legend]

Public Member Functions

 TorcDB (const QString &DatabaseName, const QString &DatabaseType)
 
virtual ~TorcDB ()
 
bool IsValid (void)
 Returns true if the datbase has been opened/created. More...
 
void CloseThreadConnection (void)
 Close the database connection for the current thread. More...
 
void LoadSettings (QMap< QString, QString > &Settings)
 Retrieve all persistent settings stored in the database. More...
 
void SetSetting (const QString &Name, const QString &Value)
 Set the setting Name to the value Value. More...
 

Protected Member Functions

virtual bool InitDatabase (void)=0
 
QString GetThreadConnection (void)
 Retrieve a database connection for the current thread. More...
 
void CloseConnections (void)
 Close all cached database connections. More...
 

Static Protected Member Functions

static bool DebugError (QSqlQuery *Query)
 Log database errors following a failed query. More...
 
static bool DebugError (QSqlDatabase *Database)
 Log database errors following database creation. More...
 

Protected Attributes

bool m_databaseValid
 
QString m_databaseName
 
QString m_databaseType
 
QMutex m_lock
 
QHash< QThread *, QString > m_connectionMap
 

Detailed Description

Base Sql database access class.

TorcDB is the base implementation for Sql database access. It is currently limited to setting and retrieving settings and whilst designed for generic Sql usage, the only current concrete subclass is TorcSQliteDB. Changes may (will) be required for other database types and usage.

By default, QSql database access is not thread safe and a new connection must be opened for each thread that wishes to use the database. TorcDB (and its subclasses) will ask for a connection each time the database is accessed and connections are cached on a per-thread basis. The thread name (and pointer) is used for cache tracking and thus any thread must have a name. This implicitly limits database access to a QThread or one its subclasses (e.g. TorcQThread) and hence database access will not work from QRunnable's or other miscellaneous threads - this is by design and is NOT a bug.

See also
TorcSQLiteDB
TorcLocalContext

Definition at line 11 of file torcdb.h.

Constructor & Destructor Documentation

TorcDB::TorcDB ( const QString &  DatabaseName,
const QString &  DatabaseType 
)

Definition at line 51 of file torcdb.cpp.

TorcDB::~TorcDB ( )
virtual

Definition at line 60 of file torcdb.cpp.

Member Function Documentation

void TorcDB::CloseConnections ( void  )
protected

Close all cached database connections.

Definition at line 79 of file torcdb.cpp.

void TorcDB::CloseThreadConnection ( void  )

Close the database connection for the current thread.

Definition at line 104 of file torcdb.cpp.

bool TorcDB::DebugError ( QSqlQuery *  Query)
staticprotected

Log database errors following a failed query.

See also
Debugerror(QSqlDatabase*)

Definition at line 162 of file torcdb.cpp.

bool TorcDB::DebugError ( QSqlDatabase *  Database)
staticprotected

Log database errors following database creation.

See also
Debugerror(QSqlQuery*)

Definition at line 193 of file torcdb.cpp.

QString TorcDB::GetThreadConnection ( void  )
protected

Retrieve a database connection for the current thread.

Database connections are cached (thus limiting connections to one per thread).

See also
CloseConnections
CloseThreadConnection

Definition at line 125 of file torcdb.cpp.

virtual bool TorcDB::InitDatabase ( void  )
protectedpure virtual

Implemented in TorcSQLiteDB.

bool TorcDB::IsValid ( void  )

Returns true if the datbase has been opened/created.

Definition at line 70 of file torcdb.cpp.

void TorcDB::LoadSettings ( QMap< QString, QString > &  Settings)

Retrieve all persistent settings stored in the database.

Settings are key/value pairs (of strings).

Settings are designed as application wide configurable settings that can only changed by the administrative user (if present, otherwise the default user).

See also
SetSetting

Definition at line 229 of file torcdb.cpp.

void TorcDB::SetSetting ( const QString &  Name,
const QString &  Value 
)

Set the setting Name to the value Value.

Definition at line 250 of file torcdb.cpp.

Member Data Documentation

QHash<QThread*,QString> TorcDB::m_connectionMap
protected

Definition at line 35 of file torcdb.h.

QString TorcDB::m_databaseName
protected

Definition at line 32 of file torcdb.h.

QString TorcDB::m_databaseType
protected

Definition at line 33 of file torcdb.h.

bool TorcDB::m_databaseValid
protected

Definition at line 31 of file torcdb.h.

QMutex TorcDB::m_lock
protected

Definition at line 34 of file torcdb.h.


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