diff --git a/src/database.cpp b/src/database.cpp index 6135f42..f9824d9 100644 --- a/src/database.cpp +++ b/src/database.cpp @@ -227,8 +227,8 @@ bool DatabasePrivate::getCurrectScheema() } } - foreach (TableModel *sch, currentModel) - foreach (RelationModel *fk, sch->foregionKeys()) + foreach (TableModel *table, currentModel) + foreach (RelationModel *fk, table->foregionKeys()) fk->table = currentModel.tableByClassName(fk->className); allTableMaps.insert(q->metaObject()->className(), currentModel); diff --git a/src/defines.h b/src/defines.h index a03b05d..f5c965a 100644 --- a/src/defines.h +++ b/src/defines.h @@ -32,6 +32,9 @@ # define NUT_EXPORT Q_DECL_EXPORT #endif +#define NUT_INFO(type, name, value) \ + Q_CLASSINFO(QT_STRINGIFY(__nut_NAME_PERFIX #name "\n" #type), #value) + // Database //TODO: remove minor version #define NUT_DB_VERSION(version) \ diff --git a/src/generators/sqlgeneratorbase.cpp b/src/generators/sqlgeneratorbase.cpp index 4d80067..9619336 100644 --- a/src/generators/sqlgeneratorbase.cpp +++ b/src/generators/sqlgeneratorbase.cpp @@ -44,6 +44,11 @@ NUT_BEGIN_NAMESPACE * REFERENCES `account` (`id`) * ON DELETE CASCADE * ON UPDATE CASCADE; + * + * SELECT + * FROM dbo.GiftTypes + * INNER JOIN dbo.GiftCards ON dbo.GiftTypes.GiftTypeID = dbo.GiftCards.GiftTypeID + * INNER JOIN dbo.Entities ON dbo.GiftCards.GiftCardID = dbo.Entities.GiftCardID */ SqlGeneratorBase::SqlGeneratorBase(Database *parent) : QObject((QObject *)parent) @@ -180,6 +185,12 @@ QString SqlGeneratorBase::diff(TableModel *oldTable, TableModel *newTable) QString SqlGeneratorBase::join(const QStringList &list) { + //TODO: make this ungly code better and bugless :-) + /* + * Known issues: + * Support onle near joins, far supports with medium table finding not support yet + */ + if (!list.count()) return ""; @@ -192,12 +203,31 @@ QString SqlGeneratorBase::join(const QStringList &list) QString ret = mainTable; do { - QString t = model.tableByClassName(clone.first())->name(); - RelationModel *rel = model.relationByTableNames(mainTable, t); - + QString table = model.tableByClassName(clone.first())->name(); + clone.takeFirst(); + RelationModel *rel = model.relationByTableNames(mainTable, table); if (rel) { - clone.takeFirst(); - ret.append(", " + _database->tableName(clone.takeFirst())); + //mainTable is master of table + ret.append(QString(" INNER JOIN %1 ON %4.%2 = %1.%3") + .arg(table) + .arg(rel->table->primaryKey()) + .arg(rel->localColumn) + .arg(mainTable)); + + } else{ + rel = model.relationByTableNames(table, mainTable); + if (rel) { + // table is master of mainTable + ret.append(QString(" INNER JOIN %1 ON %4.%2 = %1.%3") + .arg(table) + .arg(rel->localColumn) + .arg(rel->table->primaryKey()) + .arg(mainTable)); + + } else { + qInfo("Relation for %s and %s not exists", + qPrintable(table), qPrintable(mainTable)); + } } } while (clone.count()); diff --git a/src/query.h b/src/query.h index 4d14886..8f4ad76 100644 --- a/src/query.h +++ b/src/query.h @@ -209,7 +209,8 @@ Q_OUTOFLINE_TEMPLATE QList Query::toList(int count) childTable->setStatus(Table::FeatchedFromDB); childTable->setTableSet(childTableSet); childTable->clear(); - childTableSet->add(childTable); + addTableToSet(childTableSet, childTable); +// childTableSet->add(childTable); } lastPkValue = q.value(pk); diff --git a/src/querybase.cpp b/src/querybase.cpp index 56f93d0..5a9526d 100644 --- a/src/querybase.cpp +++ b/src/querybase.cpp @@ -1,5 +1,9 @@ #include "querybase_p.h" +#include "table.h" +#include "tablesetbase_p.h" + + NUT_BEGIN_NAMESPACE QueryBase::QueryBase(QObject *parent) : QObject(parent) @@ -7,4 +11,9 @@ QueryBase::QueryBase(QObject *parent) : QObject(parent) } +void QueryBase::addTableToSet(TableSetBase *set, Table *table) +{ + set->add(table); +} + NUT_END_NAMESPACE diff --git a/src/querybase_p.h b/src/querybase_p.h index c5f370d..4d53ce3 100644 --- a/src/querybase_p.h +++ b/src/querybase_p.h @@ -28,13 +28,16 @@ NUT_BEGIN_NAMESPACE //TODO: remove this class +class Table; +class TableSetBase; class QueryBase : public QObject { Q_OBJECT public: explicit QueryBase(QObject *parent = 0); -signals: +protected: + void addTableToSet(TableSetBase *set, Table *table); public slots: }; diff --git a/src/tablemodel.cpp b/src/tablemodel.cpp index 7e3758a..5696ff6 100644 --- a/src/tablemodel.cpp +++ b/src/tablemodel.cpp @@ -211,6 +211,7 @@ TableModel::TableModel(int typeId, QString tableName) fk->foregionColumn = value; fk->className = value; _foregionKeys.append(fk); + qDebug() << "REL="<query() // q->join(Post::commentsTable()); // q->join(Post::commentsTable()); - ->join() +// ->join() ->join() ->orderBy(!Post::saveDateField() & Post::bodyField()) ->setWhere(Post::idField() == postId); @@ -158,8 +158,7 @@ void MainTest::testDate() void MainTest::join() { auto q = db.comments()->query() -// ->join(Comment::author()) -// ->join(Comment::post()) + ->join() ->join() ->setWhere(Comment::saveDateField() < QDateTime::currentDateTime().addDays(-1)) ->orderBy(Comment::saveDateField()); diff --git a/test/common/post.cpp b/test/common/post.cpp index 62155fb..bf980f6 100644 --- a/test/common/post.cpp +++ b/test/common/post.cpp @@ -3,7 +3,7 @@ #include "tableset.h" Post::Post(QObject *parent) : Table(parent), - m_comments(new TableSet(this)), m_id(0), m_title("") + m_id(0), m_title(""), m_comments(new TableSet(this)) { }