Nut/src/tablemodel.h

131 lines
3.5 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{
FieldModel() : name(QString::null), length(0), defaultValue(QString::null),
notNull(false), isPrimaryKey(false), isAutoIncrement(false), isUnique(false)
2016-05-12 14:08:58 +08:00
{
}
QString name;
2017-09-10 22:18:20 +08:00
//TODO: QMetaType::Type??
2016-05-12 14:08:58 +08:00
QVariant::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;
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);
}
};
2016-05-21 16:09:03 +08:00
struct RelationModel{
2018-01-13 23:59:55 +08:00
//slave
2016-05-12 14:08:58 +08:00
QString localColumn;
2018-01-13 23:59:55 +08:00
TableModel *slaveTable;
//master
2016-05-12 14:08:58 +08:00
QString foregionColumn;
2018-01-13 23:59:55 +08:00
TableModel *masterTable;
QString masterClassName;
2016-05-12 14:08:58 +08:00
};
2016-05-21 16:09:03 +08:00
class TableModel
2016-05-12 14:08:58 +08:00
{
public:
2017-05-28 23:08:59 +08:00
TableModel(int typeId, QString tableName = QString::null);
2016-05-21 16:09:03 +08:00
TableModel(QJsonObject json, QString tableName);
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
2016-05-21 16:09:03 +08:00
FieldModel *field(QString name) const;
RelationModel *foregionKey(QString otherTable) 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;
static QSet<TableModel *> allModels();
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);
2016-05-21 16:09:03 +08:00
static TableModel *findByClassName(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;
QList<RelationModel*> _foregionKeys;
static QSet<TableModel*>_allModels;
2018-01-11 18:08:45 +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