3160 lines
92 KiB
C++
3160 lines
92 KiB
C++
#include "dataBase.h"
|
||
#include "logger.h"
|
||
#include <QDebug>
|
||
#include <QFile>
|
||
#include <QXmlStreamReader>
|
||
#include <QMessageBox>
|
||
#include <QSqlQuery>
|
||
#include <QJsonObject>
|
||
#include <QSqlError>
|
||
#include <QSqlDriver>
|
||
#include <QJsonDocument>
|
||
#include <QJsonArray>
|
||
|
||
DataBase* DataBase::dbInstance = nullptr;
|
||
int DataBase::_id = 0;
|
||
|
||
DataBase::DataBase()
|
||
{
|
||
m_sFileName = QString("setting.xml");
|
||
initial();
|
||
//createProjectDB();
|
||
//initialProjectDB();
|
||
createProjectManager();
|
||
}
|
||
|
||
DataBase::~DataBase()
|
||
{
|
||
QString connectionName = QSqlDatabase::database().connectionName();
|
||
QSqlDatabase::removeDatabase(connectionName);
|
||
}
|
||
|
||
void DataBase::initial()
|
||
{
|
||
readXML();
|
||
if (QSqlDatabase::contains(_DataBaseName))
|
||
db = QSqlDatabase::database(_DataBaseName);
|
||
else
|
||
db = QSqlDatabase::addDatabase(_DataBaseType,_DataBaseName);
|
||
|
||
db.setDatabaseName(_DataBaseName);
|
||
db.setHostName(_HostName);
|
||
db.setPort(_Port);
|
||
// 需要改成自己的用户名和密码
|
||
db.setUserName(_UserName);
|
||
db.setPassword(_PassWord);
|
||
|
||
if (db.open()) {
|
||
qDebug()<<"baseDB success";
|
||
} else {
|
||
LOG_ERROR("DB", QString("Database not open"));
|
||
}
|
||
//元模
|
||
|
||
getAttributeGroup(); //获取属性组信息
|
||
getDataType(); //获取数据类型信息
|
||
getModelType(); //获取模型类型
|
||
getModelGroup(); //获取模型组
|
||
getAttribute(); //获取属性
|
||
getModelAttribute();
|
||
getModelAttributePublic(); //获取公共属性组
|
||
getModelConnectivity(); //获取连接性
|
||
}
|
||
|
||
DataBase* DataBase::GetInstance()
|
||
{
|
||
if(dbInstance == nullptr)
|
||
{
|
||
dbInstance = new DataBase();
|
||
}
|
||
return dbInstance;
|
||
}
|
||
|
||
QSqlQuery DataBase::executeSQL(const QString& strSQL,bool isDDL,const QVariantList& params, bool useTranscation)
|
||
{
|
||
//事务
|
||
bool transactionStarted = false;
|
||
if(useTranscation)
|
||
{
|
||
if(!db.transaction())
|
||
{
|
||
LOG_ERROR("DB", QString("Start transaction failed. error: %1").arg(db.lastError().databaseText()));
|
||
throw std::runtime_error(db.lastError().text().toStdString());
|
||
}
|
||
transactionStarted = true;
|
||
}
|
||
|
||
QSqlQuery sqlQuery(db);
|
||
try
|
||
{
|
||
if(isDDL) //创建或删除直接执行sql
|
||
{
|
||
if (!sqlQuery.exec(strSQL))
|
||
{
|
||
LOG_ERROR("SQL", QString("SQL '%1' execute error: %2").arg(strSQL, sqlQuery.lastError().databaseText()));
|
||
throw std::runtime_error(db.lastError().text().toStdString());
|
||
}
|
||
}
|
||
else
|
||
{
|
||
if(!sqlQuery.prepare(strSQL))
|
||
{
|
||
LOG_ERROR("SQL", QString("SQL '%1' prepare fialed. error: %2").arg(strSQL, sqlQuery.lastError().databaseText()));
|
||
throw std::runtime_error(db.lastError().text().toStdString());
|
||
}
|
||
//绑定参数
|
||
|
||
for(int i = 0;i < params.size();++i)
|
||
{
|
||
sqlQuery.bindValue(i, params[i]);
|
||
}
|
||
|
||
if (!sqlQuery.exec())
|
||
{
|
||
LOG_ERROR("SQL", QString("SQL '%1' execute error: %2").arg(strSQL, sqlQuery.lastError().databaseText()));
|
||
throw std::runtime_error(db.lastError().text().toStdString());
|
||
}
|
||
}
|
||
|
||
// 提交事务(如果已开启)
|
||
if(transactionStarted && !db.commit())
|
||
{
|
||
throw std::runtime_error(db.lastError().text().toStdString());
|
||
LOG_ERROR("DB", QString("Commit transaction failed. connectionName: %1").arg(db.lastError().databaseText()));
|
||
}
|
||
}
|
||
catch (const std::runtime_error& e)
|
||
{
|
||
// 错误处理:回滚事务(如果已开启)
|
||
if(transactionStarted)
|
||
{
|
||
if(!db.rollback()) // 回滚失败时记录警告
|
||
{
|
||
LOG_ERROR("DB", QString("Rollback failed. connectionName: %1").arg(db.lastError().databaseText()));
|
||
}
|
||
}
|
||
|
||
throw; // 重新抛出异常
|
||
}
|
||
|
||
return sqlQuery;
|
||
}
|
||
|
||
//多条批量SQL语句执行接口
|
||
QSqlQuery DataBase::executeBatchSQL(const QStringList& sqlStatements, bool createOrDrop,const QList<QVariantList>& paramsList, bool useTranscation)
|
||
{
|
||
|
||
//参数数量校验
|
||
if(!paramsList.isEmpty() && sqlStatements.size() != paramsList.size())
|
||
{
|
||
LOG_ERROR("SQL", QString("SQL statement does not match the number of parameters"));
|
||
throw std::runtime_error(QSqlError("SQL statement does not match the number of parameters").text().toStdString());
|
||
}
|
||
|
||
//事务
|
||
bool transactionStarted = false;
|
||
if(useTranscation)
|
||
{
|
||
if(!db.transaction())
|
||
{
|
||
LOG_ERROR("DB", QString("Start transaction failed."));
|
||
throw std::runtime_error(db.lastError().text().toStdString());
|
||
}
|
||
transactionStarted = true;
|
||
}
|
||
|
||
QSqlQuery lastQuery(db);
|
||
try
|
||
{
|
||
for(int i = 0; i < sqlStatements.size(); i++)
|
||
{
|
||
const QString& strSQL = sqlStatements.at(i);
|
||
const QVariantList& params = paramsList.isEmpty() ? QVariantList() : paramsList.at(i);
|
||
|
||
QSqlQuery sqlQuery(db);
|
||
if(createOrDrop)
|
||
{
|
||
if (!sqlQuery.exec(strSQL))
|
||
{
|
||
LOG_ERROR("SQL", QString("SQL '%1' execute error: %2").arg(strSQL, sqlQuery.lastError().databaseText()));
|
||
throw std::runtime_error(db.lastError().text().toStdString());
|
||
}
|
||
}
|
||
else
|
||
{
|
||
if(!sqlQuery.prepare(strSQL))
|
||
{
|
||
LOG_ERROR("SQL", QString("SQL '%1' prepare fialed. error: %2").arg(strSQL, sqlQuery.lastError().databaseText()));
|
||
throw std::runtime_error(db.lastError().text().toStdString());
|
||
}
|
||
//绑定参数
|
||
for(int i = 0;i < params.size();++i)
|
||
{
|
||
sqlQuery.bindValue(i, params[i]);
|
||
}
|
||
|
||
if (!sqlQuery.exec())
|
||
{
|
||
LOG_ERROR("SQL", QString("SQL '%1' execute error: %2").arg(strSQL, sqlQuery.lastError().databaseText()));
|
||
throw std::runtime_error(db.lastError().text().toStdString());
|
||
}
|
||
}
|
||
|
||
lastQuery = std::move(sqlQuery);
|
||
}
|
||
// 提交事务(如果已开启)
|
||
if(transactionStarted && !db.commit())
|
||
{
|
||
throw std::runtime_error(db.lastError().text().toStdString());
|
||
LOG_ERROR("DB", QString("Commit transaction failed."));
|
||
}
|
||
}
|
||
catch (const std::runtime_error& e)
|
||
{
|
||
// 错误处理:回滚事务(如果已开启)
|
||
if(transactionStarted)
|
||
{
|
||
if(!db.rollback()) // 回滚失败时记录警告
|
||
{
|
||
LOG_ERROR("DB", QString("Rollback failed. error: %1").arg( db.lastError().databaseText()));
|
||
}
|
||
}
|
||
|
||
throw; // 重新抛出异常
|
||
}
|
||
|
||
return lastQuery;
|
||
}
|
||
|
||
|
||
bool DataBase::insertComponent(QUuid uuid,QString modelName,QString nspath,QString tag,QString name,QString description,QString grid,QString zone,QString station,int type,bool inService,int state,int status,QJsonObject connected_bus,QJsonObject label,QJsonObject context,int op)
|
||
{
|
||
if(db.open())
|
||
{
|
||
QSqlQuery qry(db);
|
||
|
||
QJsonDocument contextDoc(context);
|
||
QString strCon = contextDoc.toJson(QJsonDocument::Compact);
|
||
|
||
qry.prepare("INSERT INTO component(global_uuid, model_name,tag, name, grid, zone, station, type, context, op, ts) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
|
||
qry.bindValue(0,uuid);
|
||
qry.bindValue(1,modelName);
|
||
qry.bindValue(2,tag);
|
||
qry.bindValue(3,name);
|
||
qry.bindValue(4,grid);
|
||
qry.bindValue(5,zone);
|
||
qry.bindValue(6,station);
|
||
qry.bindValue(7,type);
|
||
qry.bindValue(8,strCon);
|
||
qry.bindValue(9,op);
|
||
qry.bindValue(10,QDateTime::currentDateTime());
|
||
bool res = qry.exec();
|
||
QString str = qry.lastQuery();
|
||
const QVariantList list = qry.boundValues();
|
||
for (qsizetype i = 0; i < list.size(); ++i)
|
||
qDebug() << i << ":" << list.at(i).toString();
|
||
if(!res)
|
||
{
|
||
qDebug()<<str<<"\n"<<qry.lastError().text();
|
||
}
|
||
qry.clear();
|
||
return res;
|
||
}
|
||
else
|
||
return false;
|
||
}
|
||
|
||
bool DataBase::updateComponent(QUuid uuid,QString tag,QString name,QJsonObject context)
|
||
{
|
||
if(db.open())
|
||
{
|
||
QSqlQuery qry(db);
|
||
|
||
QJsonDocument contextDoc(context);
|
||
QString strCon = contextDoc.toJson(QJsonDocument::Compact);
|
||
|
||
qry.prepare("UPDATE component SET tag=?, name=?, context=?, ts=? WHERE global_uuid=?");
|
||
qry.bindValue(0,tag);
|
||
qry.bindValue(1,name);
|
||
qry.bindValue(2,strCon);
|
||
qry.bindValue(3,QDateTime::currentDateTime());
|
||
qry.bindValue(4,uuid);
|
||
bool res = qry.exec();
|
||
QString str = qry.lastQuery();
|
||
const QVariantList list = qry.boundValues();
|
||
for (qsizetype i = 0; i < list.size(); ++i)
|
||
qDebug() << i << ":" << list.at(i).toString();
|
||
if(!res)
|
||
{
|
||
qDebug()<<str<<"\n"<<qry.lastError().text();
|
||
}
|
||
qry.clear();
|
||
return res;
|
||
}
|
||
else
|
||
return false;
|
||
}
|
||
|
||
bool DataBase::insertDynamicProperty(QUuid uuid,groupStateValue groupValue)
|
||
{
|
||
QString strPros;
|
||
QString strPronouns;
|
||
|
||
QVariantList params;
|
||
params.append(uuid);
|
||
params.append(groupValue.groupName);
|
||
|
||
for(auto &pro:groupValue.mapInfo[uuid])
|
||
{
|
||
strPros += QString(",")+pro.name;
|
||
strPronouns += QString(",?");
|
||
if(pro.defaultValue.userType() == qMetaTypeId<QJsonObject>()) //json特殊处理
|
||
{
|
||
QJsonDocument contextDoc(pro.defaultValue.toJsonObject());
|
||
QString strCon = contextDoc.toJson(QJsonDocument::Compact);
|
||
params.append(strCon);
|
||
}
|
||
else
|
||
params.append(pro.defaultValue);
|
||
}
|
||
QString strSQL = QString("INSERT INTO %1(global_uuid, attribute_group%2) VALUES (?, ?%3)").arg(groupValue.tableName,strPros,strPronouns);
|
||
|
||
try
|
||
{
|
||
executeSQL(strSQL,false,params);
|
||
return true;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
LOG_ERROR("DB", QString("Insert dynamic table fail"));
|
||
return false;
|
||
}
|
||
}
|
||
|
||
bool DataBase::updateDynamicProperty(QUuid uuid,groupStateValue groupValue)
|
||
{
|
||
QStringList setClauses;
|
||
|
||
QVariantList params;
|
||
for(auto &pro:groupValue.mapInfo[uuid])
|
||
{
|
||
setClauses.append(QString("%1 = ?").arg(pro.name));
|
||
if(pro.defaultValue.userType() == qMetaTypeId<QJsonObject>()) //json特殊处理
|
||
{
|
||
QJsonDocument contextDoc(pro.defaultValue.toJsonObject());
|
||
QString strCon = contextDoc.toJson(QJsonDocument::Compact);
|
||
params.append(strCon);
|
||
}
|
||
else
|
||
params.append(pro.defaultValue);
|
||
}
|
||
params.append(uuid);
|
||
|
||
QString strSQL = QString("UPDATE %1 SET %2 WHERE global_uuid = ?").arg(groupValue.tableName).arg(setClauses.join(","));
|
||
|
||
try
|
||
{
|
||
executeSQL(strSQL,false,params);
|
||
return true;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
LOG_ERROR("DB", QString("Update table %1 fail").arg(groupValue.tableName));
|
||
return false;
|
||
}
|
||
}
|
||
|
||
bool DataBase::insertPage(QString tag,QString name,QJsonObject label,QJsonObject context,QString description,int op)
|
||
{
|
||
if(db.open())
|
||
{
|
||
QSqlQuery qry(db);
|
||
|
||
QJsonDocument labelDoc(label);
|
||
QString strLabel = labelDoc.toJson(QJsonDocument::Compact);
|
||
|
||
QJsonDocument contextDoc(context);
|
||
QString strCon = contextDoc.toJson(QJsonDocument::Compact);
|
||
|
||
qry.prepare("INSERT INTO page(tag, name, label, context, description, op, ts) VALUES (?, ?, ?, ?, ?, ?, ?)");
|
||
qry.bindValue(0,tag);
|
||
qry.bindValue(1,name);
|
||
qry.bindValue(2,strLabel);
|
||
qry.bindValue(3,strCon);
|
||
qry.bindValue(4,description);
|
||
qry.bindValue(5,op);
|
||
qry.bindValue(6,QDateTime::currentDateTime());
|
||
bool res = qry.exec();
|
||
QString str = qry.lastQuery();
|
||
if(!res)
|
||
{
|
||
qDebug()<<str<<"\n"<<qry.lastError().text();
|
||
}
|
||
qry.clear();
|
||
return res;
|
||
}
|
||
else
|
||
return false;
|
||
}
|
||
|
||
bool DataBase::insertStation(int zoneId,QString name,QString description,bool isLocal,int op)
|
||
{
|
||
if(db.open())
|
||
{
|
||
QSqlQuery qry(db);
|
||
|
||
qry.prepare("INSERT INTO station(zone_id, name, description, is_local, op, ts) VALUES (?, ?, ?, ?, ?, ?)");
|
||
qry.bindValue(0,zoneId);
|
||
qry.bindValue(1,name);
|
||
qry.bindValue(2,description);
|
||
qry.bindValue(3,isLocal);
|
||
qry.bindValue(4,op);
|
||
qry.bindValue(5,QDateTime::currentDateTime());
|
||
|
||
bool res = qry.exec();
|
||
QString str = qry.lastQuery();
|
||
if(!res)
|
||
{
|
||
qDebug()<<str<<"\n"<<qry.lastError().text();
|
||
}
|
||
qry.clear();
|
||
return res;
|
||
}
|
||
else
|
||
return false;
|
||
}
|
||
|
||
bool DataBase::insertGrid(QString name,QString description,int op)
|
||
{
|
||
if(db.open())
|
||
{
|
||
QSqlQuery qry(db);
|
||
qry.prepare("INSERT INTO grid(name, description, op, ts) VALUES (?, ?, ?, ?);");
|
||
qry.bindValue(0,name);
|
||
qry.bindValue(1,description);
|
||
qry.bindValue(2,op);
|
||
qry.bindValue(3,QDateTime::currentDateTime());
|
||
bool res = qry.exec();
|
||
QString str = qry.lastQuery();
|
||
if(!res)
|
||
{
|
||
qDebug()<<str<<"\n"<<qry.lastError().text();
|
||
}
|
||
qry.clear();
|
||
return res;
|
||
}
|
||
else
|
||
return false;
|
||
}
|
||
|
||
bool DataBase::insertZone(int grid_id,QString name,QString description,int op)
|
||
{
|
||
if(db.open())
|
||
{
|
||
QSqlQuery qry(db);
|
||
qry.prepare("INSERT INTO zone(grid_id, name, description, op, ts) VALUES (?, ?, ?, ?, ?)");
|
||
qry.bindValue(0,grid_id);
|
||
qry.bindValue(1,name);
|
||
qry.bindValue(2,description);
|
||
qry.bindValue(3,op);
|
||
qry.bindValue(4,QDateTime::currentDateTime());
|
||
bool res = qry.exec();
|
||
QString str = qry.lastQuery();
|
||
if(!res)
|
||
{
|
||
qDebug()<<str<<"\n"<<qry.lastError().text();
|
||
}
|
||
qry.clear();
|
||
return res;
|
||
}
|
||
else
|
||
return false;
|
||
}
|
||
|
||
bool DataBase::insertTopologic(QUuid uuid_from,QUuid uuid_to,QJsonObject context,int flag,QString description,int op)
|
||
{
|
||
QJsonDocument contextDoc(context);
|
||
QString strContext = contextDoc.toJson(QJsonDocument::Compact);
|
||
|
||
if(db.open())
|
||
{
|
||
QSqlQuery qry(db);
|
||
qry.prepare("INSERT INTO topologic(uuid_from, uuid_to, context, flag, description, op, ts) VALUES (?, ?, ?, ?, ?, ?, ?);");
|
||
qry.bindValue(0,uuid_from);
|
||
qry.bindValue(1,uuid_to);
|
||
qry.bindValue(2,strContext);
|
||
qry.bindValue(3,flag);
|
||
qry.bindValue(4,description);
|
||
qry.bindValue(5,op);
|
||
qry.bindValue(6,QDateTime::currentDateTime());
|
||
bool res = qry.exec();
|
||
QString str = qry.lastQuery();
|
||
if(!res)
|
||
{
|
||
qDebug()<<str<<"\n"<<qry.lastError().text();
|
||
}
|
||
qry.clear();
|
||
return res;
|
||
}
|
||
else
|
||
return false;
|
||
}
|
||
|
||
QString DataBase::getGridNameById(int id)
|
||
{
|
||
QString sName;
|
||
QString strSQL = "SELECT name FROM grid WHERE id = ?";
|
||
QVariantList params;
|
||
params.append(id);
|
||
|
||
try
|
||
{
|
||
QSqlQuery query = executeSQL(strSQL,false,params);
|
||
while (query.next())
|
||
{
|
||
sName = query.value(0).toString();
|
||
}
|
||
query.clear();
|
||
return sName;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
return sName;
|
||
}
|
||
}
|
||
|
||
QString DataBase::getZoneNameById(int id)
|
||
{
|
||
QString sName;
|
||
QString strSQL = "SELECT name FROM zone WHERE id = ?";
|
||
QVariantList params;
|
||
params.append(id);
|
||
|
||
try
|
||
{
|
||
QSqlQuery query = executeSQL(strSQL,false,params);
|
||
while (query.next())
|
||
{
|
||
sName = query.value(0).toString();
|
||
}
|
||
query.clear();
|
||
return sName;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
return sName;
|
||
}
|
||
}
|
||
|
||
QString DataBase::getStationNameById(int id)
|
||
{
|
||
QString sName;
|
||
QString strSQL = "SELECT name FROM station WHERE id = ?";
|
||
QVariantList params;
|
||
params.append(id);
|
||
|
||
try
|
||
{
|
||
QSqlQuery query = executeSQL(strSQL,false,params);
|
||
while (query.next())
|
||
{
|
||
sName = query.value(0).toString();
|
||
}
|
||
query.clear();
|
||
return sName;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
return sName;
|
||
}
|
||
}
|
||
|
||
QList<gridInfo> DataBase::getAllGrid()
|
||
{
|
||
QList<gridInfo> lst;
|
||
QString strSQL = "SELECT id, name, description FROM grid";
|
||
|
||
try
|
||
{
|
||
QSqlQuery query = executeSQL(strSQL);
|
||
while (query.next())
|
||
{
|
||
gridInfo info;
|
||
info.id = query.value(0).toInt();
|
||
info.name = query.value(1).toString();
|
||
info.description = query.value(2).toString();
|
||
lst.append(info);
|
||
}
|
||
query.clear();
|
||
return lst;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
return lst;
|
||
}
|
||
}
|
||
|
||
QList<zoneInfo> DataBase::getAllZone()
|
||
{
|
||
QList<zoneInfo> lst;
|
||
QString strSQL = "SELECT id,grid_id,name,description FROM zone";
|
||
|
||
try
|
||
{
|
||
QSqlQuery query = executeSQL(strSQL);
|
||
while (query.next())
|
||
{
|
||
zoneInfo info;
|
||
info.id = query.value(0).toInt();
|
||
info.grid_id = query.value(1).toInt();
|
||
info.name = query.value(2).toString();
|
||
info.description = query.value(3).toString();
|
||
lst.append(info);
|
||
}
|
||
query.clear();
|
||
return lst;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
return lst;
|
||
}
|
||
}
|
||
|
||
QList<stationInfo> DataBase::getAllStation()
|
||
{
|
||
QList<stationInfo> lst;
|
||
QString strSQL = "SELECT id,zone_id,name,description,is_local FROM station";
|
||
|
||
try
|
||
{
|
||
QSqlQuery query = executeSQL(strSQL);
|
||
while (query.next())
|
||
{
|
||
stationInfo info;
|
||
info.id = query.value(0).toInt();
|
||
info.zone_id = query.value(1).toInt();
|
||
info.name = query.value(2).toString();
|
||
info.description = query.value(3).toString();
|
||
info.is_local = query.value(4).toBool();
|
||
lst.append(info);
|
||
}
|
||
query.clear();
|
||
return lst;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
return lst;
|
||
}
|
||
}
|
||
|
||
QList<topologicInfo> DataBase::getAllTopologics()
|
||
{
|
||
QList<topologicInfo> lst;
|
||
QString strSQL = "SELECT id,uuid_from,uuid_to,context,flag FROM topologic";
|
||
|
||
try
|
||
{
|
||
QSqlQuery query = executeSQL(strSQL);
|
||
while (query.next())
|
||
{
|
||
topologicInfo info;
|
||
info.id = query.value(0).toInt();
|
||
info.uuid_from = QUuid(query.value(1).toString());
|
||
info.uuid_to = QUuid(query.value(2).toString());
|
||
QString str = query.value(3).toString();
|
||
info.context = QstringToJson(str);
|
||
info.flag = query.value(4).toInt();
|
||
|
||
lst.append(info);
|
||
}
|
||
query.clear();
|
||
return lst;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
return lst;
|
||
}
|
||
}
|
||
|
||
int DataBase::topologicExist(QUuid fromItem,QUuid toItem)
|
||
{
|
||
QString strSQL = "SELECT id FROM topologic WHERE uuid_from = ? AND uuid_to = ?";
|
||
QVariantList params;
|
||
params.append(fromItem.toString());
|
||
params.append(toItem.toString());
|
||
|
||
try
|
||
{
|
||
QSqlQuery query = executeSQL(strSQL,false,params);
|
||
while (query.next())
|
||
{
|
||
int id = query.value(0).toInt();
|
||
return id;
|
||
}
|
||
query.clear();
|
||
return -1;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
return -1;
|
||
}
|
||
}
|
||
|
||
topologicInfo DataBase::getTopologicById(int id)
|
||
{
|
||
topologicInfo info;
|
||
QString strSQL = "SELECT id,uuid_from,uuid_to,context,flag FROM topologic WHERE id = ?";
|
||
QVariantList params;
|
||
params.append(id);
|
||
|
||
try
|
||
{
|
||
QSqlQuery query = executeSQL(strSQL);
|
||
while (query.next())
|
||
{
|
||
info.id = query.value(0).toInt();
|
||
info.uuid_from = QUuid(query.value(1).toString());
|
||
info.uuid_to = QUuid(query.value(2).toString());
|
||
QString str = query.value(3).toString();
|
||
info.context = QstringToJson(str);
|
||
info.flag = query.value(4).toInt();
|
||
}
|
||
query.clear();
|
||
return info;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
return info;
|
||
}
|
||
}
|
||
|
||
bool DataBase::deleteTopologic(QUuid fromPin,QUuid toPin)
|
||
{
|
||
QString strSQL = "DELETE FROM topologic WHERE from_pin = ? AND to_pin = ?";
|
||
QVariantList params;
|
||
params.append(fromPin.toString());
|
||
params.append(toPin.toString());
|
||
|
||
try
|
||
{
|
||
executeSQL(strSQL,false,params);
|
||
LOG_INFO("DB", QString("Delete topologic from:%1 to:%2 success").arg(fromPin.toString(),toPin.toString()));
|
||
return true;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
LOG_ERROR("DB", QString("Delete topologic from:%1 to:%2 fail").arg(fromPin.toString(),toPin.toString()));
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/************************************************************/
|
||
componentInfo DataBase::getComponentInfoByUuid(QString uuid)
|
||
{
|
||
componentInfo inf;
|
||
if(db.open())
|
||
{
|
||
QSqlQuery qry(db);
|
||
|
||
//qry.prepare("SELECT * FROM component WHERE global_uuid = ?");
|
||
qry.prepare("SELECT id, global_uuid, model_name, tag, name, grid, zone, station, type, context, op FROM component WHERE global_uuid = ?");
|
||
qry.bindValue(0,uuid);
|
||
bool res = qry.exec();
|
||
QString str = qry.lastQuery();
|
||
if(!res)
|
||
{
|
||
qDebug()<<str<<"\n"<<qry.lastError().text();
|
||
qry.clear();
|
||
}
|
||
else
|
||
{
|
||
while (qry.next())
|
||
{
|
||
inf.uuid = QUuid(qry.value(0).toString());
|
||
inf.modelName = qry.value(1).toString();
|
||
inf.tag = qry.value(2).toString();
|
||
inf.name = qry.value(3).toString();
|
||
inf.grid = qry.value(4).toString();
|
||
inf.zone = qry.value(5).toString();
|
||
inf.station = qry.value(6).toString();
|
||
inf.type = qry.value(7).toInt();
|
||
QString str = qry.value(8).toString();
|
||
inf.context = QstringToJson(str);
|
||
inf.op = qry.value(9).toInt();
|
||
qry.clear();
|
||
}
|
||
}
|
||
}
|
||
return inf;
|
||
}
|
||
|
||
QList<componentInfo> DataBase::getAllComponents()
|
||
{
|
||
QList<componentInfo> lst;
|
||
if(db.open())
|
||
{
|
||
QSqlQuery qry(db);
|
||
|
||
qry.prepare("SELECT global_uuid, model_name,tag, name, grid, zone, station, type, context, op FROM component");
|
||
bool res = qry.exec();
|
||
QString str = qry.lastQuery();
|
||
if(!res)
|
||
{
|
||
qDebug()<<str<<"\n"<<qry.lastError().text();
|
||
qry.clear();
|
||
}
|
||
else
|
||
{
|
||
while (qry.next())
|
||
{
|
||
componentInfo inf;
|
||
inf.uuid = QUuid(qry.value(0).toString());
|
||
inf.modelName = qry.value(1).toString();
|
||
inf.tag = qry.value(2).toString();
|
||
inf.name = qry.value(3).toString();
|
||
inf.grid = qry.value(4).toString();
|
||
inf.zone = qry.value(5).toString();
|
||
inf.station = qry.value(6).toString();
|
||
inf.type = qry.value(7).toInt();
|
||
QString str = qry.value(8).toString();
|
||
inf.context = QstringToJson(str);
|
||
inf.op = qry.value(9).toInt();
|
||
lst.push_back(inf);
|
||
}
|
||
qry.clear();
|
||
}
|
||
}
|
||
return lst;
|
||
}
|
||
|
||
bool DataBase::componentExist(QString uuid)
|
||
{
|
||
if(db.open())
|
||
{
|
||
QSqlQuery qry(db);
|
||
|
||
qry.prepare("SELECT global_uuid FROM component WHERE global_uuid = ?");
|
||
qry.bindValue(0,uuid);
|
||
bool res = qry.exec();
|
||
QString str = qry.lastQuery();
|
||
if(!res)
|
||
{
|
||
qDebug()<<str<<"\n"<<qry.lastError().text();
|
||
qry.clear();
|
||
}
|
||
else
|
||
{
|
||
if(qry.size() > 0)
|
||
return true;
|
||
else
|
||
return false;
|
||
qry.clear();
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
|
||
bool DataBase::deleteComponent(QString uuid)
|
||
{
|
||
if(db.open())
|
||
{
|
||
QSqlQuery qry(db);
|
||
|
||
qry.prepare("DELETE FROM component WHERE global_uuid = ?");
|
||
qry.bindValue(0,uuid);
|
||
bool res = qry.exec();
|
||
QString str = qry.lastQuery();
|
||
if(!res)
|
||
{
|
||
qDebug()<<str<<"\n"<<qry.lastError().text();
|
||
}
|
||
qry.clear();
|
||
return res;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
QMap<int,componentTypeInfo> DataBase::getAllComponentType()
|
||
{
|
||
if(_componentType.empty())
|
||
{
|
||
QMap<int,componentTypeInfo> map;
|
||
if(db.open())
|
||
{
|
||
QSqlQuery qry(db);
|
||
|
||
qry.prepare("SELECT id, type, name, config FROM component_type");
|
||
bool res = qry.exec();
|
||
QString str = qry.lastQuery();
|
||
if(!res)
|
||
{
|
||
qDebug()<<str<<"\n"<<qry.lastError().text();
|
||
qry.clear();
|
||
}
|
||
else
|
||
{
|
||
while (qry.next())
|
||
{
|
||
componentTypeInfo inf;
|
||
inf.id = qry.value(0).toInt();
|
||
inf.type = qry.value(1).toString();
|
||
inf.name = qry.value(2).toString();
|
||
QString str = qry.value(3).toString();
|
||
inf.config = QstringToJson(str);
|
||
map.insert(inf.id,inf);
|
||
}
|
||
qry.clear();
|
||
}
|
||
}
|
||
_componentType = map;
|
||
}
|
||
return _componentType;
|
||
}
|
||
/************************************************************/
|
||
|
||
bool DataBase::updatePage(QString tag,QString name,QJsonObject context)
|
||
{
|
||
if(db.open())
|
||
{
|
||
QSqlQuery qry(db);
|
||
|
||
QJsonDocument contextDoc(context);
|
||
QString strCon = contextDoc.toJson(QJsonDocument::Compact);
|
||
|
||
qry.prepare("UPDATE page SET name=?, context=?, ts=? WHERE tag=?");
|
||
qry.bindValue(0,name);
|
||
qry.bindValue(1,strCon);
|
||
qry.bindValue(2,QDateTime::currentDateTime());
|
||
qry.bindValue(3,tag);
|
||
bool res = qry.exec();
|
||
QString str = qry.lastQuery();
|
||
const QVariantList list = qry.boundValues();
|
||
for (qsizetype i = 0; i < list.size(); ++i)
|
||
qDebug() << i << ":" << list.at(i).toString();
|
||
if(!res)
|
||
{
|
||
qDebug()<<str<<"\n"<<qry.lastError().text();
|
||
}
|
||
qry.clear();
|
||
return res;
|
||
}
|
||
else
|
||
return false;
|
||
}
|
||
|
||
int DataBase::getPageIdByName(QString name)
|
||
{
|
||
if(db.open())
|
||
{
|
||
QSqlQuery qry(db);
|
||
|
||
qry.prepare("SELECT id FROM page WHERE tag = ?");
|
||
qry.bindValue(0,name);
|
||
bool res = qry.exec();
|
||
QString str = qry.lastQuery();
|
||
if(!res)
|
||
{
|
||
qDebug()<<str<<"\n"<<qry.lastError().text();
|
||
qry.clear();
|
||
return -1;
|
||
}
|
||
else
|
||
{
|
||
while (qry.next())
|
||
{
|
||
int id = qry.value(0).toInt();
|
||
qry.clear();
|
||
return id;
|
||
}
|
||
return -1;
|
||
}
|
||
}
|
||
else
|
||
return -1;
|
||
}
|
||
|
||
QJsonObject DataBase::getPageContextByName(QString name)
|
||
{
|
||
if(db.open())
|
||
{
|
||
QSqlQuery qry(db);
|
||
|
||
qry.prepare("SELECT context FROM page WHERE tag = ?");
|
||
qry.bindValue(0,name);
|
||
bool res = qry.exec();
|
||
QString str = qry.lastQuery();
|
||
if(!res)
|
||
{
|
||
qDebug()<<str<<"\n"<<qry.lastError().text();
|
||
qry.clear();
|
||
return QJsonObject();
|
||
}
|
||
else
|
||
{
|
||
while (qry.next())
|
||
{
|
||
|
||
QString str = qry.value(0).toString();
|
||
QJsonObject obj = QstringToJson(str);
|
||
qry.clear();
|
||
return obj;
|
||
}
|
||
return QJsonObject();
|
||
}
|
||
}
|
||
else
|
||
return QJsonObject();
|
||
}
|
||
|
||
QList<pageInfo> DataBase::getAllPage()
|
||
{
|
||
QList<pageInfo> lst;
|
||
QString strSQL = "SELECT id,tag,name,label,context,description,op FROM page";
|
||
|
||
try
|
||
{
|
||
QSqlQuery query = executeSQL(strSQL);
|
||
while (query.next())
|
||
{
|
||
pageInfo info;
|
||
info.id = query.value(0).toInt();
|
||
info.tag = query.value(1).toString();
|
||
info.name = query.value(2).toString();
|
||
QString label = query.value(3).toString();
|
||
info.label = QstringToJson(label);
|
||
QString context = query.value(4).toString();
|
||
info.context = QstringToJson(context);
|
||
info.description = query.value(5).toString();
|
||
info.op = query.value(6).toInt();
|
||
|
||
lst.append(info);
|
||
}
|
||
query.clear();
|
||
return lst;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
return lst;
|
||
}
|
||
}
|
||
|
||
bool DataBase::deleteComponentById(int id)
|
||
{
|
||
if(db.open())
|
||
{
|
||
QSqlQuery qry(db);
|
||
|
||
qry.prepare("DELETE FROM topologic WHERE com_from = ? or com_to = ?");
|
||
qry.bindValue(0,id);
|
||
qry.bindValue(1,id);
|
||
bool res = qry.exec();
|
||
QString str = qry.lastQuery();
|
||
if(!res)
|
||
{
|
||
qDebug()<<str<<"\n"<<qry.lastError().text();
|
||
return res;
|
||
}
|
||
qry.finish();
|
||
|
||
qry.prepare("DELETE FROM component WHERE id = ?");
|
||
qry.bindValue(0,id);
|
||
res = qry.exec();
|
||
str = qry.lastQuery();
|
||
if(!res)
|
||
{
|
||
qDebug()<<str<<"\n"<<qry.lastError().text();
|
||
}
|
||
qry.clear();
|
||
return res;
|
||
}
|
||
else
|
||
return false;
|
||
}
|
||
|
||
/*****************************************************************************/
|
||
bool DataBase::insertBay(QUuid uuid,QString name,QString tag,QString type,double unom,double fla,double capacity,QString description,bool inService,int nState,QString grid,QString zone,
|
||
QString station,QJsonObject business,QJsonObject fromUuid,QJsonObject toUuid,QJsonObject protect,QJsonObject faultRec,QJsonObject status,QJsonObject dynSense,
|
||
QJsonObject instruct,QJsonObject etc,QList<QUuid> components,QJsonObject context)
|
||
{
|
||
QJsonDocument businessDoc(business);
|
||
QString strBusiness = businessDoc.toJson(QJsonDocument::Compact);
|
||
|
||
QJsonDocument fromUuidDoc(fromUuid);
|
||
QString strFromUuid = fromUuidDoc.toJson(QJsonDocument::Compact);
|
||
|
||
QJsonDocument toUuidDoc(toUuid);
|
||
QString strToUuid = toUuidDoc.toJson(QJsonDocument::Compact);
|
||
|
||
QJsonDocument protectDoc(protect);
|
||
QString strProtect = protectDoc.toJson(QJsonDocument::Compact);
|
||
|
||
QJsonDocument faultRecDoc(faultRec);
|
||
QString strFaultRec = faultRecDoc.toJson(QJsonDocument::Compact);
|
||
|
||
QJsonDocument statusDoc(status);
|
||
QString strStatus = statusDoc.toJson(QJsonDocument::Compact);
|
||
|
||
QJsonDocument dynSenseDoc(dynSense);
|
||
QString strDynSense = dynSenseDoc.toJson(QJsonDocument::Compact);
|
||
|
||
QJsonDocument instructDoc(instruct);
|
||
QString strInstruct = instructDoc.toJson(QJsonDocument::Compact);
|
||
|
||
QJsonDocument etcDoc(etc);
|
||
QString strEtc = etcDoc.toJson(QJsonDocument::Compact);
|
||
|
||
QJsonDocument contextDoc(etc);
|
||
QString strContext = contextDoc.toJson(QJsonDocument::Compact);
|
||
|
||
QString strSQL = "INSERT INTO bay(bay_uuid, name, tag, type, unom, fla, capacity, description, in_service, state, grid, zone, station, business, from_uuids, to_uuids, dev_protect, dev_fault_record, dev_status, dev_dyn_sense, dev_instruct, dev_etc, components, context) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
|
||
|
||
QStringList uuidStrings;
|
||
for (const QUuid &uuid : components) {
|
||
// 使用WithoutBraces确保无花括号,符合PG数组元素格式
|
||
uuidStrings << "\"" + uuid.toString(QUuid::WithoutBraces) + "\"";
|
||
}
|
||
QString arrayUuid = "{" + uuidStrings.join(",") + "}";
|
||
|
||
QVariantList params;
|
||
params.append(uuid.toString());
|
||
params.append(name);
|
||
params.append(tag);
|
||
params.append(type);
|
||
params.append(unom);
|
||
params.append(fla);
|
||
params.append(capacity);
|
||
params.append(description);
|
||
params.append(inService);
|
||
params.append(nState);
|
||
params.append(grid);
|
||
params.append(zone);
|
||
params.append(station);
|
||
params.append(strBusiness);
|
||
params.append(strFromUuid);
|
||
params.append(strToUuid);
|
||
params.append(strProtect);
|
||
params.append(strFaultRec);
|
||
params.append(strStatus);
|
||
params.append(strDynSense);
|
||
params.append(strInstruct);
|
||
params.append(strEtc);
|
||
params.append(arrayUuid);
|
||
params.append(strContext);
|
||
|
||
try
|
||
{
|
||
executeSQL(strSQL,false,params);
|
||
return true;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
LOG_ERROR("DB", QString("Insert bay fail"));
|
||
return false;
|
||
}
|
||
}
|
||
|
||
bool DataBase::updateBay(QUuid uuid,QString name,QString tag,double unom,double fla,double capacity,QString description,bool inService,int nState,QJsonObject business,
|
||
QJsonObject fromUuid,QJsonObject toUuid,QJsonObject protect,QJsonObject faultRec,QJsonObject status,QJsonObject dynSense,QJsonObject instruct,
|
||
QJsonObject etc,QList<QUuid> components,QJsonObject context)
|
||
{
|
||
QJsonDocument businessDoc(business);
|
||
QString strBusiness = businessDoc.toJson(QJsonDocument::Compact);
|
||
|
||
QJsonDocument fromUuidDoc(fromUuid);
|
||
QString strFromUuid = fromUuidDoc.toJson(QJsonDocument::Compact);
|
||
|
||
QJsonDocument toUuidDoc(toUuid);
|
||
QString strToUuid = toUuidDoc.toJson(QJsonDocument::Compact);
|
||
|
||
QJsonDocument protectDoc(protect);
|
||
QString strProtect = protectDoc.toJson(QJsonDocument::Compact);
|
||
|
||
QJsonDocument faultRecDoc(faultRec);
|
||
QString strFaultRec = faultRecDoc.toJson(QJsonDocument::Compact);
|
||
|
||
QJsonDocument statusDoc(status);
|
||
QString strStatus = statusDoc.toJson(QJsonDocument::Compact);
|
||
|
||
QJsonDocument dynSenseDoc(dynSense);
|
||
QString strDynSense = dynSenseDoc.toJson(QJsonDocument::Compact);
|
||
|
||
QJsonDocument instructDoc(instruct);
|
||
QString strInstruct = instructDoc.toJson(QJsonDocument::Compact);
|
||
|
||
QJsonDocument etcDoc(etc);
|
||
QString strEtc = etcDoc.toJson(QJsonDocument::Compact);
|
||
|
||
QJsonDocument contextDoc(etc);
|
||
QString strContext = contextDoc.toJson(QJsonDocument::Compact);
|
||
|
||
QStringList uuidStrings;
|
||
for (const QUuid &uuid : components) {
|
||
// 使用WithoutBraces确保无花括号,符合PG数组元素格式
|
||
uuidStrings << "\"" + uuid.toString(QUuid::WithoutBraces) + "\"";
|
||
}
|
||
QString arrayUuid = "{" + uuidStrings.join(",") + "}";
|
||
|
||
QString strSQL = "UPDATE bay SET name = ?,tag = ?,unom = ?,fla = ?,capacity = ?,description = ?,in_service = ?, state = ?, business = ?,from_uuids = ?,to_uuids = ?,dev_protect = ?,dev_fault_record = ?, dev_status = ?,dev_dyn_sense = ?,dev_instruct = ?,dev_etc = ?,components = ?,context = ? WHERE bay_uuid = ?";
|
||
QVariantList params;
|
||
params.append(name);
|
||
params.append(tag);
|
||
params.append(unom);
|
||
params.append(fla);
|
||
params.append(capacity);
|
||
params.append(description);
|
||
params.append(inService);
|
||
params.append(nState);
|
||
params.append(strBusiness);
|
||
params.append(strFromUuid);
|
||
params.append(strToUuid);
|
||
params.append(strProtect);
|
||
params.append(strFaultRec);
|
||
params.append(strStatus);
|
||
params.append(strDynSense);
|
||
params.append(strInstruct);
|
||
params.append(strEtc);
|
||
params.append(arrayUuid);
|
||
params.append(strContext);
|
||
params.append(uuid);
|
||
|
||
try
|
||
{
|
||
executeSQL(strSQL,false,params);
|
||
return true;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
LOG_ERROR("DB", QString("Update bay fail"));
|
||
return false;
|
||
}
|
||
}
|
||
|
||
bayInfo DataBase::getBay(QUuid uuid)
|
||
{
|
||
bayInfo info;
|
||
QString strSQL = "SELECT bay_uuid, name, tag, type, unom, fla, capacity, description, in_service, state, grid, zone, station, business, from_uuids, to_uuids, dev_protect, dev_fault_record, dev_status, dev_dyn_sense, dev_instruct, dev_etc, components, context FROM bay WHERE bay_uuid = ?";
|
||
QVariantList params;
|
||
params.append(uuid);
|
||
|
||
try
|
||
{
|
||
QSqlQuery query = executeSQL(strSQL,false,params);
|
||
while (query.next())
|
||
{
|
||
info.uuid = QUuid(query.value(0).toString());
|
||
info.name = query.value(1).toString();
|
||
info.tag = query.value(2).toString();
|
||
info.type = query.value(3).toString();
|
||
info.unom = query.value(4).toDouble();
|
||
info.fla = query.value(5).toDouble();
|
||
info.capacity = query.value(6).toDouble();
|
||
info.description = query.value(7).toString();
|
||
info.inService = query.value(8).toBool();
|
||
info.nState = query.value(9).toInt();
|
||
info.grid = query.value(10).toString();
|
||
info.zone = query.value(11).toString();
|
||
info.station = query.value(12).toString();
|
||
QString strBusi = query.value(13).toString();
|
||
info.business = QstringToJson(strBusi);
|
||
QString strFrom = query.value(14).toString();
|
||
info.fromUuid = QstringToJson(strFrom);
|
||
QString strTo = query.value(15).toString();
|
||
info.toUuid = QstringToJson(strTo);
|
||
QString strProtect = query.value(16).toString();
|
||
info.protect = QstringToJson(strProtect);
|
||
QString strFaultRec= query.value(17).toString();
|
||
info.faultRec = QstringToJson(strFaultRec);
|
||
QString strStatus= query.value(18).toString();
|
||
info.status = QstringToJson(strStatus);
|
||
QString strDynSense= query.value(19).toString();
|
||
info.dynSense = QstringToJson(strDynSense);
|
||
QString strInstructe= query.value(20).toString();
|
||
info.instruct = QstringToJson(strInstructe);
|
||
QString strEtc= query.value(21).toString();
|
||
info.etc = QstringToJson(strEtc);
|
||
QString rawData = query.value(22).toString();
|
||
info.components = parseUuidArray(rawData);
|
||
QString strContext= query.value(23).toString();
|
||
info.context = QstringToJson(strContext);
|
||
}
|
||
query.clear();
|
||
return info;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
return info;
|
||
}
|
||
}
|
||
|
||
QList<bayInfo> DataBase::getAllBay()
|
||
{
|
||
QList<bayInfo> lstInfo;
|
||
QString strSQL = "SELECT bay_uuid, name, tag, type, unom, fla, capacity, description, in_service, state, grid, zone, station, business, from_uuids, to_uuids, dev_protect, dev_fault_record, dev_status, dev_dyn_sense, dev_instruct, dev_etc, components, context FROM bay";
|
||
|
||
try
|
||
{
|
||
QSqlQuery query = executeSQL(strSQL);
|
||
while (query.next())
|
||
{
|
||
bayInfo info;
|
||
info.uuid = QUuid(query.value(0).toString());
|
||
info.name = query.value(1).toString();
|
||
info.tag = query.value(2).toString();
|
||
info.type = query.value(3).toString();
|
||
info.unom = query.value(4).toDouble();
|
||
info.fla = query.value(5).toDouble();
|
||
info.capacity = query.value(6).toDouble();
|
||
info.description = query.value(7).toString();
|
||
info.inService = query.value(8).toBool();
|
||
info.nState = query.value(9).toInt();
|
||
info.grid = query.value(10).toString();
|
||
info.zone = query.value(11).toString();
|
||
info.station = query.value(12).toString();
|
||
QString strBusi = query.value(13).toString();
|
||
info.business = QstringToJson(strBusi);
|
||
QString strFrom = query.value(14).toString();
|
||
info.fromUuid = QstringToJson(strFrom);
|
||
QString strTo = query.value(15).toString();
|
||
info.toUuid = QstringToJson(strTo);
|
||
QString strProtect = query.value(16).toString();
|
||
info.protect = QstringToJson(strProtect);
|
||
QString strFaultRec= query.value(17).toString();
|
||
info.faultRec = QstringToJson(strFaultRec);
|
||
QString strStatus= query.value(18).toString();
|
||
info.status = QstringToJson(strStatus);
|
||
QString strDynSense= query.value(19).toString();
|
||
info.dynSense = QstringToJson(strDynSense);
|
||
QString strInstructe= query.value(20).toString();
|
||
info.instruct = QstringToJson(strInstructe);
|
||
QString strEtc= query.value(21).toString();
|
||
info.etc = QstringToJson(strEtc);
|
||
QString rawData = query.value(22).toString();
|
||
info.components = parseUuidArray(rawData);
|
||
QString strContext= query.value(23).toString();
|
||
info.context = QstringToJson(strContext);
|
||
lstInfo.append(info);
|
||
}
|
||
query.clear();
|
||
return lstInfo;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
return lstInfo;
|
||
}
|
||
}
|
||
|
||
bool DataBase::ifBayExist(QUuid uuid)
|
||
{
|
||
QString strSQL = "SELECT bay_uuid FROM bay WHERE bay_uuid = ?";
|
||
QVariantList params;
|
||
params.append(uuid);
|
||
|
||
try
|
||
{
|
||
QSqlQuery query = executeSQL(strSQL,false,params);
|
||
while (query.next())
|
||
{
|
||
return true;
|
||
}
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
return false;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
bool DataBase::deleteBay(QUuid uuid)
|
||
{
|
||
QString strSQL = "DELETE FROM bay WHERE bay_uuid = ?";
|
||
QVariantList params;
|
||
params.append(uuid);
|
||
|
||
try
|
||
{
|
||
executeSQL(strSQL,false,params);
|
||
LOG_INFO("DB", QString("Delete bay %1 success").arg(uuid.toString()));
|
||
return true;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
LOG_ERROR("DB", QString("Delete bay %1 failed").arg(uuid.toString()));
|
||
return false;
|
||
}
|
||
}
|
||
/*****************************************************************************/
|
||
|
||
bool DataBase::insertMeasurement(QString name,QString tag,int type,QJsonObject dataSource,QJsonObject eventPlan,int size,QUuid bayId,QUuid componentId)
|
||
{
|
||
QString strSQL = "INSERT INTO measurement(tag, name, type, data_source, event_plan, size, bay_uuid, component_uuid) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
|
||
QJsonDocument dataDoc(dataSource);
|
||
QString strData = dataDoc.toJson(QJsonDocument::Compact);
|
||
|
||
QJsonDocument eventDoc(eventPlan);
|
||
QString strEvent = eventDoc.toJson(QJsonDocument::Compact);
|
||
|
||
QVariantList params;
|
||
params.append(tag);
|
||
params.append(name);
|
||
params.append(type);
|
||
params.append(strData);
|
||
params.append(strEvent);
|
||
params.append(size);
|
||
params.append(bayId);
|
||
params.append(componentId);
|
||
|
||
try
|
||
{
|
||
executeSQL(strSQL,false,params);
|
||
return true;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
LOG_ERROR("DB", QString("Insert measurement fail"));
|
||
return false;
|
||
}
|
||
}
|
||
|
||
bool DataBase::updateMeasurement(QString name,int type,QJsonObject dataSource,QJsonObject eventPlan,int size,QUuid componentId)
|
||
{
|
||
QJsonDocument dataDoc(dataSource);
|
||
QString strData = dataDoc.toJson(QJsonDocument::Compact);
|
||
|
||
QJsonDocument eventDoc(eventPlan);
|
||
QString strEvent = eventDoc.toJson(QJsonDocument::Compact);
|
||
|
||
QString strSQL = "UPDATE measurement SET type = ?,data_source = ?,event_plan = ?,size = ? WHERE name = ? AND component_uuid = ?";
|
||
QVariantList params;
|
||
params.append(type);
|
||
params.append(strData);
|
||
params.append(strEvent);
|
||
params.append(size);
|
||
params.append(name);
|
||
params.append(componentId);
|
||
|
||
try
|
||
{
|
||
executeSQL(strSQL,false,params);
|
||
return true;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
LOG_ERROR("DB", QString("Update measurement %1 fail").arg(name));
|
||
return false;
|
||
}
|
||
}
|
||
|
||
bool DataBase::delteMeasurement(QString name,QUuid componentId)
|
||
{
|
||
QString strSQL = "DELETE FROM measurement WHERE name = ? AND component_uuid = ?";
|
||
QVariantList params;
|
||
params.append(name);
|
||
params.append(componentId);
|
||
|
||
try
|
||
{
|
||
executeSQL(strSQL,false,params);
|
||
LOG_INFO("DB", QString("Delete measurement %1 success").arg(name));
|
||
return true;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
LOG_ERROR("DB", QString("Delete measurement %1 failed").arg(name));
|
||
return false;
|
||
}
|
||
}
|
||
|
||
bool DataBase::ifMeasureExist(QString name,QUuid componentId)
|
||
{
|
||
QString strSQL = "SELECT id FROM measurement WHERE name = ? AND component_uuid = ?";
|
||
QVariantList params;
|
||
params.append(name);
|
||
params.append(componentId);
|
||
|
||
try
|
||
{
|
||
QSqlQuery query = executeSQL(strSQL,false,params);
|
||
while (query.next())
|
||
{
|
||
return true;
|
||
}
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
return false;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
QList<MeasurementInfo> DataBase::getMeasurement(QUuid componentId)
|
||
{
|
||
QList<MeasurementInfo> lst;
|
||
QString strSQL = "SELECT tag, name, type, data_source, event_plan, size, bay_uuid, component_uuid FROM measurement WHERE component_uuid = ?";
|
||
QVariantList params;
|
||
params.append(componentId);
|
||
|
||
try
|
||
{
|
||
QSqlQuery query = executeSQL(strSQL,false,params);
|
||
while (query.next())
|
||
{
|
||
MeasurementInfo info;
|
||
info.tag = query.value(0).toString();
|
||
info.name = query.value(1).toString();
|
||
info.type = query.value(2).toInt();
|
||
|
||
QString conData = query.value(3).toString();
|
||
QJsonObject objData = QstringToJson(conData);
|
||
|
||
QString sEvent = query.value(4).toString();
|
||
QJsonObject objEvent = QstringToJson(sEvent);
|
||
|
||
info.size = query.value(5).toInt();
|
||
info.bayUuid = QUuid(query.value(6).toString());
|
||
info.componentUuid = QUuid(query.value(7).toString());
|
||
|
||
info.nSource = objData["type"].toInt();
|
||
QJsonObject objIoAddress = objData["io_address"].toObject();
|
||
info.sStation = objData["station"].toString();
|
||
info.equipment = objIoAddress["device"].toString();
|
||
info.channel = objIoAddress["channel"].toString();
|
||
info.sChannel = objIoAddress["channel"].toString();
|
||
info.nPacket = objIoAddress["packet"].toInt();
|
||
info.nOffset = objIoAddress["offset"].toInt();
|
||
|
||
info.bEnable = objEvent["enable"].toBool();
|
||
QJsonObject objCause = objEvent["cause"].toObject();
|
||
if(objCause.contains("upup")){
|
||
info.mapTE.insert("upup",objCause["upup"].toDouble());
|
||
}
|
||
if(objCause.contains("up")){
|
||
info.mapTE.insert("up",objCause["up"].toDouble());
|
||
}
|
||
if(objCause.contains("down")){
|
||
info.mapTE.insert("down",objCause["down"].toDouble());
|
||
}
|
||
if(objCause.contains("downdown")){
|
||
info.mapTE.insert("downdown",objCause["downdown"].toDouble());
|
||
}
|
||
info.sEdge = objCause["edge"].toString();
|
||
QJsonObject objAction = objEvent["action"].toObject();
|
||
info.sCommand = objAction["command"].toString();
|
||
|
||
QJsonArray arrPara = objAction["parameters"].toArray();
|
||
for(const QJsonValue ¶Value:arrPara){
|
||
info.lstParameter.append(paraValue.toString());
|
||
}
|
||
lst.append(info);
|
||
}
|
||
query.clear();
|
||
return lst;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
return lst;
|
||
}
|
||
}
|
||
/*****************************************************************************/
|
||
|
||
void DataBase::readXML()
|
||
{
|
||
if (m_sFileName.isEmpty())
|
||
return;
|
||
|
||
QFile *pFile = new QFile(m_sFileName);
|
||
if (!pFile->open(QIODevice::ReadOnly | QFile::Text))
|
||
{
|
||
QMessageBox::information(NULL, QString("title"), QString::fromWCharArray(L"配置文件打开错误"));
|
||
return;
|
||
}
|
||
|
||
QXmlStreamReader* m_pReader = new QXmlStreamReader(pFile);
|
||
while (!m_pReader->atEnd() && !m_pReader->hasError())
|
||
{
|
||
m_pReader->lineNumber();
|
||
QXmlStreamReader::TokenType token = m_pReader->readNext();
|
||
if (token == QXmlStreamReader::StartDocument)
|
||
continue;
|
||
|
||
//qDebug() << m_pReader->name();
|
||
if (m_pReader->isStartElement())
|
||
{
|
||
if(m_pReader->name() == QString("DataBase"))
|
||
{
|
||
QXmlStreamAttributes attributes = m_pReader->attributes();
|
||
QString tpe = attributes.value("Type").toString();
|
||
QString sName = attributes.value("Name").toString();
|
||
//QString sProDB = attributes.value("ProjectDB").toString();
|
||
if (tpe == QString("PostgreSQL"))
|
||
{
|
||
_DataBaseType = QString("QPSQL");
|
||
_DataBaseName = sName;
|
||
//_ProjectDB = sProDB;
|
||
}
|
||
}
|
||
else if(m_pReader->name() == QString("HostName"))
|
||
{
|
||
_HostName = m_pReader->readElementText();
|
||
}
|
||
else if(m_pReader->name() == QString("Port"))
|
||
{
|
||
_Port = m_pReader->readElementText().toInt();
|
||
}
|
||
else if(m_pReader->name() == QString("UserName"))
|
||
{
|
||
_UserName = m_pReader->readElementText();
|
||
}
|
||
else if(m_pReader->name() == QString("Password"))
|
||
{
|
||
_PassWord = m_pReader->readElementText();
|
||
}
|
||
}
|
||
m_pReader->readNext();
|
||
}
|
||
if (m_pReader->hasError())
|
||
{
|
||
qDebug() << m_pReader->errorString();
|
||
}
|
||
m_pReader->clear();
|
||
delete m_pReader;
|
||
m_pReader = NULL;
|
||
pFile->close();
|
||
delete pFile;
|
||
pFile = NULL;
|
||
}
|
||
|
||
QJsonObject DataBase::QstringToJson(QString jsonString)
|
||
{
|
||
QJsonDocument jsonDocument = QJsonDocument::fromJson(jsonString.toUtf8().data());
|
||
if(jsonDocument.isNull())
|
||
{
|
||
qDebug()<< "String NULL"<< jsonString.toLocal8Bit().data();
|
||
}
|
||
QJsonObject jsonObject = jsonDocument.object();
|
||
return jsonObject;
|
||
}
|
||
|
||
QList<QUuid> DataBase::parseUuidArray(const QString& pgArray)
|
||
{
|
||
QList<QUuid> uuids;
|
||
|
||
if (pgArray.isEmpty() || pgArray == "{}")
|
||
return uuids;
|
||
|
||
// 移除花括号并分割元素
|
||
QStringList parts = pgArray.mid(1, pgArray.size() - 2).split(",");
|
||
|
||
for (QString& part : parts) {
|
||
part = part.trimmed();
|
||
|
||
// 处理带双引号的元素
|
||
if (part.startsWith('"') && part.endsWith('"')) {
|
||
part = part.mid(1, part.size() - 2);
|
||
}
|
||
|
||
// 处理 NULL 值(转为空 QUuid)
|
||
if (part == "NULL") {
|
||
uuids << QUuid();
|
||
} else {
|
||
uuids << QUuid(part);
|
||
}
|
||
}
|
||
|
||
return uuids;
|
||
}
|
||
//=================================元模=============================================//
|
||
bool DataBase::getAttributeGroup()
|
||
{
|
||
QString strSQL = "SELECT * FROM basic.attribute_group";
|
||
|
||
try
|
||
{
|
||
QSqlQuery query = executeSQL(strSQL);
|
||
while (query.next())
|
||
{
|
||
int id = query.value(0).toInt();
|
||
QString groupType = query.value(1).toString();
|
||
QString groupName = query.value(2).toString();
|
||
int ispublic = query.value(3).toInt();
|
||
QString remark = query.value(4).toString();
|
||
|
||
if(!_attributeGroup.contains(id))
|
||
{
|
||
attributeGroup ag;
|
||
ag.id = id;
|
||
ag.groupType = groupType;
|
||
ag.groupName = groupName;
|
||
ag.ispublic = ispublic;
|
||
ag.remark = remark;
|
||
_attributeGroup.insert(id,ag);
|
||
}
|
||
}
|
||
query.finish();
|
||
return true;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
|
||
bool DataBase::getDataType()
|
||
{
|
||
QString strSQL = "SELECT * FROM basic.data_type";
|
||
|
||
try
|
||
{
|
||
QSqlQuery query = executeSQL(strSQL);
|
||
while (query.next())
|
||
{
|
||
int id = query.value(0).toInt();
|
||
QString dt = query.value(1).toString();
|
||
QString dbt = query.value(2).toString();
|
||
|
||
if(!_dataType.contains(id))
|
||
{
|
||
dataType type;
|
||
type.id = id;
|
||
type.dataType = dt;
|
||
type.databaseType = dbt;
|
||
_dataType.insert(id,type);
|
||
}
|
||
}
|
||
query.finish();
|
||
return true;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
|
||
bool DataBase::getModelType()
|
||
{
|
||
QString strSQL = "SELECT id, model_type, model_name, graphic_element, icon, remark FROM basic.model_type";
|
||
|
||
try
|
||
{
|
||
QSqlQuery query = executeSQL(strSQL);
|
||
while (query.next())
|
||
{
|
||
int id = query.value(0).toInt();
|
||
QString sModelType = query.value(1).toString(); //模型类型
|
||
QString sModelName = query.value(2).toString(); //模型名称
|
||
int graphicElement = query.value(3).toInt();
|
||
QByteArray bIcon = query.value(4).toByteArray(); //图片
|
||
QString remark = query.value(5).toString(); //备注
|
||
|
||
if(!_modelType.contains(id))
|
||
{
|
||
modelType mt;
|
||
mt.id = id;
|
||
mt.modelType = sModelType;
|
||
mt.modelName = sModelName;
|
||
mt.graphicEelement = graphicElement;
|
||
mt.icon = bIcon;
|
||
mt.remark = remark;
|
||
|
||
_modelType.insert(id,mt);
|
||
}
|
||
}
|
||
query.finish();
|
||
return true;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
|
||
bool DataBase::getModelGroup()
|
||
{
|
||
QString strSQL = "SELECT * FROM basic.model_group";
|
||
|
||
try
|
||
{
|
||
QSqlQuery query = executeSQL(strSQL);
|
||
while (query.next())
|
||
{
|
||
int id = query.value(0).toInt();
|
||
int modelTypeId = query.value(1).toInt();
|
||
int attrybuteGroupId = query.value(2).toInt();
|
||
|
||
if(!_modelGroup.contains(id))
|
||
{
|
||
modelGroup mg;
|
||
mg.id = id;
|
||
mg.modelTypeId = modelTypeId;
|
||
mg.attributeGroupId = attrybuteGroupId;
|
||
|
||
_modelGroup.insert(id,mg);
|
||
}
|
||
}
|
||
query.finish();
|
||
return true;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
|
||
bool DataBase::getModelAttribute()
|
||
{
|
||
QString strSQL = "SELECT * FROM basic.model_attribute";
|
||
|
||
try
|
||
{
|
||
QSqlQuery query = executeSQL(strSQL);
|
||
while (query.next())
|
||
{
|
||
int id = query.value(0).toInt();
|
||
qint64 mti = query.value(1).toLongLong();
|
||
qint64 agi = query.value(2).toLongLong();
|
||
qint64 ai = query.value(3).toLongLong();
|
||
|
||
if(!_modelAttribute.contains(id))
|
||
{
|
||
modelAttribute ma;
|
||
ma.id = id;
|
||
ma.modelTypeId = mti;
|
||
ma.attributeGroupId = agi;
|
||
ma.attributeId = ai;
|
||
|
||
_modelAttribute.insert(id,ma);
|
||
}
|
||
}
|
||
query.finish();
|
||
return true;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
|
||
bool DataBase::getModelAttributePublic()
|
||
{
|
||
QString strSQL = "SELECT * FROM basic.model_attribute_public";
|
||
|
||
try
|
||
{
|
||
QSqlQuery query = executeSQL(strSQL);
|
||
while (query.next())
|
||
{
|
||
int id = query.value(0).toInt();
|
||
qint64 agi = query.value(1).toLongLong();
|
||
qint64 ai = query.value(2).toLongLong();
|
||
|
||
if(!_modelAttributePublic.contains(id))
|
||
{
|
||
modelAttributePublic ma;
|
||
ma.id = id;
|
||
ma.attributeGroupId = agi;
|
||
ma.attributeId = ai;
|
||
|
||
_modelAttributePublic.insert(id,ma);
|
||
}
|
||
}
|
||
query.finish();
|
||
return true;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
|
||
bool DataBase::getAttribute()
|
||
{
|
||
QString strSQL = "SELECT * FROM basic.attribute";
|
||
|
||
try
|
||
{
|
||
QSqlQuery query = executeSQL(strSQL);
|
||
while (query.next())
|
||
{
|
||
int id = query.value(0).toInt();
|
||
QString att = query.value(1).toString(); //属性
|
||
QString attn = query.value(2).toString(); //属性名
|
||
qint64 dt = query.value(3).toLongLong(); //类型号
|
||
int len = query.value(4).toInt(); //类型长度
|
||
int scale = query.value(5).toInt(); //类型精度
|
||
int inn = query.value(6).toInt(); //非空
|
||
QString dv = query.value(7).toString(); //默认值
|
||
QString vr = query.value(8).toString(); //范围
|
||
int visible = query.value(9).toInt();
|
||
|
||
if(!_attribute.contains(id))
|
||
{
|
||
attribute attribute;
|
||
attribute.id = id;
|
||
attribute.attribute = att;
|
||
attribute.attributeName = attn;
|
||
attribute.dataTypeId = dt;
|
||
attribute.lengthPrecision = len;
|
||
attribute.scale = scale;
|
||
attribute.isNotNull = inn;
|
||
attribute.defaultValue = dv;
|
||
attribute.valueRange = vr;
|
||
attribute.isVisible = visible;
|
||
|
||
_attribute.insert(id,attribute);
|
||
}
|
||
}
|
||
query.finish();
|
||
return true;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
|
||
bool DataBase::getModelConnectivity()
|
||
{
|
||
QString strSQL = "SELECT * FROM basic.model_connectivity";
|
||
|
||
try
|
||
{
|
||
QSqlQuery query = executeSQL(strSQL);
|
||
while (query.next())
|
||
{
|
||
int id = query.value(0).toInt();
|
||
QString fm = query.value(1).toString(); //from
|
||
QString tm = query.value(2).toString(); //to
|
||
int con = query.value(3).toInt(); //是否可联
|
||
|
||
if(!_modelConnectivity.contains(id))
|
||
{
|
||
modelConnectivity connect;
|
||
connect.id = id;
|
||
connect.fromModel = fm;
|
||
connect.toModel = tm;
|
||
connect.connectivity = con;
|
||
|
||
_modelConnectivity.insert(id,connect);
|
||
}
|
||
}
|
||
query.finish();
|
||
return true;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
//=================================工程模===========================================//
|
||
|
||
bool DataBase::insertProjectSetting(const QString& baseModel,const QString& modelName,QJsonObject setting)
|
||
{
|
||
QString strSQL = "INSERT INTO diagramui_projectmodelSetting(base_name,model_name, context) VALUES (?, ?, ?)";
|
||
QJsonDocument contextDoc(setting);
|
||
QString strContext = contextDoc.toJson(QJsonDocument::Compact);
|
||
|
||
QVariantList params;
|
||
params.append(baseModel);
|
||
params.append(modelName);
|
||
params.append(strContext);
|
||
try
|
||
{
|
||
executeSQL(strSQL,false,params);
|
||
return true;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
LOG_ERROR("DB", QString("Insert diagramui_projectmodelSetting fail"));
|
||
return false;
|
||
}
|
||
}
|
||
|
||
bool DataBase::updateProjectSetting(const QString& baseModel,const QString& modelName,QJsonObject setting)
|
||
{
|
||
QJsonDocument contextDoc(setting);
|
||
QString strContext = contextDoc.toJson(QJsonDocument::Compact);
|
||
|
||
QString strSQL = "UPDATE diagramui_projectmodelsetting SET context = ? WHERE base_name = ? AND model_name = ?";
|
||
QVariantList params;
|
||
params.append(strContext);
|
||
params.append(baseModel);
|
||
params.append(modelName);
|
||
|
||
try
|
||
{
|
||
executeSQL(strSQL,false,params);
|
||
return true;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
LOG_ERROR("DB", QString("Update model %1 setting %2 fail").arg(modelName,strContext));
|
||
return false;
|
||
}
|
||
}
|
||
|
||
QJsonObject DataBase::getProjectSetting(const QString& baseModel,const QString& modelName)
|
||
{
|
||
|
||
QString strSQL = "SELECT context FROM diagramui_projectmodelsetting WHERE base_name = ? AND model_name = ?";
|
||
QVariantList params;
|
||
params.append(baseModel);
|
||
params.append(modelName);
|
||
|
||
try
|
||
{
|
||
QJsonObject obj;
|
||
QSqlQuery query = executeSQL(strSQL,false,params);
|
||
while (query.next())
|
||
{
|
||
QString con = query.value(0).toString();
|
||
obj= QstringToJson(con);
|
||
}
|
||
return obj;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
return QJsonObject();
|
||
}
|
||
}
|
||
|
||
QStringList DataBase::getProjectWithinBase(const QString& baseModel)
|
||
{
|
||
QStringList lst;
|
||
|
||
QString strSQL = "SELECT model_name FROM diagramui_projectmodelsetting WHERE base_name = ?";
|
||
QVariantList params;
|
||
params.append(baseModel);
|
||
|
||
try
|
||
{
|
||
QSqlQuery query = executeSQL(strSQL,false,params);
|
||
while (query.next())
|
||
{
|
||
QString name = query.value(0).toString();
|
||
lst.append(name);
|
||
}
|
||
return lst;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
return lst;
|
||
}
|
||
}
|
||
|
||
bool DataBase::deleteProjectSetting(const QString& baseModel,const QString& modelName)
|
||
{
|
||
QString strSQL = "DELETE FROM diagramui_projectmodelsetting WHERE base_name = ? AND model_name = ?";
|
||
QVariantList params;
|
||
params.append(baseModel);
|
||
params.append(modelName);
|
||
|
||
try
|
||
{
|
||
executeSQL(strSQL,false,params);
|
||
LOG_INFO("DB", QString("Delete row %1 success").arg(modelName));
|
||
return true;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
LOG_ERROR("DB", QString("Delete row %1 failed").arg(modelName));
|
||
return false;
|
||
}
|
||
}
|
||
|
||
bool DataBase::createProjectManager()
|
||
{
|
||
QString strSQL = R"(
|
||
CREATE TABLE IF NOT EXISTS project_manager (
|
||
id SERIAL NOT NULL PRIMARY KEY,
|
||
name VARCHAR(64) NOT NULL,
|
||
tag VARCHAR(64) NOT NULL,
|
||
meta_model VARCHAR(64) NOT NULL,
|
||
group_name VARCHAR(64) NOT NULL,
|
||
link_type integer NOT NULL DEFAULT 0,
|
||
check_state JSONB NOT NULL DEFAULT '{}'::jsonb,
|
||
"ispublic" boolean NOT NULL DEFAULT false
|
||
);
|
||
)";
|
||
|
||
if(db.open())
|
||
{
|
||
QSqlQuery qry(db);
|
||
|
||
bool res = qry.exec(strSQL);
|
||
QString str = qry.lastQuery();
|
||
if(!res)
|
||
{
|
||
qDebug()<<str<<"\n"<<qry.lastError().text();
|
||
qry.clear();
|
||
}
|
||
else
|
||
{
|
||
if(qry.size() > 0)
|
||
return true;
|
||
else
|
||
return false;
|
||
qry.clear();
|
||
}
|
||
}
|
||
return false;
|
||
|
||
/*try
|
||
{
|
||
executeSQL(strSQL);
|
||
return true;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
return false;
|
||
}*/
|
||
}
|
||
|
||
bool DataBase::insertProjectManager(const QString& name,const QString& tag,const QString& metaModel,const QString& groupName,int linkType,QJsonObject checkState,bool ispublic)
|
||
{
|
||
QString strSQL = "INSERT INTO project_manager(name, tag, meta_model, group_name, link_type, check_state, ispublic) VALUES (?, ?, ?, ?, ?, ?, ?)";
|
||
QJsonDocument checkDoc(checkState);
|
||
QString strCheck = checkDoc.toJson(QJsonDocument::Compact);
|
||
|
||
QVariantList params;
|
||
params.append(name);
|
||
params.append(tag);
|
||
params.append(metaModel);
|
||
params.append(groupName);
|
||
params.append(linkType);
|
||
params.append(strCheck);
|
||
params.append(ispublic);
|
||
try
|
||
{
|
||
executeSQL(strSQL,false,params);
|
||
return true;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
LOG_ERROR("DB", QString("Insert project_manager fail"));
|
||
return false;
|
||
}
|
||
|
||
}
|
||
|
||
bool DataBase::updateCheckState(const QString& tableName,QJsonObject checkState)
|
||
{
|
||
QJsonDocument checkDoc(checkState);
|
||
QString strCheck = checkDoc.toJson(QJsonDocument::Compact);
|
||
|
||
QString strSQL = "UPDATE project_manager SET check_state = ? WHERE name = ?";
|
||
QVariantList params;
|
||
params.append(strCheck);
|
||
params.append(tableName);
|
||
|
||
try
|
||
{
|
||
executeSQL(strSQL,false,params);
|
||
return true;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
LOG_ERROR("DB", QString("Update table %1 state %2 fail").arg(tableName,strCheck));
|
||
return false;
|
||
}
|
||
}
|
||
|
||
QMap<QString,int> DataBase::getProjectFromManager(const QString& sMeta)
|
||
{
|
||
QMap<QString,int> map;
|
||
QString strSQL = "SELECT tag,link_type FROM project_manager WHERE meta_model = ?";
|
||
QVariantList params;
|
||
params.append(sMeta);
|
||
|
||
try
|
||
{
|
||
QSqlQuery query = executeSQL(strSQL,false,params);
|
||
while (query.next())
|
||
{
|
||
QString tag = query.value(0).toString();
|
||
int nType = query.value(1).toInt();
|
||
if(!map.contains(tag))
|
||
{
|
||
map.insert(tag,nType);
|
||
}
|
||
}
|
||
query.clear();
|
||
return map;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
return map;
|
||
}
|
||
}
|
||
|
||
QMap<QString,QJsonObject> DataBase::getCheckStateFromManager(const QString& sProject)
|
||
{
|
||
QMap<QString,QJsonObject> map;
|
||
|
||
QString strSQL = "SELECT group_name, check_state FROM project_manager WHERE tag = ?";
|
||
QVariantList params;
|
||
params.append(sProject);
|
||
|
||
try
|
||
{
|
||
QSqlQuery query = executeSQL(strSQL,false,params);
|
||
while (query.next())
|
||
{
|
||
QString group = query.value(0).toString();
|
||
QString state = query.value(1).toString();
|
||
QJsonObject jsonObj = QstringToJson(state);
|
||
map.insert(group,jsonObj);
|
||
}
|
||
query.clear();
|
||
return map;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
return map;
|
||
}
|
||
}
|
||
|
||
QMap<QString,QJsonObject> DataBase::getPublicStateFromManager()
|
||
{
|
||
QMap<QString,QJsonObject> map;
|
||
bool ispublic = true;
|
||
|
||
QString strSQL = "SELECT group_name, check_state FROM project_manager WHERE ispublic = ?";
|
||
QVariantList params;
|
||
params.append(ispublic);
|
||
|
||
try
|
||
{
|
||
QSqlQuery query = executeSQL(strSQL,false,params);
|
||
while (query.next())
|
||
{
|
||
QString group = query.value(0).toString();
|
||
QString state = query.value(1).toString();
|
||
QJsonObject jsonObj = QstringToJson(state);
|
||
map.insert(group,jsonObj);
|
||
}
|
||
query.clear();
|
||
return map;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
return map;
|
||
}
|
||
}
|
||
|
||
QMap<QString,QString> DataBase::getProjectTableName(const QString& sProject)
|
||
{
|
||
QMap<QString,QString> map;
|
||
QString strSQL = "SELECT group_name, name FROM project_manager WHERE tag = ?";
|
||
QVariantList params;
|
||
params.append(sProject);
|
||
|
||
try
|
||
{
|
||
QSqlQuery query = executeSQL(strSQL,false,params);
|
||
while (query.next())
|
||
{
|
||
QString group = query.value(0).toString();
|
||
QString tableName = query.value(1).toString();
|
||
map.insert(group,tableName);
|
||
}
|
||
query.clear();
|
||
return map;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
return map;
|
||
}
|
||
}
|
||
|
||
QMap<QString,int> DataBase::getAllProjectModel()
|
||
{
|
||
QMap<QString,int> map; //工程模名,类型
|
||
QString strSQL = "SELECT tag, MAX(link_type) AS link_type,ispublic FROM project_manager GROUP BY tag,ispublic";
|
||
|
||
try
|
||
{
|
||
QSqlQuery query = executeSQL(strSQL);
|
||
while (query.next())
|
||
{
|
||
QString tableName = query.value(0).toString();
|
||
int linkType = query.value(1).toInt();
|
||
bool ispublic = query.value(2).toBool();
|
||
if(!ispublic)
|
||
map.insert(tableName,linkType);
|
||
}
|
||
query.clear();
|
||
return map;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
return map;
|
||
}
|
||
}
|
||
|
||
QMap<QString,propertyGroupState> DataBase::getModelInfo(const QString& sProject)
|
||
{
|
||
QMap<QString,propertyGroupState> map;
|
||
QString strSQL = "SELECT group_name,name,check_state FROM project_manager WHERE tag = ?";
|
||
QVariantList params;
|
||
params.append(sProject);
|
||
|
||
try
|
||
{
|
||
QSqlQuery query = executeSQL(strSQL,false,params);
|
||
while (query.next())
|
||
{
|
||
propertyGroupState sta;
|
||
sta.groupName = query.value(0).toString();
|
||
sta.tableName = query.value(1).toString();
|
||
sta.propertyState = QstringToJson(query.value(2).toString());
|
||
map.insert(sta.groupName,sta);
|
||
}
|
||
query.clear();
|
||
return map;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
return map;
|
||
}
|
||
}
|
||
|
||
QMap<QString,propertyGroupState> DataBase::getPublicInfo()
|
||
{
|
||
QMap<QString,propertyGroupState> map;
|
||
bool ispublic = true;
|
||
QString strSQL = "SELECT group_name,name,check_state FROM project_manager WHERE ispublic = ?";
|
||
QVariantList params;
|
||
params.append(ispublic);
|
||
|
||
try
|
||
{
|
||
QSqlQuery query = executeSQL(strSQL,false,params);
|
||
while (query.next())
|
||
{
|
||
propertyGroupState sta;
|
||
sta.groupName = query.value(0).toString();
|
||
sta.tableName = query.value(1).toString();
|
||
sta.propertyState = QstringToJson(query.value(2).toString());
|
||
map.insert(sta.groupName,sta);
|
||
}
|
||
query.clear();
|
||
return map;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
return map;
|
||
}
|
||
}
|
||
|
||
QMap<QString,projectManager> DataBase::getProjectModelGroupInfo(const QString& sTable)
|
||
{
|
||
QMap<QString,projectManager> map;
|
||
QString strSQL = "SELECT name,tag,meta_model,group_name,link_type,check_state FROM project_manager WHERE tag = ?";
|
||
QVariantList params;
|
||
params.append(sTable);
|
||
|
||
try
|
||
{
|
||
QSqlQuery query = executeSQL(strSQL,false,params);
|
||
while (query.next())
|
||
{
|
||
projectManager info;
|
||
info.name = query.value(0).toString();
|
||
info.tag = query.value(1).toString();
|
||
info.metaModel = query.value(2).toString();
|
||
info.groupName = query.value(3).toString();
|
||
info.linkType = query.value(4).toInt();
|
||
QString json = query.value(5).toString();
|
||
info.checkState = QstringToJson(json);
|
||
|
||
if(!map.contains(info.groupName))
|
||
map.insert(info.groupName,info);
|
||
}
|
||
query.clear();
|
||
return map;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
return map;
|
||
}
|
||
}
|
||
|
||
QMap<QUuid,PropertyValueInfo> DataBase::selectGroupPropertyByState(const QString& tableName,QMap<QString,propertyStateInfo> mapPro)
|
||
{
|
||
QStringList paramList;
|
||
for(auto &pro:mapPro)
|
||
{
|
||
paramList.append(pro.name);
|
||
}
|
||
QString strSQL = QString("SELECT %1 FROM %2").arg(paramList.join(", ")).arg(tableName);
|
||
QMap<QUuid,PropertyValueInfo> map;
|
||
PropertyValueInfo info;
|
||
|
||
try
|
||
{
|
||
QSqlQuery query = executeSQL(strSQL);
|
||
while (query.next())
|
||
{
|
||
QUuid uuid;
|
||
for(auto &proVal:mapPro)
|
||
{
|
||
propertyStateInfo pro;
|
||
if(proVal.name == "global_uuid" && tableName != "baseProperty") //除基础属性组,其他组不显示uuid todo:组名适配
|
||
{
|
||
uuid = QUuid(query.value(proVal.name).toString());
|
||
continue;
|
||
}
|
||
else if(proVal.name == "global_uuid" && tableName == "baseProperty")
|
||
{
|
||
uuid = QUuid(query.value(proVal.name).toString());
|
||
}
|
||
pro.name = proVal.name;
|
||
pro.type = proVal.type;
|
||
pro.isVisibe = proVal.isVisibe;
|
||
/*if(proVal.type == "JSONB"){ //json单独处理
|
||
pro.defaultValue = query.value(proVal.name).toJsonObject();
|
||
}
|
||
else*/
|
||
pro.defaultValue = query.value(proVal.name );
|
||
info.insert(proVal.name ,pro);
|
||
}
|
||
map.insert(uuid,info);
|
||
}
|
||
query.clear();
|
||
return map;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
return map;
|
||
}
|
||
}
|
||
|
||
PropertyValueInfo DataBase::selectGroupPropertyByValue(const QString& tableName,QUuid uuid,PropertyValueInfo value)
|
||
{
|
||
PropertyValueInfo map;
|
||
QStringList paramList;
|
||
for(auto &pro:value)
|
||
{
|
||
paramList.append(pro.name);
|
||
}
|
||
QString strSQL = QString("SELECT %1 FROM %2 WHERE global_uuid = ?").arg(paramList.join(", ")).arg(tableName);
|
||
QVariantList params;
|
||
params.append(uuid);
|
||
|
||
try
|
||
{
|
||
QSqlQuery query = executeSQL(strSQL,false,params);
|
||
while (query.next())
|
||
{
|
||
QUuid uuid;
|
||
for(auto &proVal:value)
|
||
{
|
||
propertyStateInfo pro;
|
||
pro.name = proVal.name;
|
||
pro.type = proVal.type;
|
||
pro.isVisibe = proVal.isVisibe;
|
||
pro.defaultValue = query.value(proVal.name );
|
||
map.insert(proVal.name,pro);
|
||
}
|
||
}
|
||
query.clear();
|
||
return map;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
return map;
|
||
}
|
||
}
|
||
|
||
QList<measureAttributeType> DataBase::getMeasureAttributeTypes() //暂时调换获取的name与tag
|
||
{
|
||
QList<measureAttributeType> lst;
|
||
QString strSQL = "SELECT attribute,attribute_name FROM basic.attribute WHERE is_visible = ?";
|
||
QVariantList params;
|
||
params.append(2);
|
||
|
||
try
|
||
{
|
||
QSqlQuery query = executeSQL(strSQL,false,params);
|
||
while (query.next())
|
||
{
|
||
QString attName = query.value(0).toString();
|
||
QString attTag = query.value(1).toString();
|
||
|
||
if(attName.contains("$")){ //包含$
|
||
QStringList lst_dollar;
|
||
lst_dollar<<"s1"<<"s2"<<"s3";
|
||
if(attName.contains("sn")){ //同时包含$与sn,9个分支
|
||
QStringList lst_sn;
|
||
lst_sn<<"s1"<<"s2"<<"s3";
|
||
|
||
if(attName.contains("_$")){ //包含_$,特殊处理
|
||
if(attName.first(1) == "I"){ //头字母为I
|
||
QStringList lst_I;
|
||
lst_I<<"a"<<"b"<<"c";
|
||
|
||
for(auto &i:lst_I)
|
||
{
|
||
QString tn1 = attName;
|
||
QString tt1 = attTag;
|
||
|
||
QString name = tn1.replace("_$",i);
|
||
QString tag = tt1.replace("_$",i);
|
||
for(auto &sn:lst_sn)
|
||
{
|
||
QString tn2 = name;
|
||
QString tt2 = tag;
|
||
measureAttributeType measure;
|
||
measure.tag = tn2.replace("sn",sn);
|
||
measure.name = tt2.replace("sn",sn);
|
||
lst.append(measure);
|
||
}
|
||
}
|
||
}
|
||
else{ //头字母为U
|
||
QStringList lst_U;
|
||
lst_U<<"AB"<<"BC"<<"CA";
|
||
|
||
for(auto &u:lst_U)
|
||
{
|
||
QString tn1 = attName;
|
||
QString tt1 = attTag;
|
||
|
||
QString name = tn1.replace("_$",u);
|
||
QString tag = tt1.replace("_$",u);
|
||
for(auto &sn:lst_sn)
|
||
{
|
||
QString tn2 = name;
|
||
QString tt2 = tag;
|
||
measureAttributeType measure;
|
||
measure.tag = tn2.replace("sn",sn);
|
||
measure.name = tt2.replace("sn",sn);
|
||
lst.append(measure);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
else{ //只包含$与sn
|
||
for(auto &dor:lst_dollar)
|
||
{
|
||
QString tn1 = attName;
|
||
QString tt1 = attTag;
|
||
|
||
QString name = tn1.replace("$",dor);
|
||
QString tag = tt1.replace("$",dor);
|
||
for(auto &sn:lst_sn)
|
||
{
|
||
QString tn2 = name;
|
||
QString tt2 = tag;
|
||
measureAttributeType measure;
|
||
measure.tag = tn2.replace("sn",sn);
|
||
measure.name = tt2.replace("sn",sn);
|
||
lst.append(measure);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
else{ //不包含sn,3种分支
|
||
if(attName.contains("_$")){ //包含_$
|
||
if(attName.first(1) == "I"){ //头字母为I
|
||
QStringList lst_I;
|
||
lst_I<<"a"<<"b"<<"c";
|
||
|
||
for(auto &i:lst_I)
|
||
{
|
||
QString name = attName;
|
||
QString tag = attTag;
|
||
|
||
measureAttributeType measure;
|
||
measure.tag = name.replace("_$",i);
|
||
measure.name = tag.replace("_$",i);
|
||
lst.append(measure);
|
||
}
|
||
}
|
||
else{ //头字母为U
|
||
QStringList lst_U;
|
||
lst_U<<"AB"<<"BC"<<"CA";
|
||
|
||
for(auto &u:lst_U)
|
||
{
|
||
QString name = attName;
|
||
QString tag = attTag;
|
||
|
||
measureAttributeType measure;
|
||
measure.tag = name.replace("_$",u);
|
||
measure.name = tag.replace("_$",u);
|
||
lst.append(measure);
|
||
}
|
||
}
|
||
}
|
||
else{ //不包含_$
|
||
QStringList lst_dollar;
|
||
lst_dollar<<"s1"<<"s2"<<"s3";
|
||
|
||
for(auto &dor:lst_dollar)
|
||
{
|
||
QString name = attName;
|
||
QString tag = attTag;
|
||
|
||
measureAttributeType measure;
|
||
measure.tag = name.replace("$",dor);
|
||
measure.name = tag.replace("$",dor);
|
||
lst.append(measure);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
else if(attName.contains("sn")){ //只包含sn,3种分支
|
||
QStringList lst_sn;
|
||
lst_sn<<"s1"<<"s2"<<"s3";
|
||
|
||
for(auto &sn:lst_sn)
|
||
{
|
||
QString name = attName;
|
||
QString tag = attTag;
|
||
|
||
measureAttributeType measure;
|
||
measure.tag = name.replace("sn",sn);
|
||
measure.name = tag.replace("sn",sn);
|
||
lst.append(measure);
|
||
}
|
||
}
|
||
else{ //没有分支
|
||
measureAttributeType measure;
|
||
measure.tag = attName;
|
||
measure.name = attTag;
|
||
lst.append(measure);
|
||
}
|
||
}
|
||
query.clear();
|
||
return lst;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
LOG_ERROR("DB", QString("Select measureAttributeType fail"));
|
||
}
|
||
return lst;
|
||
}
|
||
|
||
bool DataBase::createDynamicTable(const QString &tableName, const QStringList &fields)
|
||
{
|
||
QString strSQL = "CREATE TABLE IF NOT EXISTS " + tableName + " (";
|
||
for (const QString &field : fields) {
|
||
strSQL += field + ", ";
|
||
}
|
||
// Remove the last comma and space
|
||
strSQL.chop(2);
|
||
strSQL += ");";
|
||
|
||
try
|
||
{
|
||
executeSQL(strSQL,true);
|
||
LOG_INFO("DB", QString("Create table %1 success").arg(tableName));
|
||
return true;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
LOG_ERROR("DB", QString("Create table %1 fail").arg(tableName));
|
||
return false;
|
||
}
|
||
}
|
||
|
||
bool DataBase::deleteProjectModel(const QString& sProject)
|
||
{
|
||
QStringList lstTable;
|
||
QString strSQL = "SELECT name FROM project_manager WHERE tag = ?";
|
||
QVariantList params;
|
||
params.append(sProject);
|
||
try
|
||
{
|
||
QSqlQuery query = executeSQL(strSQL,false,params);
|
||
while (query.next())
|
||
{
|
||
QString sName = query.value(0).toString(); //获取表名
|
||
lstTable.append(sName);
|
||
}
|
||
query.finish();
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
return false;
|
||
}
|
||
|
||
if(!db.transaction())
|
||
{
|
||
LOG_ERROR("DB", QString("Start transaction failed. error: %1.").arg( db.lastError().databaseText()));
|
||
return false;
|
||
}
|
||
QStringList sqlStatements;
|
||
for(auto &sTab:lstTable)
|
||
{
|
||
sqlStatements << QString("DROP TABLE IF EXISTS %1").arg(sTab);
|
||
}
|
||
try
|
||
{
|
||
executeBatchSQL(sqlStatements,true);
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
if(!db.rollback()) // 回滚失败时记录警告
|
||
{
|
||
LOG_ERROR("DB", QString("Rollback failed. error: %1").arg(db.lastError().databaseText()));
|
||
}
|
||
return false;
|
||
}
|
||
if(!db.commit()) // 提交
|
||
{
|
||
LOG_ERROR("DB", QString("Commit transaction failed. error: %1.").arg(db.lastError().databaseText()));
|
||
return false;
|
||
}
|
||
|
||
strSQL = "DELETE FROM project_manager WHERE tag = ?";
|
||
params.clear();
|
||
params.append(sProject);
|
||
try
|
||
{
|
||
executeSQL(strSQL,false,params);
|
||
LOG_INFO("DB", QString("Delete row %1 success").arg(sProject));
|
||
return true;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
LOG_ERROR("DB", QString("Delete row %1 failed").arg(sProject));
|
||
return false;
|
||
}
|
||
}
|
||
|
||
bool DataBase::ifDynamicTableExist(const QString& sTable)
|
||
{
|
||
QStringList lstTable;
|
||
QString strSQL = "SELECT name FROM project_manager WHERE name = ?";
|
||
QVariantList params;
|
||
params.append(sTable);
|
||
try
|
||
{
|
||
QSqlQuery query = executeSQL(strSQL,false,params);
|
||
while (query.next())
|
||
{
|
||
return true;
|
||
}
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
return false;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
bool DataBase::updateProjectName(const QString& newTable,const QString& newPro,const QString& oldTable)
|
||
{
|
||
QString strSQL = QString("UPDATE project_manager SET name = ?,tag = ? WHERE name = ?");
|
||
QVariantList params;
|
||
params.append(newTable);
|
||
params.append(newPro);
|
||
params.append(oldTable);
|
||
try
|
||
{
|
||
executeSQL(strSQL,false,params);
|
||
return true;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
LOG_ERROR("DB", QString("Update project_manager %1 fail").arg(oldTable));
|
||
return false;
|
||
}
|
||
}
|
||
|
||
bool DataBase::alterTableName(const QString& oldTable,const QString& newTable)
|
||
{
|
||
QString strSQL = QString("ALTER TABLE %1 RENAME TO %2").arg(oldTable,newTable);
|
||
QVariantList params;
|
||
try
|
||
{
|
||
executeSQL(strSQL,true);
|
||
return true;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
LOG_ERROR("DB", QString("ALTER TABLE %1 fail").arg(oldTable));
|
||
return false;
|
||
}
|
||
}
|
||
|
||
bool DataBase::updateComponentModelName(const QString& strOld,const QString& strNew)
|
||
{
|
||
QString strSQL = QString("UPDATE component SET model_name = ? WHERE model_name = ?");
|
||
QVariantList params;
|
||
params.append(strOld);
|
||
params.append(strNew);
|
||
try
|
||
{
|
||
executeSQL(strSQL,false,params);
|
||
return true;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
LOG_ERROR("DB", QString("Update component model_name %1 fail").arg(strOld));
|
||
return false;
|
||
}
|
||
}
|
||
|
||
bool DataBase::deleteTable(const QString& sName)
|
||
{
|
||
QString strSQL = QString("DROP TABLE IF EXISTS %1").arg(sName);
|
||
try
|
||
{
|
||
executeSQL(strSQL,true);
|
||
LOG_INFO("DB", QString("Drop table %1 success").arg(sName));
|
||
return true;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
LOG_ERROR("DB", QString("Drop table %1 failed").arg(sName));
|
||
return false;
|
||
}
|
||
}
|
||
|
||
bool DataBase::deleteRecordFromManager(const QString& sProject,const QString& sGroup)
|
||
{
|
||
QString strSQL = "DELETE FROM project_manager WHERE tag = ? AND group_name = ?";
|
||
QVariantList params;
|
||
params.append(sProject);
|
||
params.append(sGroup);
|
||
|
||
try
|
||
{
|
||
executeSQL(strSQL,false,params);
|
||
LOG_INFO("DB", QString("Delete row %1 success").arg(sProject));
|
||
return true;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
LOG_ERROR("DB", QString("Delete row %1 failed").arg(sProject));
|
||
return false;
|
||
}
|
||
}
|
||
|
||
bool DataBase::modifyProjectTable(QString sTable,QMap<QString,QString> mOld,QMap<QString,QString> mNew)
|
||
{
|
||
QStringList sqlStatements;
|
||
for (auto &col : mOld.keys()) {
|
||
if (!mNew.contains(col)) {
|
||
sqlStatements << QString("ALTER TABLE %1 DROP COLUMN %2")
|
||
.arg(sTable, col);
|
||
}
|
||
}
|
||
// 添加/修改列
|
||
for (auto &col : mNew.keys()) {
|
||
const QString &newType = mNew[col];
|
||
|
||
// 新增列
|
||
if (!mOld.contains(col)) {
|
||
sqlStatements << QString("ALTER TABLE %1 ADD COLUMN %2 %3")
|
||
.arg(sTable, col, newType);
|
||
}
|
||
// 修改列类型
|
||
else if (mOld[col] != newType) {
|
||
sqlStatements << QString("ALTER TABLE %1 ALTER COLUMN %2 TYPE %3 USING %2::%3")
|
||
.arg(sTable, col, newType);
|
||
}
|
||
}
|
||
|
||
if(!db.transaction())
|
||
{
|
||
LOG_ERROR("DB", QString("Start transaction failed. error: %1.").arg( db.lastError().databaseText()));
|
||
return false;
|
||
}
|
||
try
|
||
{
|
||
executeBatchSQL(sqlStatements);
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
if(!db.rollback()) // 回滚失败时记录警告
|
||
{
|
||
LOG_ERROR("DB", QString("Rollback failed. error: %1").arg(db.lastError().databaseText()));
|
||
}
|
||
return false;
|
||
}
|
||
if(!db.commit()) // 提交
|
||
{
|
||
LOG_ERROR("DB", QString("Commit transaction failed. error: %1.").arg(db.lastError().databaseText()));
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
QStringList DataBase::ifModelOccupy(const QString& sName)
|
||
{
|
||
QStringList lst;
|
||
QMap<QString,projectManager> map;
|
||
QString strSQL = "SELECT tag FROM component WHERE model_name = ?";
|
||
QVariantList params;
|
||
params.append(sName);
|
||
|
||
try
|
||
{
|
||
QSqlQuery query = executeSQL(strSQL,false,params);
|
||
while (query.next())
|
||
{
|
||
QString str = query.value(0).toString();
|
||
lst.append(str);
|
||
}
|
||
query.clear();
|
||
return lst;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
return lst;
|
||
}
|
||
}
|
||
|
||
bool DataBase::insertEditorProject(QUuid uid,QString name,QString tag)
|
||
{
|
||
QString strSQL = "INSERT INTO diagramui_editor_projects(global_uuid, name, tag) VALUES (?, ?, ?)";
|
||
|
||
QVariantList params;
|
||
params.append(uid);
|
||
params.append(name);
|
||
params.append(tag);
|
||
try
|
||
{
|
||
executeSQL(strSQL,false,params);
|
||
return true;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
LOG_ERROR("DB", QString("Insert diagramui_editor_projects fail"));
|
||
return false;
|
||
}
|
||
}
|
||
|
||
QList<editorProjectInfo> DataBase::getAllEditorProject()
|
||
{
|
||
QList<editorProjectInfo> lst;
|
||
QString strSQL = "SELECT id,global_uuid,name,tag FROM diagramui_editor_projects";
|
||
|
||
try
|
||
{
|
||
QSqlQuery query = executeSQL(strSQL);
|
||
while (query.next())
|
||
{
|
||
editorProjectInfo info;
|
||
info.id = query.value(0).toInt();
|
||
info.uuid = QUuid(query.value(1).toString());
|
||
info.name = query.value(2).toString();
|
||
info.tag = query.value(3).toString();
|
||
|
||
lst.append(info);
|
||
}
|
||
query.clear();
|
||
return lst;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
return lst;
|
||
}
|
||
}
|
||
|
||
bool DataBase::deleteEditorProject(QString name)
|
||
{
|
||
QString strSQL = "DELETE FROM diagramui_editor_projects WHERE name = ?";
|
||
QVariantList params;
|
||
params.append(name);
|
||
|
||
try
|
||
{
|
||
executeSQL(strSQL,false,params);
|
||
LOG_INFO("DB", QString("Delete diagramui_editor_projects %1 success").arg(name));
|
||
return true;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
LOG_INFO("DB", QString("Delete diagramui_editor_projects %1 fail").arg(name));
|
||
return false;
|
||
}
|
||
}
|
||
|
||
bool DataBase::ifEditorProjectExist(QString name)
|
||
{
|
||
QString strSQL = "SELECT * FROM diagramui_editor_projects WHERE name = ?";
|
||
QVariantList params;
|
||
params.append(name);
|
||
|
||
try
|
||
{
|
||
QSqlQuery query = executeSQL(strSQL,false,params);
|
||
while (query.next())
|
||
{
|
||
return true;
|
||
}
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
return false;
|
||
}
|
||
return false;
|
||
}
|
||
/***********************basesetting****************************/
|
||
bool DataBase::insertBaseSetting(QUuid uid,QString projectName,QString autorName,QByteArray context,QUuid generateId,QString ts)
|
||
{
|
||
QString strSQL = "INSERT INTO diagramui_editor_basesetting(global_uuid, project_name, autor, context, generate_uuid, ts) VALUES (?, ?, ?, ?, ?, ?)";
|
||
|
||
QVariantList params;
|
||
params.append(uid);
|
||
params.append(projectName);
|
||
params.append(autorName);
|
||
params.append(context);
|
||
params.append(generateId);
|
||
params.append(ts);
|
||
try
|
||
{
|
||
executeSQL(strSQL,false,params);
|
||
return true;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
LOG_ERROR("DB", QString("Insert diagramui_editor_basesetting fail"));
|
||
return false;
|
||
}
|
||
}
|
||
bool DataBase::updateBaseSetting(QUuid uid,QByteArray context,QString ts)
|
||
{
|
||
QString strSQL = "UPDATE diagramui_editor_basesetting SET context = ?, ts = ? WHERE global_uuid = ?";
|
||
QVariantList params;
|
||
params.append(context);
|
||
params.append(ts);
|
||
params.append(uid);
|
||
|
||
try
|
||
{
|
||
executeSQL(strSQL,false,params);
|
||
return true;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
LOG_ERROR("DB", QString("Update diagramui_editor_basesetting %1 fail").arg(uid.toString()));
|
||
return false;
|
||
}
|
||
}
|
||
|
||
QByteArray DataBase::getBaseSettingByUid(QUuid uid)
|
||
{
|
||
QByteArray byte;
|
||
QString strSQL = "SELECT context FROM diagramui_editor_basesetting WHERE global_uuid = ?";
|
||
QVariantList params;
|
||
params.append(uid);
|
||
|
||
try
|
||
{
|
||
QSqlQuery query = executeSQL(strSQL,false,params);
|
||
while (query.next())
|
||
{
|
||
byte = query.value(0).toByteArray();
|
||
}
|
||
query.clear();
|
||
return byte;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
return byte;
|
||
}
|
||
}
|
||
|
||
editorBaseSettingInfo DataBase::getBaseSettingInfo(QUuid uid)
|
||
{
|
||
editorBaseSettingInfo info;
|
||
QString strSQL = "SELECT project_name, autor, context, generate_uuid, ts FROM diagramui_editor_basesetting WHERE global_uuid = ?";
|
||
QVariantList params;
|
||
params.append(uid);
|
||
|
||
try
|
||
{
|
||
QSqlQuery query = executeSQL(strSQL,false,params);
|
||
while (query.next())
|
||
{
|
||
info.projectName = query.value(0).toString();
|
||
info.autor = query.value(1).toString();
|
||
info.context = query.value(2).toByteArray();
|
||
info.generateUid = QUuid(query.value(3).toString());
|
||
info.ts = query.value(4).toString();
|
||
info.uuid = uid;
|
||
}
|
||
query.clear();
|
||
return info;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
return info;
|
||
}
|
||
}
|
||
|
||
QList<editorBaseSettingInfo> DataBase::getAllBaseSetting()
|
||
{
|
||
QList<editorBaseSettingInfo> lst;
|
||
QString strSQL = "SELECT id,global_uuid, project_name, autor, context, generate_uuid, ts FROM diagramui_editor_basesetting";
|
||
|
||
try
|
||
{
|
||
QSqlQuery query = executeSQL(strSQL);
|
||
while (query.next())
|
||
{
|
||
editorBaseSettingInfo info;
|
||
info.id = query.value(0).toInt();
|
||
info.uuid = QUuid(query.value(1).toString());
|
||
info.projectName = query.value(2).toString();
|
||
info.autor = query.value(3).toString();
|
||
info.context = query.value(4).toByteArray();
|
||
info.generateUid = QUuid(query.value(5).toString());
|
||
info.ts = query.value(6).toString();
|
||
|
||
lst.append(info);
|
||
}
|
||
query.clear();
|
||
return lst;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
return lst;
|
||
}
|
||
}
|
||
|
||
bool DataBase::deleteBaseSetting(QUuid uid)
|
||
{
|
||
QString strSQL = "DELETE FROM diagramui_editor_basesetting WHERE global_uuid = ?";
|
||
QVariantList params;
|
||
params.append(uid);
|
||
|
||
try
|
||
{
|
||
executeSQL(strSQL,false,params);
|
||
LOG_INFO("DB", QString("Delete diagramui_editor_basesetting %1 success").arg(uid.toString()));
|
||
return true;
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
LOG_INFO("DB", QString("Delete diagramui_editor_basesetting %1 fail").arg(uid.toString()));
|
||
return false;
|
||
}
|
||
}
|
||
|
||
bool DataBase::ifBaseSettingExist(QUuid uid)
|
||
{
|
||
QString strSQL = "SELECT * FROM diagramui_editor_basesetting WHERE global_uuid = ?";
|
||
QVariantList params;
|
||
params.append(uid);
|
||
|
||
try
|
||
{
|
||
QSqlQuery query = executeSQL(strSQL,false,params);
|
||
while (query.next())
|
||
{
|
||
return true;
|
||
}
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
return false;
|
||
}
|
||
return false;
|
||
}
|