Changeset c790268


Ignore:
Timestamp:
01/17/10 17:22:03 (12 years ago)
Author:
pavelpa <pavelpa@…>
Branches:
master, qt5
Children:
6f39595
Parents:
680a4da
Message:

implemented JOINing two tables

  • modified 'ormrecord' to support JOINing two tables
  • modified 'event' accordingly, since its items/columns are splitted into two separate tables
Location:
src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • src/mvc/delegate.cpp

    r680a4da rc790268  
    137137
    138138    // draw title
    139     QPointF titlePointF(option.rect.x(),option.rect.y()+option.rect.height()-10);
    140     QString text = qVariantValue<QString>(index.data());
     139    QPointF titlePointF(option.rect.x()+SPACER,option.rect.y()+option.rect.height()-10);
     140    QString text;
     141    if(index.parent().isValid()) // event
     142        text = qVariantValue<QString>(index.data()) + ": " + static_cast<Event*>(index.internalPointer())->title();
     143    else // group
     144        text = qVariantValue<QString>(index.data());
    141145    painter->drawText(titlePointF,text);
    142146
  • src/mvc/event.cpp

    r680a4da rc790268  
    11#include "event.h"
    22
     3// 'event' record is splitted into two separate tables 'event' and 'virtual_event'
     4// for the FTS (Full-Text-Search) support and so, it is necessary to provide/use
     5// two table names + corresponding parameters/methods, see bellow
     6QString const Event::sTable1Name = QString("event");
     7QString const Event::sTable2Name = QString("virtual_event");
     8int const Event::sTable1ColCount = 8; // see 'toRecord()' for more details
     9int const Event::sTable2ColCount = 5; // see 'toRecord()' for more details
     10
    311QSqlRecord const Event::sColumns = Event::toRecord(QList<QSqlField>()
     12    /* 'columns from Table 1 */
    413    << QSqlField("id", QVariant::Int)
    514    << QSqlField("xid_conference", QVariant::Int)
     
    918    << QSqlField("type", QVariant::String)
    1019    << QSqlField("language", QVariant::String)
    11     << QSqlField("favourite", QVariant::Bool));
     20    << QSqlField("favourite", QVariant::Bool)
     21    /* 'columns' from Table2 */
     22    << QSqlField("tag", QVariant::String)
     23    << QSqlField("title", QVariant::String)
     24    << QSqlField("subtitle", QVariant::String)
     25    << QSqlField("abstract", QVariant::String)
     26    << QSqlField("description", QVariant::String));
    1227
    13 QString const Event::sTableName = QString("event");
    1428
    1529Event Event::getById(int id, int conferenceId)
    1630{
    1731    QSqlQuery query;
    18     query.prepare(selectQuery() + "WHERE id = :id AND xid_conference = :conf");
     32    query.prepare(selectQueryJoin2T("id") + "WHERE event.id = :id AND event.xid_conference = :conf");
    1933    query.bindValue(":id", id);
    2034    query.bindValue(":conf", conferenceId);
     35
    2136    return loadOne(query);
    2237}
     
    2540{
    2641    QSqlQuery query;
    27     query.prepare(selectQuery() + "WHERE xid_conference = :conf AND start >= :start AND start < :end ORDER BY start");
     42    query.prepare(selectQueryJoin2T("id") + "WHERE event.xid_conference = :conf AND event.start >= :start AND event.start < :end ORDER BY event.start");
    2843    query.bindValue(":conf", conferenceId);
    2944    query.bindValue(":start", convertToDb(date, QVariant::DateTime));
  • src/mvc/event.h

    r680a4da rc790268  
    1919public:
    2020    static QSqlRecord const sColumns;
    21     static QString const sTableName;
     21    //static QString const sTableName;
     22    static QString const sTable1Name;
     23    static QString const sTable2Name;
     24    static int const sTable1ColCount;
     25    static int const sTable2ColCount;
    2226
    2327public:
     
    2630
    2731public:
     32    // Table 1
    2833    int id() const { return value("id").toInt(); }
    2934    int conferenceId() const { return value("xid_conference").toInt(); }
     
    3439    QString language() const { return value("language").toString(); }
    3540    bool isFavourite() const { return value("favourite").toBool(); }
     41    // Table 2 : virtual table for FTS (Full Text Search)
     42    QString tag() const { return value("tag").toString(); }
     43    QString title() const { return value("title").toString(); }
     44    QString subtitle() const { return value("subtitle").toString(); }
     45    QString abstract() const { return value("abstract").toString(); }
     46    QString description() const { return value("description").toString(); }
    3647
     48    // Table 1
    3749    void setId(int id) { setValue("id", id); }
    3850    void setConferenceId(int conferenceId) { setValue("xid_conference", conferenceId); }
     
    4355    void setLanguage(const QString& language) { setValue("language", language); }
    4456    void setFavourite(bool favourite) { setValue("favourite", favourite); }
     57    // Table 2 : virtual table for FTS (Full Text Search)
     58    void setTag(const QString& tag) { setValue("tag", tag); }
     59    void setTitle(const QString& title) { setValue("title", title); }
     60    void setSubtitle(const QString& subtitle) { setValue("subtitle", subtitle); }
     61    void setAbstract(const QString& abstract) { setValue("abstract", abstract); }
     62    void setDescription(const QString& description) { setValue("description", description); }
    4563
    4664friend class EventTest;
  • src/orm/ormrecord.h

    r680a4da rc790268  
    1010#include <QDebug>
    1111
     12// INFO:
     13// all record items/columns may be defined in one table (1.), or
     14// they can be splitted in two separate tables (2.) (eg. for FTS support)
     15// 1.) you have to define "static QString const sTableName"
     16// 2.) you have to define two table names:
     17//     "static QString const sTable1Name"
     18//     "static QString const sTable2Name"
     19// and since all record items/columns are handled by one QSqlRecord,
     20// you have to also define number of columns that belongs to table 1 (1.), and table 2 (2.)
     21// 1.) "static int const sTable1ColCount"
     22// 2.) "static int const sTable2ColCount"
     23// there are also defined auxiliary methods for 1-Table/2-Tables approach, see bellow
     24
    1225class OrmException
    1326{
     
    4457
    4558    // auxiliary methods
     59    static QSqlRecord toRecord(const QList<QSqlField> & columnList);
     60    // all record items/columns are in one table
    4661    static QString columnsForSelect(const QString& prefix = QString());
    4762    static QString selectQuery();
    4863    static QString updateQuery();
    49     static QSqlRecord toRecord(const QList<QSqlField> & columnList);
     64    // record items/columns are stored in two tables
     65    static QString columnsForSelectJoin2T(); // for joining two tables
     66    static QString selectQueryJoin2T(const QString &key); // for joining two tables
    5067
    5168    static QVariant convertToC(QVariant value, QVariant::Type colType);
     
    150167
    151168template <typename T>
     169QString OrmRecord<T>::columnsForSelectJoin2T()
     170{
     171    Q_ASSERT((T::sTable1ColCount+T::sTable2ColCount) == T::sColumns.count());
     172
     173    QStringList prefixedColumns;
     174    for (int i=0; i<T::sTable1ColCount; i++)
     175    {
     176        prefixedColumns.append(QString("%1.%2").arg(T::sTable1Name, T::sColumns.field(i).name()));
     177    }
     178    for (int j=0; j<T::sTable2ColCount; j++)
     179    {
     180        prefixedColumns.append(QString("%1.%2").arg(T::sTable2Name, T::sColumns.field(T::sTable1ColCount+j).name()));
     181    }
     182    return prefixedColumns.join(",");
     183}
     184
     185template <typename T>
    152186QString OrmRecord<T>::selectQuery()
    153187{
     
    156190
    157191template <typename T>
     192QString OrmRecord<T>::selectQueryJoin2T(const QString &key)
     193{
     194    return QString("SELECT %1 FROM %2 INNER JOIN %3 ON %4.%5 = %6.%7 ").arg(
     195            columnsForSelectJoin2T(),
     196            T::sTable1Name,
     197            T::sTable2Name,
     198            T::sTable1Name,
     199            key,
     200            T::sTable2Name,
     201            key);
     202}
     203
     204template <typename T>
    158205QString OrmRecord<T>::updateQuery()
    159206{
    160     return QString("UPDATE %1 ").arg(T::sTableName);
     207    return QString("UPDATE %1 ").arg(T::sTable1Name);
    161208}
    162209
     
    198245
    199246#endif // ORMRECORD_H
     247
Note: See TracChangeset for help on using the changeset viewer.