BPS datastore class. More...
#include <bpsdatastore.h>
Signals | |
| void | connected () |
| Signal is emitted after successful connection. | |
| void | disconnected () |
| Signal is emitted after disconnection. | |
Public Member Functions | |
| QVariant | actorKey () const |
| Return the actor's key (c_key in the t_actors table). | |
| QString | application () const |
| BpsDatastore (QObject *aParent=0) | |
| Constructor for the BpsDatastore objects. | |
| void | checkin (const QString &aTerminalName=QString(), const QString &aApplicationName=QString()) |
| Check in the user by creating a record in table t_actors. | |
| void | checkout () |
| Check out the user by dropping the record in table t_actors. | |
| void | clearKeys (QVariant aId) |
| Delete a key list from table t_keys. | |
| void | commit () |
| Commit an open transaction. | |
| void | connect () |
| Connect to database server using the current settings in the properties username, password, driver, database, host and port. | |
| QString | connection () const |
| Get the connection name. | |
| QDateTime | currentTimestamp () |
| QString | database () const |
| Get database name for PostgreSQL, or TNS service name or SID for Oracle. | |
| void | disconnect () |
| Disconnect from database server if currently connected. | |
| QString | driver () const |
| Get driver name (QPSQL or QOCI). | |
| void | dropConnection (BpsSettings::Scopes aScopes=BpsSettings::LocalUserScope) |
| Drop connection. | |
| bool | hasDivisionAccess (const QVariant &aDivisionKey) |
| Checks if the current user is authorized for the division. | |
| bool | hasPermission (const QString &aPermission) |
| Checks if the current user is authorized for the named permission. | |
| bool | hasZoneAccess (const QVariant &aZoneKey) |
| Checks if the current user is authorized for the zone. | |
| QString | host () const |
| Get host IP name or address. | |
| bool | indexExists (const QString &aName, const QString &aSchema=QString()) |
| Check if a certain index exists. | |
| bool | inTransaction () const |
| bool | isConnected () const |
| Check current connection status. | |
| bool | isOracle () const |
| Check if current driver is for Oracle. | |
| bool | isOwner () const |
| Check if the current user is the BPS owner. | |
| bool | isPostgres () const |
| Check if current driver is for PostgreSQL. | |
| QSqlDatabase & | link () |
| Get QSqlDatabase object. | |
| bool | loadConnection (BpsSettings::Scopes aScopes=BpsSettings::LocalScopes) |
| Load connection settings. | |
| bool | lockTable (const QString &aTable, int aTimeout) |
| Start a transaction with the given table locked in exclusive mode. | |
| QVariant | nextIdent (const QString &aSeqenceName) |
| Get the next sequence value. | |
| QVariant | nextKey (const QString &aName) |
| Get the next unused key for a table. | |
| QString | password () const |
| Get database user password. | |
| int | port () const |
| Get host IP port number. | |
| QSqlIndex * | primaryIndex (const QString &aTableName) |
| Get the primary index for a certain table. | |
| void | purgeActors () |
| Purge actors which are no longer connected. | |
| QSqlRecord * | record (const QString &aTableName) |
| Get the record info for a certain table. | |
| void | renameConnection (const QString &aConnection, BpsSettings::Scopes aScopes=BpsSettings::LocalUserScope) |
| Rename the connection. | |
| Bps::Access | reportAccess (const QVariant &aReportKey) |
| Check access to a certain report. | |
| QStringList | roles () |
| Get a list of all roles. | |
| void | rollback () |
| Rollback an open transaction. | |
| void | saveConnection (BpsSettings::Scopes aScopes=BpsSettings::LocalUserScope) |
| Save the connection settings. | |
| QString | schema () const |
| Get BPS datastore schema name. | |
| bool | sequenceExists (const QString &aName, const QString &aSchema=QString()) |
| Check if a certain sequence exists. | |
| QVariant | sessionId () const |
| Return the current actors session id (the database internal identifier of the current session). | |
| void | setConnection (const QString &aConnection) |
| Set the connection name. | |
| void | setDatabase (const QString &aDatabase) |
| Set database name. | |
| void | setDriver (const QString &aDriver) |
| Set database driver name. | |
| void | setHost (const QString &aHost) |
| Set DB server's host name or IP address. | |
| QVariant | setKeys (const QList< qulonglong > &aKeys) |
| Insert a key list into table t_keys. | |
| QVariant | setKeys (const QList< qlonglong > &aKeys) |
| Insert a key list into table t_keys. | |
| QVariant | setKeys (const QVariantList &aKeys) |
| Insert a key list into table t_keys. | |
| void | setPassword (const QString &aPassword) |
| Set database user password. | |
| void | setPort (int aPort) |
| Set the servers database service IP port number. | |
| void | setSchema (const QString &aSchema) |
| Set BPS schema. | |
| void | setUsername (const QString &aUsername) |
| Set database user name. | |
| bool | tableExists (const QString &aName, const QString &aSchema=QString()) |
| Check if a certain table exists. | |
| QStringList | tablespaces () |
| Get a list of all tablespaces. | |
| QString | terminal () const |
| void | touchActor () |
| Update the actors timestamp. | |
| void | transaction () |
| Start a new transaction. | |
| int | userid () const |
| Returns the user id (the numeric bps user identification). | |
| QVariant | userKey () const |
| Returns the user's key (c_key in the t_users table). | |
| QString | username () const |
| Get database user name. | |
| QStringList | users () |
| Get a list of all database user names. | |
| bool | viewExists (const QString &aName, const QString &aSchema=QString()) |
| Check if a certain view exists. | |
Static Public Member Functions | |
| static QStringList | connections () |
| Get all defined connections. | |
| static QString | quote (const QString &aText) |
| Quote a literal text in single quotes to include it in a sql statement. | |
| static QString | quote (QChar aChar) |
| Quote a char in single quotes to include it in a sql statement. | |
| static QStringList | tnsnames () |
| Get a list of all Oracle TNS names. | |
BPS datastore class.
This is the datastore class used throughout the BPS class library. It supports connections to PostgreSQL and to Oracle. The class encapsulates a Qt database connection, adding generic methods for both database types supported by BPS.
| BpsDatastore::BpsDatastore | ( | QObject * | aParent = 0 | ) |
Constructor for the BpsDatastore objects.
| aParent | Pointer to an optional parent object. |
| QVariant BpsDatastore::actorKey | ( | ) | const |
Return the actor's key (c_key in the t_actors table).
A valid actorKey is only returned when checked in, otherwise an invalid QVariant is returned.
| QString BpsDatastore::application | ( | ) | const |
| void BpsDatastore::checkin | ( | const QString & | aTerminalName = QString(), |
| const QString & | aApplicationName = QString() |
||
| ) |
Check in the user by creating a record in table t_actors.
Checking in is required for active users, because all permanent locks will be referencing the entry in t_actors. In case the current connection was already checked in, it will be checked out before attempting to do the new check in.
| [in] | aTerminalName | The terminal name for the session. Pass an empty string to use bps->terminalName(). |
| [in] | aApplicationName | The name of the application. For C++ applications the file path of the executable, for standalone scripts the file path of the script. Pass an empty string to use QCoreApplication::applicationFilePath(). |
| BpsException | When not connected, no valid userid or general sql error. |
| void BpsDatastore::checkout | ( | ) |
Check out the user by dropping the record in table t_actors.
| BpsException | When not connected or general sql error. |
| void BpsDatastore::clearKeys | ( | QVariant | aId | ) |
Delete a key list from table t_keys.
| [in] | aId | The ID of the list. |
| void BpsDatastore::commit | ( | ) |
Commit an open transaction.
| BpsException | When not connected or commit fails. |
| void BpsDatastore::connect | ( | ) |
Connect to database server using the current settings in the properties username, password, driver, database, host and port.
| BpsException | When driver cannot be loaded or connect fails. |
| QString BpsDatastore::connection | ( | ) | const |
Get the connection name.
The default connection name is empty. The connection name serves to store the database connection parameters host name, port, database and schema in a convenient way so only the connection needs to be selected to set all four parameters in one go.
| static QStringList BpsDatastore::connections | ( | ) | [static] |
Get all defined connections.
| QDateTime BpsDatastore::currentTimestamp | ( | ) |
| QString BpsDatastore::database | ( | ) | const |
Get database name for PostgreSQL, or TNS service name or SID for Oracle.
| void BpsDatastore::disconnect | ( | ) |
Disconnect from database server if currently connected.
| QString BpsDatastore::driver | ( | ) | const |
Get driver name (QPSQL or QOCI).
| void BpsDatastore::dropConnection | ( | BpsSettings::Scopes | aScopes = BpsSettings::LocalUserScope | ) |
Drop connection.
The database properties are removed from the settings.
| [in] | aScopes | Scopes at which the settings are removed. Valid scopes include BpsSettings::LocalUserSettings, BpsSettings::LocalSystemSettings and BpsSettings::LocalInstallSettings. |
| bool BpsDatastore::hasDivisionAccess | ( | const QVariant & | aDivisionKey | ) |
Checks if the current user is authorized for the division.
| [in] | aDivisionKey | The key within table t_divisions. |
| bool BpsDatastore::hasPermission | ( | const QString & | aPermission | ) |
Checks if the current user is authorized for the named permission.
| [in] | aPermission | The permission name, for example settings.view |
| bool BpsDatastore::hasZoneAccess | ( | const QVariant & | aZoneKey | ) |
Checks if the current user is authorized for the zone.
| [in] | aZoneKey | The key within table t_zones. |
| QString BpsDatastore::host | ( | ) | const |
Get host IP name or address.
| bool BpsDatastore::indexExists | ( | const QString & | aName, |
| const QString & | aSchema = QString() |
||
| ) |
Check if a certain index exists.
| [in] | aName | Name of the index. |
| [in] | aSchema | Schema name. Default schema is used when omitted. |
| BpsException | When the query fails. |
| bool BpsDatastore::inTransaction | ( | ) | const |
| bool BpsDatastore::isConnected | ( | ) | const |
Check current connection status.
| bool BpsDatastore::isOracle | ( | ) | const |
Check if current driver is for Oracle.
| bool BpsDatastore::isOwner | ( | ) | const |
Check if the current user is the BPS owner.
The schema must not be empty and be equal to the username. Compare of username and schema are done case insensitive.
| bool BpsDatastore::isPostgres | ( | ) | const |
Check if current driver is for PostgreSQL.
| QSqlDatabase& BpsDatastore::link | ( | ) |
Get QSqlDatabase object.
| bool BpsDatastore::loadConnection | ( | BpsSettings::Scopes | aScopes = BpsSettings::LocalScopes | ) |
Load connection settings.
When loading a connection that has been previously saved, the properties for node, schema, database, driver, host and port are searched and loaded from the locations defined in aScopes.
| [in] | aScopes | Scopes at which the settings are searched. Valid scopes include BpsSettings::LocalUserSettings, BpsSettings::LocalSystemSettings and BpsSettings::LocalInstallSettings. |
| bool BpsDatastore::lockTable | ( | const QString & | aTable, |
| int | aTimeout | ||
| ) |
Start a transaction with the given table locked in exclusive mode.
Commit or rollback the transaction to release the lock.
| [in] | aTable | Name of the table to lock. |
| [in] | aTimeout | Time out in seconds while waiting for lock. Special values: 0 = no wait, -1 = wait endless. |
| QVariant BpsDatastore::nextIdent | ( | const QString & | aSeqenceName | ) |
Get the next sequence value.
| [in] | aSeqenceName | The name of the sequence. |
| QVariant BpsDatastore::nextKey | ( | const QString & | aName | ) |
Get the next unused key for a table.
| [in] | aName | The base name of the sequence and table, where the prefix s_ and t_ is added. Example: aName = users, sequence = s_users and table = t_users. |
| QString BpsDatastore::password | ( | ) | const |
Get database user password.
| int BpsDatastore::port | ( | ) | const |
Get host IP port number.
The default is -1 which will assume 5432 for PostgreSQL and 1531 for Oracle (in case not using TNS).
| QSqlIndex* BpsDatastore::primaryIndex | ( | const QString & | aTableName | ) |
Get the primary index for a certain table.
If no primary index exists an empty QSqlIndex is returned.
| [in] | aTableName | The name of the table. |
| void BpsDatastore::purgeActors | ( | ) |
Purge actors which are no longer connected.
Such records can be present in case of crashed sessions. Removing them will release all permanent locks that still may be held.
| BpsException | When not connected or general sql error. |
| static QString BpsDatastore::quote | ( | const QString & | aText | ) | [static] |
Quote a literal text in single quotes to include it in a sql statement.
Takes care of any single quotes within the literal.
| [in] | aText | Literal text to be quoted. |
| static QString BpsDatastore::quote | ( | QChar | aChar | ) | [static] |
Quote a char in single quotes to include it in a sql statement.
Takes care of char being a single quote itself.
| [in] | aChar | Char to be quoted. |
| QSqlRecord* BpsDatastore::record | ( | const QString & | aTableName | ) |
Get the record info for a certain table.
| [in] | aTableName | The name of the table. |
| void BpsDatastore::renameConnection | ( | const QString & | aConnection, |
| BpsSettings::Scopes | aScopes = BpsSettings::LocalUserScope |
||
| ) |
Rename the connection.
| [in] | aConnection | New name of the connection. |
| [in] | aScopes | Scopes at which the settings are renamed. Valid scopes include BpsSettings::LocalUserSettings, BpsSettings::LocalSystemSettings and BpsSettings::LocalInstallSettings. |
| Bps::Access BpsDatastore::reportAccess | ( | const QVariant & | aReportKey | ) |
Check access to a certain report.
The connection must be established and checked in.
| aReportKey | The key within table t_reports. |
| QStringList BpsDatastore::roles | ( | ) |
Get a list of all roles.
| BpsException | When the query fails. |
| void BpsDatastore::rollback | ( | ) |
Rollback an open transaction.
| BpsException | When not connected or rollback fails. |
| void BpsDatastore::saveConnection | ( | BpsSettings::Scopes | aScopes = BpsSettings::LocalUserScope | ) |
Save the connection settings.
The database properties are saved for subsequent availability by setConnection().
| [in] | aScopes | Scopes at which the settings are saved. Valid scopes include BpsSettings::LocalUserSettings, BpsSettings::LocalSystemSettings and BpsSettings::LocalInstallSettings. |
| QString BpsDatastore::schema | ( | ) | const |
Get BPS datastore schema name.
| bool BpsDatastore::sequenceExists | ( | const QString & | aName, |
| const QString & | aSchema = QString() |
||
| ) |
Check if a certain sequence exists.
| [in] | aName | Name of the sequence. |
| [in] | aSchema | Schema name. Default schema is used when omitted. |
| BpsException | When the query fails. |
| QVariant BpsDatastore::sessionId | ( | ) | const |
Return the current actors session id (the database internal identifier of the current session).
A valid sessionId is only returned when connected, otherwise an invalid QVariant is returned.
| void BpsDatastore::setConnection | ( | const QString & | aConnection | ) |
Set the connection name.
The database connection name is used to identify a certain connection within the application.
| [in] | aConnection | Name of the connection. |
| void BpsDatastore::setDatabase | ( | const QString & | aDatabase | ) |
Set database name.
| [in] | aDatabase | Database name for PostgreSQL, TNS name or SID for Oracle. |
| void BpsDatastore::setDriver | ( | const QString & | aDriver | ) |
Set database driver name.
The database driver name as "QPSQL" for PostgreSQL, or "QOCI" for Oracle.
| [in] | aDriver | Database driver name as "QPSQL" for PostgreSQL, or "QOCI" for Oracle. |
| void BpsDatastore::setHost | ( | const QString & | aHost | ) |
Set DB server's host name or IP address.
Either the database servers host name, or it's numeric IP address.
| [in] | aHost | Database server's host name or IP address. |
| QVariant BpsDatastore::setKeys | ( | const QList< qulonglong > & | aKeys | ) |
Insert a key list into table t_keys.
Only unique keys are inserted, duplicates are silently ignored. The list will get automatically deleted on checkout. However it is good practice to purge no longer used lists with method clearKeys().
Select example:
QVariant kid = mDatastore->setKeys(myKeyList); BpsQuery q(mDatastore); q.prepare(bStr( "select * from t_sometable t " "inner join t_keys k on k.c_id=? and t.c_key=k.c_key" )); q.bind(kid); q.execute(); mDatastore->clearKeys(kid);
Update example:
QVariant kid = mDatastore->setKeys(myKeyList); BpsQuery q(mDatastore); q.prepare(bStr( "update t_sometable ... " "where c_key in (select c_key from t_keys where c_id=?)" )); q.bind(kid); q.execute(); mDatastore->clearKeys(kid);
| [in] | aKeys | The list of keys to insert. |
| QVariant BpsDatastore::setKeys | ( | const QList< qlonglong > & | aKeys | ) |
Insert a key list into table t_keys.
Only unique keys are inserted, duplicates are silently ignored. The list will get automatically deleted on checkout. However it is good practice to purge no longer used lists with method clearKeys().
| [in] | aKeys | The list of keys to insert. |
| QVariant BpsDatastore::setKeys | ( | const QVariantList & | aKeys | ) |
Insert a key list into table t_keys.
Only unique keys are inserted, duplicates are silently ignored. The list will get automatically deleted on checkout. However it is good practice to purge no longer used lists with method clearKeys().
| [in] | aKeys | The list of keys to insert. |
| void BpsDatastore::setPassword | ( | const QString & | aPassword | ) |
Set database user password.
Set the password before connecting to the database.
| [in] | aPassword | Password of the database user. |
| void BpsDatastore::setPort | ( | int | aPort | ) |
Set the servers database service IP port number.
| [in] | aPort | Database service port number on server. |
| void BpsDatastore::setSchema | ( | const QString & | aSchema | ) |
Set BPS schema.
| [in] | aSchema | BPS schema name. |
| void BpsDatastore::setUsername | ( | const QString & | aUsername | ) |
Set database user name.
Set the username before connection to the database.
| [in] | aUsername | Name of the database user. |
| bool BpsDatastore::tableExists | ( | const QString & | aName, |
| const QString & | aSchema = QString() |
||
| ) |
Check if a certain table exists.
| [in] | aName | Name of the table. |
| [in] | aSchema | Schema name. Default schema is used when omitted. |
| BpsException | When the query fails. |
| QStringList BpsDatastore::tablespaces | ( | ) |
Get a list of all tablespaces.
| BpsException | When the query fails. |
| QString BpsDatastore::terminal | ( | ) | const |
| static QStringList BpsDatastore::tnsnames | ( | ) | [static] |
Get a list of all Oracle TNS names.
| void BpsDatastore::touchActor | ( | ) |
Update the actors timestamp.
Will usually be called by license check routines.
| void BpsDatastore::transaction | ( | ) |
Start a new transaction.
The transaction will be closed either by a call to commit, or a call to rollback.
| BpsException | When not connected or transaction cannot be started. |
| int BpsDatastore::userid | ( | ) | const |
Returns the user id (the numeric bps user identification).
No valid userid is returned when the username is not made up by {schema}{userid}. The userid of the BPS owner is 0.
| QVariant BpsDatastore::userKey | ( | ) | const |
Returns the user's key (c_key in the t_users table).
A valid userKey is only returned when checked in, otherwise an invalid QVariant is returned.
| QString BpsDatastore::username | ( | ) | const |
Get database user name.
| QStringList BpsDatastore::users | ( | ) |
Get a list of all database user names.
| BpsException | When the query fails. |
| bool BpsDatastore::viewExists | ( | const QString & | aName, |
| const QString & | aSchema = QString() |
||
| ) |
Check if a certain view exists.
| [in] | aName | Name of the view. |
| [in] | aSchema | Schema name. Default schema is used when omitted. |
| BpsException | When the query fails. |