This commit is contained in:
parent
b3fac0763c
commit
3acc4a4583
|
|
@ -18,12 +18,14 @@
|
||||||
**
|
**
|
||||||
**************************************************************************/
|
**************************************************************************/
|
||||||
|
|
||||||
#include "databasemodel.h"
|
|
||||||
#include "tablemodel.h"
|
|
||||||
|
|
||||||
#include <QJsonArray>
|
#include <QJsonArray>
|
||||||
#include <QJsonObject>
|
#include <QJsonObject>
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
|
#include "tablemodel.h"
|
||||||
|
#include "databasemodel.h"
|
||||||
|
|
||||||
NUT_BEGIN_NAMESPACE
|
NUT_BEGIN_NAMESPACE
|
||||||
|
|
||||||
QMap<QString, DatabaseModel*> DatabaseModel::_models;
|
QMap<QString, DatabaseModel*> DatabaseModel::_models;
|
||||||
|
|
|
||||||
|
|
@ -106,7 +106,7 @@ QString SqlGeneratorBase::recordsPhrase(QString className)
|
||||||
foreach (FieldModel *f, table->fields()) {
|
foreach (FieldModel *f, table->fields()) {
|
||||||
if (!ret.isEmpty())
|
if (!ret.isEmpty())
|
||||||
ret.append(", ");
|
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;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
82
src/query.h
82
src/query.h
|
|
@ -36,6 +36,8 @@
|
||||||
#include "wherephrase.h"
|
#include "wherephrase.h"
|
||||||
#include "tablemodel.h"
|
#include "tablemodel.h"
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
NUT_BEGIN_NAMESPACE
|
NUT_BEGIN_NAMESPACE
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
|
|
@ -73,6 +75,15 @@ public:
|
||||||
//data selecting
|
//data selecting
|
||||||
T *first();
|
T *first();
|
||||||
QList<T*> toList(int count = -1);
|
QList<T*> toList(int count = -1);
|
||||||
|
template<typename R>
|
||||||
|
QList<T*> toList(std::function<R*(T*)> func) {
|
||||||
|
QList<T*> l = toList();
|
||||||
|
QList<R*> ret;
|
||||||
|
foreach (T *t, l)
|
||||||
|
ret.append(func(t));
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
template <typename F>
|
template <typename F>
|
||||||
QList<F> select(const FieldPhrase<F> f);
|
QList<F> select(const FieldPhrase<F> f);
|
||||||
int count();
|
int count();
|
||||||
|
|
@ -122,27 +133,34 @@ Q_OUTOFLINE_TEMPLATE Query<T>::~Query()
|
||||||
template <class T>
|
template <class T>
|
||||||
Q_OUTOFLINE_TEMPLATE QList<T *> Query<T>::toList(int count)
|
Q_OUTOFLINE_TEMPLATE QList<T *> Query<T>::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_UNUSED(count);
|
||||||
Q_D(Query);
|
Q_D(Query);
|
||||||
QList<T*> result;
|
|
||||||
d->select = "*";
|
d->select = "*";
|
||||||
|
|
||||||
d->joins.prepend(d->className);
|
d->joins.prepend(d->className);
|
||||||
|
|
||||||
|
|
||||||
d->sql = d->database->sqlGenertor()->selectCommand(
|
d->sql = d->database->sqlGenertor()->selectCommand(
|
||||||
SqlGeneratorBase::SelectAll, "", d->wheres, d->orderPhrases,
|
SqlGeneratorBase::SelectAll, "", d->wheres, d->orderPhrases,
|
||||||
d->joins, d->skip, d->take);
|
d->joins, d->skip, d->take);
|
||||||
qDebug() << d->sql;
|
|
||||||
QSqlQuery q = d->database->exec(d->sql);
|
QSqlQuery q = d->database->exec(d->sql);
|
||||||
|
|
||||||
struct LevelData{
|
struct LevelData{
|
||||||
QString key;
|
QString key; // key field name
|
||||||
QString className;
|
QString className;
|
||||||
QString tableName;
|
QString tableName;
|
||||||
|
TableModel *model;
|
||||||
|
QSet<Table*> rows; // found rows related to this join level
|
||||||
|
TableSetBase *tableSet;
|
||||||
|
int parentId;
|
||||||
|
|
||||||
QVariant keyValue;
|
QVariant keyValue;
|
||||||
int typeId;
|
int typeId;
|
||||||
TableSetBase *tableSet;
|
|
||||||
Table *lastRow;
|
Table *lastRow;
|
||||||
};
|
};
|
||||||
QVector<LevelData> levels;
|
QVector<LevelData> levels;
|
||||||
|
|
@ -155,6 +173,7 @@ Q_OUTOFLINE_TEMPLATE QList<T *> Query<T>::toList(int count)
|
||||||
qWarning("Model '%s' not found!!!", qPrintable(className));
|
qWarning("Model '%s' not found!!!", qPrintable(className));
|
||||||
return returnList;
|
return returnList;
|
||||||
}
|
}
|
||||||
|
data.model = m;
|
||||||
data.key = m->name() + "_" + m->primaryKey();
|
data.key = m->name() + "_" + m->primaryKey();
|
||||||
data.typeId = m->typeId();
|
data.typeId = m->typeId();
|
||||||
data.keyValue = QVariant();
|
data.keyValue = QVariant();
|
||||||
|
|
@ -190,22 +209,39 @@ Q_OUTOFLINE_TEMPLATE QList<T *> Query<T>::toList(int count)
|
||||||
table->setStatus(Table::FeatchedFromDB);
|
table->setStatus(Table::FeatchedFromDB);
|
||||||
table->setParent(this);
|
table->setParent(this);
|
||||||
table->clear();
|
table->clear();
|
||||||
|
data.rows.insert(table);
|
||||||
|
|
||||||
//set last created row
|
//set last created row
|
||||||
data.lastRow = table;
|
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) {
|
// if (data.className == d->className) {
|
||||||
// data.tableSet = d->tableSet;
|
// data.tableSet = d->tableSet;
|
||||||
// } else
|
// } else
|
||||||
{
|
// {
|
||||||
QSet<TableSetBase *> tableSets = table->tableSets;
|
// QSet<TableSetBase *> tableSets = table->tableSets;
|
||||||
foreach (TableSetBase *ts, tableSets)
|
// foreach (TableSetBase *ts, tableSets) {
|
||||||
if (ts->childClassName() == levels[i + 1].className)
|
// qDebug() << "checking" << ts->childClassName() << levels[i + 1].className;
|
||||||
data.tableSet = ts;
|
// if (ts->childClassName() == levels[i + 1].className)
|
||||||
|
// data.tableSet = ts;
|
||||||
|
// }
|
||||||
|
|
||||||
if (!data.tableSet)
|
// if (!data.tableSet)
|
||||||
qWarning() << "Dataset not found for" << data.className;
|
// qWarning() << "Dataset not found for" << data.className;
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
if (i) {
|
if (i) {
|
||||||
// if (!table->tableSet())
|
// if (!table->tableSet())
|
||||||
|
|
@ -214,12 +250,24 @@ Q_OUTOFLINE_TEMPLATE QList<T *> Query<T>::toList(int count)
|
||||||
} else {
|
} else {
|
||||||
// table->setTableSet(d->tableSet);
|
// table->setTableSet(d->tableSet);
|
||||||
// data.tableSet = 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<Table*>::iterator it;
|
||||||
|
for (it = data.rows.begin(); it != data.rows.end(); ++it) {
|
||||||
|
// qDebug() << *i
|
||||||
|
// add *i to it's parent row
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (m_autoDelete)
|
if (m_autoDelete)
|
||||||
deleteLater();
|
deleteLater();
|
||||||
|
|
||||||
return returnList;
|
return returnList;
|
||||||
/*
|
/*
|
||||||
QString pk = d->database->model().tableByName(d->tableName)->primaryKey();
|
QString pk = d->database->model().tableByName(d->tableName)->primaryKey();
|
||||||
|
|
|
||||||
|
|
@ -14,8 +14,7 @@
|
||||||
#include "post.h"
|
#include "post.h"
|
||||||
#include "comment.h"
|
#include "comment.h"
|
||||||
|
|
||||||
#define PRINT(x)
|
#define PRINT(x) qDebug() << #x "=" << x;
|
||||||
//qDebug() << #x "=" << x;
|
|
||||||
MainTest::MainTest(QObject *parent) : QObject(parent)
|
MainTest::MainTest(QObject *parent) : QObject(parent)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
@ -175,8 +174,8 @@ void MainTest::testDate()
|
||||||
void MainTest::join()
|
void MainTest::join()
|
||||||
{
|
{
|
||||||
auto q = db.comments()->query()
|
auto q = db.comments()->query()
|
||||||
->join<User>()
|
->join<Post>()
|
||||||
->join<Post>();
|
->join<User>();
|
||||||
|
|
||||||
Comment *comment = q->first();
|
Comment *comment = q->first();
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue