Isis 3 Programmer Reference
Isis::DbAccess Class Reference

DbAccess manages programatic access to a database through profiles. More...

#include <DbAccess.h>

Inheritance diagram for Isis::DbAccess:
Inheritance graph
Collaboration diagram for Isis::DbAccess:
Collaboration graph

Public Member Functions

 DbAccess (const QString &dbaccFile, const QString &defProfileName="")
 Construct with a given database access configuration file. More...
 
 DbAccess (PvlObject &pvl, const QString &defProfileName="")
 Constructor that accepts a Database Pvl Object. More...
 
virtual ~DbAccess ()
 Destructor ensures everything is cleaned up properly. More...
 
int profileCount () const
 Reports the number of user profiles to access this database. More...
 
bool profileExists (const QString &profile) const
 Checks existance of a database user profile. More...
 
const DbProfile getProfile (const QString &name="") const
 Retrieves the specified access profile. More...
 
const DbProfile getProfile (int nth) const
 Returns the nth specified DbProfile in the list. More...
 
void addProfile (const DbProfile &profile)
 Adds a profile to the database profile. More...
 
void load (const QString &filename)
 Loads a Database access configuration file. More...
 
void load (PvlObject &pvl)
 Load a database access profile configuration from a PvlObject. More...
 
QString getDefaultProfileName () const
 Determine the name of the default profile. More...
 
bool isValid () const
 Reports if this is a valid profile. More...
 
int size () const
 Reports the number of keywords in this user profile. More...
 
void setName (const QString &name)
 Set the name of this profile. More...
 
QString Name () const
 Returns the name of this property. More...
 
bool exists (const QString &key) const
 Checks for the existance of a keyword. More...
 
void add (const QString &key, const QString &value="")
 Adds a keyword and value pair to the profile. More...
 
void replace (const QString &key, const QString &value="")
 Adds a keyword and value pair to the profile. More...
 
void remove (const QString &key)
 Removes a keyword from the profile. More...
 
int count (const QString &key) const
 Report number of values in keyword. More...
 
QString key (int nth) const
 Returns the nth key in the profile. More...
 
QString value (const QString &key, int nth=0) const
 Returns the specified value for the given keyword. More...
 
QString operator() (const QString &key, int nth=0) const
 Returns the specified value for the given keyword. More...
 

Protected Types

typedef CollectorMap< IString, PvlKeyword, NoCaseStringCompareKeyList
 

Protected Member Functions

void loadkeys (PvlContainer &pvl)
 Loads DbProfile keys from the given Pvl construct. More...
 
const KeyListgetKeyList () const
 Returns a reference to the key list. More...
 

Private Types

typedef CollectorMap< IString, DbProfile, NoCaseStringCompareProfileList
 Define the container for the DbAccess key word list. More...
 

Private Attributes

QString _defProfileName
 Name of default profile. More...
 
ProfileList _profiles
 List of profiles. More...
 

Detailed Description

DbAccess manages programatic access to a database through profiles.

This class reads a Pvl formatted file and constructs access profiles on the fly from the contents. It is intended to specify any and all information sufficient to establish a database connection in software applications.

The input file to this class is typically created with an editor. It can contain any keyword = value combination. It must contain a Database object and may optionally contain Profile groups. The Database object can contain kewords such as User, Host, DbName, and so forth. Here is an example of what the file, named upc.conf, of this type would look like:

Object = Database
Name = UPC
Dbname = upc
Type = PostgreSQL
Host = "upcdb0.wr.usgs.gov"
Port = 3309
Description = "UPC provides GIS-capable image searches"
AlternateHosts = "upcdb1.wr.usgs.gov"
EndObject

The code used to load and access this profile is:

DbAccess upc("upc.conf");
DbProfile default = upc.getProfile();

Additionally, you can add specific profiles that alter some or all of the parameters contained in the Object section of the file. Simply add one or more Profile groups that grant or specify different access profiles for the given database. For example the example below names the Profile "upcread" and adds an additional user and password to the profile.

Object = Database
Name = UPC
Dbname = upc
Type = PostgreSQL
Host = "upcdb0.wr.usgs.gov"
Port = 3309
Description = "UPC provides GIS-capable image searches"
AlternateHosts = "upcdb1.wr.usgs.gov"
DefaultProfile = "upcread"
Group = Profile
Name = "upcread"
User = "upcread"
Password = "public"
EndGroup
EndObject

To access this profile, use:

DbProfile upcread = upc.getProfile("upcread");

It will look for the Name keyword as the specifed named profile. What actually happens when the above code is invoked is all the keywords contained in the Database object, such as Dbname, Type, as well as Name are copied to a new dynamic profile named "upcread". Then any keywords found in the actual Profile group with Name = "upcread" are copied to the newly created dynamic one replacing any existing keywords with the ones found in the requested Profile. This ensures precedence is given to requested profiles and common parameters in the Database object are retained.

Author
2006-07-01 Kris Becker
History:
2007-06-05 Brendan George - Modified to work with QString/StringTool merge

Definition at line 120 of file DbAccess.h.

Member Typedef Documentation

◆ ProfileList

Define the container for the DbAccess key word list.

Definition at line 124 of file DbAccess.h.

Constructor & Destructor Documentation

◆ DbAccess() [1/2]

Isis::DbAccess::DbAccess ( const QString &  dbaccFile,
const QString &  defProfileName = "" 
)

Construct with a given database access configuration file.

This constructor accepts the name of a Pvl formatted file that must contain a object named Database. It loads keywords in the order they occur in the object. Keywords should be unique - if not, previous keywords are silently replaced by subsequent occuring keywords.

Then all groups named Profile are loaded and established as additional, distinct access profiles. They all should have unique names. Subsequent profiles with the same name are replaced.

The caller may additionally provide the name of the default profile to use when none is given. If one is not provided, then should a keyword called DefaultProfile is searched for and the value of this keyword serves as the default profile. See the getProfile() method for details on how this situation is resolved.

Parameters
dbaccFileName of a Pvl formatted file containing the access specifications for a database
defProfileNameOptional name of the default access profile

Definition at line 60 of file DbAccess.cpp.

References load().

◆ DbAccess() [2/2]

Isis::DbAccess::DbAccess ( PvlObject pvl,
const QString &  defProfileName = "" 
)

Constructor that accepts a Database Pvl Object.

The functionality of this constructor is exactly the same as file constructor except with a PvObject named "Database" as an argument.

Parameters
pvlA Database PvlObject containing access information
defProfileNameOptional name of the default profile

Definition at line 76 of file DbAccess.cpp.

References load().

◆ ~DbAccess()

virtual Isis::DbAccess::~DbAccess ( )
inlinevirtual

Destructor ensures everything is cleaned up properly.

Definition at line 134 of file DbAccess.h.

Member Function Documentation

◆ add()

void Isis::DbProfile::add ( const QString &  key,
const QString &  value = "" 
)
inherited

Adds a keyword and value pair to the profile.

This method adds a keyword and value pair to the profile if it doesn't exist. If the keyword already exists,it appends the value to the existing keyword.

Parameters
keyKeyword to add or ammend
valueValue to add to the keyword

Definition at line 96 of file DbProfile.cpp.

References Isis::DbProfile::_keys, Isis::CollectorMap< K, T, ComparePolicy, RemovalPolicy, CopyPolicy >::add(), Isis::PvlKeyword::addValue(), Isis::CollectorMap< K, T, ComparePolicy, RemovalPolicy, CopyPolicy >::exists(), Isis::CollectorMap< K, T, ComparePolicy, RemovalPolicy, CopyPolicy >::get(), Isis::DbProfile::key(), and Isis::DbProfile::value().

◆ addProfile()

void Isis::DbAccess::addProfile ( const DbProfile profile)
inline

Adds a profile to the database profile.

Inheritors may add profiles to the user profile list. Note that duplicate profiles are not allowed, therefore existing profiles with the same name is replaced.

Parameters
profileProfile to add

Definition at line 169 of file DbAccess.h.

References _profiles, Isis::CollectorMap< K, T, ComparePolicy, RemovalPolicy, CopyPolicy >::add(), and Isis::DbProfile::Name().

◆ count()

int Isis::DbProfile::count ( const QString &  key) const
inherited

Report number of values in keyword.

This method will return the number of values in the specified keyword. If the keyword does not exist, 0 is returned.

Parameters
keyName of key to get value count for
Returns
int Number values in key, or 0 if the key does not exist

Definition at line 141 of file DbProfile.cpp.

References Isis::DbProfile::_keys, Isis::CollectorMap< K, T, ComparePolicy, RemovalPolicy, CopyPolicy >::exists(), Isis::CollectorMap< K, T, ComparePolicy, RemovalPolicy, CopyPolicy >::get(), Isis::DbProfile::key(), and Isis::PvlKeyword::size().

Referenced by Isis::PhotometricFunction::ConfKey(), Isis::ConfKey(), and Isis::Gruen::ConfKey().

◆ exists()

bool Isis::DbProfile::exists ( const QString &  key) const
inlineinherited

◆ getDefaultProfileName()

QString Isis::DbAccess::getDefaultProfileName ( ) const

Determine the name of the default profile.

This method is called to determine the real name of the default profile as predetermined at load time. This determination is made either through the default specified in the configuration Database object, the DefaultProfile keyword, or provided by the application progirammer in the constructor.

Returns
QString Name of default profile it it can be determined otherwise an empty string is returned.

Definition at line 203 of file DbAccess.cpp.

References _defProfileName, Isis::DbProfile::exists(), and Isis::DbProfile::value().

Referenced by getProfile().

◆ getKeyList()

const KeyList& Isis::DbProfile::getKeyList ( ) const
inlineprotectedinherited

Returns a reference to the key list.

Direct access to the keyword container allows class developers some additional flexibility whilst maintaining integrity through the public interface.

Returns
const KeyList& Reference to keyword list

Definition at line 173 of file DbProfile.h.

References Isis::DbProfile::_keys.

◆ getProfile() [1/2]

const DbProfile Isis::DbAccess::getProfile ( const QString &  name = "") const

Retrieves the specified access profile.

This method retrieves the named profile. If no name is provided, the default profile is returned.

There are two ways to specify the default. The first source of a named default comes from within the configuration file. A keyword specified in the Database object section named DefaultProfile can specify a named profile, the value of the Name keyword in a Profile group. The second source comes from the application programmer. In the constructor call to this object, the application programmer can provide a named profile as the default, which could ultimately come from the user (interface).

If no default is specified, then only the keywords contained in the Database object section of the configuration file is returned when requesting an unnamed profile.

Parameters
nameOptional name of the profile to return
Returns
const DbProfile The specified profile. One should test the validatity of the profile returned as this is the only indication of success.

Definition at line 106 of file DbAccess.cpp.

References _profiles, Isis::CollectorMap< K, T, ComparePolicy, RemovalPolicy, CopyPolicy >::exists(), Isis::CollectorMap< K, T, ComparePolicy, RemovalPolicy, CopyPolicy >::get(), and getDefaultProfileName().

Referenced by Isis::DatabaseFactory::addAccessProfile(), and Isis::ZeroReverse::init().

◆ getProfile() [2/2]

const DbProfile Isis::DbAccess::getProfile ( int  nth) const

Returns the nth specified DbProfile in the list.

This method allows user to iterate through the list of DbProfiles in this access scheme. If the caller provides an index that exceeds the number contained, an exception is thrown. Use profileCount() to determine the number of profiles.

Parameters
nthZero-based index of profile to return
Returns
const DbProfile The requested nth profile

Definition at line 140 of file DbAccess.cpp.

References _profiles, Isis::CollectorMap< K, T, ComparePolicy, RemovalPolicy, CopyPolicy >::getNth(), and Isis::DbProfile::Name().

◆ isValid()

bool Isis::DbProfile::isValid ( ) const
inlineinherited

Reports if this is a valid profile.

A valid profile is simply defined to contain keys. If there are no keys defined for the profile, it is deemed invalid.

Returns
bool True if the profile is valid, therefore containing keys

Definition at line 91 of file DbProfile.h.

References Isis::DbProfile::size().

Referenced by Isis::Database::init().

◆ key()

QString Isis::DbProfile::key ( int  nth) const
inlineinherited

Returns the nth key in the profile.

This method returns the name of the nth keyword in the profile so one can iterate through all existing keys. Note that database passwords could be vulnerable to exposure via this method.

Keywords in the profile are sorted in alphabetical order and not in the order in which they are read.

Parameters
nthSpecifies the nth key in the profile
Returns
QString Name of nth keyword in the profile.
Exceptions
Out-of-rangeexception if the nth keyword does not exist

Definition at line 155 of file DbProfile.h.

References Isis::DbProfile::_keys, Isis::CollectorMap< K, T, ComparePolicy, RemovalPolicy, CopyPolicy >::key(), and Isis::IString::ToQt().

Referenced by Isis::DbProfile::add(), Isis::DbProfile::count(), Isis::DbProfile::exists(), Isis::DbProfile::loadkeys(), Isis::DbProfile::operator()(), Isis::DbProfile::remove(), Isis::DbProfile::replace(), and Isis::DbProfile::value().

◆ load() [1/2]

void Isis::DbAccess::load ( const QString &  filename)

Loads a Database access configuration file.

Given the name of a file, it will open the file using Isis Pvl classes. See the load(pvl) class for additonal information what takes place in this method.

Note the file may use environment variables.

Parameters
filenameName of Pvl file to open.

Definition at line 156 of file DbAccess.cpp.

References Isis::PvlObject::findObject().

Referenced by DbAccess().

◆ load() [2/2]

void Isis::DbAccess::load ( PvlObject pvl)

Load a database access profile configuration from a PvlObject.

This method loads all keywords found in the Object section of the PvlObject and then searches for each Group named Profile. Profile groups contain augmentations to the object keywords to add to or replace object level access specifications. Each profile group must contain a Name keyword to uniquely identify the (group) access parameters.

Profiles are loaded and stored in this object for subsequent access.

Parameters
pvlA PvlObject that contains keywords and option Profile groups.

Definition at line 175 of file DbAccess.cpp.

References _profiles, Isis::CollectorMap< K, T, ComparePolicy, RemovalPolicy, CopyPolicy >::add(), Isis::PvlObject::beginGroup(), Isis::PvlObject::endGroup(), Isis::PvlObject::findGroup(), Isis::DbProfile::loadkeys(), and Isis::DbProfile::Name().

◆ loadkeys()

void Isis::DbProfile::loadkeys ( PvlContainer pvl)
protectedinherited

Loads DbProfile keys from the given Pvl construct.

This method iterates through all keywords in the Pvl container and loads them into this property.

Parameters
pvlContainer of keywords that will be loaded

Definition at line 198 of file DbProfile.cpp.

References Isis::DbProfile::_keys, Isis::CollectorMap< K, T, ComparePolicy, RemovalPolicy, CopyPolicy >::add(), Isis::PvlContainer::begin(), Isis::PvlContainer::end(), and Isis::DbProfile::key().

Referenced by Isis::DbProfile::DbProfile(), and load().

◆ Name()

QString Isis::DbProfile::Name ( ) const
inlineinherited

◆ operator()()

QString Isis::DbProfile::operator() ( const QString &  key,
int  nth = 0 
) const
inherited

Returns the specified value for the given keyword.

This method returns a value from the specified keyword. If the keyword or the specified value does not exist, an exception is thrown.

Parameters
keyName of keyword to return value for.
nthSpecifies the nth value in the keyword
Returns
QString The requested value in the keyword

Definition at line 186 of file DbProfile.cpp.

References Isis::DbProfile::key(), and Isis::DbProfile::value().

◆ profileCount()

int Isis::DbAccess::profileCount ( ) const
inline

Reports the number of user profiles to access this database.

Returns
int Number access profiles

Definition at line 141 of file DbAccess.h.

References _profiles, and Isis::CollectorMap< K, T, ComparePolicy, RemovalPolicy, CopyPolicy >::size().

Referenced by Isis::DatabaseFactory::addAccessProfile().

◆ profileExists()

bool Isis::DbAccess::profileExists ( const QString &  profile) const
inline

Checks existance of a database user profile.

Parameters
profileName of profile to check for existance
Returns
bool True if the profile exists, false otherwise

Definition at line 152 of file DbAccess.h.

References _profiles, and Isis::CollectorMap< K, T, ComparePolicy, RemovalPolicy, CopyPolicy >::exists().

Referenced by Isis::ZeroReverse::init().

◆ remove()

void Isis::DbProfile::remove ( const QString &  key)
inherited

Removes a keyword from the profile.

Parameters
keyKeyword to remove

Definition at line 127 of file DbProfile.cpp.

References Isis::DbProfile::_keys, Isis::DbProfile::key(), and Isis::CollectorMap< K, T, ComparePolicy, RemovalPolicy, CopyPolicy >::remove().

◆ replace()

void Isis::DbProfile::replace ( const QString &  key,
const QString &  value = "" 
)
inherited

Adds a keyword and value pair to the profile.

This method adds a keyword and value pair to the profile if it doesn't exist.

If the keyword already exists, it is deleted and replaced with this new keyword and value.

Parameters
keyKeyword to replace
valueValue to add to the keyword

Definition at line 117 of file DbProfile.cpp.

References Isis::DbProfile::_keys, Isis::CollectorMap< K, T, ComparePolicy, RemovalPolicy, CopyPolicy >::add(), Isis::DbProfile::key(), and Isis::DbProfile::value().

◆ setName()

void Isis::DbProfile::setName ( const QString &  name)
inlineinherited

Set the name of this profile.

Parameters
nameQString used to set the name of this profile

Definition at line 109 of file DbProfile.h.

References Isis::DbProfile::_name.

Referenced by Isis::Gruen::init().

◆ size()

int Isis::DbProfile::size ( ) const
inlineinherited

Reports the number of keywords in this user profile.

Returns
int Number keywords found in profile

Definition at line 100 of file DbProfile.h.

References Isis::DbProfile::_keys, and Isis::CollectorMap< K, T, ComparePolicy, RemovalPolicy, CopyPolicy >::size().

Referenced by Isis::DbProfile::isValid().

◆ value()

QString Isis::DbProfile::value ( const QString &  key,
int  nth = 0 
) const
inherited

Returns the specified value for the given keyword.

This method returns a value from the specified keyword. If the keyword or the specified value does not exist, an exception is thrown.

Parameters
keyName of keyword to return value for.
nthSpecifies the nth value in the keyword
Returns
QString The requested value in the keyword

Definition at line 160 of file DbProfile.cpp.

References _FILEINFO_, Isis::DbProfile::_keys, Isis::CollectorMap< K, T, ComparePolicy, RemovalPolicy, CopyPolicy >::get(), Isis::DbProfile::key(), and Isis::IException::Programmer.

Referenced by Isis::DbProfile::add(), Isis::DatabaseFactory::addAccessProfile(), Isis::PhotometricFunction::ConfKey(), Isis::ConfKey(), Isis::Gruen::ConfKey(), Isis::DbProfile::DbProfile(), getDefaultProfileName(), Isis::DbProfile::operator()(), and Isis::DbProfile::replace().

Member Data Documentation

◆ _defProfileName

QString Isis::DbAccess::_defProfileName
private

Name of default profile.

Definition at line 179 of file DbAccess.h.

Referenced by getDefaultProfileName().

◆ _profiles

ProfileList Isis::DbAccess::_profiles
private

List of profiles.

Definition at line 180 of file DbAccess.h.

Referenced by addProfile(), getProfile(), load(), profileCount(), and profileExists().


The documentation for this class was generated from the following files: