Nut/src/databasemodel.cpp

179 lines
4.3 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
#include <QJsonObject>
2017-02-01 18:01:21 +08:00
NUT_BEGIN_NAMESPACE
2016-05-21 16:09:03 +08:00
DatabaseModel::DatabaseModel() : QList<TableModel*>(), _versionMajor(0), _versionMinor(0)
2016-05-12 14:08:58 +08:00
{
}
2017-02-01 18:01:21 +08:00
DatabaseModel::DatabaseModel(const DatabaseModel &other) : QList<TableModel*>(other), _versionMajor(0), _versionMinor(0)
{
}
2016-05-21 16:09:03 +08:00
TableModel *DatabaseModel::model(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
qWarning("Table with name '%s' not found in model",
qUtf8Printable(tableName));
2016-05-12 14:08:58 +08:00
return 0;
}
2016-05-21 16:09:03 +08:00
TableModel *DatabaseModel::modelByClass(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-06-04 16:16:06 +08:00
qDebug() << s->className();
2016-05-12 14:08:58 +08:00
if(s->className() == className)
return s;
}
2017-05-28 23:08:59 +08:00
qWarning("Table with class name '%s' not found in model",
qUtf8Printable(className));
2017-06-04 16:16:06 +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);
TableModel *others = other.model(mine->name());
2016-05-12 14:08:58 +08:00
if(!others)
return false;
if(*mine != *others)
return false;
}
return true;
}
QJsonObject DatabaseModel::toJson() const
{
QJsonObject obj;
2016-05-21 16:09:03 +08:00
// obj.insert(QT_STRINGIFY(versionMajor), QJsonValue(_versionMajor));
// obj.insert(QT_STRINGIFY(versionMinor), QJsonValue(_versionMinor));
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);
2016-05-12 14:08:58 +08:00
obj.insert(s->name(), s->toJson());
}
return obj;
}
2016-05-21 16:09:03 +08:00
RelationModel *DatabaseModel::relationByClassNames(QString masterClassName, QString childClassName)
2016-05-12 14:08:58 +08:00
{
2016-05-21 16:09:03 +08:00
TableModel *childTable = modelByClass(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;
}
2016-05-21 16:09:03 +08:00
RelationModel *DatabaseModel::relationByTableNames(QString masterTableName, QString childTableName)
2016-05-12 14:08:58 +08:00
{
2016-05-21 16:09:03 +08:00
TableModel *childTable = model(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)
{
DatabaseModel model;
2016-05-21 16:09:03 +08:00
// model.setVersionMajor(json.value(QT_STRINGIFY(versionMajor)).toInt());
// model.setVersionMinor(json.value(QT_STRINGIFY(versionMinor)).toInt());
2016-05-12 14:08:58 +08:00
foreach (QString key, json.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
int DatabaseModel::versionMajor() const
{
return _versionMajor;
}
void DatabaseModel::setVersionMajor(int versionMajor)
{
_versionMajor = versionMajor;
}
int DatabaseModel::versionMinor() const
{
return _versionMinor;
}
void DatabaseModel::setVersionMinor(int versionMinor)
{
_versionMinor = versionMinor;
}
2017-02-01 18:01:21 +08:00
bool DatabaseModel::remove(QString tableName)
{
for(int i = 0; i < size(); i++){
TableModel *s = at(i);
if(s->name() == tableName){
removeAt(i);
return true;
}
}
return false;
}
NUT_END_NAMESPACE