SpikeStream Library
0.2
|
00001 //SpikeStream includes 00002 #include "DatabaseDao.h" 00003 #include "SpikeStreamDBException.h" 00004 #include "SpikeStreamIOException.h" 00005 using namespace spikestream; 00006 00007 //Qt includes 00008 #include <QTextStream> 00009 00010 //Other includes 00011 #include <iostream> 00012 using namespace std; 00013 00014 00016 DatabaseDao::DatabaseDao(const DBInfo& dbInfo) : AbstractDao(dbInfo){ 00017 } 00018 00019 00021 DatabaseDao::DatabaseDao() : AbstractDao(){ 00022 } 00023 00024 00026 DatabaseDao::~DatabaseDao(){ 00027 closeDatabaseConnection(); 00028 } 00029 00030 00031 /*----------------------------------------------------------*/ 00032 /*----- PUBLIC METHODS -----*/ 00033 /*----------------------------------------------------------*/ 00034 00036 void DatabaseDao::connectToDatabase(const DBInfo& dbInfo){ 00037 //Store the information about the database 00038 setDBInfo(dbInfo); 00039 00040 //Connect to the database 00041 connectToDatabase(); 00042 } 00043 00044 00046 QList<QString> DatabaseDao::getDatabaseNames(){ 00047 QSqlQuery query = getQuery("SHOW DATABASES"); 00048 executeQuery(query); 00049 QList<QString> dbList; 00050 while ( query.next() ) { 00051 dbList.append(query.value(0).toString()); 00052 } 00053 return dbList; 00054 } 00055 00056 00058 void DatabaseDao::executeSQLFile(const QString& fileName){ 00059 QFile dbFile(fileName); 00060 if(!dbFile.exists()){ 00061 throw SpikeStreamDBException("Attempting to load file " + fileName + " that does not exist"); 00062 } 00063 QString fileStr; 00064 loadFileIntoString(dbFile, fileStr); 00065 00066 /* There is a limit to the length of string that can be executed, so have to 00067 break string down into individual commands */ 00068 QStringList cmdList = fileStr.split(";", QString::SkipEmptyParts); 00069 foreach(QString sqlCmd, cmdList){ 00070 executeQuery(sqlCmd); 00071 } 00072 } 00073 00074 00076 void DatabaseDao::executeSQLFile(const QString& dbName, const QString& fileName){ 00077 QFile dbFile(fileName); 00078 if(!dbFile.exists()){ 00079 throw SpikeStreamDBException("Attempting to load file " + fileName + " that does not exist"); 00080 } 00081 QString fileStr = "USE " + dbName + ";\n"; 00082 loadFileIntoString(dbFile, fileStr); 00083 00084 /* There is a limit to the length of string that can be executed, so have to 00085 break string down into individual commands */ 00086 QStringList cmdList = fileStr.split(";", QString::SkipEmptyParts); 00087 foreach(QString sqlCmd, cmdList){ 00088 executeQuery(sqlCmd); 00089 } 00090 } 00091 00092 00093 /*----------------------------------------------------------*/ 00094 /*----- PRIVATE METHODS -----*/ 00095 /*----------------------------------------------------------*/ 00096 00099 void DatabaseDao::loadFileIntoString(QFile& file, QString& fileString){ 00100 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) 00101 throw SpikeStreamIOException("Cannot open file: " + file.fileName()); 00102 00103 QTextStream in(&file); 00104 while (!in.atEnd()) { 00105 fileString += in.readLine(); 00106 } 00107 } 00108 00109 00110