X-Git-Url: https://git.toastfreeware.priv.at/toast/confclerk.git/blobdiff_plain/c79026865de7bff94ba081df55a4826a8b560d7d..4bf728f7906e7c5fd5ae5bfc5a0a4b26804ce97e:/src/orm/ormrecord.h diff --git a/src/orm/ormrecord.h b/src/orm/ormrecord.h index d14b861..85d32c2 100644 --- a/src/orm/ormrecord.h +++ b/src/orm/ormrecord.h @@ -1,3 +1,22 @@ +/* + * Copyright (C) 2010 Ixonos Plc. + * Copyright (C) 2011 Philipp Spitzer, gregor herrmann + * + * This file is part of ConfClerk. + * + * ConfClerk is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation, either version 2 of the License, or (at your option) + * any later version. + * + * ConfClerk is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * ConfClerk. If not, see . + */ #ifndef ORMRECORD_H #define ORMRECORD_H @@ -9,35 +28,28 @@ #include #include -// INFO: -// all record items/columns may be defined in one table (1.), or -// they can be splitted in two separate tables (2.) (eg. for FTS support) -// 1.) you have to define "static QString const sTableName" -// 2.) you have to define two table names: -// "static QString const sTable1Name" -// "static QString const sTable2Name" -// and since all record items/columns are handled by one QSqlRecord, -// you have to also define number of columns that belongs to table 1 (1.), and table 2 (2.) -// 1.) "static int const sTable1ColCount" -// 2.) "static int const sTable2ColCount" -// there are also defined auxiliary methods for 1-Table/2-Tables approach, see bellow - class OrmException { +public: + OrmException(const QString& text) : mText(text) {} + virtual ~OrmException(){} + virtual const QString& text() const { return mText; } +private: + QString mText; }; -class OrmNoObjectException : OrmException +class OrmNoObjectException : public OrmException { +public: + OrmNoObjectException() : OrmException("No object exception"){} + ~OrmNoObjectException(){} }; -class OrmSqlException : OrmException +class OrmSqlException : public OrmException { public: - OrmSqlException(const QString& text) : mText(text) {} - QString text() const { return mText; } - -private: - QString mText; + OrmSqlException(const QString& text) : OrmException( QString("Sql error: ") + text ) {} + ~OrmSqlException(){} }; template @@ -61,9 +73,6 @@ protected: static QString columnsForSelect(const QString& prefix = QString()); static QString selectQuery(); static QString updateQuery(); - // record items/columns are stored in two tables - static QString columnsForSelectJoin2T(); // for joining two tables - static QString selectQueryJoin2T(const QString &key); // for joining two tables static QVariant convertToC(QVariant value, QVariant::Type colType); static QVariant convertToDb(QVariant value, QVariant::Type colType); @@ -84,8 +93,8 @@ T OrmRecord::hydrate(const QSqlRecord& record) } // updates specified column 'col' -// if the value is not specified as an argument, -// it's taken from the reford itself +// if the value is not specified as an argument, +// it's taken from the record itself // see also: setValue() method for more details template void OrmRecord::update(QString col, QVariant value) @@ -97,7 +106,6 @@ void OrmRecord::update(QString col, QVariant value) else // take 'col' value from the record; see setValue() query.bindValue(":col", convertToDb(this->value(col), this->value(col).type())); query.bindValue(":id", this->value("id")); - //query.bindValue(":id", convertToDb(value("id"), QVariant::Int)); query.exec(); } @@ -120,13 +128,13 @@ T OrmRecord::loadOne(QSqlQuery query) { if (!query.exec()) { - throw new OrmSqlException(query.lastError().text()); + throw OrmSqlException(query.lastError().text()); } } if (!query.next()) { - throw new OrmNoObjectException(); + throw OrmNoObjectException(); } return hydrate(query.record()); @@ -139,7 +147,8 @@ QList OrmRecord::load(QSqlQuery query) { if (!query.exec()) { - throw new OrmSqlException(query.lastError().text()); + qDebug() << "Error: " << query.lastError().driverText() << "; Type: " << query.lastError().type(); + throw OrmSqlException(query.lastError().text()); } } @@ -148,7 +157,6 @@ QList OrmRecord::load(QSqlQuery query) { objects << hydrate(query.record()); } - return objects; } @@ -165,55 +173,25 @@ QString OrmRecord::columnsForSelect(const QString& prefix) return prefixedColumns.join(","); } -template -QString OrmRecord::columnsForSelectJoin2T() -{ - Q_ASSERT((T::sTable1ColCount+T::sTable2ColCount) == T::sColumns.count()); - - QStringList prefixedColumns; - for (int i=0; i QString OrmRecord::selectQuery() { return QString("SELECT %1 FROM %2 ").arg(columnsForSelect(), T::sTableName); } -template -QString OrmRecord::selectQueryJoin2T(const QString &key) -{ - return QString("SELECT %1 FROM %2 INNER JOIN %3 ON %4.%5 = %6.%7 ").arg( - columnsForSelectJoin2T(), - T::sTable1Name, - T::sTable2Name, - T::sTable1Name, - key, - T::sTable2Name, - key); -} - template QString OrmRecord::updateQuery() { - return QString("UPDATE %1 ").arg(T::sTable1Name); + return QString("UPDATE %1 ").arg(T::sTableName); } template QSqlRecord OrmRecord::toRecord(const QList & columnList) { QSqlRecord record; - foreach (const QSqlField & col, columnList) + for(int i=0; i< columnList.count(); i++) { - record.append(col); + record.append(columnList[i]); } return record; }