KanoopDatabaseQt 1.1.1
Qt database abstraction library
Loading...
Searching...
No Matches
queryloadable.h
1/**
2 * QueryLoadable
3 *
4 * A pure abstract class to provide an interface to classes which
5 * are able to load themselves via an active QSqlQuery.
6 *
7 * Stephen Punak, January 11 2025
8 */
9#ifndef QUERYLOADABLE_H
10#define QUERYLOADABLE_H
11#include <QSqlQuery>
12
13/** @brief Pure abstract interface for classes that can populate themselves from an active QSqlQuery. */
15{
16public:
17 /** @brief Load the object's state from the current row of a query result.
18 * @param query The active QSqlQuery positioned at the row to load from.
19 * @return true on success, false on failure.
20 */
21 virtual bool loadFromQuery(const QSqlQuery& query) = 0;
22
23protected:
24 /** @brief Convert a QVariant database value to a UTC QDateTime.
25 * @param value The QVariant containing the datetime value.
26 * @return The corresponding QDateTime in UTC.
27 */
28 static QDateTime utcTime(const QVariant& value);
29
30 /** @brief Convert a single-character string to an enum value by casting the first character.
31 * @param value The string representation of the enum.
32 * @return The enum value, or a default-constructed T if the string is empty.
33 */
34 template <typename T>
35 static T enumFromString(const QString& value)
36 {
37 T result = T{};
38 if(value.length() > 0) {
39 result = static_cast<T>(value.at(0).toLatin1());
40 }
41 return result;
42 }
43};
44
45#endif // QUERYLOADABLE_H
QueryLoadable.
virtual bool loadFromQuery(const QSqlQuery &query)=0
Load the object's state from the current row of a query result.
static QDateTime utcTime(const QVariant &value)
Convert a QVariant database value to a UTC QDateTime.
static T enumFromString(const QString &value)
Convert a single-character string to an enum value by casting the first character.