2 * Copyright (C) 2010 Ixonos Plc.
4 * This file is part of fosdem-schedule.
6 * fosdem-schedule is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation, either version 2 of the License, or (at your option)
11 * fosdem-schedule is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along with
17 * fosdem-schedule. If not, see <http://www.gnu.org/licenses/>.
26 #include <QStringList>
33 OrmException(const QString& text) : mText(text) {}
34 virtual ~OrmException(){}
35 virtual const QString& text() const { return mText; }
40 class OrmNoObjectException : public OrmException
43 OrmNoObjectException() : OrmException("No object exception"){}
44 ~OrmNoObjectException(){}
47 class OrmSqlException : public OrmException
50 OrmSqlException(const QString& text) : OrmException( QString("Sql error: ") + text ) {}
55 class OrmRecord : protected QSqlRecord
59 static T hydrate(const QSqlRecord& record);
60 void update(QString col, QVariant value = QVariant()); // updates specified column 'col'
63 QVariant value(QString col) const;
64 void setValue(QString col, QVariant value);
66 static T loadOne(QSqlQuery query);
67 static QList<T> load(QSqlQuery query);
70 static QSqlRecord toRecord(const QList<QSqlField> & columnList);
71 // all record items/columns are in one table
72 static QString columnsForSelect(const QString& prefix = QString());
73 static QString selectQuery();
74 static QString updateQuery();
76 static QVariant convertToC(QVariant value, QVariant::Type colType);
77 static QVariant convertToDb(QVariant value, QVariant::Type colType);
81 OrmRecord<T>::OrmRecord()
83 QSqlRecord::operator=(T::sColumns);
87 T OrmRecord<T>::hydrate(const QSqlRecord& record)
90 object.QSqlRecord::operator=(record);
94 // updates specified column 'col'
95 // if the value is not specified as an argument,
96 // it's taken from the reford itself
97 // see also: setValue() method for more details
99 void OrmRecord<T>::update(QString col, QVariant value)
102 query.prepare(QString(updateQuery() + "SET %1 = :col WHERE id = :id").arg(col));
103 if(value.isValid()) // take 'col' value from the method's arguments
104 query.bindValue(":col", value);
105 else // take 'col' value from the record; see setValue()
106 query.bindValue(":col", convertToDb(this->value(col), this->value(col).type()));
107 query.bindValue(":id", this->value("id"));
108 //query.bindValue(":id", convertToDb(value("id"), QVariant::Int));
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());
155 /*qDebug() << "SQL OK";*/
162 objects << hydrate(query.record());
164 /*qDebug() << "Fetch done";*/
168 template <typename T>
169 QString OrmRecord<T>::columnsForSelect(const QString& prefix)
171 QStringList prefixedColumns;
172 for (int i=0; i<T::sColumns.count(); i++)
174 prefixedColumns.append(prefix.isEmpty() ?
175 T::sColumns.field(i).name() :
176 QString("%1.%2").arg(prefix, T::sColumns.field(i).name()));
178 return prefixedColumns.join(",");
181 template <typename T>
182 QString OrmRecord<T>::selectQuery()
184 return QString("SELECT %1 FROM %2 ").arg(columnsForSelect(), T::sTableName);
187 template <typename T>
188 QString OrmRecord<T>::updateQuery()
190 return QString("UPDATE %1 ").arg(T::sTableName);
193 template <typename T>
194 QSqlRecord OrmRecord<T>::toRecord(const QList<QSqlField> & columnList)
197 for(int i=0; i< columnList.count(); i++)
199 record.append(columnList[i]);
204 template <typename T>
205 QVariant OrmRecord<T>::convertToC(QVariant value, QVariant::Type colType)
207 if (colType == QVariant::DateTime && value.canConvert<uint>())
210 date.setTimeSpec(Qt::UTC);
211 date.setTime_t(value.toUInt());
218 template <typename T>
219 QVariant OrmRecord<T>::convertToDb(QVariant value, QVariant::Type colType)
221 if (colType == QVariant::DateTime && value.canConvert<QDateTime>())
223 return value.toDateTime().toTime_t();
229 #endif // ORMRECORD_H