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

sql.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2006 by Graeme Foster                                   *
00003  *   email    foster.graeme@gmail.com                                      *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 #include <QMessageBox>
00021 #include <iostream>
00022 
00023 #include "sql.h"
00024 
00025 // Initialisation of static member variables
00026 QString * sql::DefaultConnectionName = new QString("guestConn");
00027 sql* sql::_instance = 0;
00028 
00029 sql * sql::Instance()
00030 {
00031         if (_instance == 0)
00032         {
00033                 _instance = new sql;
00034         }
00035         return _instance;
00036 }
00037 
00038 sql::sql ()
00039 {
00040         lastDBConnName = QString();
00041         result = 0;
00042 }
00043 
00044 void sql::initConn(const QString & uName
00045                                                 ,const QString & dbName
00046                                                 ,const QString & password
00047                                                 ,const QString & connName
00048                                                 ,const QString & host
00049                                                 ,const QString & connType
00050                                                 )
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 }
00065 
00066 int sql::prepare(const QString & functionName
00067                 ,int paramCnt
00068                 ,const QString & connName)
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 }
00090 
00091 int sql::prepare(const QString & command
00092                 ,const QString & connName)
00093 {
00094         QString dbConn;
00095         if (connName=="")
00096                 dbConn = lastDBConnName;
00097         else
00098         {
00099                 dbConn = connName;
00100                 lastDBConnName = dbConn;
00101         }
00102         QSqlQuery * preparedStmt = new QSqlQuery (QSqlDatabase::database(dbConn));
00103         preparedStmt->prepare(command);
00104         queryList.append(preparedStmt);
00105         activeQuery = preparedStmt;
00106         return queryList.size()-1;
00107 }
00108 
00109 
00110 void sql::bind(int posn
00111               ,QVariant value
00112               ,int qID)
00113 {
00114         if (qID >= 0 && qID < queryList.size())
00115         {
00116                 activeQuery = queryList.at(qID);
00117         }
00118         activeQuery->bindValue(posn, value);
00119 }
00120 
00121 bool sql::query(int qID)
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 }
00133 
00134 bool sql::first()
00135 {
00136         if (result == 0)
00137                 throw sqlE(QSqlDatabase::database(lastDBConnName));
00138         bool success = result->first();
00139         return success;
00140 }
00141 
00142 bool sql::next ()
00143 {
00144         if (result == 0)
00145                 throw sqlE(QSqlDatabase::database(lastDBConnName));
00146         bool success = result->next();
00147         return success;
00148 }
00149 
00150 bool sql::last ()
00151 {
00152         if (result == 0)
00153                 throw sqlE(QSqlDatabase::database(lastDBConnName));
00154         bool success = result->last();
00155         return success;
00156 }
00157 
00158 QVariant sql::get(int index)
00159 {
00160         if (result == 0)
00161                 throw sqlE(QSqlDatabase::database(lastDBConnName));
00162         return result->value(index);
00163 }
00164 
00165 QVariant sql::getFirst(int index)
00166 {
00167         first();
00168         return get(index);
00169 }
00170 
00171 QVariant sql::getNext(int index)
00172 {
00173         next();
00174         return get(index);
00175 }
00176 
00177 void sql::dump()
00178 {
00179         std::cout << "The number of records is:" << result->size() << std::endl;
00180 }
00181 
00182 // SQL Exception member functions
00183 sqlE::sqlE(const QSqlDatabase & dbConn
00184                          ,const QString & type)
00185 {
00186         ErrorType = type;
00187         QSqlError errorObj = dbConn.lastError();
00188         ErrorMessage = errorObj.text() + "\n" + type + "\n" + errorObj.driverText() + "\n" + errorObj.databaseText ();
00189 }

Generated on Thu Apr 6 16:27:17 2006 for time-table by  doxygen 1.4.4