QEloquent 1.1.0
Qt most flexible ORM.
Loading...
Searching...
No Matches
model.h
1#ifndef QELOQUENT_MODEL_H
2#define QELOQUENT_MODEL_H
3
4#include <QEloquent/global.h>
5#include <QEloquent/entity.h>
6#include <QEloquent/serializable.h>
7#include <QEloquent/deserializable.h>
8#include <QEloquent/result.h>
9#include <QEloquent/relation.h>
10
11#include <QObject>
12#include <QSharedDataPointer>
13
14#include <source_location>
15
16class QSqlRecord;
17class QSqlQuery;
18class QSqlError;
19
20namespace QEloquent {
21
22class MetaObject;
23class Query;
24class Error;
25class Connection;
26class ModelData;
27
28class QELOQUENT_EXPORT Model : public Entity,
29 public Serializable,
30 public Deserializable
31{
32 Q_GADGET
33
34public:
35 Model(const Model &);
36 Model(Model &&);
37 Model &operator=(const Model &);
38 Model &operator=(Model &&);
39 virtual ~Model();
40
41 QVariant primary() const;
42 void setPrimary(const QVariant &value);
43
44 QVariant property(const QString &name) const;
45 void setProperty(const QString &name, const QVariant &value);
46
47 QVariant label() const;
48
49 QVariant field(const QString &name) const;
50 void setField(const QString &name, const QVariant &value);
51
52 bool exists() override final;
53 bool get() override final;
54 bool insert() override final;
55 bool update() override final;
56 bool deleteData() override final;
57
58 bool load(const QString &relation);
59 bool load(const QStringList &relations);
60
61 Query lastQuery() const;
62 Error lastError() const;
63
64 MetaObject metaObject() const;
65 Connection connection() const;
66
67 QString serializationContext() const override final;
68 bool isListSerializable() const override final;
69 QList<DataMap> serialize() const override final;
70 void deserialize(const QList<DataMap> &data, bool all = false) override final;
71
72 DataMap fullDataMap() const;
73
74protected:
75 template<typename T, std::enable_if<std::is_base_of<Model, T>::value>::type* = nullptr> Model(T *self);
76 Model(const QMetaObject &metaObject);
77 Model(ModelData *data);
78
79 QDateTime now() const;
80
81 template<typename T>
82 Relation<T> hasOne(const QString &foreignKey = QString(), const QString &localKey = QString(),
83 const std::source_location &location = std::source_location::current()) const;
84
85 template<typename T>
86 Relation<T> hasMany(const QString &foreignKey = QString(), const QString &localKey = QString(),
87 const std::source_location &location = std::source_location::current()) const;
88
89 template<typename T, typename Through>
90 Relation<T> hasManyThrough(const QString &foreignKey = QString(), const QString &localKey = QString(),
91 const QString &throughForeignKey = QString(), const QString &throughLocalKey = QString(),
92 const std::source_location &location = std::source_location::current()) const;
93
94 template<typename T>
95 Relation<T> belongsTo(const QString &foreignKey = QString(), const QString &ownerKey = QString(),
96 const std::source_location &location = std::source_location::current()) const;
97
98 template<typename T>
99 Relation<T> belongsToMany(const QString &table = QString(), const QString &foreignPivotKey = QString(),
100 const QString &relatedPivotKey = QString(), const QString &parentKey = QString(),
101 const QString &relatedKey = QString(),
102 const std::source_location &location = std::source_location::current()) const;
103
104 template<typename T, typename Through>
105 Relation<T> belongsToManyThrough(const QString &table = QString(), const QString &foreignPivotKey = QString(),
106 const QString &relatedPivotKey = QString(), const QString &parentKey = QString(),
107 const QString &relatedKey = QString(),
108 const std::source_location &location = std::source_location::current()) const;
109
110 QSharedDataPointer<ModelData> data;
111
112private:
113 Query newQuery(const std::function<QString (const Query &)> &statementGenerator, bool filter) const;
114 Result<::QSqlQuery, QSqlError> exec(const std::function<QString (const Query &)> &statementGenerator, bool filter);
115
116 friend class MetaObject;
117 friend class MetaProperty;
118 friend class RelationData;
119};
120
121} // namespace QEloquent
122
123#include "relation_impl.h"
124
125namespace QEloquent {
126
127template<typename T, std::enable_if<std::is_base_of<Model, T>::value>::type*>
128inline Model::Model(T *) : Model(T::staticMetaObject) {}
129
138template<typename T>
139inline Relation<T> Model::hasOne(const QString &foreignKey, const QString &localKey, const std::source_location &location) const
140{
141 return Relation<T>(location, this, [=]() {
142 auto d = new HasOneRelationData<T>();
143 d->foreignKey = foreignKey;
144 d->localKey = localKey;
145 return d;
146 });
147}
148
157template<typename T>
158inline Relation<T> Model::hasMany(const QString &foreignKey, const QString &localKey, const std::source_location &location) const
159{
160 return Relation<T>(location, this, [=]() {
161 auto d = new HasManyRelationData<T>();
162 d->foreignKey = foreignKey;
163 d->localKey = localKey;
164 return d;
165 });
166}
167
177template<typename T, typename Through>
178inline Relation<T> Model::hasManyThrough(const QString &foreignKey, const QString &localKey,
179 const QString &throughForeignKey, const QString &throughLocalKey, const std::source_location &location) const
180{
181 return Relation<T>(location, this, [=]() {
182 auto d = new HasManyThroughRelationData<T, Through>();
183 d->foreignKey = foreignKey;
184 d->localKey = localKey;
185 d->throughForeignKey = throughForeignKey;
186 d->throughLocalKey = throughLocalKey;
187 return d;
188 });
189}
190
199template<typename T>
200inline Relation<T> Model::belongsTo(const QString &foreignKey, const QString &ownerKey, const std::source_location &location) const
201{
202 return Relation<T>(location, this, [=]() {
203 auto d = new BelongsToRelationData<T>();
204 d->foreignKey = foreignKey;
205 d->ownerKey = ownerKey;
206 return d;
207 });
208}
209
219template<typename T>
220inline Relation<T> Model::belongsToMany(const QString &table, const QString &foreignPivotKey,
221 const QString &relatedPivotKey, const QString &parentKey,
222 const QString &relatedKey, const std::source_location &location) const
223{
224 return Relation<T>(location, this, [=]() {
225 auto d = new BelongsToManyRelationData<T>();
226 d->table = table;
227 d->foreignPivotKey = foreignPivotKey;
228 d->relatedPivotKey = relatedPivotKey;
229 d->parentKey = parentKey;
230 d->relatedKey = relatedKey;
231 return d;
232 });
233}
234
244template<typename T, typename Through>
245inline Relation<T> Model::belongsToManyThrough(const QString &table, const QString &foreignPivotKey,
246 const QString &relatedPivotKey, const QString &parentKey,
247 const QString &relatedKey, const std::source_location &location) const
248{
249 return Relation<T>(location, this, [=]() {
250 auto d = new BelongsToManyThroughRelationData<T, Through>();
251 d->table = table;
252 d->foreignPivotKey = foreignPivotKey;
253 d->relatedPivotKey = relatedPivotKey;
254 d->parentKey = parentKey;
255 d->relatedKey = relatedKey;
256 return d;
257 });
258}
259
260} // namespace QEloquent
261
262namespace QEloquent {
263
264class QELOQUENT_EXPORT SimpleModel : public Model
265{
266 Q_GADGET
267 Q_PROPERTY(qint64 id READ id WRITE setId FINAL)
268
269public:
270 template<typename T> SimpleModel(T *m) : Model(m) {}
271 virtual ~SimpleModel() = default;
272
273 qint64 id() const { return m_id; }
274 virtual void setId(qint64 id) { m_id = (id < 0 ? 0 : id); }
275
276protected:
277 qint64 m_id;
278};
279
280}
281
282#endif // QELOQUENT_MODEL_H
Manages database connections and transactions.
Definition connection.h:22
Represents an error in a database operation.
Definition error.h:16
The Model class is the base class for all ORM models.
Definition model.h:31
Relation< T > hasMany(const QString &foreignKey=QString(), const QString &localKey=QString(), const std::source_location &location=std::source_location::current()) const
Defines a one-to-many relationship.
Definition model.h:158
Relation< T > belongsToManyThrough(const QString &table=QString(), const QString &foreignPivotKey=QString(), const QString &relatedPivotKey=QString(), const QString &parentKey=QString(), const QString &relatedKey=QString(), const std::source_location &location=std::source_location::current()) const
Defines a belongs-to-many-through relationship.
Definition model.h:245
Relation< T > hasManyThrough(const QString &foreignKey=QString(), const QString &localKey=QString(), const QString &throughForeignKey=QString(), const QString &throughLocalKey=QString(), const std::source_location &location=std::source_location::current()) const
Defines a has-many-through relationship.
Definition model.h:178
Relation< T > hasOne(const QString &foreignKey=QString(), const QString &localKey=QString(), const std::source_location &location=std::source_location::current()) const
Defines a one-to-one relationship.
Definition model.h:139
Model(const Model &)
Copy constructor.
Definition model.cpp:38
Relation< T > belongsTo(const QString &foreignKey=QString(), const QString &ownerKey=QString(), const std::source_location &location=std::source_location::current()) const
Defines an inverse one-to-one or many-to-one relationship.
Definition model.h:200
Relation< T > belongsToMany(const QString &table=QString(), const QString &foreignPivotKey=QString(), const QString &relatedPivotKey=QString(), const QString &parentKey=QString(), const QString &relatedKey=QString(), const std::source_location &location=std::source_location::current()) const
Defines a many-to-many relationship.
Definition model.h:220
Fluent SQL query builder.
Definition query.h:15
User-facing class for managing model relationships.
Definition relation.h:88