Isis 3.0 Object Programmers' Reference |
Home |
#include <Database.h>
Inheritance diagram for Isis::Database:
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.
// Use the UPC profile Database upc("upc")
Connections can be made to specific databases using named drivers also provided from the DatabaseFactory class.
// Set up one for UPC directly Database upc1("upcDirect", "PostgreSQL"); // "QPSQL" also works upc1.setHostName("upcdb0.wr.usgs.gov"); upc1.setDatabaseName("upc"); upc1.setUserName("upcread"); upc1.setPort(3309); upc1.open();
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.
For internal use only.
Definition at line 84 of file 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 std::string &connName, const std::string &driverType) | |
Create database connection specified by name and driver type. | |
Database (const std::string &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. | |
std::string | 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 std::string &name) const |
Clones this database into another giving it another name. | |
std::vector< std::string > | getTables () const |
Returns a vector string containing all the tables in the database. | |
std::vector< std::string > | getViews () const |
Returns a vector string containing all views within the database. | |
std::vector< std::string > | getSystemTables () const |
Returns vector strings of all available system tables in the database. | |
Static Public Member Functions | |
static void | remove (const std::string &name) |
Removes the named database from pool. | |
static bool | addAccessConfig (const std::string &confFile) |
Adds a user specifed access configuration file to system. | |
static DbProfile | getProfile (const std::string &name) |
Retrieves the named database access profile. | |
Protected Member Functions | |
Database (const QSqlDatabase &other, const std::string &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 std::string &name="", const std::string &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 std::string &message, const char *f, int l) const throw (iException &) |
Generic exception tosser. | |
Private Attributes | |
std::string | _name |
Needed due to peculiar issues with Database construction techniques Name of the connection. | |
Static Private Attributes | |
static std::string | _actualConnectionName = "" |
|
Access status for database creation.
Definition at line 87 of file Database.h. |
|
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 46 of file Database.cpp. Referenced by clone(). |
|
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.
Definition at line 66 of file Database.cpp. References _actualConnectionName, _FILEINFO_, _name, Connect, and tossDbError(). |
|
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". Database db("sparky", "postgresql"); 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".
Definition at line 117 of file Database.cpp. References _actualConnectionName, and _name. |
|
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.
Definition at line 88 of file Database.cpp. References _actualConnectionName, _FILEINFO_, _name, Connect, and tossDbError(). |
|
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.
Definition at line 137 of file Database.cpp. References _actualConnectionName, _FILEINFO_, _name, Connect, and tossDbError(). |
|
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 173 of file Database.cpp. References _name, Isis::DatabaseFactory::getInstance(), Isis::DatabaseFactory::isPersistant(), and Isis::DatabaseFactory::remove(). |
|
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.
Definition at line 159 of file Database.cpp. |
|
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.
Definition at line 286 of file Database.cpp. References Isis::DatabaseFactory::addAccessProfile(), and Isis::DatabaseFactory::getInstance(). |
|
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.
Definition at line 512 of file Database.cpp. References Database(). |
|
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.
Definition at line 468 of file Database.cpp. References Isis::DbProfile::exists(), and Isis::iString::ToQt(). |
|
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.
DbProfile default = Database::getProfile(); if (!default.isValid()) { cerr << "No default access profile established!" << endl; } // Open the database (after optional modification) Database mydb(default);
Definition at line 323 of file Database.cpp. References Isis::DatabaseFactory::getInstance(), and Isis::DatabaseFactory::getProfile(). |
|
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 549 of file Database.cpp. References Isis::iString::ToStd(). |
|
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 524 of file Database.cpp. References Isis::iString::ToStd(). |
|
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 537 of file Database.cpp. References Isis::iString::ToStd(). |
|
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.
Definition at line 354 of file Database.cpp. References _actualConnectionName, Isis::DatabaseFactory::create(), DoNotConnect, Isis::DatabaseFactory::getDefault(), Isis::DatabaseFactory::getInstance(), Isis::DatabaseFactory::getProfile(), init(), Isis::DatabaseFactory::isAvailable(), and Isis::DbProfile::isValid(). |
|
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.
Definition at line 414 of file Database.cpp. References _FILEINFO_, Isis::DbProfile::isValid(), Isis::iException::Message(), and Isis::DbProfile::Name(). Referenced by init(). |
|
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 218 of file Database.cpp. References _name, Isis::DatabaseFactory::getInstance(), and Isis::DatabaseFactory::isPersistant(). |
|
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 202 of file Database.cpp. References _name, Isis::DatabaseFactory::add(), Isis::DatabaseFactory::getInstance(), and Isis::DatabaseFactory::isPersistant(). |
|
Return the name of this database as specifed upon creation.
Definition at line 104 of file Database.h. References _name. |
|
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!
Definition at line 265 of file Database.cpp. References Isis::DatabaseFactory::destroy(), and Isis::DatabaseFactory::getInstance(). |
|
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 237 of file Database.cpp. References _name, Isis::DatabaseFactory::add(), Isis::DatabaseFactory::getInstance(), and Isis::DatabaseFactory::isPersistant(). |
|
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.
Definition at line 563 of file Database.cpp. References Isis::iException::Message(), and Isis::iString::ToStd(). Referenced by Database(). |
|
Needed due to peculiar issues with Database construction techniques Name of the connection.
Definition at line 130 of file Database.h. Referenced by Database(), isPersistant(), makePersistant(), Name(), setAsDefault(), and ~Database(). |