Nut/src/tablemodel.h

153 lines
4.2 KiB
C
Raw Normal View History

2016-05-12 14:08:58 +08:00
/**************************************************************************
**
** 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 TABLESCHEEMA_H
#define TABLESCHEEMA_H
#include <QtCore/QVariant>
#include <QDebug>
2017-02-01 18:01:21 +08:00
#include "defines.h"
2016-05-12 14:08:58 +08:00
class QJsonObject;
2017-02-01 18:01:21 +08:00
NUT_BEGIN_NAMESPACE
class TableModel;
2016-05-21 16:09:03 +08:00
struct FieldModel{
explicit FieldModel() : name(QString()), length(0), defaultValue(QString()),
2016-05-21 16:09:03 +08:00
notNull(false), isPrimaryKey(false), isAutoIncrement(false), isUnique(false)
2016-05-12 14:08:58 +08:00
{
}
2018-02-26 18:14:36 +08:00
explicit FieldModel(const QJsonObject &json);
2018-02-24 23:43:15 +08:00
2016-05-12 14:08:58 +08:00
QString name;
2019-01-21 00:55:32 +08:00
QMetaType::Type type;
QString typeName;
2016-05-12 14:08:58 +08:00
int length;
QString defaultValue;
bool notNull;
bool isPrimaryKey;
bool isAutoIncrement;
2016-05-21 16:09:03 +08:00
bool isUnique;
2018-02-13 23:39:21 +08:00
QString displayName;
2016-05-12 14:08:58 +08:00
2016-05-21 16:09:03 +08:00
bool operator ==(const FieldModel &f) const{
2016-05-12 14:08:58 +08:00
2017-02-01 18:01:21 +08:00
bool b = name.toLower() == f.name.toLower()
2016-05-12 14:08:58 +08:00
&& type == f.type
&& length == f.length
&& defaultValue == f.defaultValue
&& notNull == f.notNull;
return b;
}
2016-05-21 16:09:03 +08:00
bool operator !=(const FieldModel &f) const{
2016-05-12 14:08:58 +08:00
return !(*this == f);
}
2018-02-24 23:43:15 +08:00
QJsonObject toJson() const;
2016-05-12 14:08:58 +08:00
};
2016-05-21 16:09:03 +08:00
struct RelationModel{
2019-01-21 00:29:54 +08:00
RelationModel() : localColumn(QString()), localProperty(QString()),
slaveTable(nullptr), foreignColumn(QString()), masterTable(nullptr),
masterClassName(QString())
2018-02-24 23:43:15 +08:00
{}
2018-02-26 18:14:36 +08:00
explicit RelationModel(const QJsonObject &obj);
2018-02-24 23:43:15 +08:00
2018-01-13 23:59:55 +08:00
//slave
2016-05-12 14:08:58 +08:00
QString localColumn;
2018-01-15 21:50:40 +08:00
QString localProperty;
2018-01-13 23:59:55 +08:00
TableModel *slaveTable;
//master
2018-02-24 23:43:15 +08:00
QString foreignColumn;
2018-01-13 23:59:55 +08:00
TableModel *masterTable;
QString masterClassName;
2018-02-24 23:43:15 +08:00
QJsonObject toJson() const;
2016-05-12 14:08:58 +08:00
};
2018-02-24 23:43:15 +08:00
bool operator ==(const RelationModel &l, const RelationModel &r);
bool operator !=(const RelationModel &l, const RelationModel &r);
2018-03-03 22:36:04 +08:00
class TableModel
2016-05-12 14:08:58 +08:00
{
public:
2019-02-10 22:11:22 +08:00
explicit TableModel(int typeId, const QString &tableName = QString());
explicit TableModel(const QJsonObject &json, const QString &tableName);
2018-03-11 21:13:13 +08:00
virtual ~TableModel();
2016-05-12 14:08:58 +08:00
QJsonObject toJson() const;
// static TableScheema *registerTable(int typeId, QString tableName);
// static void createForegionKeys();
2017-02-01 18:01:21 +08:00
// static TableModel* model(QString className);
2016-05-12 14:08:58 +08:00
2018-02-13 23:39:21 +08:00
FieldModel *field(int n) const;
2019-02-10 22:11:22 +08:00
FieldModel *field(const QString &name) const;
2018-02-24 23:43:15 +08:00
RelationModel *foregionKey(const QString &otherTable) const;
RelationModel *foregionKeyByField(const QString &fieldName) const;
2016-05-12 14:08:58 +08:00
QString toString() const;
QString primaryKey() const;
QString name() const;
void setName(const QString &name);
QString className() const;
void setClassName(const QString &className);
int typeId() const;
void setTypeId(const int &typeId);
2016-05-21 16:09:03 +08:00
QList<FieldModel *> fields() const;
QList<RelationModel *> foregionKeys() const;
2016-05-12 14:08:58 +08:00
QStringList fieldsNames() const;
2019-02-25 22:55:16 +08:00
Q_DECL_DEPRECATED
static QSet<TableModel *> allModels();
2019-02-25 22:55:16 +08:00
Q_DECL_DEPRECATED
2016-05-21 16:09:03 +08:00
static TableModel *findByTypeId(int typeId);
2017-05-28 23:08:59 +08:00
// static TableModel *findByName(QString name);
2019-02-25 22:55:16 +08:00
Q_DECL_DEPRECATED
2019-02-10 22:11:22 +08:00
static TableModel *findByClassName(const QString &className);
2016-05-12 14:08:58 +08:00
2016-05-21 16:09:03 +08:00
bool operator ==(const TableModel &t) const;
bool operator !=(const TableModel &t) const;
2016-05-12 14:08:58 +08:00
private:
QString _name;
QString _className;
int _typeId;
2016-05-21 16:09:03 +08:00
QList<FieldModel*> _fields;
2018-02-24 23:43:15 +08:00
QList<RelationModel*> _foreignKeys;
2016-05-21 16:09:03 +08:00
static QSet<TableModel*>_allModels;
2018-04-08 23:16:44 +08:00
// bool checkClassInfo(const QMetaClassInfo &classInfo,
// QString &type, QString &name, QString &value);
2016-05-12 14:08:58 +08:00
};
2017-02-01 18:01:21 +08:00
NUT_END_NAMESPACE
2016-05-12 14:08:58 +08:00
#endif // TABLESCHEEMA_H