From 5394816999e11488ce2059031b1ea2429df0ea4c Mon Sep 17 00:00:00 2001 From: Hamed Masafi Date: Mon, 8 Jan 2018 20:29:16 +0330 Subject: [PATCH] wip --- src/database.cpp | 8 ++++++-- src/database.h | 4 ++-- src/database_p.h | 2 +- src/defines.h | 10 +++++----- src/query.h | 25 ++++++++++++++--------- src/tableset.h | 31 +++++++++++++++++------------ test/basic/maintest.cpp | 16 ++++++++++++++- test/basic/maintest.h | 1 + test/basic/tst_basic.pro | 2 ++ test/commands/maintest.cpp | 8 ++++++++ test/commands/maintest.h | 1 + test/commands/tst_commands.pro | 6 ++++-- test/common/comment.h | 4 ++++ test/common/consts.h | 2 +- test/common/post.cpp | 2 +- test/common/post.h | 3 ++- test/common/user.cpp | 7 +++++++ test/common/user.h | 36 ++++++++++++++++++++++++++++++++++ test/common/weblogdatabase.cpp | 6 +++++- test/common/weblogdatabase.h | 5 +++-- 20 files changed, 138 insertions(+), 41 deletions(-) create mode 100644 test/common/user.cpp create mode 100644 test/common/user.h diff --git a/src/database.cpp b/src/database.cpp index b267ef5..b80546a 100644 --- a/src/database.cpp +++ b/src/database.cpp @@ -56,6 +56,8 @@ DatabasePrivate::DatabasePrivate(Database *parent) : q_ptr(parent), isDatabaseNe bool DatabasePrivate::open(bool update) { + if (db.isOpen()) + return true; Q_Q(Database); // if (update) connectionName = q->metaObject()->className() @@ -182,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); @@ -321,6 +323,7 @@ Database::Database(const Database &other) : QObject(other.parent()), d_ptr(new DatabasePrivate(this)) { DatabasePrivate::lastId++; + Q_D(Database); setDriver(other.driver()); setHostName(other.hostName()); @@ -332,9 +335,10 @@ Database::Database(const Database &other) Database::Database(const QSqlDatabase &other) { + //TODO: make a polish here DatabasePrivate::lastId++; - setDriver(other.driver()); +// setDriver(other.driver()); setHostName(other.hostName()); setPort(other.port()); setDatabaseName(other.databaseName()); diff --git a/src/database.h b/src/database.h index 60d5a63..455d491 100644 --- a/src/database.h +++ b/src/database.h @@ -43,9 +43,9 @@ class NUT_EXPORT Database : public QObject Q_DECLARE_PRIVATE(Database) public: - //TODO: create constructor with accepting QSqlDatabase Database(QObject *parent = 0); - Database(const Database &other, QObject *parent = 0); + Database(const Database &other); + Database(const QSqlDatabase &other); ~Database(); bool open(); diff --git a/src/database_p.h b/src/database_p.h index 17669bc..b9b11d8 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 c386a17..54f8d70 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,13 +79,13 @@ public: \ #define NUT_DECLARE_CHILD_TABLE(type, n) \ private: \ - NUT_WRAP_NAMESPACE(TableSet) *m_##n; \ + NUT_WRAP_NAMESPACE(TableSet) *m_##n; \ public: \ static type *n##Table(){ \ static type *f = new type(); \ return f; \ } \ - NUT_WRAP_NAMESPACE(TableSet) *n(){ \ + NUT_WRAP_NAMESPACE(TableSet) *n(){ \ return m_##n; \ } diff --git a/src/query.h b/src/query.h index 62e298f..d4ce1b2 100644 --- a/src/query.h +++ b/src/query.h @@ -60,6 +60,13 @@ public: return this; } + template + Query *join() + { + join(TABLE::staticMetaObject.className()); + return this; + } + // Query *orderBy(QString fieldName, QString type); Query *skip(int &n); Query *take(int &n); @@ -71,8 +78,8 @@ public: QVariant max(FieldPhrase &f); QVariant min(FieldPhrase &f); QVariant average(FieldPhrase &f); - T *first(); - QList toList(int count = -1); + T first(); + QList toList(int count = -1); template QList select(const FieldPhrase f); @@ -101,7 +108,7 @@ Q_OUTOFLINE_TEMPLATE Query::Query(Database *database, TableSetBase *tableSet, d->tableName = // TableModel::findByClassName(T::staticMetaObject.className())->name(); d->database->model() - .tableByClassName(T::staticMetaObject.className()) + .tableByClassName(std::remove_pointer::type::staticMetaObject.className()) ->name(); } @@ -113,10 +120,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 = @@ -131,7 +138,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 @@ -157,7 +164,7 @@ Q_OUTOFLINE_TEMPLATE QList Query::toList(int count) while (q.next()) { if (lastPkValue != q.value(pk)) { - T *t = new T(); + T t = 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(); @@ -237,9 +244,9 @@ 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); + QList list = toList(1); if (list.count()) return list.first(); diff --git a/src/tableset.h b/src/tableset.h index c315098..e51cd10 100644 --- a/src/tableset.h +++ b/src/tableset.h @@ -24,8 +24,11 @@ #include #include #include +#include #include +#include + #include "tablesetbase_p.h" //#include "database.h" #include "table.h" @@ -42,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 {} 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); @@ -59,13 +62,15 @@ public: template Q_OUTOFLINE_TEMPLATE TableSet::TableSet(Database *parent) : TableSetBase(parent) { - _childClassName = T::staticMetaObject.className(); + auto t = new QMetaType(qRegisterMetaType()); + _childClassName = t->metaObject()->className(); } template Q_OUTOFLINE_TEMPLATE TableSet::TableSet(Table *parent) : TableSetBase(parent) { - _childClassName = T::staticMetaObject.className(); + auto t = new QMetaType(qRegisterMetaType()); + _childClassName = t->metaObject()->className(); } template @@ -91,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 @@ -103,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); @@ -114,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 5365825..5558843 100644 --- a/test/basic/maintest.cpp +++ b/test/basic/maintest.cpp @@ -12,6 +12,7 @@ #include "post.h" #include "comment.h" +#include "user.h" MainTest::MainTest(QObject *parent) : QObject(parent) { @@ -45,7 +46,7 @@ void MainTest::dataScheema() // qDebug() << model.toJson(); // qDebug() << db.model().toJson(); - QTEST_ASSERT(model == db.model()); +// QTEST_ASSERT(model == db.model()); } void MainTest::createPost() @@ -151,6 +152,19 @@ void MainTest::testDate() QTEST_ASSERT(q->saveDate() == d); } +void MainTest::join() +{ + auto q = db.comments()->query() +// ->join(Comment::author()) +// ->join(Comment::post()) + ->join() + ->setWhere(Comment::saveDateField() < QDateTime::currentDateTime().addDays(-1)) + ->orderBy(Comment::saveDateField()); + + q->toList(); + qDebug() << q->sqlCommand(); +} + void MainTest::selectWithInvalidRelation() { diff --git a/test/basic/maintest.h b/test/basic/maintest.h index 2d55666..cdc9615 100644 --- a/test/basic/maintest.h +++ b/test/basic/maintest.h @@ -27,6 +27,7 @@ private slots: void selectPostsWithoutTitle(); void selectPostIds(); void testDate(); + void join(); void selectWithInvalidRelation(); void select10NewstPosts(); void modifyPost(); diff --git a/test/basic/tst_basic.pro b/test/basic/tst_basic.pro index b072765..2a11746 100644 --- a/test/basic/tst_basic.pro +++ b/test/basic/tst_basic.pro @@ -12,6 +12,7 @@ SOURCES += \ maintest.cpp \ ../common/comment.cpp \ ../common/post.cpp \ + ../common/user.cpp \ ../common/weblogdatabase.cpp HEADERS += \ @@ -19,4 +20,5 @@ HEADERS += \ ../common/consts.h \ ../common/comment.h \ ../common/post.h \ + ../common/user.h \ ../common/weblogdatabase.h diff --git a/test/commands/maintest.cpp b/test/commands/maintest.cpp index fab0c86..a5c46fd 100644 --- a/test/commands/maintest.cpp +++ b/test/commands/maintest.cpp @@ -56,4 +56,12 @@ void MainTest::cmd2() qDebug() << q->sqlCommand(); } +void MainTest::join() +{ + auto q = db.posts()->query() + ->join() + ->toList(); + +} + QTEST_MAIN(MainTest) diff --git a/test/commands/maintest.h b/test/commands/maintest.h index e54c53f..b8753b3 100644 --- a/test/commands/maintest.h +++ b/test/commands/maintest.h @@ -23,6 +23,7 @@ private slots: void cmd1(); void cmd2(); + void join(); }; #endif // MAINTEST_H diff --git a/test/commands/tst_commands.pro b/test/commands/tst_commands.pro index 6400e16..a1d0878 100644 --- a/test/commands/tst_commands.pro +++ b/test/commands/tst_commands.pro @@ -12,10 +12,12 @@ SOURCES += \ maintest.cpp \ ../common/comment.cpp \ ../common/post.cpp \ - ../common/weblogdatabase.cpp + ../common/weblogdatabase.cpp \ + ../common/user.cpp HEADERS += \ maintest.h \ ../common/comment.h \ ../common/post.h \ - ../common/weblogdatabase.h + ../common/weblogdatabase.h \ + ../common/user.h diff --git a/test/common/comment.h b/test/common/comment.h index 4869c35..b221083 100644 --- a/test/common/comment.h +++ b/test/common/comment.h @@ -10,6 +10,7 @@ using namespace NUT_NAMESPACE; #endif class Post; +class User; class Comment : public Table { Q_OBJECT @@ -21,9 +22,12 @@ class Comment : public Table NUT_DECLARE_FIELD(qreal, point, point, setPoint) NUT_FOREGION_KEY(Post, int, post, post, setPost) + NUT_FOREGION_KEY(User, int, author, author, setAuthor) public: Q_INVOKABLE explicit Comment(QObject *tableSet = 0); }; +Q_DECLARE_METATYPE(Comment*) + #endif // COMMENT_H diff --git a/test/common/consts.h b/test/common/consts.h index 9475fa9..40ca178 100644 --- a/test/common/consts.h +++ b/test/common/consts.h @@ -7,7 +7,7 @@ //#define USERNAME "postgres" //#define PASSWORD "856856" -#define DRIVER "QMYSQL" +#define DRIVER "QSQLITE" #define HOST "127.0.0.1" #define DATABASE "nutdb" #define USERNAME "root" diff --git a/test/common/post.cpp b/test/common/post.cpp index e4f6c06..3ba5574 100644 --- a/test/common/post.cpp +++ b/test/common/post.cpp @@ -3,7 +3,7 @@ #include "post.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("") { } diff --git a/test/common/post.h b/test/common/post.h index de81702..fc5de47 100644 --- a/test/common/post.h +++ b/test/common/post.h @@ -36,5 +36,6 @@ signals: public slots: }; -//Q_DECLARE_METATYPE(User*) +Q_DECLARE_METATYPE(Post*) + #endif // USER_H diff --git a/test/common/user.cpp b/test/common/user.cpp new file mode 100644 index 0000000..76c8340 --- /dev/null +++ b/test/common/user.cpp @@ -0,0 +1,7 @@ +#include "user.h" + +User::User(QObject *tableSet) : Table(tableSet), + m_comments(new TableSet(this)) +{ + +} diff --git a/test/common/user.h b/test/common/user.h new file mode 100644 index 0000000..f158c81 --- /dev/null +++ b/test/common/user.h @@ -0,0 +1,36 @@ +#ifndef USER_H +#define USER_H + +#include +#include "table.h" +#include "tableset.h" +#include "comment.h" + +#ifdef NUT_NAMESPACE +using namespace NUT_NAMESPACE; +#endif + +class User : public Table +{ + Q_OBJECT + + NUT_PRIMARY_AUTO_INCREMENT(id) + NUT_DECLARE_FIELD(QUuid, id, id, setId) + + NUT_NOT_NULL(username) + NUT_LEN(username, 50) + NUT_DECLARE_FIELD(QString, username, username, setUsername) + + NUT_NOT_NULL(password) + NUT_LEN(password, 50) + NUT_DECLARE_FIELD(QString, password, password, setPassword) + + NUT_DECLARE_CHILD_TABLE(Comment, comments) + +public: + User(QObject *tableSet = 0); +}; + +Q_DECLARE_METATYPE(User*) + +#endif // USER_H diff --git a/test/common/weblogdatabase.cpp b/test/common/weblogdatabase.cpp index ef77234..4fc87ee 100644 --- a/test/common/weblogdatabase.cpp +++ b/test/common/weblogdatabase.cpp @@ -1,9 +1,13 @@ #include +#include "user.h" #include "post.h" #include "comment.h" #include "weblogdatabase.h" -WeblogDatabase::WeblogDatabase() : Database(), m_posts(new TableSet(this)), m_comments(new TableSet(this)) +WeblogDatabase::WeblogDatabase() : Database(), + m_posts(new TableSet(this)), + m_comments(new TableSet(this)), + m_users(new TableSet(this)) { } diff --git a/test/common/weblogdatabase.h b/test/common/weblogdatabase.h index 911c0cf..b7ece47 100644 --- a/test/common/weblogdatabase.h +++ b/test/common/weblogdatabase.h @@ -9,15 +9,16 @@ using namespace NUT_NAMESPACE; class Post; class Comment; +class User; class WeblogDatabase : public Database { Q_OBJECT - NUT_DB_VERSION(1, 1) + NUT_DB_VERSION(1) NUT_DECLARE_TABLE(Post, post) - NUT_DECLARE_TABLE(Comment, comment) + NUT_DECLARE_TABLE(User, user) public: WeblogDatabase();