X-Git-Url: https://git.toastfreeware.priv.at/toast/confclerk.git/blobdiff_plain/5a73d273c52b554484cf5e3ae56deee9255d9389..83481c684518420be3c2d2c604ba85b2e72be9e4:/src/orm/ormrecord.h
diff --git a/src/orm/ormrecord.h b/src/orm/ormrecord.h
index 754bc4f..8357b6b 100644
--- a/src/orm/ormrecord.h
+++ b/src/orm/ormrecord.h
@@ -1,42 +1,227 @@
+/*
+ * Copyright (C) 2010 Ixonos Plc.
+ * Copyright (C) 2011-2013 Philipp Spitzer, gregor herrmann, Stefan Stahl
+ *
+ * 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
+
+#include
#include
#include
+#include
+#include
+#include
+#include
+
+class OrmException
+{
+public:
+ OrmException(const QString& text) : mText(text) {}
+ virtual ~OrmException(){}
+ virtual const QString& text() const { return mText; }
+private:
+ QString mText;
+};
+
+class OrmNoObjectException : public OrmException
+{
+public:
+ OrmNoObjectException() : OrmException("No object exception"){}
+ ~OrmNoObjectException(){}
+};
+
+class OrmSqlException : public OrmException
+{
+public:
+ OrmSqlException(const QString& text) : OrmException( QString("Sql error: ") + text ) {}
+ ~OrmSqlException(){}
+};
template
class OrmRecord : protected QSqlRecord
{
public:
- static QString colName(int col);
+ OrmRecord();
+ static T hydrate(const QSqlRecord& record);
+ void update(QString col, QVariant value = QVariant()); // updates specified column 'col'
protected:
- QVariant value(int col) const;
- void setValue(int col, QVariant value);
+ QVariant value(QString col) const;
+ void setValue(QString col, QVariant value);
+
+ static T loadOne(QSqlQuery query);
+ static QList load(QSqlQuery query);
+
+ // auxiliary methods
+ static QSqlRecord toRecord(const QList & columnList);
+ // all record items/columns are in one table
+ static QString columnsForSelect(const QString& prefix = QString());
+ static QString selectQuery();
+ static QString updateQuery();
+
+ static QVariant convertToC(QVariant value, QVariant::Type colType);
+ static QVariant convertToDb(QVariant value, QVariant::Type colType);
};
template
-QString OrmRecord::colName(int col)
+OrmRecord::OrmRecord()
{
- return T::sColNames.at(col);
+ QSqlRecord::operator=(T::sColumns);
}
template
-QVariant OrmRecord::value(int col) const
+T OrmRecord::hydrate(const QSqlRecord& record)
{
- Q_ASSERT(col >= 0 && col < T::sColNames.count());
+ T object;
+ object.QSqlRecord::operator=(record);
+ return object;
+}
- return QSqlRecord::value(T::sColNames.at(col));
+// updates specified column 'col'
+// 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)
+{
+ QSqlQuery query;
+ query.prepare(QString(updateQuery() + "SET %1 = :col WHERE id = :id").arg(col));
+ if(value.isValid()) // take 'col' value from the method's arguments
+ query.bindValue(":col", 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.exec();
}
template
-void OrmRecord::setValue(int col, QVariant value)
+QVariant OrmRecord::value(QString col) const
{
- Q_ASSERT(col >= 0 && col < T::sColNames.count());
+ return convertToC(QSqlRecord::value(col), T::sColumns.field(col).type());
+}
+
+template
+void OrmRecord::setValue(QString col, QVariant value)
+{
+ QSqlRecord::setValue(col, convertToDb(value, T::sColumns.field(col).type()));
+}
+
+template
+T OrmRecord::loadOne(QSqlQuery query)
+{
+ if (!query.isActive())
+ {
+ if (!query.exec())
+ {
+ throw OrmSqlException(query.lastError().text());
+ }
+ }
- QString fieldName = T::sColNames.at(col);
+ if (!query.next())
+ {
+ throw OrmNoObjectException();
+ }
+
+ return hydrate(query.record());
+}
- if (!contains(fieldName))
+template
+QList OrmRecord::load(QSqlQuery query)
+{
+ if (!query.isActive())
{
- append(QSqlField(fieldName, value.type()));
+ if (!query.exec())
+ {
+ qDebug() << "Error: " << query.lastError().driverText() << "; Type: " << query.lastError().type();
+ throw OrmSqlException(query.lastError().text());
+ }
}
- QSqlRecord::setValue(fieldName, value);
+ QList objects;
+ while (query.next())
+ {
+ objects << hydrate(query.record());
+ }
+ return objects;
}
+
+template
+QString OrmRecord::columnsForSelect(const QString& prefix)
+{
+ QStringList prefixedColumns;
+ for (int i=0; i
+QString OrmRecord::selectQuery()
+{
+ return QString("SELECT %1 FROM %2 ").arg(columnsForSelect(), T::sTableName);
+}
+
+template
+QString OrmRecord::updateQuery()
+{
+ return QString("UPDATE %1 ").arg(T::sTableName);
+}
+
+template
+QSqlRecord OrmRecord::toRecord(const QList & columnList)
+{
+ QSqlRecord record;
+ for(int i=0; i< columnList.count(); i++)
+ {
+ record.append(columnList[i]);
+ }
+ return record;
+}
+
+template
+QVariant OrmRecord::convertToC(QVariant value, QVariant::Type colType)
+{
+ if (colType == QVariant::DateTime && value.canConvert())
+ {
+ QDateTime date;
+ date.setTimeSpec(Qt::UTC);
+ date.setTime_t(value.toUInt());
+ return date;
+ }
+
+ return value;
+}
+
+template
+QVariant OrmRecord::convertToDb(QVariant value, QVariant::Type colType)
+{
+ if (colType == QVariant::DateTime && value.canConvert())
+ {
+ QDateTime dateTime = value.toDateTime();
+ dateTime.setTimeSpec(Qt::UTC); // this is to avoid that dateTime.toTime_t changes the time depending on the local time zone
+ return dateTime.toTime_t();
+ }
+
+ return value;
+}
+
+#endif // ORMRECORD_H
+