DiagramDesigner/common/source/dataBase.cpp

1192 lines
33 KiB
C++
Raw Normal View History

2025-02-06 16:36:50 +08:00
#include "dataBase.h"
#include <QDebug>
#include <QFile>
#include <QXmlStreamReader>
#include <QMessageBox>
#include <QSqlQuery>
#include <QJsonObject>
#include <QSqlError>
#include <QSqlDriver>
#include <QJsonDocument>
DataBase* DataBase::instance = nullptr;
int DataBase::_id = 0;
DataBase::DataBase()
{
m_sFileName = QString("setting.xml");
initial();
2025-03-04 09:44:03 +08:00
//createProjectDB();
//initialProjectDB();
createProjectManager();
2025-02-06 16:36:50 +08:00
}
DataBase::~DataBase()
{
QString connectionName = QSqlDatabase::database().connectionName();
QSqlDatabase::removeDatabase(connectionName);
}
void DataBase::initial()
{
readXML();
2025-03-04 09:44:03 +08:00
if (QSqlDatabase::contains(_DataBaseName))
db = QSqlDatabase::database(_DataBaseName);
2025-02-06 16:36:50 +08:00
else
2025-03-04 09:44:03 +08:00
db = QSqlDatabase::addDatabase(_DataBaseType,_DataBaseName);
2025-02-06 16:36:50 +08:00
db.setDatabaseName(_DataBaseName);
db.setHostName(_HostName);
db.setPort(_Port);
// 需要改成自己的用户名和密码
db.setUserName(_UserName);
db.setPassword(_PassWord);
if (db.open()) {
2025-03-04 09:44:03 +08:00
qDebug()<<"baseDB success";
2025-02-06 16:36:50 +08:00
} else {
2025-03-04 09:44:03 +08:00
qDebug()<<"baseDB failed";
2025-02-06 16:36:50 +08:00
}
}
2025-03-04 09:44:03 +08:00
/*bool DataBase::createProjectDB()
{
QSqlQuery query(db);
QString sql = QString("CREATE DATABASE %1").arg(_ProjectDB);
if (!query.exec(sql)) {
qDebug() << "创建数据库失败:" << query.lastError().text();
return false;
}
return true;
}
void DataBase::initialProjectDB()
{
if (QSqlDatabase::contains(_ProjectDB))
prodb = QSqlDatabase::database(_ProjectDB);
else
prodb = QSqlDatabase::addDatabase(_DataBaseType,_ProjectDB);
prodb.setDatabaseName(_ProjectDB);
prodb.setHostName(_HostName);
prodb.setPort(_Port);
// 需要改成自己的用户名和密码
prodb.setUserName(_UserName);
prodb.setPassword(_PassWord);
if (prodb.open()) {
qDebug()<<"ProjectDB success";
} else {
//qDebug()<<"ProjectDB failed";
qDebug()<<prodb.lastError();
}
}*/
2025-02-06 16:36:50 +08:00
void DataBase::updateId()
{
if(db.open())
{
QSqlQuery qry(db);
bool success = qry.exec("SELECT id FROM component");
if (!success) {
qDebug()<<qry.lastError().text();
}
else
{
while (qry.next())
{
int id = qry.value(0).toInt();
structID.componentId = structID.componentId > id ? structID.componentId:id;
}
}
qry.finish();
success = qry.exec("SELECT id FROM station");
if (!success) {
qDebug()<<qry.lastError().text();
}
else
{
while (qry.next())
{
int id = qry.value(0).toInt();
structID.stationId = structID.stationId > id ? structID.stationId:id;
}
}
qry.finish();
success = qry.exec("SELECT id FROM page");
if (!success) {
qDebug()<<qry.lastError().text();
}
else
{
while (qry.next())
{
int id = qry.value(0).toInt();
structID.pageId = structID.pageId > id ? structID.pageId:id;
}
}
qry.finish();
success = qry.exec("SELECT id FROM grid");
if (!success) {
qDebug()<<qry.lastError().text();
}
else
{
while (qry.next())
{
int id = qry.value(0).toInt();
structID.gridId = structID.gridId > id ? structID.gridId:id;
}
}
qry.finish();
success = qry.exec("SELECT id FROM zone");
if (!success) {
qDebug()<<qry.lastError().text();
}
else
{
while (qry.next())
{
int id = qry.value(0).toInt();
structID.zoneId = structID.zoneId > id ? structID.zoneId:id;
}
}
qry.finish();
success = qry.exec("SELECT id FROM topologic");
if (!success) {
qDebug()<<qry.lastError().text();
}
else
{
while (qry.next())
{
int id = qry.value(0).toInt();
structID.topoId = structID.topoId > id ? structID.topoId:id;
}
}
qry.clear();
}
}
bool DataBase::insertComponent(QUuid uuid,QString nspath,QString tag,QString name,QString description,QString grid,QString zone,QString station,int type,bool inService,int state,QJsonObject connected_bus,QJsonObject label,QJsonObject context,int page_id,int op)
{
if(db.open())
{
QSqlQuery qry(db);
/*QJsonDocument busDoc(connected_bus);
QString strBus = busDoc.toJson(QJsonDocument::Compact);
QJsonDocument labelDoc(label);
QString strLabel = labelDoc.toJson(QJsonDocument::Compact);*/
QJsonDocument contextDoc(context);
QString strCon = contextDoc.toJson(QJsonDocument::Compact);
/*qry.prepare("INSERT INTO component(global_uuid, nspath, tag, name, description, grid, zone, station, type, in_service, state, connected_bus, label, context, page_id, op, ts) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
qry.bindValue(0,uuid);
qry.bindValue(1,nspath);
qry.bindValue(2,tag);
qry.bindValue(3,name);
qry.bindValue(4,description);
qry.bindValue(5,grid);
qry.bindValue(6,zone);
qry.bindValue(7,station);
qry.bindValue(8,type);
qry.bindValue(9,inService);
qry.bindValue(10,state);
qry.bindValue(11,strBus);
qry.bindValue(12,strLabel);
qry.bindValue(13,strCon);
qry.bindValue(14,page_id);
qry.bindValue(15,op);
qry.bindValue(16,QDateTime::currentDateTime());*/
qry.prepare("INSERT INTO component(global_uuid, tag, name, grid, zone, station, type, context, page_id, op, ts) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
qry.bindValue(0,uuid);
qry.bindValue(1,tag);
qry.bindValue(2,name);
qry.bindValue(3,grid);
qry.bindValue(4,zone);
qry.bindValue(5,station);
qry.bindValue(6,type);
qry.bindValue(7,strCon);
qry.bindValue(8,page_id);
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::insertPage(QString tag,QString name,int status,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, status, label, context, description, op, ts) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
qry.bindValue(0,tag);
qry.bindValue(1,name);
qry.bindValue(2,status);
qry.bindValue(3,strLabel);
qry.bindValue(4,strCon);
qry.bindValue(5,description);
qry.bindValue(6,op);
qry.bindValue(7,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(int page_id,QUuid uuid_from,QUuid uuid_to,int flag,QString description,int op)
{
if(db.open())
{
QSqlQuery qry(db);
qry.prepare("INSERT INTO topologic(page_id, uuid_from, uuid_to, flag, description, op, ts) VALUES (?, ?, ?, ?, ?, ?, ?);");
qry.bindValue(0,page_id);
qry.bindValue(1,uuid_from);
qry.bindValue(2,uuid_to);
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;
}
bool DataBase::insertBus_stability(int componentId,double resistance,bool anchor_v,double uv_alarm,double ov_alarm,bool anchor_i,double ui_alarm,double oi_alarm,int op)
{
if(db.open())
{
QSqlQuery qry(db);
qry.prepare("INSERT INTO bus_stability(component_id, resistance, anchor_v, uv_alarm, ov_alarm, anchor_i, ui_alarm, oi_alarm, op, ts) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
qry.bindValue(0,componentId);
qry.bindValue(1,resistance);
qry.bindValue(2,anchor_v);
qry.bindValue(3,uv_alarm);
qry.bindValue(4,ov_alarm);
qry.bindValue(5,anchor_i);
qry.bindValue(6,ui_alarm);
qry.bindValue(7,oi_alarm);
qry.bindValue(8,op);
qry.bindValue(9,QDateTime::currentDateTime());
bool res = qry.exec();
QString str = qry.lastQuery();
if(!res)
{
qDebug()<<str<<"\n"<<qry.lastError().text();
}
}
return false;
}
bool DataBase::updateBus_stability(int componentId,double resistance,bool anchor_v,double uv_alarm,double ov_alarm,bool anchor_i,double ui_alarm,double oi_alarm,int op)
{
if(db.open())
{
QSqlQuery qry(db);
qry.prepare("UPDATE bus_stability SET resistance=?, anchor_v=?, uv_alarm=?, ov_alarm=?, anchor_i=?, ui_alarm=?, oi_alarm=?, op=?, ts=? WHERE component_id=?");
qry.bindValue(0,resistance);
qry.bindValue(1,anchor_v);
qry.bindValue(2,uv_alarm);
qry.bindValue(3,ov_alarm);
qry.bindValue(4,anchor_i);
qry.bindValue(5,ui_alarm);
qry.bindValue(6,oi_alarm);
qry.bindValue(7,op);
qry.bindValue(8,QDateTime::currentDateTime());
qry.bindValue(9,componentId);
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;
}
busStability DataBase::getBusStabilityById(int componentId)
{
busStability inf;
if(db.open())
{
QSqlQuery qry(db);
//qry.prepare("SELECT * FROM component WHERE global_uuid = ?");
qry.prepare("SELECT id, component_id, resistance, anchor_v, uv_alarm, ov_alarm, anchor_i, ui_alarm, oi_alarm, op FROM bus_stability WHERE component_id = ?");
qry.bindValue(0,componentId);
bool res = qry.exec();
QString str = qry.lastQuery();
if(!res)
{
qDebug()<<str<<"\n"<<qry.lastError().text();
qry.clear();
}
else
{
while (qry.next())
{
int id = qry.value(0).toInt();
inf.componentId = qry.value(1).toInt();
inf.resistance = qry.value(2).toDouble();
inf.anchor_v = qry.value(3).toBool();
inf.uv_alarm = qry.value(4).toDouble();
inf.ov_alarm = qry.value(5).toDouble();
inf.anchor_i = qry.value(6).toBool();
inf.ui_alarm = qry.value(7).toDouble();
inf.oi_alarm = qry.value(8).toDouble();
qry.clear();
}
}
}
return inf;
}
/************************************************************/
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, tag, name, grid, zone, station, type, context, page_id, 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.id = qry.value(0).toInt();
inf.uuid = QUuid(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.page_id = qry.value(9).toInt();
inf.op = qry.value(10).toInt();
qry.clear();
}
}
}
return inf;
}
QList<componentInfo> DataBase::getAllComponents()
{
QList<componentInfo> lst;
if(db.open())
{
QSqlQuery qry(db);
qry.prepare("SELECT id, global_uuid, tag, name, grid, zone, station, type, context, page_id, 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.id = qry.value(0).toInt();
inf.uuid = QUuid(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.page_id = qry.value(9).toInt();
inf.op = qry.value(10).toInt();
lst.push_back(inf);
}
qry.clear();
}
}
return lst;
}
bool DataBase::componentExist(QString uuid)
{
if(db.open())
{
QSqlQuery qry(db);
qry.prepare("SELECT id 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;
}
/************************************************************/
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();
}
QStringList DataBase::getAllPage()
{
if(db.open())
{
QSqlQuery qry(db);
qry.prepare("SELECT tag FROM page");
bool res = qry.exec();
if(!res)
{
qDebug()<<qry.lastError().text();
qry.clear();
return QStringList();
}
else
{
QStringList lst;
while (qry.next())
{
QString name = qry.value(0).toString();
lst.append(name);
}
qry.clear();
return lst;
}
}
else
return QStringList();
}
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;
}
void DataBase::select()
{
}
void DataBase::parallelUpdate()
{
}
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();
2025-03-04 09:44:03 +08:00
//QString sProDB = attributes.value("ProjectDB").toString();
2025-02-06 16:36:50 +08:00
if (tpe == QString("PostgreSQL"))
{
_DataBaseType = QString("QPSQL");
_DataBaseName = sName;
2025-03-04 09:44:03 +08:00
//_ProjectDB = sProDB;
2025-02-06 16:36:50 +08:00
}
}
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;
}
DataBase* DataBase::GetInstance()
{
if(instance == nullptr)
{
instance = new DataBase();
}
return instance;
}
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;
}
2025-03-04 09:44:03 +08:00
//=================================元模=============================================//
bool DataBase::getAttributeGroup()
{
if(db.open())
{
QSqlQuery qry(db);
bool success = qry.exec("SELECT * FROM attribute_group");
if (!success) {
qDebug()<<qry.lastError().text();
}
else
{
while (qry.next())
{
int id = qry.value(0).toInt();
QString group = qry.value(1).toString();
QString groupName = qry.value(2).toString();
if(!_attributeGroup.contains(id))
{
attributeGroup ag;
ag.id = id;
ag.group = group;
ag.groupName = groupName;
_attributeGroup.insert(id,ag);
}
}
}
qry.finish();
return success;
}
return false;
}
bool DataBase::getDataType()
{
if(db.open())
{
QSqlQuery qry(db);
bool success = qry.exec("SELECT * FROM data_type");
if (!success) {
qDebug()<<qry.lastError().text();
}
else
{
while (qry.next())
{
int id = qry.value(0).toInt();
QString dt = qry.value(1).toString();
QString dbt = qry.value(2).toString();
if(!_dataType.contains(id))
{
dataType type;
type.id = id;
type.dataType = dt;
type.databaseType = dbt;
_dataType.insert(id,type);
}
}
}
qry.finish();
return success;
}
return false;
}
bool DataBase::getAttribute()
{
if(db.open())
{
QSqlQuery qry(db);
bool success = qry.exec("SELECT * FROM attribute");
if (!success) {
qDebug()<<qry.lastError().text();
}
else
{
while (qry.next())
{
int id = qry.value(0).toInt();
QString att = qry.value(1).toString(); //属性名
QString dt = qry.value(2).toString(); //类型名
int len = qry.value(3).toInt(); //类型长度
int scale = qry.value(4).toInt(); //类型精度
QString dv = qry.value(5).toString(); //默认值
QString vr = qry.value(6).toString(); //范围
int ag = qry.value(7).toInt(); //属性组
int inn = qry.value(8).toInt(); //非空
int ipk = qry.value(9).toInt(); //是否主键
if(!_dataType.contains(id))
{
attribute attribute;
attribute.id = id;
attribute.attribute = att;
attribute.dataType = dt;
attribute.lengthPrecision = len;
attribute.scale = scale;
attribute.defaultValue = dv;
attribute.valueRange = vr;
attribute.attributeGroup = ag;
attribute.isNotNull = inn;
attribute.isPrimaryKey = ipk;
_attribute.insert(id,attribute);
}
}
}
qry.finish();
return success;
}
return false;
}
bool DataBase::getModelAttribute()
{
if(db.open())
{
QSqlQuery qry(db);
bool success = qry.exec("SELECT * FROM model_attribute");
if (!success) {
qDebug()<<qry.lastError().text();
}
else
{
while (qry.next())
{
int id = qry.value(0).toInt();
QString mt = qry.value(1).toString(); //模型名
int ai = qry.value(2).toInt(); //属性id
if(!_modelAttribute.contains(id))
{
modelAttribute model;
model.id = id;
model.modelType = mt;
model.attributeId = ai;
_modelAttribute.insert(id,model);
}
}
}
qry.finish();
return success;
}
return false;
}
bool DataBase::getModelConnectivity()
{
if(db.open())
{
QSqlQuery qry(db);
bool success = qry.exec("SELECT * FROM model_connectivity");
if (!success) {
qDebug()<<qry.lastError().text();
}
else
{
while (qry.next())
{
int id = qry.value(0).toInt();
QString fm = qry.value(1).toString(); //from
QString tm = qry.value(2).toString(); //to
int con = qry.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);
}
}
}
qry.finish();
return success;
}
return false;
}
//=================================工程模===========================================//
bool DataBase::createProjectManager()
{
if(db.open())
{
QSqlQuery qry(db);
//qry.exec("CREATE SEQUENCE IF NOT EXISTS project_manager_id_seq;");
//qry.finish();
//id integer NOT NULL DEFAULT nextval('project_manager_id_seq'::regclass) PRIMARY KEY,
QString createTableSQL = 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,
)
)";
if (!qry.exec(createTableSQL)) {
qDebug() << "Error creating table:" << qry.lastError().text();
} else {
qDebug() << "Table 'project_manager' created successfully!";
return true;
}
}
return false;
}
bool DataBase::insertProjectManager(const QString& name,const QString& tag,const QString& metaModel,const QString& groupName,int linkType,QJsonObject checkState)
{
if(db.open())
{
QSqlQuery qry(db);
QJsonDocument checkDoc(checkState);
QString strCheck = checkDoc.toJson(QJsonDocument::Compact);
qry.prepare("INSERT INTO project_manager(name, tag, meta_model, group_name, linkType, check_state) VALUES (?, ?, ?, ?, ?, ?)");
qry.bindValue(0,name);
qry.bindValue(1,tag);
qry.bindValue(2,metaModel);
qry.bindValue(3,groupName);
qry.bindValue(4,linkType);
qry.bindValue(5,strCheck);
bool res = qry.exec();
QString str = qry.lastQuery();
if(!res)
{
qDebug()<<str<<"\n"<<qry.lastError().text();
}
qry.clear();
return res;
}
return false;
}
bool DataBase::getProjectManager()
{
}
QMap<QString,QJsonObject> DataBase::getCheckStateFromManager(const QString& sProject)
{
QMap<QString,QJsonObject> map;
if(sProject == QString::fromWCharArray(L"新建"))
{
return map;
}
if(db.open())
{
QSqlQuery qry(db);
qry.prepare("SELECT group_name, check_state FROM project_manager WHERE tag = ?");
qry.bindValue(0,sProject);
bool res = qry.exec();
QString str = qry.lastQuery();
if(!res)
{
qDebug()<<str<<"\n"<<qry.lastError().text();
qry.clear();
return map;
}
else
{
while (qry.next())
{
QString group = qry.value(0).toString();
QString state = qry.value(1).toString();
QJsonObject jsonObj = QstringToJson(state);
qry.clear();
map.insert(group,jsonObj);
}
return map;
}
}
else
return map;
}
bool DataBase::createDynamicTable(const QString &tableName, const QStringList &fields)
{
if(db.open())
{
QString createTableSQL = "CREATE TABLE IF NOT EXISTS " + tableName + " (";
for (const QString &field : fields) {
createTableSQL += field + ", ";
}
// Remove the last comma and space
createTableSQL.chop(2);
createTableSQL += ")";
QSqlQuery query;
bool res = query.exec();
if (!res) {
qDebug() << "Error creating dynamic table:" << query.lastError().text();
} else {
qDebug() << "Dynamic table created successfully!";
}
return res;
}
return false;
}