Quark  0.1
Database Class Reference

A base class for objects that represent SQL databases. More...

#include <Database.h++>

Public Member Functions

virtual ~Database ()
 Destructor. More...
 
virtual bool open ()
 Opens the database. More...
 
virtual void close ()
 Closes the database. More...
 
QString file () const
 Returns the database filename. More...
 
RowID lastInsertRowID (const QSqlQuery &query)
 

Protected Member Functions

 Database (const QString &schemaFile, const QString &file)
 Constructs a new Database with the given schema file and database file. More...
 
QSqlQuery * buildQuery (const QString &sql)
 Builds a QSqlQuery object from the SQL statement(s) in a string. More...
 
QSqlQuery * buildQueryFromFile (const QString &schemaFile)
 Builds a QSqlQuery object from the SQL statement(s) in a text file. More...
 
bool transaction ()
 Begins a transaction. More...
 
bool rollback ()
 Rolls back the current transaction. More...
 
bool commit ()
 Commits the current transaction. More...
 
virtual bool check ()
 Checks if the database is compatible with the caller's requirements. More...
 
virtual bool prepareStatements ()
 Builds any prepared statements (QSqlQuery objects) that are necessary for this database. More...
 
virtual void teardownStatements ()
 Tears down any prepared statements that were created in prepareStatements(). More...
 
virtual bool populateTables ()
 Performs any necessary initial population of database tables in a newly created database. More...
 
virtual bool cacheData ()
 Performs any necessary in-memory caching of any existing data from the database when the database is opened. More...
 
int executeSQL (const QString &sql)
 Executes SQL statement(s) from a string. More...
 
int executeSQLFromFile (const QString &sqlFile)
 Executes SQL statement(s) from a text file. More...
 
QString generateGUID ()
 Generates a GUID string. More...
 

Detailed Description

A base class for objects that represent SQL databases.

Subclasses must provide a suitable public constructor and may override check(), prepareStatements(), teardownStatements(), populateTables(), and cacheData(); these methods are no-ops by default and are called at the appropriate times during the object's lifecycle.

Author
Mark Lindner

Constructor & Destructor Documentation

◆ ~Database()

virtual ~Database ( )
virtual

Destructor.

Implicitly closes the database.

◆ Database()

Database ( const QString &  schemaFile,
const QString &  file 
)
protected

Constructs a new Database with the given schema file and database file.

Parameters
schemaFileThe file containing the SQL statements that construct the database's schema. file The file in which the database is stored.

Member Function Documentation

◆ buildQuery()

QSqlQuery* buildQuery ( const QString &  sql)
protected

Builds a QSqlQuery object from the SQL statement(s) in a string.

Parameters
sqlThe string containing the SQL.
Returns
a new QSqlQuery object.

◆ buildQueryFromFile()

QSqlQuery* buildQueryFromFile ( const QString &  schemaFile)
protected

Builds a QSqlQuery object from the SQL statement(s) in a text file.

Parameters
schemaFileThe file containing the SQL.
Returns
a new QSqlQuery object.

◆ cacheData()

virtual bool cacheData ( )
protectedvirtual

Performs any necessary in-memory caching of any existing data from the database when the database is opened.

The default implementation is a no-op that returns true.

Returns
true on success, false on failure.

◆ check()

virtual bool check ( )
protectedvirtual

Checks if the database is compatible with the caller's requirements.

Only called when opening existing databases. The default implementation is a no-op that returns true.

Returns
true if the validation succeeds, false otherwise.

◆ close()

virtual void close ( )
virtual

Closes the database.

Calls the teardownStatements() method to tear down any prepared statements that were created in prepareStatements() when the database was opened.

This method may be overridden by subclasses if additional work is necessary when closing the database, but the overriden method must call through to the superclass implementation.

◆ commit()

bool commit ( )
inlineprotected

Commits the current transaction.

Returns
true on success, false on failure.

◆ executeSQL()

int executeSQL ( const QString &  sql)
protected

Executes SQL statement(s) from a string.

Parameters
sqlThe SQL to execute.
Returns
The number of rows affected or number of rows returned by the last statement executed.

◆ executeSQLFromFile()

int executeSQLFromFile ( const QString &  sqlFile)
protected

Executes SQL statement(s) from a text file.

Parameters
sqlFileThe text file that contains the SQL to execute.
Returns
The number of rows affected or number of rows returned by the last statement executed.

◆ file()

QString file ( ) const
inline

Returns the database filename.

◆ generateGUID()

QString generateGUID ( )
protected

Generates a GUID string.

◆ lastInsertRowID()

RowID lastInsertRowID ( const QSqlQuery &  query)

◆ open()

virtual bool open ( )
virtual

Opens the database.

The following steps take place:

  1. If the database file did not already exist, it is created, and the database schema is initialized; otherwise the existing database file is opened.
  2. The prepareStatements() method is called to build any prepared statements that are necessary.
  3. If the database is newly created, the populateTables() method is called to perform any necessary initial population of database tables. Otherwise, the cacheData() method is called to load existing data from the database into memory cache, if necessary.
  4. If the database is not newly created, the check() method is called to perform any necessary validation of the database's existing contents.

    This method may be overridden by subclasses if additional work is necessary when opening the database, but the overriden method must call through to the superclass implementation.

    Returns
    true on success, false on failure.

◆ populateTables()

virtual bool populateTables ( )
protectedvirtual

Performs any necessary initial population of database tables in a newly created database.

The default implementation is a no-op that returns true.

Returns
true on success, false on failure.

◆ prepareStatements()

virtual bool prepareStatements ( )
protectedvirtual

Builds any prepared statements (QSqlQuery objects) that are necessary for this database.

Typically, a subclass would cache such statements in member variables. The default implementation is a no-op that returns true.

Returns
true on success, false on failure.

◆ rollback()

bool rollback ( )
inlineprotected

Rolls back the current transaction.

Returns
true on success, false on failure.

◆ teardownStatements()

virtual void teardownStatements ( )
protectedvirtual

Tears down any prepared statements that were created in prepareStatements().

◆ transaction()

bool transaction ( )
inlineprotected

Begins a transaction.

Returns
true on success, false on failure.

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