qDebug() << "Failed to create event logger.";
}
-RtcomEventLogger::RtcomEventLogger(const Settings &settings, const EventTypes::RtcomEvent &event) :
+RtcomEventLogger::RtcomEventLogger(const Settings &settings, const EventTypes::RtcomEvent &/*event*/) :
m_Settings(settings)
{
}
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();
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());
- 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
+ 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);
}
}while(changesRequired > 0);
- // Update the group cache so the last events are correct
- {
- qDebug() << "Updating most recent events.";
-
- // Grab group UIDs from group cache
- QSqlQuery * dbq(new QSqlQuery( db ));
- dbq->setForwardOnly( true );
-
- const char * groupUIDListSQL("SELECT group_uid FROM GroupCache");
- if (dbq->exec(groupUIDListSQL))
- {
- qDebug() << "Query OK, " << dbq->numRowsAffected() << " rows affected.";
- qDebug() << "GroupUIDs:";
-
- QSet<QString> groupUIDs;
- while( dbq->next() )
- {
- QString groupUID(dbq->value(0).value<QString>());
-
- qDebug() << groupUID;
- groupUIDs.insert(groupUID);
- }
-
- // Iterate over group UIDS
- if(groupUIDs.count() > 0)
- {
- // Build a batch statement to update every group with
- // the most recent event
-
- // Ignore 'data' failures (i.e. no events but present in the
- // cache)- something else's been monkeying with the DB, and
- // we can't account for everything.
- const QString updateGroupCacheWithLatestEventsSQL(
- "UPDATE OR IGNORE GroupCache SET event_id = "
- "(SELECT id FROM events WHERE group_uid = \"%1\" "
- " ORDER BY id DESC LIMIT 1)"
- " WHERE group_uid = \"%1\";");
- QString updateGroupCacheWithLatestEventsBatchSQL;
- foreach(QString groupUID, groupUIDs)
- {
- updateGroupCacheWithLatestEventsBatchSQL.append(
- updateGroupCacheWithLatestEventsSQL
- .arg(groupUID)
- ).append("\n");
- }
-
- // Execute the statement in single-statement chunks thanks
- // to QT's inability to call the SQLite function supporting
- // multiple statements
-
- QSqlQuery * setLatestEventInGroupCacheSQL(new QSqlQuery( db ));
- if(NULL != setLatestEventInGroupCacheSQL)
- {
- setLatestEventInGroupCacheSQL->setForwardOnly( true );
-
- if(db.transaction())
- {
- QStringList statements = updateGroupCacheWithLatestEventsBatchSQL.trimmed().split(";", QString::SkipEmptyParts);
- try
- {
- for( QStringList::const_iterator currentStatement(statements.constBegin()); currentStatement != statements.constEnd(); ++currentStatement)
- {
- if ( setLatestEventInGroupCacheSQL->exec( *currentStatement ))
- qDebug() << "Query OK, " << setLatestEventInGroupCacheSQL->numRowsAffected() << " rows affected.";
- else
- {
- qDebug() << "Query Failed: " << *currentStatement;
- throw std::exception();
- }
- }
-
- qDebug() << "Committing.";
- db.commit();
- }
- catch(...)
- {
- qDebug() << "Rolling back.";
- db.rollback();
- }
- }
- else
- qDebug() << "Unable to start transaction.";
- }
- }
- }
- else
- {
- qDebug() << "SQL EXEC Error: "<< "EXEC query failed";
- qDebug() << "Query: " << groupUIDListSQL;
- }
- }
-
qDebug() << "Closing.";
db.close();
QSqlDatabase::removeDatabase( "QSQLITE" );
}
+ else
+ throw std::runtime_error("Cannot open database: Unable to establish database connection");
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";