2 * Copyright (C) 2010 Ixonos Plc.
3 * Copyright (C) 2011-2013 Philipp Spitzer, gregor herrmann, Stefan Stahl
5 * This file is part of ConfClerk.
7 * ConfClerk is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation, either version 2 of the License, or (at your option)
12 * ConfClerk is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * You should have received a copy of the GNU General Public License along with
18 * ConfClerk. If not, see <http://www.gnu.org/licenses/>.
27 #include <QStringList>
34 OrmException(const QString& text) : mText(text) {}
35 virtual ~OrmException(){}
36 virtual const QString& text() const { return mText; }
41 class OrmNoObjectException : public OrmException
44 OrmNoObjectException() : OrmException("No object exception"){}
45 ~OrmNoObjectException(){}
48 class OrmSqlException : public OrmException
51 OrmSqlException(const QString& text) : OrmException( QString("Sql error: ") + text ) {}
56 class OrmRecord : protected QSqlRecord
60 static T hydrate(const QSqlRecord& record);
61 void update(QString col, QVariant value = QVariant()); // updates specified column 'col'
64 QVariant value(QString col) const;
65 void setValue(QString col, QVariant value);
67 static T loadOne(QSqlQuery query);
68 static QList<T> load(QSqlQuery query);
71 static QSqlRecord toRecord(const QList<QSqlField> & columnList);
72 // all record items/columns are in one table
73 static QString columnsForSelect(const QString& prefix = QString());
74 static QString selectQuery();
75 static QString updateQuery();
77 static QVariant convertToC(QVariant value, QVariant::Type colType);
78 static QVariant convertToDb(QVariant value, QVariant::Type colType);
82 OrmRecord<T>::OrmRecord()
84 QSqlRecord::operator=(T::sColumns);
88 T OrmRecord<T>::hydrate(const QSqlRecord& record)
91 object.QSqlRecord::operator=(record);
95 // updates specified column 'col'
96 // if the value is not specified as an argument,
97 // it's taken from the record itself
98 // see also: setValue() method for more details
100 void OrmRecord<T>::update(QString col, QVariant value)
103 query.prepare(QString(updateQuery() + "SET %1 = :col WHERE id = :id").arg(col));
104 if(value.isValid()) // take 'col' value from the method's arguments
105 query.bindValue(":col", value);
106 else // take 'col' value from the record; see setValue()
107 query.bindValue(":col", convertToDb(this->value(col), this->value(col).type()));
108 query.bindValue(":id", this->value("id"));
112 template <typename T>
113 QVariant OrmRecord<T>::value(QString col) const
115 return convertToC(QSqlRecord::value(col), T::sColumns.field(col).type());
118 template <typename T>
119 void OrmRecord<T>::setValue(QString col, QVariant value)
121 QSqlRecord::setValue(col, convertToDb(value, T::sColumns.field(col).type()));
124 template <typename T>
125 T OrmRecord<T>::loadOne(QSqlQuery query)
127 if (!query.isActive())
131 throw OrmSqlException(query.lastError().text());
137 throw OrmNoObjectException();
140 return hydrate(query.record());
143 template <typename T>
144 QList<T> OrmRecord<T>::load(QSqlQuery query)
146 if (!query.isActive())
150 qDebug() << "Error: " << query.lastError().driverText() << "; Type: " << query.lastError().type();
151 throw OrmSqlException(query.lastError().text());
158 objects << hydrate(query.record());
163 template <typename T>
164 QString OrmRecord<T>::columnsForSelect(const QString& prefix)
166 QStringList prefixedColumns;
167 for (int i=0; i<T::sColumns.count(); i++)
169 prefixedColumns.append(prefix.isEmpty() ?
170 T::sColumns.field(i).name() :
171 QString("%1.%2").arg(prefix, T::sColumns.field(i).name()));
173 return prefixedColumns.join(",");
176 template <typename T>
177 QString OrmRecord<T>::selectQuery()
179 return QString("SELECT %1 FROM %2 ").arg(columnsForSelect(), T::sTableName);
182 template <typename T>
183 QString OrmRecord<T>::updateQuery()
185 return QString("UPDATE %1 ").arg(T::sTableName);
188 template <typename T>
189 QSqlRecord OrmRecord<T>::toRecord(const QList<QSqlField> & columnList)
192 for(int i=0; i< columnList.count(); i++)
194 record.append(columnList[i]);
199 template <typename T>
200 QVariant OrmRecord<T>::convertToC(QVariant value, QVariant::Type colType)
202 if (colType == QVariant::DateTime && value.canConvert<uint>())
205 date.setTimeSpec(Qt::UTC);
206 date.setTime_t(value.toUInt());
213 template <typename T>
214 QVariant OrmRecord<T>::convertToDb(QVariant value, QVariant::Type colType)
216 if (colType == QVariant::DateTime && value.canConvert<QDateTime>())
218 QDateTime dateTime = value.toDateTime();
219 dateTime.setTimeSpec(Qt::UTC); // this is to avoid that dateTime.toTime_t changes the time depending on the local time zone
220 return dateTime.toTime_t();
226 #endif // ORMRECORD_H