Merge pull request #46 from HamedMasafi/plan/new_macro_model

Plan/new macro model
This commit is contained in:
Hamed Masafi 2019-06-26 19:51:45 +04:30 committed by GitHub
commit 91b1701475
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 100 additions and 19 deletions

View File

@ -1,3 +1,3 @@
#QT -= gui
DEFINES += DOC_PATH=\\\"$$PWD/../../doc\\\"
DEFINES += NUT_PATH=\\\"$$PWD/../../\\\"

View File

@ -93,6 +93,47 @@ public: \
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<type> m_##name; \
keytype m_##name##Id; \
Q_PROPERTY(Nut::Row<type> name READ read WRITE write) \
Q_PROPERTY(keytype name##Id READ read##Id WRITE write##Id) \
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>) f = \
NUT_WRAP_NAMESPACE(FieldPhrase<keytype>) \
(staticMetaObject.className(), #name); \
return f; \
} \
keytype read##Id() const; \
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 class::write##Id(keytype name##Id){ \
propertyChanged(QT_STRINGIFY2(name##Id)); \
m_##name##Id = name##Id; \
m_##name = nullptr; \
propertyChanged(QT_STRINGIFY2(name##Id)); \
}
#define NUT_DECLARE_CHILD_TABLE(type, n) \
private: \
NUT_WRAP_NAMESPACE(TableSet)<type> *m_##n; \
@ -110,9 +151,20 @@ 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) \
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)
#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 +302,32 @@ inline T *get(const QSharedPointer<T> row) {
#endif
//template<class C, typename T>
//struct ForeignKeyData {
// Nut::Row<C> _table;
// T _id;
// ForeignKeyData() : _table(nullptr)
// {}
// void setTable(Nut::Row<C> t) {
// _table = t;
// _id = t->primaryValue().value<T>();
// }
// Nut::Row<C> table() const {
// return _table;
// }
// void setValue(const T& val) {
// _table = nullptr;
// _id = val;
// }
// T value() const {
// if (_table)
// return _table->primaryValue().value<T>();
// return _id;
// }
//};
NUT_END_NAMESPACE
#endif // SYNTAX_DEFINES_H

View File

@ -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);

View File

@ -1,7 +1,11 @@
#include "comment.h"
#include "post.h"
#include "user.h"
Comment::Comment(QObject *parent) : Table(parent),
m_author(Q_NULLPTR), m_post(Q_NULLPTR)
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)

View File

@ -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);

View File

@ -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)

View File

@ -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);

View File

@ -78,7 +78,7 @@ 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>();

View File

@ -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();