SpikeStream Library
0.2
|
00001 #include "AbstractDao.h" 00002 #include "SpikeStreamDBException.h" 00003 using namespace spikestream; 00004 00005 //Qt includes 00006 #include <QDebug> 00007 00008 //Declare static variables 00009 unsigned int AbstractDao::dbCounter = 0; 00010 00011 00013 AbstractDao::AbstractDao(const DBInfo& dbInfo){ 00014 //Store the information about the database 00015 this->dbInfo = dbInfo; 00016 00017 //Default name of database - this changes when it is connected. 00018 dbName = ""; 00019 00020 //Initialize dbThread to NULL 00021 dbThread = NULL; 00022 00023 //Connect to the database 00024 connectToDatabase(); 00025 } 00026 00027 00029 AbstractDao::AbstractDao(){ 00030 //Default name of database - this changes when it is connected. 00031 dbName = ""; 00032 } 00033 00034 00036 AbstractDao::~AbstractDao(){ 00037 closeDatabaseConnection(); 00038 } 00039 00040 00041 /*----------------------------------------------------------*/ 00042 /*----- PUBLIC METHODS -----*/ 00043 /*----------------------------------------------------------*/ 00044 00046 DBInfo AbstractDao::getDBInfo(){ 00047 return dbInfo; 00048 } 00049 00050 00053 QThread* AbstractDao::getThread(){ 00054 return dbThread; 00055 } 00056 00057 00058 /*----------------------------------------------------------*/ 00059 /*----- PROTECTED METHODS -----*/ 00060 /*----------------------------------------------------------*/ 00061 00065 void AbstractDao::checkDatabase(){ 00066 if(!isConnected()) 00067 connectToDatabase(); 00068 00069 /* Check that we are accessing datbase connection from the thread it was created on. 00070 The Qt database class only works within a single thread.*/ 00071 if(QThread::currentThread() != dbThread){ 00072 throw SpikeStreamDBException("Attempting to access database from different thread. This is not supported in Qt4"); 00073 } 00074 } 00075 00076 00078 bool AbstractDao::isConnected(){ 00079 QSqlDatabase db = QSqlDatabase::database(dbName); 00080 if(db.isValid()) 00081 if(db.isOpen()) 00082 return true; 00083 return false; 00084 } 00085 00086 00088 void AbstractDao::closeDatabaseConnection(){ 00089 QSqlDatabase::removeDatabase(dbName); 00090 } 00091 00092 00094 void AbstractDao::connectToDatabase(){ 00095 //Record the thread that this database was created in - this class cannot be used across multiple threads 00096 dbThread = QThread::currentThread(); 00097 00098 //Get a unique name that can be used to access the database 00099 dbName = AbstractDao::getUniqueDBName(); 00100 00101 //Create database unique to this thread. No need to store reference because it is held statically 00102 QSqlDatabase database = QSqlDatabase::addDatabase("QMYSQL", dbName); 00103 database.setHostName(this->dbInfo.getHost()); 00104 database.setDatabaseName(this->dbInfo.getDatabase()); 00105 database.setUserName(this->dbInfo.getUser()); 00106 database.setPassword(this->dbInfo.getPassword()); 00107 00108 //Set connection options to prevent it from timing out 00109 database.setConnectOptions("MYSQL_OPT_RECONNECT=1"); 00110 //database.setConnectOptions("CLIENT_SSL=1;CLIENT_IGNORE_SPACE=1"); 00111 00112 //Open database connection 00113 bool ok = database.open(); 00114 if(!ok) 00115 throw SpikeStreamDBException( QString("Cannot connect to database ") + this->dbInfo.toString() + ". Error: " + database.lastError().text() ); 00116 } 00117 00118 00120 void AbstractDao::executeQuery(QSqlQuery& query){ 00121 bool result = query.exec(); 00122 if(!result){ 00123 throw SpikeStreamDBException(QString("Error executing query: '") + query.lastQuery() + "'; Error='" + query.lastError().text() + "'."); 00124 } 00125 } 00126 00127 00129 void AbstractDao::executeQuery(const QString& queryStr){ 00130 QSqlQuery query = getQuery(queryStr); 00131 executeQuery(query); 00132 } 00133 00134 00137 QSqlQuery AbstractDao::getQuery(){ 00138 checkDatabase(); 00139 QSqlQuery tmpQuery(QSqlDatabase::database(dbName)); 00140 tmpQuery.setForwardOnly(true); 00141 return tmpQuery; 00142 } 00143 00144 00147 QSqlQuery AbstractDao::getQuery(const QString& queryStr){ 00148 checkDatabase(); 00149 QSqlQuery tmpQuery(QSqlDatabase::database(dbName)); 00150 tmpQuery.setForwardOnly(true); 00151 tmpQuery.prepare(queryStr); 00152 return tmpQuery; 00153 } 00154 00155 00156 /*----------------------------------------------------------*/ 00157 /*----- PRIVATE METHODS -----*/ 00158 /*----------------------------------------------------------*/ 00159 00162 QString AbstractDao::getUniqueDBName(){ 00163 ++dbCounter; 00164 return QString("Database-") + QString::number(dbCounter); 00165 } 00166 00167