From: timkoma Date: Thu, 21 Jan 2010 09:49:41 +0000 (+0000) Subject: first working version of the search X-Git-Tag: 0.5.0~237 X-Git-Url: https://git.toastfreeware.priv.at/toast/confclerk.git/commitdiff_plain/e662750178972435a940730b0a228c1548e20d63?ds=sidebyside first working version of the search --- diff --git a/src/gui/mainwindow.cpp b/src/gui/mainwindow.cpp index 00ae49b..defed3c 100644 --- a/src/gui/mainwindow.cpp +++ b/src/gui/mainwindow.cpp @@ -70,7 +70,7 @@ MainWindow::MainWindow(int aEventId, QWidget *aParent) trackTreeView->setModel(new EventModel()); trackTreeView->setItemDelegate(new Delegate(trackTreeView)); - // DAY EVENTS View + // SEARCH EVENTS View searchTreeView->setHeaderHidden(true); searchTreeView->setRootIsDecorated(false); searchTreeView->setIndentation(0); @@ -111,6 +111,7 @@ MainWindow::MainWindow(int aEventId, QWidget *aParent) dayNavigator->setDates(aStartDate, aEndDate); trackDayNavigator->setDates(aStartDate, aEndDate); favouriteDayNavigator->setDates(aStartDate, aEndDate); + searchDayNavigator->setDates(aStartDate, aEndDate); } connect(tabWidget, SIGNAL(currentChanged(int)), this, SLOT(updateTab(int))); @@ -274,9 +275,11 @@ void MainWindow::searchClicked() if( searchAbstract->isChecked() ) columns.append( "abstract" ); + searchTreeView->reset(); if( mSqlEngine->searchEvent( confId, columns, searchEdit->text() ) > 0 ){ - searchTreeView->setVisible(true); - searchDayNavigator->setVisible(true); + static_cast(searchTreeView->model())->loadSearchResultEvents(Conference::getById(confId).start(),confId); + searchDayNavigator->show(); + searchTreeView->show(); } } diff --git a/src/gui/mainwindow.ui b/src/gui/mainwindow.ui index 57d3afa..abde2b0 100644 --- a/src/gui/mainwindow.ui +++ b/src/gui/mainwindow.ui @@ -90,86 +90,86 @@ - - + + Search - - - - - QFormLayout::AllNonFixedFieldsGrow - - - - - Title - - - - - - - - - true - - - type a keyword to search - - - - - - - Search - - - false - - - false - - - true - - - false - - - - - - - - - Abstract - - - - - - - - - - 16777215 - 16777215 - - - - - - - - - 16777215 - 16777215 - - - - - + + + + + QFormLayout::AllNonFixedFieldsGrow + + + + + Title + + + + + + + + + true + + + type a keyword to search + + + + + + + Search + + + false + + + false + + + true + + + false + + + + + + + + + Abstract + + + + + + + + + + 16777215 + 16777215 + + + + + + + + + 16777215 + 16777215 + + + + + diff --git a/src/mvc/event.cpp b/src/mvc/event.cpp index 022a8ac..8bf984d 100644 --- a/src/mvc/event.cpp +++ b/src/mvc/event.cpp @@ -113,3 +113,20 @@ void Event::setPersons(const QStringList &persons) // TODO: implement } +QList Event::getSearchResultByDate(const QDate& date, int conferenceId, QString orderBy) +{ + + QString strQuery = QString("SELECT %1 FROM EVENT INNER JOIN VIRTUAL_EVENT USING (xid_conference, id) " + "INNER JOIN SEARCH_EVENT USING (xid_conference, id) ").arg( columnsForSelectJoin2T() ); + strQuery += QString( + "WHERE %1.xid_conference = :conf AND %1.start >= :start AND %1.start < :end ORDER BY %1.%2").arg(sTable1Name, orderBy); + qDebug() << strQuery; + QSqlQuery query; + query.prepare( strQuery ); + query.bindValue(":conf", conferenceId); + query.bindValue(":start", convertToDb(date, QVariant::DateTime)); + query.bindValue(":end", convertToDb(date.addDays(1), QVariant::DateTime)); + + return load(query); +} + diff --git a/src/mvc/event.h b/src/mvc/event.h index 2b392dc..93ca4e3 100644 --- a/src/mvc/event.h +++ b/src/mvc/event.h @@ -27,6 +27,7 @@ public: static Event getById(int id, int conferenceId); static QList getByDate(const QDate & date, int conferenceId, QString orderBy); static QList getFavByDate(const QDate & date, int conferenceId); // get Favourities by Date + static QList getSearchResultByDate(const QDate& date, int conferenceId, QString orderBy); public: // Table 1 int id() const { return value("id").toInt(); } diff --git a/src/mvc/eventmodel.cpp b/src/mvc/eventmodel.cpp index c877f02..abd688f 100644 --- a/src/mvc/eventmodel.cpp +++ b/src/mvc/eventmodel.cpp @@ -182,6 +182,18 @@ void EventModel::loadFavEvents(const QDate &aDate, int aConferenceId) createTimeGroups(); } +void EventModel::loadSearchResultEvents(const QDate &aDate, int aConferenceId) +{ + clearModel(); + // check for existence of the conference in the DB + if(Conference::getAll().count()) + { + qDebug() << "Loading search result Data: [" << Conference::getById(aConferenceId).title() << "] " << aDate; + mEvents = Event::getSearchResultByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "start"); + } + createTimeGroups(); +} + void EventModel::loadEventsByTrack(const QDate &aDate, int aConferenceId) { clearModel(); diff --git a/src/mvc/eventmodel.h b/src/mvc/eventmodel.h index 0fdbe9b..c36917b 100644 --- a/src/mvc/eventmodel.h +++ b/src/mvc/eventmodel.h @@ -19,6 +19,7 @@ public: void loadEvents(const QDate &aDate, int aConferenceId); // loads Events from the DB void loadFavEvents(const QDate &aDate, int aConferenceId); // loads Favourite events from the DB void loadEventsByTrack(const QDate &aDate, int aConferenceId); // loads Events grouped by Track from the DB + void loadSearchResultEvents(const QDate &aDate, int aConferenceId); // a method to force 'EventModel' emit signal 'dataChanged()' // a 'view', eg. 'TreeView' listens for this signal and redraws changed items(indexes) void emitDataChangedSignal(const QModelIndex &aTopLeft, const QModelIndex &aBottomRight); diff --git a/src/orm/ormrecord.h b/src/orm/ormrecord.h index ae848ea..a4923d7 100644 --- a/src/orm/ormrecord.h +++ b/src/orm/ormrecord.h @@ -142,6 +142,10 @@ QList OrmRecord::load(QSqlQuery query) qDebug() << "Error: " << query.lastError().driverText() << "; Type: " << query.lastError().type(); throw new OrmSqlException(query.lastError().text()); } + else + { + qDebug() << "SQL OK"; + } } QList objects; @@ -149,7 +153,7 @@ QList OrmRecord::load(QSqlQuery query) { objects << hydrate(query.record()); } - + qDebug() << "Fetch done"; return objects; } diff --git a/src/sql/sqlengine.cpp b/src/sql/sqlengine.cpp index d74792b..c9a0e21 100644 --- a/src/sql/sqlengine.cpp +++ b/src/sql/sqlengine.cpp @@ -37,8 +37,10 @@ QString SqlEngine::login(const QString &aDatabaseType, const QString &aDatabaseN // - create new DB from resource, which contains empty DB with tables result = createTables(database); } - - database.open(); + else + { + database.open(); + } //LOG_INFO(QString("Opening '%1' database '%2'").arg(aDatabaseType).arg(aDatabaseName)); @@ -317,13 +319,15 @@ int SqlEngine::searchEvent(int aConferenceId, const QList &aColumns, co if ( !db.isValid() || !db.isOpen()) return -1; - QString query = QString( - "DROP TABLE IF EXISTS SEARCH_EVENT;" - "CREATE TEMP TABLE SEARCH_EVENT ( xid_conference INTEGER NOT NULL, id INTEGER NOT NULL );" - "INSERT INTO SEARCH_EVENT ( xid_conference, id) " - "SELECT xid_conference, id FROM EVENT AS e INNER JOIN VIRTUAL_EVENT AS ve USING (xid_conference, id) " - "WHERE xid_conference = %1 AND (").arg( aConferenceId ); + // DROP + execQuery( db, "DROP TABLE IF EXISTS SEARCH_EVENT;"); + // CREATE + execQuery( db, "CREATE TABLE SEARCH_EVENT ( xid_conference INTEGER NOT NULL, id INTEGER NOT NULL );"); + // INSERT + QString query = QString("INSERT INTO SEARCH_EVENT ( xid_conference, id) " + "SELECT xid_conference, id FROM EVENT AS e INNER JOIN VIRTUAL_EVENT AS ve USING (xid_conference, id) " + "WHERE xid_conference = %1 AND (").arg( aConferenceId ); int i = 0; foreach (QString str, aColumns){ query += QString("%1 LIKE '\%%2\%' OR ").arg( aColumns.at(i++), aKeyword ); @@ -331,17 +335,23 @@ int SqlEngine::searchEvent(int aConferenceId, const QList &aColumns, co query.chop( QString(" OR ").length() ); query += QString(");"); - qDebug() << "\nSQL: " << query; + execQuery( db, query ); - db.exec(); + //TODO: retun number of rows from SEARCH_EVENT + return 1; +} + +bool SqlEngine::execQuery(QSqlDatabase &aDatabase, const QString &aQuery) +{ + qDebug() << "\nSQL: " << aQuery; - if( db.lastError().isValid() && db.lastError().number() != 0 ){ - qDebug() << "SQL ERR: " << db.lastError().number() << ", " << db.lastError().text(); - return 0; + QSqlQuery sqlQuery(aDatabase); + if( !sqlQuery.exec(aQuery) ){ + qDebug() << "SQL ERR: " << sqlQuery.lastError().number() << ", " << sqlQuery.lastError().text(); + return false; } else{ - qDebug() << "SQL OK.\n"; - return 1; + qDebug() << "SQL OK.\n"; + return true; } - } diff --git a/src/sql/sqlengine.h b/src/sql/sqlengine.h index 110eb24..272865f 100644 --- a/src/sql/sqlengine.h +++ b/src/sql/sqlengine.h @@ -24,6 +24,7 @@ class SqlEngine : public QObject private: QString login(const QString &aDatabaseType, const QString &aDatabaseName); bool createTables(QSqlDatabase &aDatabase); + bool execQuery(QSqlDatabase &aDatabase, const QString &aQuery); }; #endif /* SQLENGINE_H */