19#include <QCoreApplication>
20#include <QSqlDatabase>
22#include "DatabaseFactory.h"
24#include "Preference.h"
58 _defDatabase(
""), _dbList() {
64 QCoreApplication *cApp = QCoreApplication::instance();
66 static char **argv = 0;
70 argv[0] =
new char[1024];
71 strcpy(argv[0],
"DatabaseFactory");
76 new QCoreApplication(argc, argv);
155 if(dbconf.fileExists()) {
159 const DbProfile &topProf = acp.getProfile();
163 for(
int i = 0 ; i < acp.profileCount() ; i++) {
164 const DbProfile &newProf = acp.getProfile(i);
169 if(acp.exists(
"DefaultProfile")) {
208 std::vector<QString> plist;
236 QString profName(name);
262 std::vector<QString> dblist;
263 for(
int i = 0 ; i < drivers.size() ; i++) {
264 dblist.push_back(drivers.key(i).ToQt());
281 if(!drivers.exists(dbname)) {
284 return (QSqlDatabase::isDriverAvailable(drivers.get(dbname)));
299 QString name(dbname);
303 return (dbdrivers.exists(name));
316 QString name(dbname);
320 return (QSqlDatabase::contains(name));
359 const QString &dbname) {
364 QString mess =
"Driver [" + driver +
"] for database [" + dbname
365 +
"] does not exist";
371 return (QSqlDatabase::addDatabase(drivers.get(driver), dbname));
393 QString mess =
"Database [" + dbname +
"] does not exist";
437 QSqlDatabase::removeDatabase(name);
487 Preference &userPref = Preference::Preferences();
488 if(userPref.hasGroup(
"Database")) {
489 PvlGroup &dbgroup = userPref.findGroup(
"Database");
490 if(dbgroup.hasKeyword(
"AccessConfig")) {
494 if(dbgroup.hasKeyword(
"DefaultProfile"))
520 if(drivers) dblist += QSqlDatabase::drivers();
521 if(connections) dblist += QSqlDatabase::connectionNames();
523 for(
int i = 0 ; i < dblist.size() ; i++) {
524 QString dbname(dblist.at(i));
525 dbDrivers.add(dbname, dbname);
531 if(dbDrivers.exists(
"QPSQL")) {
532 dbDrivers.add(
"PostgreSQL",
"QPSQL");
536 if(dbDrivers.exists(
"QMYSQL")) {
537 dbDrivers.add(
"MySQL",
"QMYSQL");
541 if(dbDrivers.exists(
"QOCI")) {
542 dbDrivers.add(
"Oracle",
"QOCI");
546 if(dbDrivers.exists(
"QSQLITE")) {
547 dbDrivers.add(
"SQLite",
"QSQLITE");
565 QSqlDatabase::drivers();
580 QStringList dblist = QSqlDatabase::connectionNames();
581 for(
int i = 0 ; i < dblist.size() ; i++) {
582 QSqlDatabase::removeDatabase(dblist[i]);
const K & key(int nth) const
Returns the nth key in the collection.
int size() const
Returns the size of the collection.
int remove(const K &key)
Removes and entry from the list.
T & get(const K &key)
Returns the value associated with the name provided.
bool exists(const K &key) const
Checks the existance of a particular key in the list.
void add(const K &key, const T &value)
Adds the element to the list.
Create database interfaces using access profiles or generic drivers.
QSqlDatabase create(const QString &driver, const QString &dbname)
Create a database using the named driver.
static DatabaseFactory * getInstance()
Returns and instance of this DatabaseFactory singleton.
void destroy(const QString &dbname)
Removes the database from the connection pool and destroys it.
void remove(const QString &dbname)
Removes the database from the connection pool.
DatabaseFactory()
Constructor establishing the startup state of this singleton.
DbProfile getProfile(const QString &name="") const
Get the specified database access profile.
static DatabaseFactory * _factory
Pointer to self (singleton)
void add(const QSqlDatabase &db, const QString &name, bool setAsDefault=false)
Adds the database to the connection pool making it persistant.
bool isDriverAvailable(const QString &driver) const
Check for the existance of a specific database driver.
bool isPersistant(const QString &name) const
Checks if the database resource is persistant.
std::vector< QString > available() const
Determine what database drivers are available.
bool addAccessProfile(const QString &profileFile)
Establishes an access profile for subsequent database connections.
void selfDestruct()
Destroy all elements associated with this object.
Drivers getResourceList(bool drivers, bool connections) const
Get a list of available database drivers and connections.
static void DieAtExit()
Exit termination routine.
bool isAvailable(const QString &dbname="") const
Check for availablity of a database connection resource.
void addProfile(const DbProfile &profile)
Adds a database access profile to the list of profiles.
Databases _dbList
Maintains active databases.
void init()
Initializes this object upon instantiation.
void initPreferences()
Initializes user database preferences.
std::vector< QString > getProfileList() const
Return list of names of currently available profiles.
bool isConnected(const QString &dbname) const
Determines if the database resource is connected.
~DatabaseFactory()
Destructor implements self-destruction of this object.
void loadDrivers()
Load any drivers explicity.
QString _defDatabase
Name of default database.
Profiles _profiles
Maintain list of profiles.
CollectorMap< IString, QString, NoCaseStringCompare > Drivers
Define list of drivers and/or databases
QString _defProfName
Default profile name.
DbAccess manages programatic access to a database through profiles.
A DbProfile is a container for access parameters to a database.
QString Name() const
Returns the name of this property.
File name manipulation and expansion.
@ Unknown
A type of error that cannot be classified as any of the other error types.
bool Equal(const std::string &str) const
Compare a string to the object IString.
Reads user preferences from a data file.
Contains multiple PvlContainers.
This is free and unencumbered software released into the public domain.
Namespace for the standard library.