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 <typename T>
public:
OrmRecord();
static T hydrate(const QSqlRecord& record);
+ void update(QString col, QVariant value = QVariant()); // updates specified column 'col'
protected:
QVariant value(QString col) const;
static QList<T> load(QSqlQuery query);
// auxiliary methods
+ static QSqlRecord toRecord(const QList<QSqlField> & columnList);
+ // all record items/columns are in one table
static QString columnsForSelect(const QString& prefix = QString());
static QString selectQuery();
- static QSqlRecord toRecord(const QList<QSqlField> & columnList);
+ static QString updateQuery();
static QVariant convertToC(QVariant value, QVariant::Type colType);
static QVariant convertToDb(QVariant value, QVariant::Type colType);
return object;
}
+// updates specified column 'col'
+// if the value is not specified as an argument,
+// it's taken from the reford itself
+// see also: setValue() method for more details
+template <typename T>
+void OrmRecord<T>::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.bindValue(":id", convertToDb(value("id"), QVariant::Int));
+ query.exec();
+}
+
template <typename T>
QVariant OrmRecord<T>::value(QString col) const
{
{
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());
{
if (!query.exec())
{
- throw new OrmSqlException(query.lastError().text());
+ qDebug() << "Error: " << query.lastError().driverText() << "; Type: " << query.lastError().type();
+ throw OrmSqlException(query.lastError().text());
+ }
+ else
+ {
+ qDebug() << "SQL OK";
}
}
{
objects << hydrate(query.record());
}
-
+ qDebug() << "Fetch done";
return objects;
}
return QString("SELECT %1 FROM %2 ").arg(columnsForSelect(), T::sTableName);
}
+template <typename T>
+QString OrmRecord<T>::updateQuery()
+{
+ return QString("UPDATE %1 ").arg(T::sTableName);
+}
+
template <typename T>
QSqlRecord OrmRecord<T>::toRecord(const QList<QSqlField> & columnList)
{
template <typename T>
QVariant OrmRecord<T>::convertToC(QVariant value, QVariant::Type colType)
{
- if (colType == QVariant::DateTime &&
- (value.type() == QVariant::UInt || value.type() == QVariant::Int))
+ if (colType == QVariant::DateTime && value.canConvert<uint>())
{
QDateTime date;
+ date.setTimeSpec(Qt::UTC);
date.setTime_t(value.toUInt());
return date;
}
template <typename T>
QVariant OrmRecord<T>::convertToDb(QVariant value, QVariant::Type colType)
{
- if (colType == QVariant::DateTime && value.type() == QVariant::DateTime)
+ if (colType == QVariant::DateTime && value.canConvert<QDateTime>())
{
return value.toDateTime().toTime_t();
}
}
#endif // ORMRECORD_H
+