Into

Modules

Documentation

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 Stopped.

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.

( )

Shorthand for db()->driver().

void
( )

Throws a PiiExecutionException with the given message, if ignoreErrors is false.

bool
( )

Executes query.

bool
bool

Returns true if the database connection is open and false otherwise.

bool

Opens a database connection to the given databaseUrl.

int
int
void
( )
void
( )
void
(
  • bool ignoreErrors
)
void
(
  • int retryCount
)
void
(
  • int retryDelay
)

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 higher

    • ibase - Borland InterBase

    • mysql - MySQL

    • oci - Oracle

    • odbc - ODBC (includes Microsoft SQL Server)

    • psql - PostgreSQL 7.3 and above

    • sqlite - SQLite version 3 or above

    • sqlite2 - SQLite version 2

    • tds - Sybase Adaptive Server

    • qt - 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)

    Some examples:
    • mysql://user:password@10.10.10.2/dumpdb

    • null://

    • qt://main

    databaseName

  • bool ignoreErrors

    [read, write]

    By default, all errors cause a run-time exception and stop the operation.

    Setting this flag to true suppresses 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

    ()
  • virtual void aboutToChangeState

    ()
    [protected, virtual]

    Closes the database connection if state is Stopped.

    Reimplemented from PiiBasicOperation.

  • bool checkQuery

    ()
    [protected]

    Checks an executed query for errors.

    Returns true if the query was successfully executed, false otherwise. Throws a PiiExecutionException if the query failed and ignoreErrors is false.

  • void closeConnection

    ()
    [protected]

    Closes the database connection.

  • virtual QSqlDatabase * createDatabase

    ()
    [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.

  • QString databaseName

    ()
    [protected]
  • QString databaseUrl

    ()
    [protected]
  • QSqlDatabase * db

    ()
    [protected]

    Returns a pointer to the internal database connection, or a null pointer if no connection is open.

  • QSqlDriver * driver

    ()
    [protected]

    Shorthand for db()->driver().

  • void error

    ()
    [protected]

    Throws a PiiExecutionException with the given message, if ignoreErrors is false.

    Otherwise, writes message to log.

  • bool exec

    ()
    [protected]

    Executes query.

    If the query fails with a connection-related error, retries retryCount times.

  • bool ignoreErrors

    ()
    [protected]
  • bool isConnected

    ()
    [protected]

    Returns true if the database connection is open and false otherwise.

  • 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

    true if the connection was successfully opened, false otherwise.

    Exceptions
    PiiExecutionException&

    if the connection cannot be opened and ignoreErrors is false.

  • int retryCount

    ()
    [protected]
  • int retryDelay

    ()
    [protected]
  • void setDatabaseName

    ()
    [protected]
  • void setDatabaseUrl

    ()
    [protected]
  • void setIgnoreErrors

    (
    • bool ignoreErrors
    )
    [protected]
  • void setRetryCount

    (
    • int retryCount
    )
    [protected]
  • void setRetryDelay

    (
    • int retryDelay
    )
    [protected]
Notes (0)

Add a note

Not a single note added yet. Be the first, add yours.