From a1755df74c1e0a03617fae5731669669eeae6131 Mon Sep 17 00:00:00 2001 From: timkoma Date: Wed, 3 Feb 2010 14:08:05 +0000 Subject: [PATCH] performance improvement for Events --- src/mvc/event.cpp | 94 +++++++++++++++++++++++++++-------------------- src/mvc/event.h | 12 ++++-- 2 files changed, 63 insertions(+), 43 deletions(-) diff --git a/src/mvc/event.cpp b/src/mvc/event.cpp index 49ce16f..cf0a2bd 100644 --- a/src/mvc/event.cpp +++ b/src/mvc/event.cpp @@ -37,6 +37,10 @@ QSqlRecord const Event::sColumns = Event::toRecord(QList() << QSqlField("abstract", QVariant::String) << QSqlField("description", QVariant::String)); +Event::Event() : + mRoomId( 0 ) +{ +} Event Event::getById(int id, int conferenceId) { @@ -115,37 +119,48 @@ QList Event::getFavByDate(const QDate& date, int conferenceId) return load(query); } -QString Event::room() const +QString Event::room() { - QSqlQuery query; - // TODO: conference ID isn't used here - query.prepare("SELECT name FROM room WHERE id = (SELECT xid_room FROM event_room WHERE xid_event = :id)"); - query.bindValue(":id", id()); - query.exec(); - // TODO: handle qeury error - //qDebug() << query.lastError(); - if(query.next()) - return query.record().value("name").toString(); - else - return QString("not-available"); + if ( mRoomName.isEmpty() ) + { + QSqlQuery query; + // TODO: conference ID isn't used here + query.prepare("SELECT name FROM room WHERE id = :roomId"); + query.bindValue(":roomId", roomId()); + query.exec(); + // TODO: handle qeury error + //qDebug() << query.lastError(); + if(query.next()) + mRoomName = query.record().value("name").toString(); + else + mRoomName = QString("not-available"); + } + return mRoomName; } -int Event::roomId() const +int Event::roomId() { - QSqlQuery query; - query.prepare("SELECT xid_room FROM event_room WHERE xid_event = :id"); - query.bindValue(":id", id()); - if (!query.isActive()) - if (!query.exec()) - throw OrmSqlException(query.lastError().text()); - if (!query.next()) - throw OrmNoObjectException(); - return query.record().value("xid_room").toInt(); + if ( mRoomId == 0 ) + { + QSqlQuery query; + query.prepare("SELECT xid_room FROM event_room WHERE xid_event = :id"); + query.bindValue(":id", id()); + if (!query.isActive()) + if (!query.exec()) + throw OrmSqlException(query.lastError().text()); + if (!query.next()) + { + qDebug() << "No room found for event id: " << id(); + throw OrmNoObjectException(); + } + mRoomId = query.record().value("xid_room").toInt(); + } + return mRoomId; } QStringList Event::persons() { - if( personsList.isEmpty() ) + if( mPersonsList.isEmpty() ) { QSqlQuery query; // TODO: conference ID isn't used here @@ -156,27 +171,28 @@ QStringList Event::persons() //qDebug() << query.lastError(); while(query.next()) - personsList.append(query.record().value("name").toString()); + mPersonsList.append(query.record().value("name").toString()); } - return personsList; + return mPersonsList; } -QMap Event::links() const +QMap Event::links() { - QSqlQuery query; - query.prepare("SELECT name,url FROM link WHERE xid_event = :id AND xid_conference = :conf"); - query.bindValue(":id", id()); - query.bindValue(":conf", conferenceId()); - query.exec(); - // TODO: handle qeury error - //qDebug() << query.lastError(); - - QMap links; - while(query.next()) - links.insert(query.record().value("name").toString(), query.record().value("url").toString()); - - return links; + if ( mLinksList.isEmpty() ) + { + QSqlQuery query; + query.prepare("SELECT name,url FROM link WHERE xid_event = :id AND xid_conference = :conf"); + query.bindValue(":id", id()); + query.bindValue(":conf", conferenceId()); + query.exec(); + // TODO: handle qeury error + //qDebug() << query.lastError(); + + while(query.next()) + mLinksList.insert(query.record().value("name").toString(), query.record().value("url").toString()); + } + return mLinksList; } bool Event::hasTimeConflict() const diff --git a/src/mvc/event.h b/src/mvc/event.h index 4fcd3ed..a4a73f0 100644 --- a/src/mvc/event.h +++ b/src/mvc/event.h @@ -35,6 +35,7 @@ class NoSuchEventException class Event : public OrmRecord { public: + Event(); static const QSqlRecord sColumns; static QString const sTableName; public: @@ -63,10 +64,10 @@ public: QString abstract() const { return value("abstract").toString(); } QString description() const { return value("description").toString(); } // records from other tables associated with 'id' - QString room() const; - int roomId() const; + QString room(); + int roomId(); QStringList persons(); - QMap links() const; + QMap links(); void setId(int id) { setValue("id", id); } void setConferenceId(int conferenceId) { setValue("xid_conference", conferenceId); } @@ -90,7 +91,10 @@ public: friend class EventTest; private: - QStringList personsList; + QStringList mPersonsList; + QMap mLinksList; + int mRoomId; + QString mRoomName; }; #endif // EVENT_H -- 2.39.5