classPiiDatabaseOperation
#include <PiiDatabaseOperation.h>
An abstract superclass for operations that read/write databases.
Inherits PiiDefaultOperation
Inherited by PiiDatabaseReader, PiiDatabaseWriter
Description
This class provides functionality for maintaining the database connection.
Due to limitations of the low-level SQL drivers, database queries must be always made from the thread that initiated the driver. Therefore, PiiDatabaseOperation disallows changing its processing mode.
Properties
|
The name of the database. |
|
|
Database URL specified the details necessary for connecting to a database. |
|
|
bool
|
By default, all errors cause a run-time exception and stop the operation. |
|
int
|
The number of times to retry a failed query before giving up. |
|
int
|
The number of milliseconds to wait after a failed query. |
Constructors and destructor
Protected member functions
|
virtual void
|
Closes the database connection if state is |
|
bool
|
Checks an executed query for errors. |
|
void
|
(
)
Closes the database connection. |
|
virtual QSqlDatabase *
|
Returns a pointer to a newly allocated QSqlDatabase object. |
|
(
)
|
|
|
(
)
|
|
|
(
)
Returns a pointer to the internal database connection, or a null pointer if no connection is open. |
|
|
(
)
|
|
|
void
|
Throws a PiiExecutionException with the given message, if ignoreErrors is |
|
bool
|
Executes query. |
|
bool
|
(
)
|
|
bool
|
(
)
Returns |
|
bool
|
(
)
Opens a database connection to the given databaseUrl. |
|
int
|
(
)
|
|
int
|
(
)
|
|
void
|
|
|
void
|
|
|
void
|
(
|
|
void
|
(
|
|
void
|
(
|
Property details
-
QString databaseName
[read, write]The name of the database.
Database naming depends on the database engine. For Oracle, use the TNS service name. For ODBC, use either a DSN, a DSN file name (must end with .dsn), or a connection string.
PiiOperation* pDb = engine.createOperation("PiiDatabaseWriter"); // Open a Microsoft Access database pDb->setProperty("databaseUrl", "odbc://"); pDb->setProperty("databaseName", "Driver={Microsoft Access Driver (*.mdb)};" "Dbq=accessfile.mdb;" "Uid=Admin;Pwd=;");. // Append to or read from a CSV file pDb->setProperty("databaseUrl", "csv://"); pDb->setProperty("databaseName", "outputfile.csv");
The database name can also be omitted. In such a case you can set it directly with
databaseUrl. -
QString databaseUrl
[read, write]Database URL specified the details necessary for connecting to a database.
The format is scheme://[user:password@]host[:port][/database]. Bracketed parts are optional. "scheme" tells the database type. Valid values are:
db2- IBM DB2, v7.1 and higheribase- Borland InterBasemysql- MySQLoci- Oracleodbc- ODBC (includes Microsoft SQL Server)psql- PostgreSQL 7.3 and abovesqlite- SQLite version 3 or abovesqlite2- SQLite version 2tds- Sybase Adaptive Serverqt- use a connection created by QSqlDatabase::addDatabase(). If the database name is empty, the default database connection will be used.null- no output (default value)
mysql://user:password@10.10.10.2/dumpdbnull://qt://main
-
bool ignoreErrors
[read, write]By default, all errors cause a run-time exception and stop the operation.
Setting this flag to
truesuppresses the exceptions and allows the operation to continue running. A log message will be generated instead. -
int retryCount
[read, write]The number of times to retry a failed query before giving up.
If ignoreErrors is
false, the operation will stop after this many unsuccessful retry attempts. Default is 0. Only connection-related errors (not SQL errors) may cause a retrial. -
int retryDelay
[read, write]The number of milliseconds to wait after a failed query.
Default is 50.
Function details
-
PiiDatabaseOperation
() -
~PiiDatabaseOperation
() -
Closes the database connection if state is
Stopped.Reimplemented from PiiBasicOperation.
-
Checks an executed query for errors.
Returns
trueif the query was successfully executed,falseotherwise. Throws a PiiExecutionException if the query failed and ignoreErrors isfalse. -
void closeConnection
()[protected]Closes the database connection.
-
virtual QSqlDatabase * createDatabase
(- const QString & driver
- const QString & user
- const QString & password
- const QString & host
- int port
- const QString & database
[protected, virtual]Returns a pointer to a newly allocated QSqlDatabase object.
Subclasses may override this function to provide new connection schemes and to perform additional initialization.
-
-
-
Returns a pointer to the internal database connection, or a null pointer if no connection is open.
-
Throws a PiiExecutionException with the given message, if ignoreErrors is
false.Otherwise, writes message to log.
-
Executes query.
If the query fails with a connection-related error, retries retryCount times.
-
bool ignoreErrors
()[protected] -
bool isConnected
()[protected]Returns
trueif the database connection is open andfalseotherwise. -
bool openConnection
()[protected]Opens a database connection to the given databaseUrl.
This function must be called from the process() function to ensure the database is always accessed from the same thread. This is a limitation of the low-level SQL drivers.
Returns
trueif the connection was successfully opened,falseotherwise.Exceptions
- PiiExecutionException&
if the connection cannot be opened and ignoreErrors is
false.
-
int retryCount
()[protected] -
int retryDelay
()[protected] -
-
-
void setIgnoreErrors
(- bool ignoreErrors
[protected] -
void setRetryCount
(- int retryCount
[protected] -
void setRetryDelay
(- int retryDelay
[protected]
Add a note
Not a single note added yet. Be the first, add yours.