00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <QMessageBox>
00021 #include <iostream>
00022
00023 #include "sql.h"
00024
00025
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
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 }