SpikeStream Library  0.2
ArchiveDao.cpp
Go to the documentation of this file.
00001 #include "ArchiveDao.h"
00002 #include "GlobalVariables.h"
00003 #include "SpikeStreamDBException.h"
00004 #include "Util.h"
00005 using namespace spikestream;
00006 
00008 ArchiveDao::ArchiveDao(const DBInfo& dbInfo) : AbstractDao(dbInfo){
00009 }
00010 
00011 
00013 ArchiveDao::~ArchiveDao(){
00014 }
00015 
00016 
00017 /*----------------------------------------------------------*/
00018 /*-----                 PUBLIC METHODS                 -----*/
00019 /*----------------------------------------------------------*/
00020 
00022 void ArchiveDao::addArchive(ArchiveInfo& archInfo){
00023         //Get date at which archive is being created.
00024         archInfo.setDateTime(QDateTime::currentDateTime());
00025 
00026         //Create the archive
00027     QSqlQuery query = getQuery("INSERT INTO Archives (StartTime, NetworkID, Description) VALUES (" + QString::number(archInfo.getDateTime().toTime_t()) + ", " + QString::number(archInfo.getNetworkID()) + ", '" + archInfo.getDescription() + "')");
00028     executeQuery(query);
00029 
00030         //Check id is correct and add to archive info if it is
00031     int lastInsertID = query.lastInsertId().toInt();
00032     if(lastInsertID >= START_ARCHIVE_ID)
00033                 archInfo.setID(lastInsertID);
00034     else
00035                 throw SpikeStreamDBException("Insert ID for Archives is invalid: " + QString::number(lastInsertID));
00036 }
00037 
00038 
00040 void ArchiveDao::addArchiveData(unsigned int archiveID, unsigned int timeStep, const QString& firingNeuronString){
00041     executeQuery("INSERT INTO ArchiveData(ArchiveID, TimeStep, FiringNeurons) VALUES (" + QString::number(archiveID) + ", " + QString::number(timeStep) + ", '"  + firingNeuronString + "')");
00042 }
00043 
00044 
00046 void ArchiveDao::addArchiveData(unsigned int archiveID, unsigned int timeStep, const QList<unsigned>& firingNeuronList){
00047 
00048 //      QByteArray ba;
00049 //      QFile f(fileName);
00050 //      if(f.open(QIODevice::ReadOnly))
00051 //      {
00052 //      ba = f.readAll();
00053 //      f.close();
00054 //      }
00055 //
00056 //      // Writing the image into table
00057 //      QSqlDatabase::database().transaction();
00058 //      QSqlQuery query;
00059 //      query.prepare( "INSERT INTO picture ( IMAGE ) VALUES (:IMAGE)" );
00060 //      query.bindValue(":IMAGE", ba);
00061 
00062         //FIXME: SORT THIS OUT WHEN I CONVERT TO BLOB
00063         //Build firing neuron string
00064         QString tmpStr;
00065         QList<unsigned>::const_iterator endList = firingNeuronList.end();
00066         for(QList<unsigned>::const_iterator iter = firingNeuronList.begin(); iter != endList; ++iter)
00067                 tmpStr += QString::number(*iter) + ",";
00068         tmpStr.truncate(tmpStr.size() - 1);
00069         addArchiveData(archiveID, timeStep, tmpStr);
00070 }
00071 
00072 
00074 void ArchiveDao::deleteArchive(unsigned int archiveID){
00075     executeQuery("DELETE FROM Archives WHERE ArchiveID = " + QString::number(archiveID));
00076 }
00077 
00078 
00080 void ArchiveDao::deleteAllArchives(){
00081         executeQuery("DELETE FROM Archives");
00082 }
00083 
00084 
00086 QList<ArchiveInfo> ArchiveDao::getArchivesInfo(unsigned int networkID){
00087     QSqlQuery query = getQuery("SELECT ArchiveID, StartTime, Description FROM Archives WHERE NetworkID=" + QString::number(networkID) + " ORDER BY StartTime");
00088     executeQuery(query);
00089     QList<ArchiveInfo> tmpList;
00090     for(int i=0; i<query.size(); ++i){
00091                 query.next();
00092                 unsigned int archiveID = Util::getUInt(query.value(0).toString());
00093                 tmpList.append(
00094                                 ArchiveInfo(
00095                                                 archiveID,
00096                                                 networkID,
00097                                                 Util::getUInt(query.value(1).toString()),//Start time as a unix timestamp
00098                                                 query.value(2).toString()//Description
00099                                                 )
00100                                 );
00101     }
00102     return tmpList;
00103 }
00104 
00105 
00107 int ArchiveDao::getArchiveSize(unsigned int archiveID){
00108     QSqlQuery query = getQuery("SELECT COUNT(*) FROM ArchiveData WHERE ArchiveID=" + QString::number(archiveID));
00109     executeQuery(query);
00110     query.next();
00111     unsigned int archiveSize = Util::getInt(query.value(0).toString());
00112     return archiveSize;
00113 }
00114 
00115 
00117 unsigned int ArchiveDao::getMaxTimeStep(unsigned int archiveID){
00118     QSqlQuery query = getQuery("SELECT MAX(TimeStep) FROM ArchiveData WHERE ArchiveID=" + QString::number(archiveID));
00119     executeQuery(query);
00120     query.next();
00121     return Util::getUInt(query.value(0).toString());
00122 }
00123 
00124 
00126 unsigned int ArchiveDao::getMinTimeStep(unsigned int archiveID){
00127     QSqlQuery query = getQuery("SELECT MIN(TimeStep) FROM ArchiveData WHERE ArchiveID=" + QString::number(archiveID));
00128     executeQuery(query);
00129     query.next();
00130     return Util::getUInt(query.value(0).toString());
00131 }
00132 
00133 
00135 QList<unsigned> ArchiveDao::getFiringNeuronIDs(unsigned int archiveID, unsigned int timeStep){
00136     QSqlQuery query = getQuery("SELECT FiringNeurons FROM ArchiveData WHERE TimeStep=" + QString::number(timeStep) + " AND ArchiveID=" + QString::number(archiveID));
00137     executeQuery(query);
00138         query.next();
00139 
00140         //Build list of firing neuron ids
00141         QList<unsigned> newList;
00142         QStringList strList = query.value(0).toString().split(",", QString::SkipEmptyParts);
00143         QStringListIterator iter(strList);
00144         while (iter.hasNext()){
00145                 newList.append(Util::getUInt(iter.next()));
00146         }
00147 
00148         //Return the list we have built
00149         return newList;
00150 }
00151 
00152 
00155 bool ArchiveDao::networkHasArchives(unsigned int networkID){
00156     QSqlQuery query = getQuery("SELECT COUNT(*) FROM Archives WHERE NetworkID=" + QString::number(networkID));
00157     executeQuery(query);
00158     query.next();
00159     if(query.value(0).toUInt() == 0)
00160                 return false;
00161     return true;
00162 }
00163 
00164 
00166 void ArchiveDao::setArchiveProperties(unsigned archiveID, const QString &description){
00167         executeQuery("UPDATE Archives SET Description='" + description + "' WHERE ArchiveID=" + QString::number(archiveID));
00168 }
00169 
00170 
00171 
 All Classes Files Functions Variables Typedefs Defines