A base class for objects that represent SQL databases.
More...
#include <Database.h++>
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
◆ ~Database()
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
-
schemaFile | The file containing the SQL statements that construct the database's schema. file The file in which the database is stored. |
◆ buildQuery()
QSqlQuery* buildQuery |
( |
const QString & |
sql | ) |
|
|
protected |
Builds a QSqlQuery object from the SQL statement(s) in a string.
- Parameters
-
sql | The 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
-
schemaFile | The 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()
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()
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()
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
-
- 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
-
sqlFile | The 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()
Returns the database filename.
◆ generateGUID()
◆ lastInsertRowID()
RowID lastInsertRowID |
( |
const QSqlQuery & |
query | ) |
|
◆ open()
Opens the database.
The following steps take place:
-
If the database file did not already exist, it is created, and the database schema is initialized; otherwise the existing database file is opened.
-
The prepareStatements() method is called to build any prepared statements that are necessary.
-
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.
-
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()
Rolls back the current transaction.
- Returns
- true on success, false on failure.
◆ teardownStatements()
virtual void teardownStatements |
( |
| ) |
|
|
protectedvirtual |
◆ transaction()
Begins a transaction.
- Returns
- true on success, false on failure.
The documentation for this class was generated from the following file: