diff --git a/src/database.cpp b/src/database.cpp index b80546a..40b7be7 100644 --- a/src/database.cpp +++ b/src/database.cpp @@ -184,7 +184,7 @@ bool DatabasePrivate::getCurrectScheema() tables.insert(ChangeLogTable::staticMetaObject.className(), __CHANGE_LOG_TABLE_NAME); - changeLogs = new TableSet(q); + changeLogs = new TableSet(q); for (int i = 0; i < q->metaObject()->classInfoCount(); i++) { QMetaClassInfo ci = q->metaObject()->classInfo(i); diff --git a/src/database_p.h b/src/database_p.h index b9b11d8..17669bc 100644 --- a/src/database_p.h +++ b/src/database_p.h @@ -58,7 +58,7 @@ public: SqlGeneratorBase *sqlGenertor; DatabaseModel currentModel; - TableSet *changeLogs; + TableSet *changeLogs; QT_DEPRECATED QHash tables; diff --git a/src/defines.h b/src/defines.h index fc17d57..a03b05d 100644 --- a/src/defines.h +++ b/src/defines.h @@ -40,13 +40,13 @@ #define NUT_DECLARE_TABLE(type, name) \ Q_CLASSINFO(QT_STRINGIFY(__nut_NAME_PERFIX __nut_TABLE " " #type), #name) \ Q_PROPERTY(type* name READ name) \ - Q_PROPERTY(NUT_WRAP_NAMESPACE(TableSet) name##s READ name##s) \ + Q_PROPERTY(NUT_WRAP_NAMESPACE(TableSet) name##s READ name##s) \ type* m_##name; \ - NUT_WRAP_NAMESPACE(TableSet) *m_##name##s; \ + NUT_WRAP_NAMESPACE(TableSet) *m_##name##s; \ public: \ static const type _##name; \ type* name() const{ return m_##name; } \ - NUT_WRAP_NAMESPACE(TableSet) *name##s() const { return m_##name##s; } + NUT_WRAP_NAMESPACE(TableSet) *name##s() const { return m_##name##s; } //Table #define NUT_DECLARE_FIELD(type, name, read, write) \ @@ -79,30 +79,35 @@ public: \ #define NUT_DECLARE_CHILD_TABLE(type, n) \ private: \ - NUT_WRAP_NAMESPACE(TableSet) *m_##n; \ + NUT_WRAP_NAMESPACE(TableSet) *m_##n; \ public: \ - NUT_WRAP_NAMESPACE(TableSet) *n(){ \ - return m_##n; \ - } -//static type *n##Table(){ \ -// /*static type *f = new type();*/ \ -// /*return f;*/ \ -//} \ + static type *n##Table(); \ + NUT_WRAP_NAMESPACE(TableSet) *n(); + +#define NUT_IMPLEMENT_CHILD_TABLE(class, type, n) \ + type *class::n##Table(){ \ + static type *f = new type(); \ + return f; \ + } \ + NUT_WRAP_NAMESPACE(TableSet) *class::n(){ \ + return m_##n; \ + } + -#define NUT_INDEX(name, field, order) #define NUT_PRIMARY_KEY(x) Q_CLASSINFO(QT_STRINGIFY(__nut_NAME_PERFIX #x " " __nut_PRIMARY_KEY), #x) #define NUT_AUTO_INCREMENT(x) Q_CLASSINFO(QT_STRINGIFY(__nut_NAME_PERFIX #x " " __nut_AUTO_INCREMENT), #x) -#define NUT_PRIMARY_AUTO_INCREMENT(x) NUT_PRIMARY_KEY(x) \ - NUT_AUTO_INCREMENT(x) +#define NUT_PRIMARY_AUTO_INCREMENT(x) NUT_PRIMARY_KEY(x) \ + NUT_AUTO_INCREMENT(x) #define NUT_UNIQUE(x) Q_CLASSINFO(QT_STRINGIFY(__nut_NAME_PERFIX #x " " __nut_UNIQUE), #x) #define NUT_LEN(field, len) Q_CLASSINFO(QT_STRINGIFY(__nut_NAME_PERFIX #field " " __nut_LEN), #len) #define NUT_DEFAULT_VALUE(x, n) Q_CLASSINFO(QT_STRINGIFY(__nut_NAME_PERFIX #x " " __nut_DEFAULT_VALUE), #n) #define NUT_NOT_NULL(x) Q_CLASSINFO(QT_STRINGIFY(__nut_NAME_PERFIX #x " " __nut_NOT_NULL), "1") +#define NUT_INDEX(name, field, order) #ifndef NUT_NO_KEYWORDS # define FROM(x) (x->query()) # define WHERE(x) ->setWhere(x) -# define JOIN(x) ->join(#x) +# define JOIN(x) ->join() # define ORDERBY(x) ->orderBy(#x); # define ORDERBY_DESC(x) ->orderBy(!#x); diff --git a/src/defines_p.h b/src/defines_p.h index e2ed9c9..4c9bea2 100644 --- a/src/defines_p.h +++ b/src/defines_p.h @@ -44,13 +44,13 @@ #define __nut_CHANGE "change" #ifdef NUT_NAMESPACE -# define NUT_BEGIN_NAMESPACE namespace NUT_NAMESPACE{ -# define NUT_END_NAMESPACE } -# define NUT_WRAP_NAMESPACE(x) NUT_NAMESPACE::x +# define NUT_BEGIN_NAMESPACE namespace NUT_NAMESPACE{ +# define NUT_END_NAMESPACE } +# define NUT_WRAP_NAMESPACE(x) NUT_NAMESPACE::x #else # define NUT_BEGIN_NAMESPACE # define NUT_END_NAMESPACE -# define NUT_WRAP_NAMESPACE(x) x +# define NUT_WRAP_NAMESPACE(x) x #endif #endif // DEFINES_P_H diff --git a/src/query.h b/src/query.h index d4ce1b2..3ec0dc1 100644 --- a/src/query.h +++ b/src/query.h @@ -48,17 +48,13 @@ class NUT_EXPORT Query : public QueryBase public: Query(Database *database, TableSetBase *tableSet, bool autoDelete); - ~Query(); - Query *join(const QString &tableName); + //ddl Query *setWhere(WherePhrase where); - Query *join(Table *c) - { - join(c->metaObject()->className()); - return this; - } + Query *join(const QString &tableName); + Query *join(Table *c); template Query *join() @@ -68,24 +64,27 @@ public: } // Query *orderBy(QString fieldName, QString type); - Query *skip(int &n); - Query *take(int &n); + Query *skip(int n); + Query *take(int n); Query *orderBy(WherePhrase phrase); Query *include(TableSetBase *t); Query *include(Table *t); + //data selecting + T *first(); + QList toList(int count = -1); + template + QList select(const FieldPhrase f); int count(); QVariant max(FieldPhrase &f); QVariant min(FieldPhrase &f); QVariant average(FieldPhrase &f); - T first(); - QList toList(int count = -1); - template - QList select(const FieldPhrase f); + //data mailpulation int update(WherePhrase phrase); int remove(); + //debug purpose QString sqlCommand() const; }; @@ -120,10 +119,10 @@ Q_OUTOFLINE_TEMPLATE Query::~Query() } template -Q_OUTOFLINE_TEMPLATE QList Query::toList(int count) +Q_OUTOFLINE_TEMPLATE QList Query::toList(int count) { Q_D(Query); - QList result; + QList result; d->select = "*"; // QSqlQuery q = @@ -138,7 +137,7 @@ Q_OUTOFLINE_TEMPLATE QList Query::toList(int count) QString pk = d->database->model().tableByName(d->tableName)->primaryKey(); QVariant lastPkValue = QVariant(); int childTypeId = 0; - T lastRow = 0; + T *lastRow = 0; TableSetBase *childTableSet = Q_NULLPTR; // FIXME: getting table error @@ -164,7 +163,7 @@ Q_OUTOFLINE_TEMPLATE QList Query::toList(int count) while (q.next()) { if (lastPkValue != q.value(pk)) { - T t = T();//new std::remove_pointer::type(); + T *t = new T();//new std::remove_pointer::type(); foreach (QString field, masterFields) t->setProperty(field.toLatin1().data(), q.value(field)); // for (int i = 0; i < t->metaObject()->propertyCount(); @@ -244,9 +243,11 @@ Q_OUTOFLINE_TEMPLATE QList Query::select(const FieldPhrase f) } template -Q_OUTOFLINE_TEMPLATE T Query::first() +Q_OUTOFLINE_TEMPLATE T *Query::first() { - QList list = toList(1); + skip(0); + take(1); + QList list = toList(1); if (list.count()) return list.first(); @@ -322,6 +323,13 @@ Q_OUTOFLINE_TEMPLATE Query *Query::join(const QString &tableName) return this; } +template +Q_OUTOFLINE_TEMPLATE Query *Query::join(Table *c) +{ + join(c->metaObject()->className()); + return this; +} + template Q_OUTOFLINE_TEMPLATE Query *Query::setWhere(WherePhrase where) { @@ -331,7 +339,7 @@ Q_OUTOFLINE_TEMPLATE Query *Query::setWhere(WherePhrase where) } template -Q_OUTOFLINE_TEMPLATE Query *Query::skip(int &n) +Q_OUTOFLINE_TEMPLATE Query *Query::skip(int n) { Q_D(Query); d->skip = n; @@ -339,7 +347,7 @@ Q_OUTOFLINE_TEMPLATE Query *Query::skip(int &n) } template -Q_OUTOFLINE_TEMPLATE Query *Query::take(int &n) +Q_OUTOFLINE_TEMPLATE Query *Query::take(int n) { Q_D(Query); d->take = n; diff --git a/src/tableset.h b/src/tableset.h index e567ede..2a9c6be 100644 --- a/src/tableset.h +++ b/src/tableset.h @@ -45,15 +45,15 @@ public: TableSet(Database *parent); TableSet(Table *parent); - void append(T t); - void append(QList t); - void remove(T t); - void remove(QList t); + void append(T *t); + void append(QList t); + void remove(T *t); + void remove(QList t); - inline T type() const {} + inline T *type() const {} int length() const; - T at(int i) const; + T *at(int i) const; const T &operator[](int i) const; Query *query(); Query *query(bool autoDelete); @@ -96,9 +96,9 @@ Q_OUTOFLINE_TEMPLATE int TableSet::length() const } template -Q_OUTOFLINE_TEMPLATE T TableSet::at(int i) const +Q_OUTOFLINE_TEMPLATE T *TableSet::at(int i) const { - return (T)_tablesList.at(i); + return (T*)_tablesList.at(i); } template @@ -108,7 +108,7 @@ Q_OUTOFLINE_TEMPLATE const T &TableSet::operator[](int i) const } template -Q_OUTOFLINE_TEMPLATE void TableSet::append(T t) +Q_OUTOFLINE_TEMPLATE void TableSet::append(T *t) { _tables.insert(t); _tablesList.append(t); @@ -119,21 +119,21 @@ Q_OUTOFLINE_TEMPLATE void TableSet::append(T t) } template -Q_OUTOFLINE_TEMPLATE void TableSet::append(QList t) +Q_OUTOFLINE_TEMPLATE void TableSet::append(QList t) { foreach (T* i, t) append(i); } template -Q_OUTOFLINE_TEMPLATE void TableSet::remove(T t) +Q_OUTOFLINE_TEMPLATE void TableSet::remove(T *t) { _tables.remove(t); t->setStatus(Table::Deleted); } template -Q_OUTOFLINE_TEMPLATE void TableSet::remove(QList t) +Q_OUTOFLINE_TEMPLATE void TableSet::remove(QList t) { foreach (T* i, t) remove(i); diff --git a/test/basic/maintest.cpp b/test/basic/maintest.cpp index 366f805..908e796 100644 --- a/test/basic/maintest.cpp +++ b/test/basic/maintest.cpp @@ -10,9 +10,9 @@ #include "tablemodel.h" #include "databasemodel.h" +#include "user.h" #include "post.h" #include "comment.h" -#include "user.h" MainTest::MainTest(QObject *parent) : QObject(parent) { @@ -95,13 +95,15 @@ void MainTest::createPost2() void MainTest::selectPosts() { - auto q = db.posts()->query(); + auto q = db.posts()->query() // q->join(Post::commentsTable()); - q->orderBy(!Post::saveDateField() & Post::bodyField()); - q->setWhere(Post::idField() == postId); +// q->join(Post::commentsTable()); +// ->join() + ->orderBy(!Post::saveDateField() & Post::bodyField()) + ->setWhere(Post::idField() == postId); auto posts = q->toList(); - + qDebug() << "SQL="<sqlCommand(); post = posts.at(0); post->setBody(""); diff --git a/test/basic/maintest.h b/test/basic/maintest.h index cdc9615..b00d259 100644 --- a/test/basic/maintest.h +++ b/test/basic/maintest.h @@ -12,6 +12,7 @@ class MainTest : public QObject WeblogDatabase db; int postId; Post *post; + public: explicit MainTest(QObject *parent = 0); diff --git a/test/common/post.cpp b/test/common/post.cpp index f4f871c..62155fb 100644 --- a/test/common/post.cpp +++ b/test/common/post.cpp @@ -3,7 +3,9 @@ #include "tableset.h" Post::Post(QObject *parent) : Table(parent), - m_comments(new TableSet(this)), m_id(0), m_title("") + m_comments(new TableSet(this)), m_id(0), m_title("") { } + +NUT_IMPLEMENT_CHILD_TABLE(Post, Comment, comments) diff --git a/test/common/post.h b/test/common/post.h index d7b2492..fbca48f 100644 --- a/test/common/post.h +++ b/test/common/post.h @@ -1,10 +1,9 @@ -#ifndef USER_H -#define USER_H +#ifndef POST_H +#define POST_H #include #include "table.h" #include "database.h" -//#include "comment.h" #include "databasemodel.h" #ifdef NUT_NAMESPACE @@ -39,4 +38,4 @@ public slots: Q_DECLARE_METATYPE(Post*) -#endif // USER_H +#endif // POST_H diff --git a/test/common/user.cpp b/test/common/user.cpp index d318923..90abc0c 100644 --- a/test/common/user.cpp +++ b/test/common/user.cpp @@ -3,7 +3,9 @@ #include "comment.h" User::User(QObject *tableSet) : Table(tableSet), - m_comments(new TableSet(this)) + m_comments(new TableSet(this)) { } + +NUT_IMPLEMENT_CHILD_TABLE(User, Comment, comments) diff --git a/test/common/user.h b/test/common/user.h index d1f6ed8..d2350f2 100644 --- a/test/common/user.h +++ b/test/common/user.h @@ -1,17 +1,18 @@ #ifndef USER_H #define USER_H -#include #include "table.h" #include "tableset.h" -//#include "comment.h" + +#include +#include #ifdef NUT_NAMESPACE using namespace NUT_NAMESPACE; #endif class Comment; -class User : public Table +class User : public Nut::Table { Q_OBJECT @@ -29,7 +30,7 @@ class User : public Table NUT_DECLARE_CHILD_TABLE(Comment, comments) public: - User(QObject *tableSet = 0); + Q_INVOKABLE User(QObject *tableSet = 0); }; Q_DECLARE_METATYPE(User*) diff --git a/test/common/weblogdatabase.cpp b/test/common/weblogdatabase.cpp index 4fc87ee..40cba75 100644 --- a/test/common/weblogdatabase.cpp +++ b/test/common/weblogdatabase.cpp @@ -3,11 +3,12 @@ #include "user.h" #include "post.h" #include "comment.h" +#include "user.h" #include "weblogdatabase.h" WeblogDatabase::WeblogDatabase() : Database(), - m_posts(new TableSet(this)), - m_comments(new TableSet(this)), - m_users(new TableSet(this)) + m_posts(new TableSet(this)), + m_comments(new TableSet(this)), + m_users(new TableSet(this)) { }