779 lines
27 KiB
C++
779 lines
27 KiB
C++
|
|
#include <QJsonArray>
|
|||
|
|
#include "projectModelManager.h"
|
|||
|
|
#include "dataBase.h"
|
|||
|
|
#include "logger.h"
|
|||
|
|
const QSet<QString> stringDataTypes = {"VARCHAR", "CHAR", "TEXT", "DATE", "TIME", "TIMESTAMP","JSONB","JSON"};
|
|||
|
|
|
|||
|
|
ProjectModelManager& ProjectModelManager::instance()
|
|||
|
|
{
|
|||
|
|
//采用静态局部变量的方式,静态局部变量的初始化是在第一次访问时,以后的调用不会多次初始化,并且生命周期和程序一致
|
|||
|
|
static ProjectModelManager instance;
|
|||
|
|
return instance;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
ProjectModelManager::ProjectModelManager(QObject *parent)
|
|||
|
|
: QObject(parent)
|
|||
|
|
{
|
|||
|
|
_bInitialised = false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
ProjectModelManager::~ProjectModelManager()
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void ProjectModelManager::initialModel()
|
|||
|
|
{
|
|||
|
|
if(_bInitialised)
|
|||
|
|
return;
|
|||
|
|
QStringList lstModel = getModelList();
|
|||
|
|
for(auto &model:lstModel)
|
|||
|
|
{
|
|||
|
|
MapProject mp;
|
|||
|
|
QMap<QString,int> mapProject = DataBase::GetInstance()->getProjectFromManager(model);
|
|||
|
|
|
|||
|
|
|
|||
|
|
QMap<QString,int>::Iterator iter;
|
|||
|
|
for(iter = mapProject.begin();iter != mapProject.end(); ++iter)
|
|||
|
|
{
|
|||
|
|
//QStandardItem* propertyItem = new QStandardItem(iter.key());
|
|||
|
|
|
|||
|
|
PropertyModel pm;
|
|||
|
|
pm.pBase = new QStandardItemModel(this);
|
|||
|
|
pm.pSelect = new QStandardItemModel(this);
|
|||
|
|
pm.mapProperty = addNewProject(model,iter.key(),pm);
|
|||
|
|
pm.nType = iter.value();
|
|||
|
|
pm.formerMeta.sName = model;
|
|||
|
|
pm.formerProject.sName = iter.key();
|
|||
|
|
pm.dataInfo = DataBase::GetInstance()->getProjectModelGroupInfo(iter.key());
|
|||
|
|
mp.insert(iter.key(),pm);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//todo:读取存储,按分类遍历名称
|
|||
|
|
m_mapTotalData.insert(model,mp);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
_bInitialised = true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
void ProjectModelManager::generate(const QString& sMeta,const QString& sPro)
|
|||
|
|
{
|
|||
|
|
MapMeta::Iterator iter = m_mapTotalData.find(sMeta); //获取元模下的工程
|
|||
|
|
if(iter != m_mapTotalData.end())
|
|||
|
|
{
|
|||
|
|
MapProject mp = iter.value();
|
|||
|
|
MapProject::Iterator ite = mp.find(sPro); //获取工程下的属性组
|
|||
|
|
if(ite != mp.end())
|
|||
|
|
{
|
|||
|
|
MapProperty mapProperty = ite.value().mapProperty;
|
|||
|
|
|
|||
|
|
int createRes = 0; //动态表生成结果
|
|||
|
|
for(MapProperty::Iterator it = mapProperty.begin();it != mapProperty.end();++it){ //每个属性组单独生成表
|
|||
|
|
int nType = ite.value().nType;
|
|||
|
|
QStandardItemModel* pSelectModel = ite->pSelect;
|
|||
|
|
QStandardItemModel* pBaseModel = ite->pBase;
|
|||
|
|
|
|||
|
|
QList<QStandardItem*> lstSelected = getGroupSub(pSelectModel,it.key());
|
|||
|
|
QList<QStandardItem*> lstBase = getGroupSub(pBaseModel,it.key());
|
|||
|
|
if(!lstSelected.isEmpty())
|
|||
|
|
{
|
|||
|
|
bool isPub = it->isPublic;
|
|||
|
|
int res = createPropertyTable(sMeta,sPro,it.key(),lstSelected,lstBase,nType,isPub);
|
|||
|
|
switch (res){
|
|||
|
|
case int(AlertInfo::success):
|
|||
|
|
LOG_INFO("DB", QString("create %1 dynamicTable success").arg(sPro));
|
|||
|
|
break;
|
|||
|
|
case int(AlertInfo::fail):
|
|||
|
|
LOG_WARN("DB", QString("create %1 dynamicTable fail").arg(sPro));
|
|||
|
|
break;
|
|||
|
|
case int(AlertInfo::exist):
|
|||
|
|
LOG_WARN("DB", QString("%1 dynamicTable exist").arg(sPro));
|
|||
|
|
break;
|
|||
|
|
default:
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
createRes = createRes | res;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
if(!(createRes & int(AlertInfo::fail))) //结果不含失败就成功
|
|||
|
|
{
|
|||
|
|
//QMessageBox::information(NULL, QString::fromWCharArray(L"提示"), QString::fromWCharArray(L"创建表成功"));
|
|||
|
|
emit modelChange();
|
|||
|
|
}
|
|||
|
|
else //创建失败
|
|||
|
|
{
|
|||
|
|
//QMessageBox::information(NULL, QString::fromWCharArray(L"提示"), QString::fromWCharArray(L"创建表失败"));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
MapProperty ProjectModelManager::addNewProject(const QString& sMeta,const QString& sProject,PropertyModel& model)
|
|||
|
|
{
|
|||
|
|
MapProperty mt;
|
|||
|
|
QStringList lstProperty = getGroupList(sMeta); //返回元模下的属性组名
|
|||
|
|
QStringList lstProPublic = getPublicGroupList(); //返回公共属性组
|
|||
|
|
//lstProperty<<QString("base")<<QString("seperation");
|
|||
|
|
|
|||
|
|
QMap<QString,QJsonObject> mapCheckState = DataBase::GetInstance()->getCheckStateFromManager(sProject); //获取选择状态
|
|||
|
|
for(auto &property:lstProperty)
|
|||
|
|
{
|
|||
|
|
PropertyPage struProperty;
|
|||
|
|
struProperty.isPublic = false;
|
|||
|
|
|
|||
|
|
if(mapCheckState.contains(property) && !model.formerMeta.bChanged) //生成的模型中勾选了该属性组且元模未改变过
|
|||
|
|
{
|
|||
|
|
QJsonObject obj = mapCheckState[property];
|
|||
|
|
QJsonArray nodesJsonArray = obj["checkState"].toArray();
|
|||
|
|
|
|||
|
|
for (QJsonValueRef nodeJson : nodesJsonArray)
|
|||
|
|
{
|
|||
|
|
QJsonObject node = nodeJson.toObject();
|
|||
|
|
QString propertyName = node["name"].toString();
|
|||
|
|
int nState = node["checked"].toInt();
|
|||
|
|
QString dataType = node["type"].toString();
|
|||
|
|
QString defaultValue = node["defaultValue"].toString();
|
|||
|
|
|
|||
|
|
QStandardItem* pItem = new QStandardItem(propertyName);
|
|||
|
|
setItemAttribute(propertyName,pItem);
|
|||
|
|
PropertyState sta;
|
|||
|
|
sta.dataType = dataType;
|
|||
|
|
if(nState)
|
|||
|
|
{
|
|||
|
|
QStandardItem* pGroup = nullptr;
|
|||
|
|
if(model.pSelect->findItems(propertyName).isEmpty())
|
|||
|
|
{
|
|||
|
|
pGroup = new QStandardItem(property);
|
|||
|
|
model.pSelect->appendRow(pGroup); //属性的组未存在,将组添加到model
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
pGroup = model.pSelect->findItems(propertyName)[0];
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if(pGroup){
|
|||
|
|
pGroup->appendRow(pItem);
|
|||
|
|
sta.checkState = true;
|
|||
|
|
struProperty.mCheckState.insert(propertyName,sta);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
QStandardItem* pGroup = nullptr;
|
|||
|
|
if(model.pBase->findItems(propertyName).isEmpty())
|
|||
|
|
{
|
|||
|
|
pGroup = new QStandardItem(property);
|
|||
|
|
model.pBase->appendRow(pGroup); //属性的组未存在,将组添加到model
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
pGroup = model.pBase->findItems(propertyName)[0];
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if(pGroup){
|
|||
|
|
pGroup->appendRow(pItem);
|
|||
|
|
sta.checkState = false;
|
|||
|
|
struProperty.mCheckState.insert(propertyName,sta);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else //未勾选属性组或是新建或元模已改变
|
|||
|
|
{
|
|||
|
|
QStandardItem* pGroup = nullptr; //新建的不包含属性组
|
|||
|
|
pGroup = new QStandardItem(property);
|
|||
|
|
model.pBase->appendRow(pGroup);
|
|||
|
|
|
|||
|
|
QStringList lstName = getAttributeList(sMeta,property);
|
|||
|
|
for(auto &name:lstName)
|
|||
|
|
{
|
|||
|
|
QStandardItem* pItem = new QStandardItem(name);
|
|||
|
|
setItemAttribute(name,pItem);
|
|||
|
|
pGroup->appendRow(pItem);
|
|||
|
|
QString dataType = getItemDataType(pItem);
|
|||
|
|
PropertyState sta;
|
|||
|
|
sta.dataType = dataType;
|
|||
|
|
sta.checkState = false;
|
|||
|
|
struProperty.mCheckState.insert(name,sta); //初始都是未选择状态
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
mt.insert(property,struProperty);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
MapProperty pubMap;
|
|||
|
|
for(auto &pro:lstProPublic) //公共属性组
|
|||
|
|
{
|
|||
|
|
PropertyPage struProperty;
|
|||
|
|
struProperty.isPublic = true;
|
|||
|
|
|
|||
|
|
QStandardItem* pGroup = nullptr;
|
|||
|
|
pGroup = new QStandardItem(pro);
|
|||
|
|
//model.pSelect->appendRow(pGroup); //公共属性组默认都包含
|
|||
|
|
model.pSelect->insertRow(0,pGroup);
|
|||
|
|
|
|||
|
|
QStringList lstName = getPublicAttributeList(pro);
|
|||
|
|
for(auto &name:lstName)
|
|||
|
|
{
|
|||
|
|
QStandardItem* pItem = new QStandardItem(name);
|
|||
|
|
pItem->setData(QColor(60,140,180,180), Qt::BackgroundRole);
|
|||
|
|
setItemAttribute(name,pItem);
|
|||
|
|
pGroup->appendRow(pItem);
|
|||
|
|
QString dataType = getItemDataType(pItem);
|
|||
|
|
PropertyState sta;
|
|||
|
|
sta.dataType = dataType;
|
|||
|
|
sta.checkState = true;
|
|||
|
|
sta.editable = false;
|
|||
|
|
struProperty.mCheckState.insert(name,sta); //初始都已经选择
|
|||
|
|
}
|
|||
|
|
pubMap.insert(pro,struProperty);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
for (auto it = mt.begin(); it != mt.end(); ++it) { //将正常属性添到公共属性后
|
|||
|
|
if(pubMap.contains(it.key()))
|
|||
|
|
continue; //公共属性组已有的不重复添加
|
|||
|
|
pubMap.insert(it.key(), it.value());
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return pubMap;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
QList<QStandardItem*> ProjectModelManager::getGroupSub(QStandardItemModel* pModel,const QString& str)
|
|||
|
|
{
|
|||
|
|
QList<QStandardItem*> lst;
|
|||
|
|
if(!pModel)
|
|||
|
|
return lst;
|
|||
|
|
QList<QStandardItem*> items = pModel->findItems(str);
|
|||
|
|
if (!items.isEmpty()) {
|
|||
|
|
// 存在该文本的项
|
|||
|
|
QStandardItem* item = items[0];
|
|||
|
|
for (int row = 0; row < item->rowCount(); ++row) {
|
|||
|
|
// 默认获取第 0 列的子项
|
|||
|
|
QStandardItem* child = item->child(row, 0);
|
|||
|
|
if (child) {
|
|||
|
|
lst.append(child);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return lst;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void ProjectModelManager::deleteData(const QString& sMeta,const QString& sProject)
|
|||
|
|
{
|
|||
|
|
delete m_mapTotalData[sMeta][sProject].pBase; //手动释放
|
|||
|
|
delete m_mapTotalData[sMeta][sProject].pSelect;
|
|||
|
|
m_mapTotalData[sMeta].remove(sProject);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
QStringList ProjectModelManager::getModelList() const
|
|||
|
|
{
|
|||
|
|
QMap<int,modelType> modelMap = DataBase::GetInstance()->ModelType();
|
|||
|
|
|
|||
|
|
QSet<QString> modelSet;
|
|||
|
|
for(auto &model:modelMap)
|
|||
|
|
{
|
|||
|
|
modelSet.insert(model.modelType);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return QStringList(modelSet.values());
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
QStringList ProjectModelManager::getGroupList(const QString& sM) const
|
|||
|
|
{
|
|||
|
|
QMap<int,modelType> modelType = DataBase::GetInstance()->ModelType();
|
|||
|
|
QMap<int,modelGroup> modelGroupMap = DataBase::GetInstance()->ModelGroup();
|
|||
|
|
QMap<int,attributeGroup> groupMap = DataBase::GetInstance()->AttributeGroup();
|
|||
|
|
|
|||
|
|
int metaId = 0;
|
|||
|
|
for(auto &meta:modelType)
|
|||
|
|
{
|
|||
|
|
if(sM == meta.modelType) //查找元模对应的id
|
|||
|
|
{
|
|||
|
|
metaId = meta.id;
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
QList<int> lstGroupId;
|
|||
|
|
for(auto &group:modelGroupMap) //找到元模id对应的属性组id
|
|||
|
|
{
|
|||
|
|
if(group.modelTypeId == metaId)
|
|||
|
|
{
|
|||
|
|
lstGroupId.push_back(group.attributeGroupId);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
QStringList groupList;
|
|||
|
|
for(auto &id:lstGroupId) //从属性组中找到id对应的组名
|
|||
|
|
{
|
|||
|
|
groupList.append(groupMap[id].groupType);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return groupList;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
QStringList ProjectModelManager::getPublicGroupList() const
|
|||
|
|
{
|
|||
|
|
QMap<int,modelAttributePublic> modelAttPublic = DataBase::GetInstance()->ModelAttributePublic();
|
|||
|
|
QMap<int,attributeGroup> groupMap = DataBase::GetInstance()->AttributeGroup();
|
|||
|
|
|
|||
|
|
QSet<int> setGroup;
|
|||
|
|
for(auto &model:modelAttPublic)
|
|||
|
|
{
|
|||
|
|
setGroup.insert(model.attributeGroupId);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
QStringList groupList;
|
|||
|
|
for(auto &id:setGroup)
|
|||
|
|
{
|
|||
|
|
groupList.append(groupMap[id].groupType);
|
|||
|
|
}
|
|||
|
|
return groupList;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
QStringList ProjectModelManager::getAttributeList(const QString& sM,const QString& sG) const
|
|||
|
|
{
|
|||
|
|
QMap<int,modelType> modelType = DataBase::GetInstance()->ModelType();
|
|||
|
|
//QMap<int,modelGroup> modelGroupMap = DataBase::GetInstance()->ModelGroup();
|
|||
|
|
QMap<int,attributeGroup> groupMap = DataBase::GetInstance()->AttributeGroup();
|
|||
|
|
QMap<int,modelAttribute> modelAttMap = DataBase::GetInstance()->ModelAttribute();
|
|||
|
|
QMap<int,attribute> attMap = DataBase::GetInstance()->Attribute();
|
|||
|
|
|
|||
|
|
int metaId = -1;
|
|||
|
|
for(auto &meta:modelType)
|
|||
|
|
{
|
|||
|
|
if(sM == meta.modelType) //查找元模对应的id
|
|||
|
|
{
|
|||
|
|
metaId = meta.id;
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
int groupId = -1;
|
|||
|
|
for(auto &attGroup:groupMap)
|
|||
|
|
{
|
|||
|
|
if(attGroup.groupType == sG) //返回参数属性组名对应的id
|
|||
|
|
{
|
|||
|
|
groupId = attGroup.id;
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
QStringList lst;
|
|||
|
|
for(auto &mt:modelAttMap)
|
|||
|
|
{
|
|||
|
|
if(mt.modelTypeId == metaId && mt.attributeGroupId == groupId)
|
|||
|
|
{
|
|||
|
|
lst.append(attMap[mt.attributeId].attribute);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return lst;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
QStringList ProjectModelManager::getPublicAttributeList(const QString& group)
|
|||
|
|
{
|
|||
|
|
QMap<int,modelAttributePublic> modelAttPublic = DataBase::GetInstance()->ModelAttributePublic();
|
|||
|
|
QMap<int,attributeGroup> groupMap = DataBase::GetInstance()->AttributeGroup();
|
|||
|
|
QMap<int,attribute> attMap = DataBase::GetInstance()->Attribute();
|
|||
|
|
|
|||
|
|
int groupId = -1;
|
|||
|
|
for(auto &attGroup:groupMap)
|
|||
|
|
{
|
|||
|
|
if(attGroup.groupType == group) //返回参数属性组名对应的id
|
|||
|
|
{
|
|||
|
|
groupId = attGroup.id;
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
QStringList lst;
|
|||
|
|
for(auto &mt:modelAttPublic)
|
|||
|
|
{
|
|||
|
|
if(mt.attributeGroupId == groupId)
|
|||
|
|
{
|
|||
|
|
lst.append(attMap[mt.attributeId].attribute);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return lst;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void ProjectModelManager::setItemAttribute(const QString& name,QStandardItem* p)
|
|||
|
|
{
|
|||
|
|
QMap<int,attribute> attMap = DataBase::GetInstance()->Attribute();
|
|||
|
|
QMap<int,dataType> dt = DataBase::GetInstance()->DataType();
|
|||
|
|
|
|||
|
|
for(auto &att:attMap)
|
|||
|
|
{
|
|||
|
|
QString sType = dt[att.dataTypeId].dataType; //获得属性id对应的属性名
|
|||
|
|
if(name == att.attribute)
|
|||
|
|
{
|
|||
|
|
p->setData(att.id,Id);
|
|||
|
|
p->setData(att.attribute,Attribute);
|
|||
|
|
p->setData(att.attributeName,AttributeName);
|
|||
|
|
p->setData(sType,DataType); //不直接使用id,拼接完成str
|
|||
|
|
p->setData(att.lengthPrecision,LengthPrecision);
|
|||
|
|
p->setData(att.scale,Scale);
|
|||
|
|
p->setData(att.isNotNull,IsNotNull);
|
|||
|
|
p->setData(att.defaultValue,DefaultValue);
|
|||
|
|
p->setData(att.valueRange,ValueRange);
|
|||
|
|
p->setData(att.isVisible,IsVisible);
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
QPair<QString,QString> ProjectModelManager::combinePropertySql(const QStandardItem* pItem)
|
|||
|
|
{
|
|||
|
|
QMap<int,dataType> dt = DataBase::GetInstance()->DataType();
|
|||
|
|
|
|||
|
|
int id = pItem->data(Id).toInt();
|
|||
|
|
QString attribute = pItem->data(Attribute).toString();
|
|||
|
|
QString dataType = pItem->data(DataType).toString();
|
|||
|
|
int lengthPrecision = pItem->data(LengthPrecision).toInt();
|
|||
|
|
int scale = pItem->data(Scale).toInt();
|
|||
|
|
QString defaultValue = pItem->data(DefaultValue).toString();
|
|||
|
|
QString valueRange = pItem->data(ValueRange).toString();
|
|||
|
|
int isNotNull = pItem->data(IsNotNull).toInt();
|
|||
|
|
|
|||
|
|
bool needsQuotes = stringDataTypes.contains(dataType);
|
|||
|
|
// 处理数据类型及其长度精度
|
|||
|
|
QString dataTypePart = dataType;
|
|||
|
|
if (lengthPrecision > 0) {
|
|||
|
|
dataTypePart += QString("(%1").arg(lengthPrecision);
|
|||
|
|
if (scale > 0) {
|
|||
|
|
dataTypePart += QString(",%1").arg(scale);
|
|||
|
|
}
|
|||
|
|
dataTypePart += ")";
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 开始拼接SQL
|
|||
|
|
QString sql = QString("%1 %2").arg(attribute, dataTypePart);
|
|||
|
|
|
|||
|
|
// 处理约束条件
|
|||
|
|
if (isNotNull != -1) {
|
|||
|
|
sql += " NOT NULL";
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (!defaultValue.isEmpty()) {
|
|||
|
|
QString defValue = defaultValue;
|
|||
|
|
if (needsQuotes && defValue != "null" && defValue != "NULL") {
|
|||
|
|
// 转义单引号并包裹
|
|||
|
|
defValue.replace("'", "''");
|
|||
|
|
defValue = QString("'%1'").arg(defValue);
|
|||
|
|
}
|
|||
|
|
sql += QString(" DEFAULT %1").arg(defValue);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/*if (isPrimaryKey != 0) {
|
|||
|
|
sql += " PRIMARY KEY";
|
|||
|
|
}*/
|
|||
|
|
|
|||
|
|
return qMakePair(sql,dataTypePart);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
bool ProjectModelManager::ifProjectEqual(const QString& sMeta,const QString& sProject,QMap<QString,QJsonObject> map)
|
|||
|
|
{
|
|||
|
|
//todo:判断关联的模型类型
|
|||
|
|
MapProperty curPro = m_mapTotalData[sMeta][sProject].mapProperty;
|
|||
|
|
|
|||
|
|
QMap<QString,PropertyPage>::Iterator iter;
|
|||
|
|
for(iter = curPro.begin();iter != curPro.end();++iter)
|
|||
|
|
{
|
|||
|
|
if(iter->isPublic) //公共组跳过判断
|
|||
|
|
continue;
|
|||
|
|
if(!map.contains(iter.key())) //已存在的模型中不包含该属性组
|
|||
|
|
{
|
|||
|
|
QMap<QString,PropertyState> curCheckState = iter.value().mCheckState;
|
|||
|
|
for(auto &val:curCheckState)
|
|||
|
|
{
|
|||
|
|
if(val.checkState) //该属性组不在模型中且被勾选
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
continue; //该属性组不在模型中且未被勾选,不做判断
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
QJsonObject dbCheckState = map[iter.key()]; //数据库中该属性组的勾选状态
|
|||
|
|
QMap<QString,PropertyState> curCheckState = iter.value().mCheckState; //当前程序中的勾选状态
|
|||
|
|
|
|||
|
|
QJsonArray nodesJsonArray = dbCheckState["checkState"].toArray();
|
|||
|
|
if(nodesJsonArray.size() != curCheckState.size()) //属性个数对不上,模型不同
|
|||
|
|
return false;
|
|||
|
|
for (QJsonValueRef nodeJson : nodesJsonArray)
|
|||
|
|
{
|
|||
|
|
QJsonObject node = nodeJson.toObject();
|
|||
|
|
QString propertyName = node["name"].toString();
|
|||
|
|
int nState = node["checked"].toInt();
|
|||
|
|
if(curCheckState[propertyName].checkState != nState) //相同属性选中状态不同
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
QString ProjectModelManager::modifyProjectModel(const QString& sMeta,const QString& sProject,QMap<QString,QJsonObject> mapOld)
|
|||
|
|
{
|
|||
|
|
QString sRes;
|
|||
|
|
MapProperty curPro = m_mapTotalData[sMeta][sProject].mapProperty;
|
|||
|
|
QStandardItemModel* pSelectModel = m_mapTotalData[sMeta][sProject].pSelect;
|
|||
|
|
QStandardItemModel* pBaseModel = m_mapTotalData[sMeta][sProject].pBase;
|
|||
|
|
int nType = m_mapTotalData[sMeta][sProject].nType;
|
|||
|
|
|
|||
|
|
QMap<QString,PropertyPage>::Iterator iter;
|
|||
|
|
for(iter = curPro.begin();iter != curPro.end();++iter) //遍历当前模型所有属性组
|
|||
|
|
{
|
|||
|
|
QMap<QString,PropertyState> curCheckState = iter.value().mCheckState; //当前程序中的属性勾选状态
|
|||
|
|
bool isNull = true; //当前属性组是否未空
|
|||
|
|
for(auto &val:curCheckState)
|
|||
|
|
{
|
|||
|
|
if(val.checkState){
|
|||
|
|
isNull = false; //当前程序模型有勾选,不为空
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
if(isNull){
|
|||
|
|
if(mapOld.contains(iter.key())){ //当前模型勾选为空且库模型中包含此属性组,移除库中该属性组表
|
|||
|
|
QMap<QString,QString> map = DataBase::GetInstance()->getProjectTableName(sProject);
|
|||
|
|
bool res = DataBase::GetInstance()->deleteTable(map[iter.key()]);
|
|||
|
|
if(res){
|
|||
|
|
DataBase::GetInstance()->deleteRecordFromManager(sProject,iter.key());
|
|||
|
|
sRes = QString::fromWCharArray(L"修改模型成功");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else{
|
|||
|
|
QList<QStandardItem*> lstSelected = ProjectModelManager::instance().getGroupSub(pSelectModel,iter.key());
|
|||
|
|
QList<QStandardItem*> lstBase = ProjectModelManager::instance().getGroupSub(pBaseModel,iter.key());
|
|||
|
|
if(mapOld.contains(iter.key())){ //新旧模型中都存在,修改模型
|
|||
|
|
QMap<QString,QString> oldSchema; //库中模型 属性名/数据类型
|
|||
|
|
QMap<QString,QString> newSchema; //现有模型
|
|||
|
|
|
|||
|
|
QJsonObject obj = mapOld[iter.key()];
|
|||
|
|
QJsonArray nodesJsonArray = obj["checkState"].toArray();
|
|||
|
|
|
|||
|
|
for (QJsonValueRef nodeJson : nodesJsonArray)
|
|||
|
|
{
|
|||
|
|
QJsonObject node = nodeJson.toObject();
|
|||
|
|
QString propertyName = node["name"].toString();
|
|||
|
|
int nState = node["checked"].toInt();
|
|||
|
|
QString dataType = node["type"].toString();
|
|||
|
|
|
|||
|
|
if(nState)
|
|||
|
|
oldSchema.insert(propertyName,dataType);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
QMap<QString,PropertyState>::Iterator it;
|
|||
|
|
for(it = curCheckState.begin(); it != curCheckState.end();++it)
|
|||
|
|
{
|
|||
|
|
if(it->checkState){
|
|||
|
|
newSchema.insert(it.key(),it->dataType);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
if(oldSchema == newSchema)
|
|||
|
|
continue;
|
|||
|
|
|
|||
|
|
QMap<QString,QString> map = DataBase::GetInstance()->getProjectTableName(sProject);
|
|||
|
|
|
|||
|
|
bool res = DataBase::GetInstance()->modifyProjectTable(map[iter.key()],oldSchema,newSchema);
|
|||
|
|
|
|||
|
|
QJsonObject objState = getSelectedState(lstSelected,lstBase);
|
|||
|
|
DataBase::GetInstance()->updateCheckState(map[iter.key()],objState);
|
|||
|
|
if(res)
|
|||
|
|
{
|
|||
|
|
sRes = QString::fromWCharArray(L"修改模型成功");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else{ //非空且库模型中不存在,新增
|
|||
|
|
bool res = createPropertyTable(sMeta,sProject,iter.key(),lstSelected,lstBase,nType);
|
|||
|
|
if(res)
|
|||
|
|
{
|
|||
|
|
sRes = QString::fromWCharArray(L"修改模型成功");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return sRes;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
bool ProjectModelManager::renameProjectModel(const QString& strCur,QMap<QString,projectManager> datas)
|
|||
|
|
{
|
|||
|
|
for(auto &data:datas)
|
|||
|
|
{
|
|||
|
|
QString sTable = data.metaModel + QString("_") + strCur + QString("_")+data.groupName;
|
|||
|
|
|
|||
|
|
DataBase::GetInstance()->updateProjectName(sTable,strCur,data.name);
|
|||
|
|
DataBase::GetInstance()->alterTableName(data.name,sTable);
|
|||
|
|
}
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void ProjectModelManager::updateComponentModelName(const QString& strOld,const QString& strNew)
|
|||
|
|
{
|
|||
|
|
DataBase::GetInstance()->updateComponentModelName(strOld,strNew);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
bool ProjectModelManager::ifProjectExsit(const QString& sPro)
|
|||
|
|
{
|
|||
|
|
for(auto &meta:m_mapTotalData)
|
|||
|
|
{
|
|||
|
|
if(meta.contains(sPro))
|
|||
|
|
{
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
int ProjectModelManager::createPropertyTable(const QString& sMeta,const QString& sProject,const QString& sGroup,QList<QStandardItem*> lstSelect,QList<QStandardItem*> lstBase,int nLinkType,bool isPublic)
|
|||
|
|
{
|
|||
|
|
if(!isPublic)
|
|||
|
|
{
|
|||
|
|
QString sName = sMeta + QString("_") + sProject + QString("_")+sGroup;
|
|||
|
|
|
|||
|
|
QStringList fields;
|
|||
|
|
fields.append("id SERIAL NOT NULL PRIMARY KEY");
|
|||
|
|
fields.append("global_uuid uuid NOT NULL DEFAULT gen_random_uuid()");
|
|||
|
|
fields.append("attribute_group VARCHAR(64) NOT NULL");
|
|||
|
|
|
|||
|
|
for(auto &item:lstSelect)
|
|||
|
|
{
|
|||
|
|
QPair<QString,QString> pair = combinePropertySql(item); //拼接单句sql
|
|||
|
|
fields.append(pair.first);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
QJsonObject objState = getSelectedState(lstSelect,lstBase);
|
|||
|
|
|
|||
|
|
if(!DataBase::GetInstance()->createDynamicTable(sName,fields))
|
|||
|
|
{
|
|||
|
|
return int(AlertInfo::fail);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
DataBase::GetInstance()->insertProjectManager(sName,sProject,sMeta,sGroup,nLinkType,objState);
|
|||
|
|
return int(AlertInfo::success);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
QString sName = sGroup;
|
|||
|
|
|
|||
|
|
QStringList fields;
|
|||
|
|
fields.append("id SERIAL NOT NULL PRIMARY KEY");
|
|||
|
|
fields.append("global_uuid uuid NOT NULL DEFAULT gen_random_uuid()");
|
|||
|
|
//fields.append("attribute_group VARCHAR(64) NOT NULL");
|
|||
|
|
|
|||
|
|
for(auto &item:lstSelect)
|
|||
|
|
{
|
|||
|
|
QString attribute = item->data(Attribute).toString();
|
|||
|
|
if(attribute.contains("global_uuid"))
|
|||
|
|
continue;
|
|||
|
|
QPair<QString,QString> pair = combinePropertySql(item); //拼接单句sql
|
|||
|
|
fields.append(pair.first);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
QJsonObject objState = getSelectedState(lstSelect,lstBase);
|
|||
|
|
|
|||
|
|
if(!DataBase::GetInstance()->ifDynamicTableExist(sName))
|
|||
|
|
{
|
|||
|
|
bool val = DataBase::GetInstance()->createDynamicTable(sName,fields);
|
|||
|
|
if(val)
|
|||
|
|
{
|
|||
|
|
DataBase::GetInstance()->insertProjectManager(sName,sName,"NULL",sGroup,0,objState,true);
|
|||
|
|
return int(AlertInfo::success);
|
|||
|
|
}
|
|||
|
|
else {
|
|||
|
|
return int(AlertInfo::fail);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
return int(AlertInfo::exist);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
QJsonObject ProjectModelManager::getSelectedState(QList<QStandardItem*> select,QList<QStandardItem*> base)
|
|||
|
|
{
|
|||
|
|
QJsonObject objState;
|
|||
|
|
QJsonArray arrState;
|
|||
|
|
|
|||
|
|
for(auto &item:select)
|
|||
|
|
{
|
|||
|
|
QString dataType = item->data(DataType).toString();
|
|||
|
|
int lengthPrecision = item->data(LengthPrecision).toInt();
|
|||
|
|
int scale = item->data(Scale).toInt();
|
|||
|
|
QString defaultValue = item->data(DefaultValue).toString();
|
|||
|
|
int isVisible = item->data(IsVisible).toInt();
|
|||
|
|
|
|||
|
|
QString dataTypePart = dataType; //拼接数据类型
|
|||
|
|
if (lengthPrecision > 0) {
|
|||
|
|
dataTypePart += QString("(%1").arg(lengthPrecision);
|
|||
|
|
if (scale > 0) {
|
|||
|
|
dataTypePart += QString(",%1").arg(scale);
|
|||
|
|
}
|
|||
|
|
dataTypePart += ")";
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
QJsonObject node; //保存已选择状态
|
|||
|
|
node["name"] = item->text();
|
|||
|
|
node["checked"] = 1;
|
|||
|
|
node["type"] = dataTypePart;
|
|||
|
|
node["defaultValue"] = defaultValue;
|
|||
|
|
node["lengthPrecision"] = lengthPrecision;
|
|||
|
|
node["isVisible"] = isVisible;
|
|||
|
|
arrState.append(node);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
for(auto &item:base)
|
|||
|
|
{
|
|||
|
|
QString dataType = item->data(DataType).toString();
|
|||
|
|
int lengthPrecision = item->data(LengthPrecision).toInt();
|
|||
|
|
int scale = item->data(Scale).toInt();
|
|||
|
|
QString defaultValue = item->data(DefaultValue).toString();
|
|||
|
|
int isVisible = item->data(IsVisible).toInt();
|
|||
|
|
|
|||
|
|
QString dataTypePart = dataType; //拼接数据类型
|
|||
|
|
if (lengthPrecision > 0) {
|
|||
|
|
dataTypePart += QString("(%1").arg(lengthPrecision);
|
|||
|
|
if (scale > 0) {
|
|||
|
|
dataTypePart += QString(",%1").arg(scale);
|
|||
|
|
}
|
|||
|
|
dataTypePart += ")";
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
QJsonObject node; //保存未选择状态
|
|||
|
|
node["name"] = item->text();
|
|||
|
|
node["checked"] = 0;
|
|||
|
|
node["type"] = dataTypePart;
|
|||
|
|
node["defaultValue"] = defaultValue;
|
|||
|
|
node["lengthPrecision"] = lengthPrecision;
|
|||
|
|
node["isVisible"] = isVisible;
|
|||
|
|
arrState.append(node);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
objState["checkState"] = arrState;
|
|||
|
|
return objState;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
QString ProjectModelManager::getItemDataType(const QStandardItem* pItem)
|
|||
|
|
{
|
|||
|
|
QString dataType = pItem->data(DataType).toString();
|
|||
|
|
int lengthPrecision = pItem->data(LengthPrecision).toInt();
|
|||
|
|
int scale = pItem->data(Scale).toInt();
|
|||
|
|
|
|||
|
|
QString dataTypePart = dataType;
|
|||
|
|
if (lengthPrecision > 0) {
|
|||
|
|
dataTypePart += QString("(%1").arg(lengthPrecision);
|
|||
|
|
if (scale > 0) {
|
|||
|
|
dataTypePart += QString(",%1").arg(scale);
|
|||
|
|
}
|
|||
|
|
dataTypePart += ")";
|
|||
|
|
}
|
|||
|
|
return dataTypePart;
|
|||
|
|
}
|