Nut/src/databasemodel.cpp

216 lines
5.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/>.
**
**************************************************************************/
#include "databasemodel.h"
2016-05-21 16:09:03 +08:00
#include "tablemodel.h"
2016-05-12 14:08:58 +08:00
2018-01-08 17:08:10 +08:00
#include <QJsonArray>
2016-05-12 14:08:58 +08:00
#include <QJsonObject>
2017-02-01 18:01:21 +08:00
NUT_BEGIN_NAMESPACE
2016-05-21 16:09:03 +08:00
2017-08-10 23:09:41 +08:00
QMap<QString, DatabaseModel*> DatabaseModel::_models;
2016-05-12 14:08:58 +08:00
2018-01-08 17:08:10 +08:00
#define NODE_VERSION "version"
#define NODE_TABLES "tables"
2017-10-07 13:38:09 +08:00
DatabaseModel::DatabaseModel(const QString &name) : QList<TableModel*>(), _databaseClassName(name), _version(QString::null)
2017-08-10 23:09:41 +08:00
{
_models.insert(name, this);
2016-05-12 14:08:58 +08:00
}
2017-10-07 13:38:09 +08:00
DatabaseModel::DatabaseModel(const DatabaseModel &other) : QList<TableModel*>(other), _version(QString::null)
2017-02-01 18:01:21 +08:00
{
}
2017-10-07 18:26:05 +08:00
DatabaseModel::DatabaseModel(const QJsonObject &json) : QList<TableModel*>()
{
2018-01-08 17:08:10 +08:00
setVersion(json.value(NODE_VERSION).toString());
2017-10-07 18:26:05 +08:00
2018-01-08 17:08:10 +08:00
QJsonObject tables = json.value(NODE_TABLES).toObject();
foreach (QString key, tables.keys()) {
if(!tables.value(key).isObject())
2017-10-07 18:26:05 +08:00
continue;
2018-01-08 17:08:10 +08:00
TableModel *sch = new TableModel(tables.value(key).toObject(), key);
2017-10-07 18:26:05 +08:00
append(sch);
}
}
2017-08-10 23:09:41 +08:00
TableModel *DatabaseModel::tableByName(QString tableName) const
2016-05-12 14:08:58 +08:00
{
for(int i = 0; i < size(); i++){
2016-05-21 16:09:03 +08:00
TableModel *s = at(i);
2017-02-01 18:01:21 +08:00
2016-05-12 14:08:58 +08:00
if(s->name() == tableName)
return s;
}
2017-05-28 23:08:59 +08:00
2018-01-08 19:56:58 +08:00
// qWarning("Table with name '%s' not found in model",
// qUtf8Printable(tableName));
2016-05-12 14:08:58 +08:00
return 0;
}
2017-08-10 23:09:41 +08:00
TableModel *DatabaseModel::tableByClassName(QString className) const
2016-05-12 14:08:58 +08:00
{
for(int i = 0; i < size(); i++){
2016-05-21 16:09:03 +08:00
TableModel *s = at(i);
2017-07-02 22:12:15 +08:00
2016-05-12 14:08:58 +08:00
if(s->className() == className)
return s;
}
2017-05-28 23:08:59 +08:00
2018-01-08 19:56:58 +08:00
// qWarning("Table with class name '%s' not found in model",
// qUtf8Printable(className));
2018-01-11 18:08:45 +08:00
Q_UNREACHABLE();
2016-05-12 14:08:58 +08:00
return 0;
}
bool DatabaseModel::operator ==(const DatabaseModel &other) const
{
if(size() != other.size())
return false;
for(int i = 0; i < size(); i++){
2016-05-21 16:09:03 +08:00
TableModel *mine = at(i);
2017-08-10 23:09:41 +08:00
TableModel *others = other.tableByName(mine->name());
2016-05-12 14:08:58 +08:00
if(!others)
return false;
if(*mine != *others)
return false;
}
return true;
}
2018-01-08 17:08:10 +08:00
DatabaseModel DatabaseModel::operator +(const DatabaseModel &other)
{
DatabaseModel model;
DatabaseModel::const_iterator i;
for (i = constBegin(); i != constEnd(); ++i)
model.append(*i);
for (i = other.constBegin(); i != other.constEnd(); ++i)
model.append(*i);
return model;
}
2016-05-12 14:08:58 +08:00
QJsonObject DatabaseModel::toJson() const
{
QJsonObject obj;
2018-01-08 19:56:58 +08:00
obj.insert(NODE_VERSION, QJsonValue(_version));
2018-01-08 17:08:10 +08:00
QJsonObject tables;
2016-05-12 14:08:58 +08:00
for(int i = 0; i < size(); i++){
2016-05-21 16:09:03 +08:00
TableModel *s = at(i);
2018-01-08 17:08:10 +08:00
tables.insert(s->name(), s->toJson());
2016-05-12 14:08:58 +08:00
}
2018-01-08 17:08:10 +08:00
obj.insert(NODE_TABLES, tables);
2016-05-12 14:08:58 +08:00
return obj;
}
2017-10-07 18:26:05 +08:00
DatabaseModel::operator QJsonObject()
{
return toJson();
}
2017-08-10 23:09:41 +08:00
RelationModel *DatabaseModel::relationByClassNames(const QString &masterClassName, const QString &childClassName)
2016-05-12 14:08:58 +08:00
{
2017-08-10 23:09:41 +08:00
TableModel *childTable = tableByClassName(childClassName);
2016-05-12 14:08:58 +08:00
if(!childTable)
return 0;
2016-05-21 16:09:03 +08:00
foreach (RelationModel *rel, childTable->foregionKeys())
2016-05-12 14:08:58 +08:00
if(rel->className == masterClassName)
return rel;
return 0;
}
2017-08-10 23:09:41 +08:00
RelationModel *DatabaseModel::relationByTableNames(const QString &masterTableName, const QString &childTableName)
2016-05-12 14:08:58 +08:00
{
2017-08-10 23:09:41 +08:00
TableModel *childTable = tableByName(childTableName);
2016-05-12 14:08:58 +08:00
if(!childTable)
return 0;
2016-05-21 16:09:03 +08:00
foreach (RelationModel *rel, childTable->foregionKeys())
2016-05-12 14:08:58 +08:00
if(rel->table->name() == masterTableName)
return rel;
return 0;
}
DatabaseModel DatabaseModel::fromJson(QJsonObject &json)
{
2017-08-10 23:09:41 +08:00
DatabaseModel model(QString::null);
2016-05-21 16:09:03 +08:00
2018-01-08 17:08:10 +08:00
model.setVersion(json.value(NODE_VERSION).toString());
2016-05-21 16:09:03 +08:00
2018-01-08 17:08:10 +08:00
QJsonObject tables = json.value(NODE_TABLES).toObject();
foreach (QString key, tables.keys()) {
2016-05-21 16:09:03 +08:00
if(!json.value(key).isObject())
continue;
TableModel *sch = new TableModel(json.value(key).toObject(), key);
2016-05-12 14:08:58 +08:00
model.append(sch);
}
return model;
}
2016-05-21 16:09:03 +08:00
2017-10-07 13:38:09 +08:00
QString DatabaseModel::version() const
2016-05-21 16:09:03 +08:00
{
2017-10-07 13:38:09 +08:00
return _version;
2016-05-21 16:09:03 +08:00
}
2017-10-07 13:38:09 +08:00
void DatabaseModel::setVersion(QString version)
2016-05-21 16:09:03 +08:00
{
2017-10-07 13:38:09 +08:00
_version = version;
2016-05-21 16:09:03 +08:00
}
2017-08-10 23:09:41 +08:00
bool DatabaseModel::remove(const QString &tableName)
2017-02-01 18:01:21 +08:00
{
for(int i = 0; i < size(); i++){
TableModel *s = at(i);
if(s->name() == tableName){
removeAt(i);
return true;
}
}
return false;
}
2017-08-10 23:09:41 +08:00
DatabaseModel *DatabaseModel::modelByName(const QString &name)
{
if (_models.contains(name))
return _models[name];
return Q_NULLPTR;
}
2017-02-01 18:01:21 +08:00
NUT_END_NAMESPACE