base join detector and code generator in sql generator base

This commit is contained in:
Hamed Masafi 2018-01-10 01:38:19 +03:30
parent e45860eb59
commit 597134dc23
11 changed files with 66 additions and 15 deletions

View File

@ -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);

View File

@ -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) \

View File

@ -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());

View File

@ -209,7 +209,8 @@ Q_OUTOFLINE_TEMPLATE QList<T *> Query<T>::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);

View File

@ -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

View File

@ -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:
};

View File

@ -211,6 +211,7 @@ TableModel::TableModel(int typeId, QString tableName)
fk->foregionColumn = value;
fk->className = value;
_foregionKeys.append(fk);
qDebug() << "REL="<<parts<<value<<fk;
}
if(propName == __nut_FIELD){

View File

@ -65,9 +65,9 @@ struct FieldModel{
};
struct RelationModel{
TableModel *table;
QString className;
QString localColumn;
TableModel *table;
QString foregionColumn;
};
class TableModel

View File

@ -40,7 +40,6 @@ public:
virtual int save(Database *db, bool cleanUp = false);
void clearChilds();
void add(Table* t);
QString childClassName() const;
Database *database() const;
@ -53,6 +52,12 @@ protected:
Database *_database;
Table *_table;
QString _childClassName;
private:
void add(Table* t);
friend class Table;
friend class QueryBase;
};
NUT_END_NAMESPACE

View File

@ -98,7 +98,7 @@ void MainTest::selectPosts()
auto q = db.posts()->query()
// q->join(Post::commentsTable());
// q->join(Post::commentsTable());
->join<User>()
// ->join<User>()
->join<Comment>()
->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<User>()
->join<Post>()
->setWhere(Comment::saveDateField() < QDateTime::currentDateTime().addDays(-1))
->orderBy(Comment::saveDateField());

View File

@ -3,7 +3,7 @@
#include "tableset.h"
Post::Post(QObject *parent) : Table(parent),
m_comments(new TableSet<Comment>(this)), m_id(0), m_title("")
m_id(0), m_title(""), m_comments(new TableSet<Comment>(this))
{
}