From 3acc4a45837c6810cd6b2ce43d32c5f982cbb714 Mon Sep 17 00:00:00 2001 From: Hamed Masafi Date: Fri, 12 Jan 2018 14:10:06 +0330 Subject: [PATCH] hw --- src/databasemodel.cpp | 8 +-- src/generators/sqlgeneratorbase.cpp | 2 +- src/query.h | 82 +++++++++++++++++++++++------ test/basic/maintest.cpp | 7 ++- 4 files changed, 74 insertions(+), 25 deletions(-) diff --git a/src/databasemodel.cpp b/src/databasemodel.cpp index 92987ea..3ed8282 100644 --- a/src/databasemodel.cpp +++ b/src/databasemodel.cpp @@ -18,12 +18,14 @@ ** **************************************************************************/ -#include "databasemodel.h" -#include "tablemodel.h" - #include #include +#include + +#include "tablemodel.h" +#include "databasemodel.h" + NUT_BEGIN_NAMESPACE QMap DatabaseModel::_models; diff --git a/src/generators/sqlgeneratorbase.cpp b/src/generators/sqlgeneratorbase.cpp index a1a174b..14ea252 100644 --- a/src/generators/sqlgeneratorbase.cpp +++ b/src/generators/sqlgeneratorbase.cpp @@ -106,7 +106,7 @@ QString SqlGeneratorBase::recordsPhrase(QString className) foreach (FieldModel *f, table->fields()) { if (!ret.isEmpty()) ret.append(", "); - ret.append(QString("%1.%2 AS %1_%2").arg(table->name()).arg(f->name)); + ret.append(QString("%1.%2 AS [%1_%2]").arg(table->name()).arg(f->name)); } return ret; } diff --git a/src/query.h b/src/query.h index 03740b8..da676a2 100644 --- a/src/query.h +++ b/src/query.h @@ -36,6 +36,8 @@ #include "wherephrase.h" #include "tablemodel.h" +#include + NUT_BEGIN_NAMESPACE template @@ -73,6 +75,15 @@ public: //data selecting T *first(); QList toList(int count = -1); + template + QList toList(std::function func) { + QList l = toList(); + QList ret; + foreach (T *t, l) + ret.append(func(t)); + return ret; + } + template QList select(const FieldPhrase f); int count(); @@ -122,27 +133,34 @@ Q_OUTOFLINE_TEMPLATE Query::~Query() template Q_OUTOFLINE_TEMPLATE QList Query::toList(int count) { + /* + * TODO: make this as good as possible, this is heart of Nut + * Known issues: + * Is complex: O(n*j) n=db rows count, j=joins count + */ + Q_UNUSED(count); Q_D(Query); - QList result; + d->select = "*"; - d->joins.prepend(d->className); - - d->sql = d->database->sqlGenertor()->selectCommand( SqlGeneratorBase::SelectAll, "", d->wheres, d->orderPhrases, d->joins, d->skip, d->take); - qDebug() << d->sql; + QSqlQuery q = d->database->exec(d->sql); struct LevelData{ - QString key; + QString key; // key field name QString className; QString tableName; + TableModel *model; + QSet rows; // found rows related to this join level + TableSetBase *tableSet; + int parentId; + QVariant keyValue; int typeId; - TableSetBase *tableSet; Table *lastRow; }; QVector levels; @@ -155,6 +173,7 @@ Q_OUTOFLINE_TEMPLATE QList Query::toList(int count) qWarning("Model '%s' not found!!!", qPrintable(className)); return returnList; } + data.model = m; data.key = m->name() + "_" + m->primaryKey(); data.typeId = m->typeId(); data.keyValue = QVariant(); @@ -190,22 +209,39 @@ Q_OUTOFLINE_TEMPLATE QList Query::toList(int count) table->setStatus(Table::FeatchedFromDB); table->setParent(this); table->clear(); + data.rows.insert(table); //set last created row data.lastRow = table; - if (i < levels.count() - 1) { + foreach (RelationModel *rel, data.model->foregionKeys()) { + for (int j = 0; j < levels.count(); ++j) { + if (rel->className == levels[j].className && i != j) { + if (levels[j].tableSet) { + data.tableSet = levels[j].tableSet; + table->setTableSet(levels[j].tableSet); + table->setParentTable(levels[j].lastRow); + } else { + qWarning() << "Dataset not found for" << data.className; + } + } + } + } + /*if (i < levels.count() - 1) { + // if (data.className == d->className) { // data.tableSet = d->tableSet; // } else - { - QSet tableSets = table->tableSets; - foreach (TableSetBase *ts, tableSets) - if (ts->childClassName() == levels[i + 1].className) - data.tableSet = ts; +// { +// QSet tableSets = table->tableSets; +// foreach (TableSetBase *ts, tableSets) { +// qDebug() << "checking" << ts->childClassName() << levels[i + 1].className; +// if (ts->childClassName() == levels[i + 1].className) +// data.tableSet = ts; +// } - if (!data.tableSet) - qWarning() << "Dataset not found for" << data.className; - } +// if (!data.tableSet) +// qWarning() << "Dataset not found for" << data.className; +// } } if (i) { // if (!table->tableSet()) @@ -214,12 +250,24 @@ Q_OUTOFLINE_TEMPLATE QList Query::toList(int count) } else { // table->setTableSet(d->tableSet); // data.tableSet = d->tableSet; - } + }*/ } } } + + for (int i = 0; i < levels.count(); i++) { + LevelData &data = levels[i]; + qDebug() <<"RESULT" << data.className << data.rows.count(); + QSet::iterator it; + for (it = data.rows.begin(); it != data.rows.end(); ++it) { +// qDebug() << *i +// add *i to it's parent row + } + } + if (m_autoDelete) deleteLater(); + return returnList; /* QString pk = d->database->model().tableByName(d->tableName)->primaryKey(); diff --git a/test/basic/maintest.cpp b/test/basic/maintest.cpp index 1f12625..91be639 100644 --- a/test/basic/maintest.cpp +++ b/test/basic/maintest.cpp @@ -14,8 +14,7 @@ #include "post.h" #include "comment.h" -#define PRINT(x) -//qDebug() << #x "=" << x; +#define PRINT(x) qDebug() << #x "=" << x; MainTest::MainTest(QObject *parent) : QObject(parent) { } @@ -175,8 +174,8 @@ void MainTest::testDate() void MainTest::join() { auto q = db.comments()->query() - ->join() - ->join(); + ->join() + ->join(); Comment *comment = q->first();