diff --git a/3rdparty/serializer b/3rdparty/serializer index f9d81fc..51500a4 160000 --- a/3rdparty/serializer +++ b/3rdparty/serializer @@ -1 +1 @@ -Subproject commit f9d81fcc6244271b3926891b0c86554e1e6b967e +Subproject commit 51500a497933444196942ee0db6628338a0422af diff --git a/src/defines.h b/src/defines.h index 50e3cbe..318f99b 100644 --- a/src/defines.h +++ b/src/defines.h @@ -83,10 +83,22 @@ public: \ } #define NUT_FOREIGN_KEY(type, keytype, name, read, write) \ + Q_PROPERTY(Nut::Row name READ read WRITE write) \ + NUT_DECLARE_FIELD(keytype, name##Id, read##Id, write##Id) \ + NUT_INFO(__nut_FOREIGN_KEY, name, type) \ + Nut::Row m_##name; \ +public: \ + Nut::Row read() const { return m_##name ; } \ + void write(Nut::Row name){ \ + m_##name = name; \ + } + +#define NUT_FOREIGN_KEY_DECLARE(type, keytype, name, read, write) \ NUT_INFO(__nut_FIELD, name##Id, 0) \ NUT_INFO(__nut_FOREIGN_KEY, name, type) \ Nut::Row m_##name; \ keytype m_##name##Id; \ + Q_PROPERTY(Nut::Row name READ read WRITE write) \ Q_PROPERTY(keytype name##Id READ read##Id WRITE write##Id) \ public: \ Nut::Row read() const; \ @@ -115,6 +127,7 @@ public: \ return m_##name##Id; \ } \ void class::write##Id(keytype name##Id){ \ + propertyChanged(QT_STRINGIFY2(name##Id)); \ m_##name##Id = name##Id; \ m_##name = nullptr; \ propertyChanged(QT_STRINGIFY2(name##Id)); \ diff --git a/test/common/comment.h b/test/common/comment.h index 83fd55b..e7628ab 100644 --- a/test/common/comment.h +++ b/test/common/comment.h @@ -21,8 +21,8 @@ class Comment : public Table NUT_DECLARE_FIELD(QDateTime, saveDate, saveDate, setSaveDate) NUT_DECLARE_FIELD(qreal, point, point, setPoint) - NUT_FOREIGN_KEY(Post, int, post, post, setPost) - NUT_FOREIGN_KEY(User, int, author, author, setAuthor) + NUT_FOREIGN_KEY_DECLARE(Post, int, post, post, setPost) + NUT_FOREIGN_KEY_DECLARE(User, int, author, author, setAuthor) public: Q_INVOKABLE explicit Comment(QObject *parentTableSet = nullptr); diff --git a/test/common/score.h b/test/common/score.h index a87fa08..f8c3579 100644 --- a/test/common/score.h +++ b/test/common/score.h @@ -15,8 +15,8 @@ class Score : public Nut::Table NUT_DECLARE_FIELD(int, score, score, setScore) - NUT_FOREIGN_KEY(Post, int, post, post, setPost) - NUT_FOREIGN_KEY(User, QUuid, author, author, setAuthor) + NUT_FOREIGN_KEY_DECLARE(Post, int, post, post, setPost) + NUT_FOREIGN_KEY_DECLARE(User, QUuid, author, author, setAuthor) public: Q_INVOKABLE Score(QObject *parent = Q_NULLPTR); diff --git a/test/tst_basic/tst_basic.cpp b/test/tst_basic/tst_basic.cpp index c512790..55d59c3 100644 --- a/test/tst_basic/tst_basic.cpp +++ b/test/tst_basic/tst_basic.cpp @@ -78,13 +78,13 @@ void BasicTest::createPost() comment->setMessage("comment #" + QString::number(i)); comment->setSaveDate(QDateTime::currentDateTime()); comment->setAuthorId(user->id()); - db.comments()->append(comment); + newPost->comments()->append(comment); + } + for (int i = 0; i < 10; ++i) { + auto score = Nut::create(); + score->setScore(i % 5); + newPost->scores()->append(score); } -// for (int i = 0; i < 10; ++i) { -// auto score = Nut::create(); -// score->setScore(i % 5); -// newPost->scores()->append(score); -// } db.saveChanges();