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 (TableModel *table, currentModel)
foreach (RelationModel *fk, sch->foregionKeys()) foreach (RelationModel *fk, table->foregionKeys())
fk->table = currentModel.tableByClassName(fk->className); fk->table = currentModel.tableByClassName(fk->className);
allTableMaps.insert(q->metaObject()->className(), currentModel); allTableMaps.insert(q->metaObject()->className(), currentModel);

View File

@ -32,6 +32,9 @@
# define NUT_EXPORT Q_DECL_EXPORT # define NUT_EXPORT Q_DECL_EXPORT
#endif #endif
#define NUT_INFO(type, name, value) \
Q_CLASSINFO(QT_STRINGIFY(__nut_NAME_PERFIX #name "\n" #type), #value)
// Database // Database
//TODO: remove minor version //TODO: remove minor version
#define NUT_DB_VERSION(version) \ #define NUT_DB_VERSION(version) \

View File

@ -44,6 +44,11 @@ NUT_BEGIN_NAMESPACE
* REFERENCES `account` (`id`) * REFERENCES `account` (`id`)
* ON DELETE CASCADE * ON DELETE CASCADE
* ON UPDATE 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) SqlGeneratorBase::SqlGeneratorBase(Database *parent)
: QObject((QObject *)parent) : QObject((QObject *)parent)
@ -180,6 +185,12 @@ QString SqlGeneratorBase::diff(TableModel *oldTable, TableModel *newTable)
QString SqlGeneratorBase::join(const QStringList &list) 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()) if (!list.count())
return ""; return "";
@ -192,12 +203,31 @@ QString SqlGeneratorBase::join(const QStringList &list)
QString ret = mainTable; QString ret = mainTable;
do { do {
QString t = model.tableByClassName(clone.first())->name(); QString table = model.tableByClassName(clone.first())->name();
RelationModel *rel = model.relationByTableNames(mainTable, t);
if (rel) {
clone.takeFirst(); clone.takeFirst();
ret.append(", " + _database->tableName(clone.takeFirst())); RelationModel *rel = model.relationByTableNames(mainTable, table);
if (rel) {
//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()); } while (clone.count());

View File

@ -209,7 +209,8 @@ Q_OUTOFLINE_TEMPLATE QList<T *> Query<T>::toList(int count)
childTable->setStatus(Table::FeatchedFromDB); childTable->setStatus(Table::FeatchedFromDB);
childTable->setTableSet(childTableSet); childTable->setTableSet(childTableSet);
childTable->clear(); childTable->clear();
childTableSet->add(childTable); addTableToSet(childTableSet, childTable);
// childTableSet->add(childTable);
} }
lastPkValue = q.value(pk); lastPkValue = q.value(pk);

View File

@ -1,5 +1,9 @@
#include "querybase_p.h" #include "querybase_p.h"
#include "table.h"
#include "tablesetbase_p.h"
NUT_BEGIN_NAMESPACE NUT_BEGIN_NAMESPACE
QueryBase::QueryBase(QObject *parent) : QObject(parent) 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 NUT_END_NAMESPACE

View File

@ -28,13 +28,16 @@
NUT_BEGIN_NAMESPACE NUT_BEGIN_NAMESPACE
//TODO: remove this class //TODO: remove this class
class Table;
class TableSetBase;
class QueryBase : public QObject class QueryBase : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit QueryBase(QObject *parent = 0); explicit QueryBase(QObject *parent = 0);
signals: protected:
void addTableToSet(TableSetBase *set, Table *table);
public slots: public slots:
}; };

View File

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

View File

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

View File

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

View File

@ -98,7 +98,7 @@ void MainTest::selectPosts()
auto q = db.posts()->query() auto q = db.posts()->query()
// q->join(Post::commentsTable()); // q->join(Post::commentsTable());
// q->join(Post::commentsTable()); // q->join(Post::commentsTable());
->join<User>() // ->join<User>()
->join<Comment>() ->join<Comment>()
->orderBy(!Post::saveDateField() & Post::bodyField()) ->orderBy(!Post::saveDateField() & Post::bodyField())
->setWhere(Post::idField() == postId); ->setWhere(Post::idField() == postId);
@ -158,8 +158,7 @@ void MainTest::testDate()
void MainTest::join() void MainTest::join()
{ {
auto q = db.comments()->query() auto q = db.comments()->query()
// ->join(Comment::author()) ->join<User>()
// ->join(Comment::post())
->join<Post>() ->join<Post>()
->setWhere(Comment::saveDateField() < QDateTime::currentDateTime().addDays(-1)) ->setWhere(Comment::saveDateField() < QDateTime::currentDateTime().addDays(-1))
->orderBy(Comment::saveDateField()); ->orderBy(Comment::saveDateField());

View File

@ -3,7 +3,7 @@
#include "tableset.h" #include "tableset.h"
Post::Post(QObject *parent) : Table(parent), 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))
{ {
} }