Main Page | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

sql Class Reference

class to manage all database interactions More...

#include <sql.h>

Collaboration diagram for sql:

Collaboration graph
[legend]
List of all members.

Public Member Functions

void setCursor ()
 Method to store the result of a query for later retrieval.
void restoreCursor ()
 Method to retrieve a previously stored query result.
void initConn (const QString &uName, const QString &dbName="guest", const QString &password="", const QString &connName="guestConn", const QString &host="localhost", const QString &connType="QPSQL")
int prepare (const QString &functionName, int paramCnt, const QString &connName="")
int prepare (const QString &command, const QString &connName="")
 Method used to prepare a SQL command.
void bind (int posn, QVariant value, int qID=-1)
bool query (int qID=-1)
bool first ()
 Method to get the first result from the current query.
bool next ()
 Method to get the next result from the current query.
bool last ()
 Method to get the last result from the current query.
QVariant get (int index)
QVariant getFirst (int index)
QVariant getNext (int index)
void dump ()
 Method to dump out some details about the current result set.
QString * useModuleConn ()

Static Public Member Functions

static sqlInstance ()
 Method to get the singleton instance.

Static Public Attributes

static QString * DefaultConnectionName = new QString("guestConn")
 The name of the default connection.

Detailed Description

class to manage all database interactions

This is a singleton class that will manage the connections with the database. It will support any number of databases by using the QDatabase class.
All interactions with the database should be done with the instance of this class. If there is an error then an exception will be thrown, this can then be managed in the appropriate way.
If a cursor needs to be set within the scanning of a database then this is supported, however it is up to the developer to use these responsibly. That is they must be set and then restored in pairs.

Definition at line 39 of file sql.h.


Member Function Documentation

void sql::bind int  posn,
QVariant  value,
int  qID = -1
 

Method used to bind values to a prepared query

Warning:
This method has a side effect of updating the activeQuery if qID is a valid index in queryList
Parameters:
posn An index to the argument to be bound to
value The value of the argument to be set
qID The index of the prepared query to use

Definition at line 110 of file sql.cpp.

Referenced by ModLecAlloc::bindPKey(), DataClass::bindPKey(), TimeSlot::bindValues(), Staff::bindValues(), Room::bindValues(), Programme::bindValues(), Module::bindValues(), ModLecAlloc::bindValues(), Institute::bindValues(), Faculty::bindValues(), Department::bindValues(), TimeSlot::remove(), Programme::uniqueAbbrev(), Institute::uniqueAbbrev(), Faculty::uniqueAbbrev(), Department::uniqueAbbrev(), Room::uniqueCode(), Programme::uniqueName(), Module::uniqueName(), Institute::uniqueName(), Faculty::uniqueName(), Department::uniqueName(), Module::uniqueNomenclature(), ModLecAlloc::updateClassSizeonDB(), ModLecAlloc::updateGroupsonDB(), ModLecAlloc::updateLabGroupsonDB(), and ModLecAlloc::updateLabHoursonDB().

00113 {
00114         if (qID >= 0 && qID < queryList.size())
00115         {
00116                 activeQuery = queryList.at(qID);
00117         }
00118         activeQuery->bindValue(posn, value);
00119 }

QVariant sql::get int  index  ) 
 

Method to get the value indicated by index from the current record

Parameters:
index The index of result column to use

Definition at line 158 of file sql.cpp.

Referenced by getFirst(), and getNext().

00159 {
00160         if (result == 0)
00161                 throw sqlE(QSqlDatabase::database(lastDBConnName));
00162         return result->value(index);
00163 }

QVariant sql::getFirst int  index  ) 
 

Method to position the record on the first record and get the value indicated by index

Parameters:
index The index of result column to use

Definition at line 165 of file sql.cpp.

References first(), and get().

Referenced by DataClass::insert(), ModLecAlloc::ModLecAlloc(), TimeSlot::remove(), DataClass::remove(), DataClass::unique(), DataClass::update(), ModLecAlloc::updateClassSizeonDB(), ModLecAlloc::updateGroupsonDB(), ModLecAlloc::updateLabGroupsonDB(), and ModLecAlloc::updateLabHoursonDB().

00166 {
00167         first();
00168         return get(index);
00169 }

QVariant sql::getNext int  index  ) 
 

Method to get the value from the current record and move to the next result

Parameters:
index The index of result column to use

Definition at line 171 of file sql.cpp.

References get(), and next().

00172 {
00173         next();
00174         return get(index);
00175 }

void sql::initConn const QString &  uName,
const QString &  dbName = "guest",
const QString &  password = "",
const QString &  connName = "guestConn",
const QString &  host = "localhost",
const QString &  connType = "QPSQL"
 

Method to create a connection to a database

Warning:
This method has a side effect for changing the value of lastDBConnName
Parameters:
uName the name of database user
dbName the name of the database
password the users password to access the database
connName the name that will be used for the connection
host the name of the server where the database is hosted
connType the type of database, so the appropriate driver can be selected

Definition at line 44 of file sql.cpp.

00051 {       
00052         QSqlDatabase dbConn = QSqlDatabase::addDatabase(connType,connName);
00053         
00054         dbConn.setDatabaseName(dbName);
00055         dbConn.setUserName(uName);
00056         dbConn.setPassword(password);
00057         dbConn.setHostName(host);
00058         
00059         if (!dbConn.open())
00060         {
00061                 throw sqlE(dbConn, "Failed to log on to the Database");
00062         }
00063         lastDBConnName = connName;
00064 }

int sql::prepare const QString &  functionName,
int  paramCnt,
const QString &  connName = ""
 

Method used to prepare a SQL function with the number of parameter given

Warning:
This method has a side effect of updating the lstDBConnName if connName is provided

This method has a side effect of updating the activeQuery

Parameters:
functionName the name of the SQL function that is being prepared
paramCnt the number of parameters the function requires
connName the name of the database connection to be used if this is blank then lastDBConnName will be used
Returns:
An index to the query stored in the queryList

Definition at line 66 of file sql.cpp.

Referenced by TimeSlot::PrepareSQL(), Staff::PrepareSQL(), Room::PrepareSQL(), Programme::PrepareSQL(), Module::PrepareSQL(), ModLecAlloc::PrepareSQL(), Institute::PrepareSQL(), Faculty::PrepareSQL(), and Department::PrepareSQL().

00069 {
00070         QString paramList = "?";
00071         for (int cnt = 1; cnt < paramCnt; cnt++)
00072         {
00073                 paramList += ", ?";
00074         }
00075         QString command = "SELECT " + functionName + "(" + paramList + ")";
00076         QString dbConn;
00077         if (connName=="")
00078                 dbConn = lastDBConnName;
00079         else
00080         {
00081                 dbConn = connName;
00082                 lastDBConnName = dbConn;
00083         }
00084         QSqlQuery * preparedStmt = new QSqlQuery (QSqlDatabase::database(dbConn));
00085         preparedStmt->prepare(command);
00086         queryList.append(preparedStmt);
00087         activeQuery = preparedStmt;
00088         return queryList.size()-1;
00089 }

bool sql::query int  qID = -1  ) 
 

Method to execute a query

Warning:
This method has a side effect of creating a new query and storing it in result. Any old query in result will be lost unless it it set as a cursor.
Parameters:
qID The index of the prepared query to use
See also:
setCursor and restoreCursor

Definition at line 121 of file sql.cpp.

Referenced by DataClass::insert(), ModLecAlloc::ModLecAlloc(), TimeSlot::remove(), DataClass::remove(), DataClass::unique(), DataClass::update(), ModLecAlloc::updateClassSizeonDB(), ModLecAlloc::updateGroupsonDB(), ModLecAlloc::updateLabGroupsonDB(), and ModLecAlloc::updateLabHoursonDB().

00122 {
00123         if (qID >= 0 && qID < queryList.size())
00124         {
00125                 activeQuery = queryList.at(qID);
00126         }
00127         result = new QSqlQuery(*activeQuery);
00128         bool success = result->exec();
00129         if  (success == false)
00130                 throw sqlE(QSqlDatabase::database(lastDBConnName), "Query Error: \"" + result->lastQuery() +"\"");
00131         return success;
00132 }


The documentation for this class was generated from the following files:
Generated on Thu Apr 6 16:27:21 2006 for time-table by  doxygen 1.4.4