Isis 3 Programmer Reference
|
Isis database class providing generalized access to a variety of databases. More...
#include <Database.h>
Public Types | |
enum | Access { Connect , DoNotConnect } |
Access status for database creation. More... | |
Public Member Functions | |
Database () | |
Default database constructor. | |
Database (Access dbConn) | |
Construction of the unamed database with optional connection. | |
Database (const QString &connName, const QString &driverType) | |
Create database connection specified by name and driver type. | |
Database (const QString &name, Access dbConn=Connect) | |
Create a named database object. | |
Database (const DbProfile &profile, Access dbConn=Connect) | |
Create database connection using the supplied DbProfile. | |
virtual | ~Database () |
Database destructor. | |
QString | Name () const |
Return the name of this database as specifed upon creation. | |
void | makePersistant () |
Makes this instance persistant. | |
bool | isPersistant () const |
Checks persistancy state of a database instantiation. | |
void | setAsDefault () |
Sets this database connection/profile as the default. | |
Database | clone (const QString &name) const |
Clones this database into another giving it another name. | |
QStringList | getTables () const |
Returns a vector string containing all the tables in the database. | |
QStringList | getViews () const |
Returns a vector string containing all views within the database. | |
QStringList | getSystemTables () const |
Returns vector strings of all available system tables in the database. | |
Static Public Member Functions | |
static void | remove (const QString &name) |
Removes the named database from pool. | |
static bool | addAccessConfig (const QString &confFile) |
Adds a user specifed access configuration file to system. | |
static DbProfile | getProfile (const QString &name) |
Retrieves the named database access profile. | |
Protected Member Functions | |
Database (const QSqlDatabase &other, const QString &name) | |
Constructor creates a clone/copy from an existing one. | |
QSqlDatabase | init (const DbProfile &profile, Access dbConn=Connect) |
Create and initialize a new database connection from a DbProfile. | |
QSqlDatabase | init (const QString &name="", const QString &driverType="") |
Initializes a database by connection name and driver type. | |
void | configureAccess (QSqlDatabase &db, const DbProfile &profile) |
Set access parameters from a database DbProfile access specification. | |
Private Member Functions | |
void | tossDbError (const QString &message, const char *f, int l) const |
Generic exception tosser. | |
Private Attributes | |
QString | _name |
Needed due to peculiar issues with Database construction techniques. | |
Static Private Attributes | |
static QString | _actualConnectionName = "" |
Isis database class providing generalized access to a variety of databases.
This class provides database connections within the Isis application programming interface (API) environment. It is based upon the DatabaseFactory class and utilizes its features to let users control access to databases. See the documentation for that class to get a full description of generalized access methods as defined by users.
This class also provides programmer derived access using either database profiles (DbProfile) containing access (DbAccess) specifications.
Connections can be made to specific databases using named drivers also provided from the DatabaseFactory class.
Since this class is derived from the Qt QSqlDatabase class, it can and is intended to be used in the Qt environment directly. IMPORTANT NOTE: The init() function returns a QSqlDatabase instance in all constructors which means that the class has not yet completed constructing. The implications of this are that the Database class elements, namely data constructs, are generally off limits until after the return from init(). This is primarily the reason for some of the implimentation decisions made in this class.
See also SqlQuery and SqlRecord.
2007-06-05 Brendan George - Modified to work with QString/StringTools merge
2008-10-30 Steven Lambright - tossDbError now accepts a const char* for a filename, issue pointed out by "novus0x2a" (Support Board Member)
Definition at line 70 of file Database.h.
Access status for database creation.
Enumerator | |
---|---|
Connect | Connect to database immediately. |
DoNotConnect | Do not connect to database. |
Definition at line 73 of file Database.h.
Isis::Database::Database | ( | ) |
Default database constructor.
This constructor does not interact at all with the DatabaseFactory class but uses the default condition for the Qt QSqlDatabase state.
Definition at line 33 of file Database.cpp.
Referenced by clone().
Isis::Database::Database | ( | Database::Access | dbConn | ) |
Construction of the unamed database with optional connection.
This Database constructor essentially will attempt to invoke the default profile as provided by the DatabaseFactory class. That profile is read when the factory is created using the IsisPreferences class. If there is a Database object there and it contains a specification of an AccessConfig profile, the contents of the profile mentioned there govern the action of this constructor.
If the caller provides true to this class, it will immediately attempt a connection. Otherwise it will not attempt a connection to the database.
Use the isOpen Qt method to check for the status of the connection.
dbConn | If Connect, an immediate connection is attempted, otherwise connection is deferred. |
Definition at line 53 of file Database.cpp.
References _name, Connect, and tossDbError().
Isis::Database::Database | ( | const QString & | connName, |
const QString & | driverType ) |
Create database connection specified by name and driver type.
This constructor is useful for creating a named database with a specific driver type. The following example creates a PostgreSQL database named "sparky".
If you do not provide a driver (driverType = ""), then it will attempt to find an existing database connection named "sparky" and use it or it will attempt to resolve the request by searching for a DbProfile named "sparky".
connName | Name of connect to create or return |
driverType | Type of database to created. This is typically MySQL, PostgreSQL or SQLite. |
Definition at line 104 of file Database.cpp.
References _name.
Isis::Database::Database | ( | const QString & | name, |
Database::Access | dbConn = Connect ) |
Create a named database object.
This construction scheme assumes the named database either already exists as a persistant database connection or exists as a user specified profile in the DatabaseFactory environment.
name | Name of the desired database connection to establish |
dbConn | If Connect, an immediate connection is attempted, otherwise connection is deferred. |
Definition at line 75 of file Database.cpp.
References _name, Connect, and tossDbError().
Isis::Database::Database | ( | const DbProfile & | profile, |
Database::Access | dbConn = Connect ) |
Create database connection using the supplied DbProfile.
This constructor accepts a DbProfile that contains sufficient information to create a complete database connection. The caller can optional request that the connection be established meaning that the profile contain enough information to do so. If connect = false, then upon return, the caller can further add or modify connection parameters as needed.
profile | DbProfile containing a single database connection profile or one that provides enough information to determine appropriate access information. |
dbConn | If Connect, an immediate connection is attempted, otherwise connection is deferred. |
Definition at line 124 of file Database.cpp.
References _name, Connect, and tossDbError().
|
virtual |
Database destructor.
This will close the Database connection if it is still open, and, if it is not marked as persistant, it is removed from the named Database pool. It is not completely removed (from the Qt QSqlDatabase pool), however. Use the remove() method to ensure it is completely destroyed/removed from the pool.
Definition at line 160 of file Database.cpp.
References _name, and Isis::DatabaseFactory::getInstance().
|
protected |
Constructor creates a clone/copy from an existing one.
This constructor creates a clone or copy of an existing one. You can be sure that you can send it a Database object as well as a Qt QSqlDatabase since the Database class inherits the QT QSqlDatabase class.
other | Database to clone from this one |
newName | New name of the cloned database (it can't be the same name) |
Definition at line 146 of file Database.cpp.
|
static |
Adds a user specifed access configuration file to system.
This method accepts a file name that contains a Database access configuration file and adds it to the database access profile system. This is actually performed by the DatabaseFactory class.
confFile | Name of file to add. This can have any valid Isis or environment variable as part of the file specfication. |
Definition at line 275 of file Database.cpp.
References Isis::DatabaseFactory::getInstance().
Database Isis::Database::clone | ( | const QString & | name | ) | const |
Clones this database into another giving it another name.
This database object is cloned into another one and names it the provided name. All access parameters are retained as initiallyt set up.
name | Name to give the cloned database. |
Definition at line 499 of file Database.cpp.
References Database().
|
protected |
Set access parameters from a database DbProfile access specification.
This method takes a database and a database access configuration setup and applies the parameters to it setting up access. This method does not intiate the connection, only sets known, common parameters. These parameters are Host, DbName, User, password, Port and Options. They follow the specifications of the Qt SQL QSqlDatabase class methods.
db | The Qt database object to set access parameters for. |
profile | The database access parameter source. |
Definition at line 455 of file Database.cpp.
References Isis::DbProfile::exists(), Isis::toInt(), and Isis::IException::User.
Referenced by init().
|
static |
Retrieves the named database access profile.
This method is provided to the calling environment to retrieve any named profile. If an empty string is provided, it returns the default as determined by the DatabaseFactory class rules.
This can be used to determine the default and potentially augment its contents prior to creating a database connection.
For example, here is a small code segment that retrieves the default access profile and tests for its validity. If it is not valid, chances are there is no default established.
name | Name of profile to retrieve. An empty string will return the default profile. |
Definition at line 312 of file Database.cpp.
References Isis::DatabaseFactory::getInstance().
QStringList Isis::Database::getSystemTables | ( | ) | const |
Returns vector strings of all available system tables in the database.
This method returns a vector of strings containing a list of all system tables accessable to the user within the database.
Definition at line 536 of file Database.cpp.
QStringList Isis::Database::getTables | ( | ) | const |
Returns a vector string containing all the tables in the database.
This method returns a complete list of accessable tables within the database. It is assumed the database connections is established and open.
Definition at line 511 of file Database.cpp.
QStringList Isis::Database::getViews | ( | ) | const |
Returns a vector string containing all views within the database.
This method returns a vector of strings with all views accessable to the user in each element in the vector.
Definition at line 524 of file Database.cpp.
|
protected |
Create and initialize a new database connection from a DbProfile.
This init method accepts a DbProfile database access profile that is assumed to contain sufficient information to establish a connection and open it. Note that the connection is opened only if the connect = true. Otherwise, the parameters from teh profile is set but the database is returned without initiating a connection to the database - this so the caller can adjust or provide additional parameters.
NOTE: This method is implemented in such a way that it assumes it is part of the QSqlDatabase initialization phase upon object construction. You will see some implementation decisions based upon this expeectation.
profile | A valid database profile specifying access parameters. |
dbConn | If Connect, an immediate connection is attempted, otherwise connection is deferred. |
Definition at line 403 of file Database.cpp.
References configureAccess(), Connect, Isis::DatabaseFactory::getInstance(), Isis::DbProfile::isValid(), Isis::DbProfile::Name(), Isis::IException::Programmer, tossDbError(), and Isis::IException::User.
Referenced by init().
|
protected |
Initializes a database by connection name and driver type.
This method accepts (optional) connection name and driver type to establish a database connection. If both passed string parameters are empty, then either the default will be returned or a new database connection is returned using the default profile - if one is established. If neither of these conditions are met, this routine will throw an error.
If only a connection name is given but no driver, then either a persistant connection or a default profile must exist.
If both a name and driver is provided, then a clean database object is returned without any connection parameters set and the application programmer must set them.
NOTE: This method is implemented in such a way that it assumes it is part of the QSqlDatabase initialization phase upon object construction. You will see some implementation decisions based upon this expeectation.
connName | Name of the connection to create |
driverType | Type of driver/database to create. This is typically MySQL, PostgreSQL or SQLite. |
Definition at line 343 of file Database.cpp.
References DoNotConnect, Isis::DatabaseFactory::getInstance(), init(), and Isis::DbProfile::isValid().
bool Isis::Database::isPersistant | ( | ) | const |
Checks persistancy state of a database instantiation.
This method tests to determine if this database connection is persistant so that future access can be utilized in this state.
Definition at line 207 of file Database.cpp.
References _name, and Isis::DatabaseFactory::getInstance().
void Isis::Database::makePersistant | ( | ) |
Makes this instance persistant.
Database persistancy in this context means the database remains in whatever state the user leaves it in, such as open, and ensures that the configuration remains available for other uses of the same connection.
This feature is useful if you have a long running application that will make prepeated attempts to access the database using the same configuration parameters. It saves overhead and provides a guaranteed state of access. It can and perhaps should be closed when not used in between long accesses. This will prevent timeouts from the database.
The intended usefulness of the persistant database state is so that at anytime in the life or processing point in the program, the database connection is available.
Note that this uses the DatabaseFactory class to retain its persistancy.
Definition at line 191 of file Database.cpp.
References _name, and Isis::DatabaseFactory::getInstance().
|
inline |
Return the name of this database as specifed upon creation.
Definition at line 90 of file Database.h.
References _name.
|
static |
Removes the named database from pool.
This static method is required in order to remove a previous used Database from the database pool. Database configurations hang around after they are used. To completely remove them from application space, you must call this method.
NOTE: The Database destructor only ensure the connection is closed. It does not complete remove them. Persistant databases have their connect state preserved from one Database construction/instantiation to the next. This method is the only way to completely remove a database from global application space connectivity.
WARNING: Do not attempt to remove an active Database! This will cause a spurious warning from Qt and render the database inoperative!
name | Name of database to remove (and destroy) |
Definition at line 254 of file Database.cpp.
References Isis::DatabaseFactory::getInstance().
void Isis::Database::setAsDefault | ( | ) |
Sets this database connection/profile as the default.
Calling this method sets this database instance/connection as the default connection. It is added to the list of persistant connections and can be retreived at will at any point in an application. This will be true even if this instance is released.
It uses the DatabaseFactory class to register it as the default. Note that there is only one default ever and it is designated by name. By definition it is also marked as a persistant connection.
Definition at line 226 of file Database.cpp.
References _name, and Isis::DatabaseFactory::getInstance().
|
private |
Generic exception tosser.
This method is used from within this class to construct and deploy an exception when an error occurs in some of the methods in this class.
message | Text of message to include in exception |
f | Name of method initiating the exception |
l | Line number the error occured |
Definition at line 550 of file Database.cpp.
References Isis::IException::Programmer.
Referenced by Database(), Database(), Database(), and init().
|
staticprivate |
Definition at line 115 of file Database.h.
|
private |
Needed due to peculiar issues with Database construction techniques.
Name of the connection
Definition at line 118 of file Database.h.
Referenced by Database(), Database(), Database(), Database(), isPersistant(), makePersistant(), Name(), setAsDefault(), and ~Database().