wip: model

This commit is contained in:
Hamed Masafi 2018-02-13 19:09:21 +03:30
parent 56588b9eb1
commit eb44eea463
16 changed files with 241 additions and 8 deletions

View File

@ -23,7 +23,9 @@ HEADERS += \
$$PWD/src/table.h \
$$PWD/src/database.h \
$$PWD/src/database_p.h \
$$PWD/src/serializableobject.h
$$PWD/src/serializableobject.h \
$$PWD/src/sqlmodel.h \
$$PWD/src/sqlmodel_p.h
SOURCES += \
$$PWD/src/generators/sqlgeneratorbase.cpp \
@ -42,4 +44,5 @@ SOURCES += \
$$PWD/src/wherephrase.cpp \
$$PWD/src/table.cpp \
$$PWD/src/database.cpp \
$$PWD/src/serializableobject.cpp
$$PWD/src/serializableobject.cpp \
$$PWD/src/sqlmodel.cpp

View File

@ -495,6 +495,12 @@ SqlGeneratorBase *Database::sqlGenertor() const
return d->sqlGenertor;
}
QSqlDatabase Database::database()
{
Q_D(Database);
return d->db;
}
void Database::databaseUpdated(QString oldVersion, QString newVersion)
{
Q_UNUSED(oldVersion);

View File

@ -69,6 +69,7 @@ public:
QString tableName(QString className);
SqlGeneratorBase *sqlGenertor() const;
QSqlDatabase database();
protected:
//remove minor version

View File

@ -63,7 +63,7 @@ public:
TableSet<ChangeLogTable> *changeLogs;
QT_DEPRECATED
QHash<QString, QString> tables;
QMap<QString, QString> tables;
static QMap<QString, DatabaseModel> allTableMaps;
static qulonglong lastId;

View File

@ -100,6 +100,7 @@ public: \
#define NUT_AUTO_INCREMENT(x) NUT_INFO(__nut_AUTO_INCREMENT, x, 0)
#define NUT_PRIMARY_AUTO_INCREMENT(x) NUT_PRIMARY_KEY(x) \
NUT_AUTO_INCREMENT(x)
#define NUT_DISPLAY_NAME(field, name) NUT_INFO(__nut_DISPLAY, field, name)
#define NUT_UNIQUE(x) NUT_INFO(__nut_UNIQUE, x, 0)
#define NUT_LEN(field, len) NUT_INFO(__nut_LEN, field, len)
#define NUT_DEFAULT_VALUE(x, n) NUT_INFO(__nut_DEFAULT_VALUE, x, n)

View File

@ -34,6 +34,7 @@
#define __nut_TABLE "table"
#define __nut_TABLE_NAME "table_name"
#define __nut_DISPLAY "display"
#define __nut_LEN "len"
#define __nut_DEFAULT_VALUE "def"
#define __nut_NOT_NULL "notnull"

View File

@ -100,11 +100,11 @@ public:
virtual QString phraseUpdate(const PhraseData *d) const;
virtual QString operatorString(const PhraseData::Condition &cond) const;
QString phraseOrder(const PhraseData *d) const;
private:
QString agregateText(const AgregateType &t, const QString &arg = QString::null) const;
QString fromTableText(const QString &tableName, QString &joinClassName, QString &orderBy) const;
QString createWhere(QList<WherePhrase> &wheres);
QString phraseOrder(const PhraseData *d) const;
void replaceTableNames(QString &command);
void removeTableNames(QString &command);
};

View File

@ -28,6 +28,7 @@
#include <QtCore/QMetaObject>
#include <QtSql/QSqlResult>
#include <QtSql/QSqlError>
#include <QtSql/QSqlQueryModel>
#include "query_p.h"
#include "database.h"
@ -68,6 +69,7 @@ public:
// Query<T> *orderBy(QString fieldName, QString type);
Query<T> *skip(int n);
Query<T> *take(int n);
Query<T> *fields(WherePhrase phrase);
Query<T> *orderBy(WherePhrase phrase);
Query<T> *include(TableSetBase *t);
Query<T> *include(Table *t);
@ -86,6 +88,8 @@ public:
int update(WherePhrase phrase);
int remove();
QSqlQueryModel *toModal();
//debug purpose
QString sqlCommand() const;
};
@ -251,6 +255,9 @@ Q_OUTOFLINE_TEMPLATE QList<T *> Query<T>::toList(int count)
= QMetaType::metaObjectForType(data.table->typeId());
table = qobject_cast<Table *>(childMetaObject->newInstance());
if (!table)
qFatal("Could not create instance of %s",
qPrintable(data.table->name()));
}
QStringList childFields = data.table->fieldsNames();
@ -446,6 +453,14 @@ Q_OUTOFLINE_TEMPLATE Query<T> *Query<T>::take(int n)
return this;
}
template<class T>
Q_OUTOFLINE_TEMPLATE Query<T> *Query<T>::fields(WherePhrase phrase)
{
Q_D(Query);
d->fields.append(phrase);
return this;
}
//template <class T>
//Q_OUTOFLINE_TEMPLATE Query<T> *Query<T>::orderBy(QString fieldName,
// QString type)
@ -504,6 +519,37 @@ Q_OUTOFLINE_TEMPLATE int Query<T>::remove()
return q.numRowsAffected();
}
template <class T>
Q_OUTOFLINE_TEMPLATE QSqlQueryModel *Query<T>::toModal()
{
Q_D(Query);
QString fff = "";
foreach (WherePhrase p, d->fields) {
if (fff != "")
fff.append(", ");
fff.append(d->database->sqlGenertor()->phraseOrder(p.data()));
}
d->sql = d->database->sqlGenertor()->selectCommand(
SqlGeneratorBase::SelectAll, "",
d->tableName,
d->wheres, d->orderPhrases, d->relations,
d->skip, d->take);
qDebug()<<"Model to" << fff;
QSqlQueryModel *model = new QSqlQueryModel;
TableModel *tableModel = d->database->model().tableByName(d->tableName);
model->setQuery(d->sql, d->database->database());
int fieldIndex = 0;
foreach (FieldModel *f, tableModel->fields()) {
model->setHeaderData(fieldIndex++, Qt::Horizontal, f->displayName);
}
return model;
}
template <class T>
Q_OUTOFLINE_TEMPLATE QString Query<T>::sqlCommand() const
{

View File

@ -50,6 +50,7 @@ public:
QList<RelationModel*> relations;
QList<WherePhrase> wheres;
QList<WherePhrase> orderPhrases;
QList<WherePhrase> fields;
QHash<QString, QString> orders;
int skip;
int take;

88
src/sqlmodel.cpp Normal file
View File

@ -0,0 +1,88 @@
/**************************************************************************
**
** This file is part of Nut project.
** https://github.com/HamedMasafi/Nut
**
** Nut is free software: you can redistribute it and/or modify
** it under the terms of the GNU Lesser General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Nut is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public License
** along with Nut. If not, see <http://www.gnu.org/licenses/>.
**
**************************************************************************/
#include "database.h"
#include "tablesetbase_p.h"
#include "databasemodel.h"
#include "sqlmodel_p.h"
#include "sqlmodel.h"
NUT_BEGIN_NAMESPACE
SqlModel::SqlModel(Database *database, TableSetBase *tableSet, QObject *parent) :
QAbstractTableModel(parent)
{
Q_D(SqlModel);
d->model = database->model()
.tableByClassName(tableSet->childClassName());
d->tableName = d->model->name();
// setQuery("SELECT * FROM " + d->tableName, database->databaseName());
}
int SqlModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
Q_D(const SqlModel);
return d->rows.count();
}
int SqlModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
Q_D(const SqlModel);
return d->model->fields().count();
}
QVariant SqlModel::data(const QModelIndex &index, int role) const
{
Q_D(const SqlModel);
if (!index.isValid())
return QVariant();
if (index.row() >= d->rows.count() || index.row() < 0)
return QVariant("-");
if (role == Qt::DisplayRole) {
Table *t = d->rows.at(index.row());
return t->property(d->model->field(index.column())->name.toLocal8Bit().data());
// LogData *d = dataList.at(index.row());
// switch (index.column()) {
// case COL_ID:
// return index.row() + 1;
// case COL_Type: {
// return typeText(d->type);
// }
// case COL_TITLE:
// return d->title;
// case COL_File:
// return d->file;
// case COL_Function:
// return d->function;
// case COL_Line:
// return d->line;
// }
}
return QVariant();
}
NUT_END_NAMESPACE

52
src/sqlmodel.h Normal file
View File

@ -0,0 +1,52 @@
/**************************************************************************
**
** This file is part of Nut project.
** https://github.com/HamedMasafi/Nut
**
** Nut is free software: you can redistribute it and/or modify
** it under the terms of the GNU Lesser General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Nut is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public License
** along with Nut. If not, see <http://www.gnu.org/licenses/>.
**
**************************************************************************/
#ifndef SQLMODEL_H
#define SQLMODEL_H
#include <QtCore/QAbstractTableModel>
#include "defines.h"
NUT_BEGIN_NAMESPACE
class Database;
class TableSetBase;
class SqlModelPrivate;
class Table;
class TableModel;
class SqlModel : public QAbstractTableModel
{
Q_OBJECT
public:
SqlModel(Database *database, TableSetBase *tableSet, QObject *parent = Q_NULLPTR);
int rowCount(const QModelIndex &parent) const;
int columnCount(const QModelIndex &parent) const;
QVariant data(const QModelIndex &index, int role) const;
private:
SqlModelPrivate *d_ptr;
Q_DECLARE_PRIVATE(SqlModel)
};
NUT_END_NAMESPACE
#endif // SQLMODEL_H

24
src/sqlmodel_p.h Normal file
View File

@ -0,0 +1,24 @@
#ifndef SQLMODEL_P_H
#define SQLMODEL_P_H
#include <QString>
#include "defines.h"
NUT_BEGIN_NAMESPACE
class SqlModel;
class Table;
class TableModel;
class SqlModelPrivate {
SqlModel *q_ptr;
Q_DECLARE_PUBLIC(SqlModel)
public:
QString tableName;
QList<Table*> rows;
TableModel *model;
};
NUT_END_NAMESPACE
#endif // SQLMODEL_P_H

View File

@ -66,6 +66,14 @@ void TableModel::setTypeId(const int &typeId)
_typeId = typeId;
}
FieldModel *TableModel::field(int n) const
{
if (n < 0 || n >= _fields.count())
return 0;
return _fields.at(n);
}
FieldModel *TableModel::field(QString name) const
{
foreach (FieldModel *f, _fields)
@ -195,7 +203,7 @@ TableModel::TableModel(int typeId, QString tableName)
if(type == __nut_FIELD){
FieldModel *f = new FieldModel;
f->name = name;
f->name = f->displayName = name;
_fields.append(f);
}
}
@ -255,8 +263,8 @@ TableModel::TableModel(int typeId, QString tableName)
f->isAutoIncrement = true;
else if(type == __nut_UNIQUE)
f->isUnique = true;
else if(type == __nut_DISPLAY)
f->displayName = value;
}
if(!findByTypeId(typeId) && !tableName.isNull())

View File

@ -47,6 +47,7 @@ struct FieldModel{
bool isPrimaryKey;
bool isAutoIncrement;
bool isUnique;
QString displayName;
bool operator ==(const FieldModel &f) const{
@ -88,6 +89,7 @@ public:
// static void createForegionKeys();
// static TableModel* model(QString className);
FieldModel *field(int n) const;
FieldModel *field(QString name) const;
RelationModel *foregionKey(QString otherTable) const;

View File

@ -19,3 +19,4 @@
**************************************************************************/
#include "tableset.h"

View File

@ -28,7 +28,6 @@
#include <QtSql/QSqlQuery>
#include "tablesetbase_p.h"
//#include "database.h"
#include "table.h"
NUT_BEGIN_NAMESPACE