[20a6010] | 1 | #ifndef ORMRECORD_H |
---|
| 2 | #define ORMRECORD_H |
---|
| 3 | |
---|
| 4 | #include <QSqlQuery> |
---|
[5a73d27] | 5 | #include <QSqlRecord> |
---|
| 6 | #include <QSqlField> |
---|
[20a6010] | 7 | #include <QSqlError> |
---|
| 8 | #include <QStringList> |
---|
| 9 | #include <QDateTime> |
---|
| 10 | #include <QDebug> |
---|
| 11 | |
---|
[c790268] | 12 | // INFO: |
---|
| 13 | // all record items/columns may be defined in one table (1.), or |
---|
| 14 | // they can be splitted in two separate tables (2.) (eg. for FTS support) |
---|
| 15 | // 1.) you have to define "static QString const sTableName" |
---|
| 16 | // 2.) you have to define two table names: |
---|
| 17 | // "static QString const sTable1Name" |
---|
| 18 | // "static QString const sTable2Name" |
---|
| 19 | // and since all record items/columns are handled by one QSqlRecord, |
---|
| 20 | // you have to also define number of columns that belongs to table 1 (1.), and table 2 (2.) |
---|
| 21 | // 1.) "static int const sTable1ColCount" |
---|
| 22 | // 2.) "static int const sTable2ColCount" |
---|
| 23 | // there are also defined auxiliary methods for 1-Table/2-Tables approach, see bellow |
---|
| 24 | |
---|
[20a6010] | 25 | class OrmException |
---|
| 26 | { |
---|
| 27 | }; |
---|
| 28 | |
---|
| 29 | class OrmNoObjectException : OrmException |
---|
| 30 | { |
---|
| 31 | }; |
---|
| 32 | |
---|
| 33 | class OrmSqlException : OrmException |
---|
| 34 | { |
---|
| 35 | public: |
---|
| 36 | OrmSqlException(const QString& text) : mText(text) {} |
---|
| 37 | QString text() const { return mText; } |
---|
| 38 | |
---|
| 39 | private: |
---|
| 40 | QString mText; |
---|
| 41 | }; |
---|
[5a73d27] | 42 | |
---|
| 43 | template <typename T> |
---|
| 44 | class OrmRecord : protected QSqlRecord |
---|
| 45 | { |
---|
| 46 | public: |
---|
[20a6010] | 47 | OrmRecord(); |
---|
| 48 | static T hydrate(const QSqlRecord& record); |
---|
[680a4da] | 49 | void update(QString col, QVariant value = QVariant()); // updates specified column 'col' |
---|
[5a73d27] | 50 | |
---|
| 51 | protected: |
---|
[20a6010] | 52 | QVariant value(QString col) const; |
---|
| 53 | void setValue(QString col, QVariant value); |
---|
| 54 | |
---|
| 55 | static T loadOne(QSqlQuery query); |
---|
| 56 | static QList<T> load(QSqlQuery query); |
---|
| 57 | |
---|
| 58 | // auxiliary methods |
---|
[c790268] | 59 | static QSqlRecord toRecord(const QList<QSqlField> & columnList); |
---|
| 60 | // all record items/columns are in one table |
---|
[20a6010] | 61 | static QString columnsForSelect(const QString& prefix = QString()); |
---|
| 62 | static QString selectQuery(); |
---|
[680a4da] | 63 | static QString updateQuery(); |
---|
[c790268] | 64 | // record items/columns are stored in two tables |
---|
| 65 | static QString columnsForSelectJoin2T(); // for joining two tables |
---|
| 66 | static QString selectQueryJoin2T(const QString &key); // for joining two tables |
---|
[20a6010] | 67 | |
---|
| 68 | static QVariant convertToC(QVariant value, QVariant::Type colType); |
---|
| 69 | static QVariant convertToDb(QVariant value, QVariant::Type colType); |
---|
[5a73d27] | 70 | }; |
---|
| 71 | |
---|
| 72 | template <typename T> |
---|
[20a6010] | 73 | OrmRecord<T>::OrmRecord() |
---|
| 74 | { |
---|
| 75 | QSqlRecord::operator=(T::sColumns); |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | template <typename T> |
---|
| 79 | T OrmRecord<T>::hydrate(const QSqlRecord& record) |
---|
| 80 | { |
---|
| 81 | T object; |
---|
| 82 | object.QSqlRecord::operator=(record); |
---|
| 83 | return object; |
---|
| 84 | } |
---|
| 85 | |
---|
[680a4da] | 86 | // updates specified column 'col' |
---|
| 87 | // if the value is not specified as an argument, |
---|
| 88 | // it's taken from the reford itself |
---|
| 89 | // see also: setValue() method for more details |
---|
| 90 | template <typename T> |
---|
| 91 | void OrmRecord<T>::update(QString col, QVariant value) |
---|
| 92 | { |
---|
| 93 | QSqlQuery query; |
---|
| 94 | query.prepare(QString(updateQuery() + "SET %1 = :col WHERE id = :id").arg(col)); |
---|
| 95 | if(value.isValid()) // take 'col' value from the method's arguments |
---|
| 96 | query.bindValue(":col", value); |
---|
| 97 | else // take 'col' value from the record; see setValue() |
---|
| 98 | query.bindValue(":col", convertToDb(this->value(col), this->value(col).type())); |
---|
| 99 | query.bindValue(":id", this->value("id")); |
---|
| 100 | //query.bindValue(":id", convertToDb(value("id"), QVariant::Int)); |
---|
| 101 | query.exec(); |
---|
| 102 | } |
---|
| 103 | |
---|
[20a6010] | 104 | template <typename T> |
---|
| 105 | QVariant OrmRecord<T>::value(QString col) const |
---|
| 106 | { |
---|
| 107 | return convertToC(QSqlRecord::value(col), T::sColumns.field(col).type()); |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | template <typename T> |
---|
| 111 | void OrmRecord<T>::setValue(QString col, QVariant value) |
---|
| 112 | { |
---|
| 113 | QSqlRecord::setValue(col, convertToDb(value, T::sColumns.field(col).type())); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | template <typename T> |
---|
| 117 | T OrmRecord<T>::loadOne(QSqlQuery query) |
---|
| 118 | { |
---|
| 119 | if (!query.isActive()) |
---|
| 120 | { |
---|
| 121 | if (!query.exec()) |
---|
| 122 | { |
---|
| 123 | throw new OrmSqlException(query.lastError().text()); |
---|
| 124 | } |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | if (!query.next()) |
---|
| 128 | { |
---|
| 129 | throw new OrmNoObjectException(); |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | return hydrate(query.record()); |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | template <typename T> |
---|
| 136 | QList<T> OrmRecord<T>::load(QSqlQuery query) |
---|
| 137 | { |
---|
| 138 | if (!query.isActive()) |
---|
| 139 | { |
---|
| 140 | if (!query.exec()) |
---|
| 141 | { |
---|
[fbc1646] | 142 | qDebug() << "Error: " << query.lastError().driverText() << "; Type: " << query.lastError().type(); |
---|
[20a6010] | 143 | throw new OrmSqlException(query.lastError().text()); |
---|
| 144 | } |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | QList<T> objects; |
---|
| 148 | while (query.next()) |
---|
| 149 | { |
---|
| 150 | objects << hydrate(query.record()); |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | return objects; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | template <typename T> |
---|
| 157 | QString OrmRecord<T>::columnsForSelect(const QString& prefix) |
---|
[5a73d27] | 158 | { |
---|
[20a6010] | 159 | QStringList prefixedColumns; |
---|
| 160 | for (int i=0; i<T::sColumns.count(); i++) |
---|
| 161 | { |
---|
| 162 | prefixedColumns.append(prefix.isEmpty() ? |
---|
| 163 | T::sColumns.field(i).name() : |
---|
| 164 | QString("%1.%2").arg(prefix, T::sColumns.field(i).name())); |
---|
| 165 | } |
---|
| 166 | return prefixedColumns.join(","); |
---|
[5a73d27] | 167 | } |
---|
| 168 | |
---|
[c790268] | 169 | template <typename T> |
---|
| 170 | QString OrmRecord<T>::columnsForSelectJoin2T() |
---|
| 171 | { |
---|
| 172 | Q_ASSERT((T::sTable1ColCount+T::sTable2ColCount) == T::sColumns.count()); |
---|
| 173 | |
---|
| 174 | QStringList prefixedColumns; |
---|
| 175 | for (int i=0; i<T::sTable1ColCount; i++) |
---|
| 176 | { |
---|
| 177 | prefixedColumns.append(QString("%1.%2").arg(T::sTable1Name, T::sColumns.field(i).name())); |
---|
| 178 | } |
---|
| 179 | for (int j=0; j<T::sTable2ColCount; j++) |
---|
| 180 | { |
---|
| 181 | prefixedColumns.append(QString("%1.%2").arg(T::sTable2Name, T::sColumns.field(T::sTable1ColCount+j).name())); |
---|
| 182 | } |
---|
| 183 | return prefixedColumns.join(","); |
---|
| 184 | } |
---|
| 185 | |
---|
[5a73d27] | 186 | template <typename T> |
---|
[20a6010] | 187 | QString OrmRecord<T>::selectQuery() |
---|
[5a73d27] | 188 | { |
---|
[20a6010] | 189 | return QString("SELECT %1 FROM %2 ").arg(columnsForSelect(), T::sTableName); |
---|
| 190 | } |
---|
[5a73d27] | 191 | |
---|
[27102d5] | 192 | |
---|
[c790268] | 193 | template <typename T> |
---|
| 194 | QString OrmRecord<T>::selectQueryJoin2T(const QString &key) |
---|
| 195 | { |
---|
| 196 | return QString("SELECT %1 FROM %2 INNER JOIN %3 ON %4.%5 = %6.%7 ").arg( |
---|
| 197 | columnsForSelectJoin2T(), |
---|
| 198 | T::sTable1Name, |
---|
| 199 | T::sTable2Name, |
---|
| 200 | T::sTable1Name, |
---|
| 201 | key, |
---|
| 202 | T::sTable2Name, |
---|
| 203 | key); |
---|
| 204 | } |
---|
| 205 | |
---|
[680a4da] | 206 | template <typename T> |
---|
| 207 | QString OrmRecord<T>::updateQuery() |
---|
| 208 | { |
---|
[c790268] | 209 | return QString("UPDATE %1 ").arg(T::sTable1Name); |
---|
[680a4da] | 210 | } |
---|
| 211 | |
---|
[20a6010] | 212 | template <typename T> |
---|
| 213 | QSqlRecord OrmRecord<T>::toRecord(const QList<QSqlField> & columnList) |
---|
| 214 | { |
---|
| 215 | QSqlRecord record; |
---|
| 216 | foreach (const QSqlField & col, columnList) |
---|
| 217 | { |
---|
| 218 | record.append(col); |
---|
| 219 | } |
---|
| 220 | return record; |
---|
[5a73d27] | 221 | } |
---|
| 222 | |
---|
| 223 | template <typename T> |
---|
[20a6010] | 224 | QVariant OrmRecord<T>::convertToC(QVariant value, QVariant::Type colType) |
---|
[5a73d27] | 225 | { |
---|
[489f262] | 226 | if (colType == QVariant::DateTime && value.canConvert<uint>()) |
---|
[20a6010] | 227 | { |
---|
| 228 | QDateTime date; |
---|
[489f262] | 229 | date.setTimeSpec(Qt::UTC); |
---|
[20a6010] | 230 | date.setTime_t(value.toUInt()); |
---|
| 231 | return date; |
---|
| 232 | } |
---|
[5a73d27] | 233 | |
---|
[20a6010] | 234 | return value; |
---|
| 235 | } |
---|
[5a73d27] | 236 | |
---|
[20a6010] | 237 | template <typename T> |
---|
| 238 | QVariant OrmRecord<T>::convertToDb(QVariant value, QVariant::Type colType) |
---|
| 239 | { |
---|
[d0d0a66] | 240 | if (colType == QVariant::DateTime && value.canConvert<QDateTime>()) |
---|
[5a73d27] | 241 | { |
---|
[20a6010] | 242 | return value.toDateTime().toTime_t(); |
---|
[5a73d27] | 243 | } |
---|
| 244 | |
---|
[20a6010] | 245 | return value; |
---|
[5a73d27] | 246 | } |
---|
[20a6010] | 247 | |
---|
| 248 | #endif // ORMRECORD_H |
---|
[c790268] | 249 | |
---|