Update the post insert behaviour of the RtcomEventLogger backend to update storage...
authorJamie Thompson <jamie@.(none)>
Sat, 1 Oct 2011 22:52:39 +0000 (23:52 +0100)
committerJamie Thompson <coding@jamie-thompson.co.uk>
Sun, 2 Oct 2011 18:35:23 +0000 (19:35 +0100)
DBBackends/RtcomEventLogger.cpp
DBBackends/RtcomEventLogger.h

index c34a8df..0766fa2 100644 (file)
@@ -329,6 +329,10 @@ void RtcomEventLogger::Insert(EventTypes::iEvent &event, const NumberToNameLooku
 
 void RtcomEventLogger::PostInsert()
 {
 
 void RtcomEventLogger::PostInsert()
 {
+       // Our new events get the specified storage times ignored, and some things
+       // use these, so bodge them for now.
+       UpdateInsertedStorageTimes();
+
        // Reorder the DB IDs as Nokia are guilty of both premature
        // optimisation as well as closed source UIs...
        Reindex();
        // Reorder the DB IDs as Nokia are guilty of both premature
        // optimisation as well as closed source UIs...
        Reindex();
@@ -341,19 +345,59 @@ void RtcomEventLogger::ClearInsertedIDs()
        InsertedIDs().clear();
 }
 
        InsertedIDs().clear();
 }
 
-// Reorder the DB IDs as Nokia are guilty of both premature
-// optimisation as well as closed source UIs...
-void RtcomEventLogger::Reindex()
+void RtcomEventLogger::UpdateInsertedStorageTimes()
 {
        // Set up the database connection...
        QSqlDatabase db(QSqlDatabase::addDatabase("QSQLITE"));
 
        db.setDatabaseName(CurrentSettings().DBPath());
 {
        // Set up the database connection...
        QSqlDatabase db(QSqlDatabase::addDatabase("QSQLITE"));
 
        db.setDatabaseName(CurrentSettings().DBPath());
-       if(!db.open())
+       if(db.open())
        {
        {
-               throw std::runtime_error("Cannot open database: Unable to establish database connection");
+               // Update storage time as some software uses it...
+               QSqlQuery * updateStorageTimeQuery(new QSqlQuery(db));
+               if(updateStorageTimeQuery != NULL)
+               {
+                       updateStorageTimeQuery->setForwardOnly( true );
+
+                       if(db.transaction())
+                       {
+                               try
+                               {
+                                       QString sqlUpdateStorageTime(QString("UPDATE events SET storage_time = start_time WHERE id IN (%1)")
+                                               .arg(IntsToStringList(InsertedIDs()).join(",")));
+                                       if (!updateStorageTimeQuery->exec(sqlUpdateStorageTime))
+                                       {
+                                               qDebug() << "Query Failed: " << sqlUpdateStorageTime;
+                                               throw std::exception();
+                                       }
+
+                                       qDebug() << "Committing.";
+                                       db.commit();
+                               }
+                               catch(...)
+                               {
+                                       qDebug() << "Rolling back.";
+                                       db.rollback();
+                               }
+                       }
+                       else
+                               qDebug() << "Unable to start transaction.";
+               }
        }
        else
        }
        else
+               throw std::runtime_error("Cannot open database: Unable to establish database connection");
+}
+
+// Reorder the DB IDs as Nokia are guilty of both premature
+// optimisation as well as closed source UIs...
+// NOTE: The InsertedID list will be invalid after this so call it last...
+void RtcomEventLogger::Reindex()
+{
+       // Set up the database connection...
+       QSqlDatabase db(QSqlDatabase::addDatabase("QSQLITE"));
+
+       db.setDatabaseName(CurrentSettings().DBPath());
+       if(db.open())
        {
                // Reorder the evnts by their start time
                uint changesRequired(0);
        {
                // Reorder the evnts by their start time
                uint changesRequired(0);
@@ -527,10 +571,23 @@ void RtcomEventLogger::Reindex()
                db.close();
                QSqlDatabase::removeDatabase( "QSQLITE" );
        }
                db.close();
                QSqlDatabase::removeDatabase( "QSQLITE" );
        }
+       else
+               throw std::runtime_error("Cannot open database: Unable to establish database connection");
 
        return;
 }
 
 
        return;
 }
 
+QStringList RtcomEventLogger::IntsToStringList(QList<uint> &values)
+{
+       QStringList returnValues;
+       returnValues.reserve(values.count());
+
+       foreach(uint value, values)
+               returnValues.append(QString::number(value));
+
+       return returnValues;
+}
+
 QDebug operator<<(QDebug dbg, RTComElEvent &event)
 {
        dbg.nospace() << "\tid:\t\t" << event.fld_id << "\n";
 QDebug operator<<(QDebug dbg, RTComElEvent &event)
 {
        dbg.nospace() << "\tid:\t\t" << event.fld_id << "\n";
index c5b3696..728c137 100644 (file)
@@ -31,6 +31,7 @@ typedef _RTComEl RTComEl;
 
 template <typename T1> class QList;
 class QString;
 
 template <typename T1> class QList;
 class QString;
+class QStringList;
 
 #include <QHash>
 
 
 #include <QHash>
 
@@ -62,7 +63,9 @@ namespace DBBackends
                virtual void ClearInsertedIDs();
 
        private:
                virtual void ClearInsertedIDs();
 
        private:
+               void UpdateInsertedStorageTimes();
                void Reindex();
                void Reindex();
+               QStringList IntsToStringList(QList<uint> &values);
 
                const Settings &m_Settings;
 
 
                const Settings &m_Settings;