Changeset 680a4da for src/orm


Ignore:
Timestamp:
01/16/10 16:31:15 (12 years ago)
Author:
pavelpa <pavelpa@…>
Branches:
master, qt5
Children:
c790268
Parents:
8860ff4
Message:

work on favourite

  • created 'favourite' column in EVENT table
  • modified 'ormrecord' for setting record's elements
  • favourities view not implemented
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/orm/ormrecord.h

    r8860ff4 r680a4da  
    3434    OrmRecord();
    3535    static T hydrate(const QSqlRecord& record);
     36    void update(QString col, QVariant value = QVariant()); // updates specified column 'col'
    3637
    3738protected:
     
    4546    static QString columnsForSelect(const QString& prefix = QString());
    4647    static QString selectQuery();
     48    static QString updateQuery();
    4749    static QSqlRecord toRecord(const QList<QSqlField> & columnList);
    4850
     
    6365    object.QSqlRecord::operator=(record);
    6466    return object;
     67}
     68
     69// updates specified column 'col'
     70// if the value is not specified  as an argument,
     71// it's taken from the reford itself
     72// see also: setValue() method for more details
     73template <typename T>
     74void OrmRecord<T>::update(QString col, QVariant value)
     75{
     76    QSqlQuery query;
     77    query.prepare(QString(updateQuery() + "SET %1 = :col WHERE id = :id").arg(col));
     78    if(value.isValid()) // take 'col' value from the method's arguments
     79        query.bindValue(":col", value);
     80    else // take 'col' value from the record; see setValue()
     81        query.bindValue(":col", convertToDb(this->value(col), this->value(col).type()));
     82    query.bindValue(":id", this->value("id"));
     83    //query.bindValue(":id", convertToDb(value("id"), QVariant::Int));
     84    query.exec();
    6585}
    6686
     
    103123        if (!query.exec())
    104124        {
    105             //TODO Palo: exception handling !
    106                 QString start = "START\n";
    107                 QString end = "\nEND\n";
    108                 QString message = start + query.lastError().text() + end;
    109                 const char *data = message.toLatin1().data();
    110                 qDebug(data);
    111125            throw new OrmSqlException(query.lastError().text());
    112126        }
     
    139153{
    140154    return QString("SELECT %1 FROM %2 ").arg(columnsForSelect(), T::sTableName);
     155}
     156
     157template <typename T>
     158QString OrmRecord<T>::updateQuery()
     159{
     160    return QString("UPDATE %1 ").arg(T::sTableName);
    141161}
    142162
Note: See TracChangeset for help on using the changeset viewer.