From 74b2a4524ac058f9db043db013e15811a198febe Mon Sep 17 00:00:00 2001 From: Hamed Masafi Date: Sat, 22 Jun 2019 01:32:37 +0430 Subject: [PATCH 1/3] initial --- 3rdparty/serializer | 2 +- ci-test-init.pri | 2 +- src/defines.h | 65 +++++++++++++++++++++++--- src/table.h | 10 +--- test/common/comment.cpp | 3 +- test/tst_generators/tst_generators.cpp | 2 +- 6 files changed, 65 insertions(+), 19 deletions(-) diff --git a/3rdparty/serializer b/3rdparty/serializer index f9d81fc..b3c550c 160000 --- a/3rdparty/serializer +++ b/3rdparty/serializer @@ -1 +1 @@ -Subproject commit f9d81fcc6244271b3926891b0c86554e1e6b967e +Subproject commit b3c550c5bb7c570b1b10492fcedf287c1915af39 diff --git a/ci-test-init.pri b/ci-test-init.pri index a204047..a24cc9d 100644 --- a/ci-test-init.pri +++ b/ci-test-init.pri @@ -1,3 +1,3 @@ #QT -= gui -DEFINES += DOC_PATH=\\\"$$PWD/../../doc\\\" +DEFINES += NUT_PATH=\\\"$$PWD/../../\\\" diff --git a/src/defines.h b/src/defines.h index 144ae10..a547688 100644 --- a/src/defines.h +++ b/src/defines.h @@ -84,15 +84,33 @@ 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_DECLARE_FIELD(keytype, name##Id, read##Id, write##Id)*/ \ NUT_INFO(__nut_FOREIGN_KEY, name, type) \ - Nut::Row m_##name; \ + Nut::ForeignKeyData m_##name; \ public: \ - Nut::Row read() const { return m_##name ; } \ + Nut::Row read() const { return m_##name.table() ; } \ void write(Nut::Row name){ \ - m_##name = name; \ + m_##name.setTable(name); \ + }\ + \ + Q_PROPERTY(keytype name##Id READ read##Id WRITE write##Id) \ + NUT_INFO(__nut_FIELD, name##Id, 0) \ +public: \ + static NUT_WRAP_NAMESPACE(FieldPhrase)& name##Id ## Field(){ \ + static NUT_WRAP_NAMESPACE(FieldPhrase) f = \ + NUT_WRAP_NAMESPACE(FieldPhrase) \ + (staticMetaObject.className(), #name); \ + return f; \ + } \ + keytype read##Id() const{ \ + return m_##name.value(); \ + } \ + void write##Id(keytype name##Id){ \ + m_##name.setValue(name##Id); \ + propertyChanged(QT_STRINGIFY2(name##Id)); \ } + #define NUT_DECLARE_CHILD_TABLE(type, n) \ private: \ NUT_WRAP_NAMESPACE(TableSet) *m_##n; \ @@ -110,9 +128,18 @@ public: \ } #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) \ + QVariant primaryValue() const override { \ + return property(#x); \ + } \ + void setPrimaryValue(const QVariant &value) override { \ + setProperty(#x, value); \ + } + + #define NUT_AUTO_INCREMENT(x) NUT_INFO(__nut_AUTO_INCREMENT, x, 0) -#define NUT_PRIMARY_AUTO_INCREMENT(x) NUT_INFO(__nut_PRIMARY_KEY_AI, x, 0) +#define NUT_PRIMARY_AUTO_INCREMENT(x) NUT_INFO(__nut_PRIMARY_KEY_AI, x, 0)\ + NUT_PRIMARY_KEY(X) NUT_AUTO_INCREMENT(X) #define NUT_DISPLAY_NAME(field, name) NUT_INFO(__nut_DISPLAY, field, name) #define NUT_UNIQUE(x) NUT_INFO(__nut_UNIQUE, x, 0) #define NUT_LEN(field, len) NUT_INFO(__nut_LEN, field, len) @@ -250,6 +277,32 @@ inline T *get(const QSharedPointer row) { #endif +template +struct ForeignKeyData { + Nut::Row _table; + T _id; + + ForeignKeyData() : _table(nullptr) + {} + + void setTable(Nut::Row t) { + _table = t; + _id = t->primaryValue().value(); + } + Nut::Row table() const { + return _table; + } + void setValue(const T& val) { + _table = nullptr; + _id = val; + } + T value() const { + if (_table) + return _table->primaryValue().value(); + return _id; + } +}; + NUT_END_NAMESPACE #endif // SYNTAX_DEFINES_H diff --git a/src/table.h b/src/table.h index d661784..16dfbd3 100644 --- a/src/table.h +++ b/src/table.h @@ -55,14 +55,8 @@ public: int save(Database *db); -// Q_DECL_DEPRECATED -// QString primaryKey() const; - -// Q_DECL_DEPRECATED -// bool isPrimaryKeyAutoIncrement() const; - -// Q_DECL_DEPRECATED -// QVariant primaryValue() const; + virtual QVariant primaryValue() const = 0; + virtual void setPrimaryValue(const QVariant &value) = 0; Status status() const; void setStatus(const Status &status); diff --git a/test/common/comment.cpp b/test/common/comment.cpp index d732145..87ec017 100644 --- a/test/common/comment.cpp +++ b/test/common/comment.cpp @@ -1,7 +1,6 @@ #include "comment.h" -Comment::Comment(QObject *parent) : Table(parent), - m_author(Q_NULLPTR), m_post(Q_NULLPTR) +Comment::Comment(QObject *parent) : Table(parent) { } diff --git a/test/tst_generators/tst_generators.cpp b/test/tst_generators/tst_generators.cpp index 140a6e1..8eb4d5f 100644 --- a/test/tst_generators/tst_generators.cpp +++ b/test/tst_generators/tst_generators.cpp @@ -140,7 +140,7 @@ void GeneratorsTest::cleanupTestCase() i.value().psql, i.value().mssql)); } - QFile file(DOC_PATH "/datatypes.md"); + QFile file(NUT_PATH "/doc/datatypes.md"); if (file.open(QIODevice::WriteOnly)) { file.write(p.toUtf8()); file.close(); From f696ffc5c4befcd89f2fe9c05b6e67f6bb38e475 Mon Sep 17 00:00:00 2001 From: Hamed Masafi Date: Sat, 22 Jun 2019 09:49:06 +0430 Subject: [PATCH 2/3] working on it --- 3rdparty/serializer | 2 +- src/defines.h | 90 ++++++++++++++++++++---------------- test/common/comment.cpp | 5 ++ test/common/score.cpp | 5 ++ test/tst_basic/tst_basic.cpp | 10 ++-- 5 files changed, 67 insertions(+), 45 deletions(-) diff --git a/3rdparty/serializer b/3rdparty/serializer index b3c550c..f9d81fc 160000 --- a/3rdparty/serializer +++ b/3rdparty/serializer @@ -1 +1 @@ -Subproject commit b3c550c5bb7c570b1b10492fcedf287c1915af39 +Subproject commit f9d81fcc6244271b3926891b0c86554e1e6b967e diff --git a/src/defines.h b/src/defines.h index a547688..50e3cbe 100644 --- a/src/defines.h +++ b/src/defines.h @@ -83,30 +83,40 @@ 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::ForeignKeyData m_##name; \ -public: \ - Nut::Row read() const { return m_##name.table() ; } \ - void write(Nut::Row 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_FOREIGN_KEY, name, type) \ + Nut::Row m_##name; \ + keytype m_##name##Id; \ + Q_PROPERTY(keytype name##Id READ read##Id WRITE write##Id) \ public: \ + Nut::Row read() const; \ + void write(Nut::Row name); \ static NUT_WRAP_NAMESPACE(FieldPhrase)& name##Id ## Field(){ \ static NUT_WRAP_NAMESPACE(FieldPhrase) f = \ NUT_WRAP_NAMESPACE(FieldPhrase) \ (staticMetaObject.className(), #name); \ return f; \ } \ - keytype read##Id() const{ \ - return m_##name.value(); \ + keytype read##Id() const; \ + void write##Id(keytype name##Id); + +#define NUT_FOREIGN_KEY_IMPLEMENT(class, type, keytype, name, read, write) \ + \ + Nut::Row class::read() const { return m_##name ; } \ + void class::write(Nut::Row name){ \ + propertyChanged(QT_STRINGIFY2(name##Id)); \ + m_##name = name; \ + m_##name##Id = name->primaryValue().value(); \ + } \ + \ + keytype class::read##Id() const{ \ + if (m_##name) \ + return m_##name->primaryValue().value(); \ + return m_##name##Id; \ } \ - void write##Id(keytype name##Id){ \ - m_##name.setValue(name##Id); \ + void class::write##Id(keytype name##Id){ \ + m_##name##Id = name##Id; \ + m_##name = nullptr; \ propertyChanged(QT_STRINGIFY2(name##Id)); \ } @@ -129,12 +139,14 @@ public: \ #define NUT_FIELD(name) NUT_INFO(__nut_FIELD, name, 0) #define NUT_PRIMARY_KEY(x) NUT_INFO(__nut_PRIMARY_KEY, x, 0) \ + public: \ QVariant primaryValue() const override { \ return property(#x); \ } \ void setPrimaryValue(const QVariant &value) override { \ setProperty(#x, value); \ - } + } \ + private: #define NUT_AUTO_INCREMENT(x) NUT_INFO(__nut_AUTO_INCREMENT, x, 0) @@ -277,31 +289,31 @@ inline T *get(const QSharedPointer row) { #endif -template -struct ForeignKeyData { - Nut::Row _table; - T _id; +//template +//struct ForeignKeyData { +// Nut::Row _table; +// T _id; - ForeignKeyData() : _table(nullptr) - {} +// ForeignKeyData() : _table(nullptr) +// {} - void setTable(Nut::Row t) { - _table = t; - _id = t->primaryValue().value(); - } - Nut::Row table() const { - return _table; - } - void setValue(const T& val) { - _table = nullptr; - _id = val; - } - T value() const { - if (_table) - return _table->primaryValue().value(); - return _id; - } -}; +// void setTable(Nut::Row t) { +// _table = t; +// _id = t->primaryValue().value(); +// } +// Nut::Row table() const { +// return _table; +// } +// void setValue(const T& val) { +// _table = nullptr; +// _id = val; +// } +// T value() const { +// if (_table) +// return _table->primaryValue().value(); +// return _id; +// } +//}; NUT_END_NAMESPACE diff --git a/test/common/comment.cpp b/test/common/comment.cpp index 87ec017..59b2e0d 100644 --- a/test/common/comment.cpp +++ b/test/common/comment.cpp @@ -1,6 +1,11 @@ #include "comment.h" +#include "post.h" +#include "user.h" 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) diff --git a/test/common/score.cpp b/test/common/score.cpp index 08466a2..91881b5 100644 --- a/test/common/score.cpp +++ b/test/common/score.cpp @@ -1,6 +1,11 @@ #include "score.h" +#include "user.h" +#include "post.h" 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) diff --git a/test/tst_basic/tst_basic.cpp b/test/tst_basic/tst_basic.cpp index f728887..c512790 100644 --- a/test/tst_basic/tst_basic.cpp +++ b/test/tst_basic/tst_basic.cpp @@ -80,11 +80,11 @@ void BasicTest::createPost() comment->setAuthorId(user->id()); db.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(); From 2c4495bfb48a7f0bd0b0617d3a4effa75e9f2878 Mon Sep 17 00:00:00 2001 From: Hamed Masafi Date: Wed, 26 Jun 2019 19:36:44 +0430 Subject: [PATCH 3/3] basic test passed --- 3rdparty/serializer | 2 +- src/defines.h | 13 +++++++++++++ test/common/comment.h | 4 ++-- test/common/score.h | 4 ++-- test/tst_basic/tst_basic.cpp | 12 ++++++------ 5 files changed, 24 insertions(+), 11 deletions(-) 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();