working on it
This commit is contained in:
parent
74b2a4524a
commit
f696ffc5c4
|
|
@ -1 +1 @@
|
||||||
Subproject commit b3c550c5bb7c570b1b10492fcedf287c1915af39
|
Subproject commit f9d81fcc6244271b3926891b0c86554e1e6b967e
|
||||||
|
|
@ -83,30 +83,40 @@ public: \
|
||||||
}
|
}
|
||||||
|
|
||||||
#define NUT_FOREIGN_KEY(type, keytype, name, read, write) \
|
#define NUT_FOREIGN_KEY(type, keytype, name, read, write) \
|
||||||
Q_PROPERTY(Nut::Row<type> name READ read WRITE write) \
|
|
||||||
/*NUT_DECLARE_FIELD(keytype, name##Id, read##Id, write##Id)*/ \
|
|
||||||
NUT_INFO(__nut_FOREIGN_KEY, name, type) \
|
|
||||||
Nut::ForeignKeyData<type, keytype> m_##name; \
|
|
||||||
public: \
|
|
||||||
Nut::Row<type> read() const { return m_##name.table() ; } \
|
|
||||||
void write(Nut::Row<type> name){ \
|
|
||||||
m_##name.setTable(name); \
|
|
||||||
}\
|
|
||||||
\
|
|
||||||
Q_PROPERTY(keytype name##Id READ read##Id WRITE write##Id) \
|
|
||||||
NUT_INFO(__nut_FIELD, name##Id, 0) \
|
NUT_INFO(__nut_FIELD, name##Id, 0) \
|
||||||
|
NUT_INFO(__nut_FOREIGN_KEY, name, type) \
|
||||||
|
Nut::Row<type> m_##name; \
|
||||||
|
keytype m_##name##Id; \
|
||||||
|
Q_PROPERTY(keytype name##Id READ read##Id WRITE write##Id) \
|
||||||
public: \
|
public: \
|
||||||
|
Nut::Row<type> read() const; \
|
||||||
|
void write(Nut::Row<type> name); \
|
||||||
static NUT_WRAP_NAMESPACE(FieldPhrase<keytype>)& name##Id ## Field(){ \
|
static NUT_WRAP_NAMESPACE(FieldPhrase<keytype>)& name##Id ## Field(){ \
|
||||||
static NUT_WRAP_NAMESPACE(FieldPhrase<keytype>) f = \
|
static NUT_WRAP_NAMESPACE(FieldPhrase<keytype>) f = \
|
||||||
NUT_WRAP_NAMESPACE(FieldPhrase<keytype>) \
|
NUT_WRAP_NAMESPACE(FieldPhrase<keytype>) \
|
||||||
(staticMetaObject.className(), #name); \
|
(staticMetaObject.className(), #name); \
|
||||||
return f; \
|
return f; \
|
||||||
} \
|
} \
|
||||||
keytype read##Id() const{ \
|
keytype read##Id() const; \
|
||||||
return m_##name.value(); \
|
void write##Id(keytype name##Id);
|
||||||
|
|
||||||
|
#define NUT_FOREIGN_KEY_IMPLEMENT(class, type, keytype, name, read, write) \
|
||||||
|
\
|
||||||
|
Nut::Row<type> class::read() const { return m_##name ; } \
|
||||||
|
void class::write(Nut::Row<type> name){ \
|
||||||
|
propertyChanged(QT_STRINGIFY2(name##Id)); \
|
||||||
|
m_##name = name; \
|
||||||
|
m_##name##Id = name->primaryValue().value<keytype>(); \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
keytype class::read##Id() const{ \
|
||||||
|
if (m_##name) \
|
||||||
|
return m_##name->primaryValue().value<keytype>(); \
|
||||||
|
return m_##name##Id; \
|
||||||
} \
|
} \
|
||||||
void write##Id(keytype name##Id){ \
|
void class::write##Id(keytype name##Id){ \
|
||||||
m_##name.setValue(name##Id); \
|
m_##name##Id = name##Id; \
|
||||||
|
m_##name = nullptr; \
|
||||||
propertyChanged(QT_STRINGIFY2(name##Id)); \
|
propertyChanged(QT_STRINGIFY2(name##Id)); \
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -129,12 +139,14 @@ public: \
|
||||||
|
|
||||||
#define NUT_FIELD(name) NUT_INFO(__nut_FIELD, name, 0)
|
#define NUT_FIELD(name) NUT_INFO(__nut_FIELD, name, 0)
|
||||||
#define NUT_PRIMARY_KEY(x) NUT_INFO(__nut_PRIMARY_KEY, x, 0) \
|
#define NUT_PRIMARY_KEY(x) NUT_INFO(__nut_PRIMARY_KEY, x, 0) \
|
||||||
|
public: \
|
||||||
QVariant primaryValue() const override { \
|
QVariant primaryValue() const override { \
|
||||||
return property(#x); \
|
return property(#x); \
|
||||||
} \
|
} \
|
||||||
void setPrimaryValue(const QVariant &value) override { \
|
void setPrimaryValue(const QVariant &value) override { \
|
||||||
setProperty(#x, value); \
|
setProperty(#x, value); \
|
||||||
}
|
} \
|
||||||
|
private:
|
||||||
|
|
||||||
|
|
||||||
#define NUT_AUTO_INCREMENT(x) NUT_INFO(__nut_AUTO_INCREMENT, x, 0)
|
#define NUT_AUTO_INCREMENT(x) NUT_INFO(__nut_AUTO_INCREMENT, x, 0)
|
||||||
|
|
@ -277,31 +289,31 @@ inline T *get(const QSharedPointer<T> row) {
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
template<class C, typename T>
|
//template<class C, typename T>
|
||||||
struct ForeignKeyData {
|
//struct ForeignKeyData {
|
||||||
Nut::Row<C> _table;
|
// Nut::Row<C> _table;
|
||||||
T _id;
|
// T _id;
|
||||||
|
|
||||||
ForeignKeyData() : _table(nullptr)
|
// ForeignKeyData() : _table(nullptr)
|
||||||
{}
|
// {}
|
||||||
|
|
||||||
void setTable(Nut::Row<C> t) {
|
// void setTable(Nut::Row<C> t) {
|
||||||
_table = t;
|
// _table = t;
|
||||||
_id = t->primaryValue().value<T>();
|
// _id = t->primaryValue().value<T>();
|
||||||
}
|
// }
|
||||||
Nut::Row<C> table() const {
|
// Nut::Row<C> table() const {
|
||||||
return _table;
|
// return _table;
|
||||||
}
|
// }
|
||||||
void setValue(const T& val) {
|
// void setValue(const T& val) {
|
||||||
_table = nullptr;
|
// _table = nullptr;
|
||||||
_id = val;
|
// _id = val;
|
||||||
}
|
// }
|
||||||
T value() const {
|
// T value() const {
|
||||||
if (_table)
|
// if (_table)
|
||||||
return _table->primaryValue().value<T>();
|
// return _table->primaryValue().value<T>();
|
||||||
return _id;
|
// return _id;
|
||||||
}
|
// }
|
||||||
};
|
//};
|
||||||
|
|
||||||
NUT_END_NAMESPACE
|
NUT_END_NAMESPACE
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,11 @@
|
||||||
#include "comment.h"
|
#include "comment.h"
|
||||||
|
#include "post.h"
|
||||||
|
#include "user.h"
|
||||||
|
|
||||||
Comment::Comment(QObject *parent) : Table(parent)
|
Comment::Comment(QObject *parent) : Table(parent)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
NUT_FOREIGN_KEY_IMPLEMENT(Comment, Post, int, post, post, setPost)
|
||||||
|
NUT_FOREIGN_KEY_IMPLEMENT(Comment, User, int, author, author, setAuthor)
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,11 @@
|
||||||
#include "score.h"
|
#include "score.h"
|
||||||
|
#include "user.h"
|
||||||
|
#include "post.h"
|
||||||
|
|
||||||
Score::Score(QObject *parent) : Nut::Table(parent)
|
Score::Score(QObject *parent) : Nut::Table(parent)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
NUT_FOREIGN_KEY_IMPLEMENT(Score, Post, int, post, post, setPost)
|
||||||
|
NUT_FOREIGN_KEY_IMPLEMENT(Score, User, QUuid, author, author, setAuthor)
|
||||||
|
|
|
||||||
|
|
@ -80,11 +80,11 @@ void BasicTest::createPost()
|
||||||
comment->setAuthorId(user->id());
|
comment->setAuthorId(user->id());
|
||||||
db.comments()->append(comment);
|
db.comments()->append(comment);
|
||||||
}
|
}
|
||||||
for (int i = 0; i < 10; ++i) {
|
// for (int i = 0; i < 10; ++i) {
|
||||||
auto score = Nut::create<Score>();
|
// auto score = Nut::create<Score>();
|
||||||
score->setScore(i % 5);
|
// score->setScore(i % 5);
|
||||||
newPost->scores()->append(score);
|
// newPost->scores()->append(score);
|
||||||
}
|
// }
|
||||||
|
|
||||||
db.saveChanges();
|
db.saveChanges();
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue