DiagramDesigner/common/source/dataBase.cpp

850 lines
24 KiB
C++

#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();
//insertComponent(QUuid::createUuid(),QString("111"),QString("111"),QString("111"),QString("111"),QString("111"),QString("111"),QString("111"),1,true,1,QJsonObject(),QJsonObject(),QJsonObject(),1,1);
//insertPage(QString("111"),QString("111"),4,QJsonObject(),QJsonObject(),QString("111"),1);
//insertStation(1,QString("111"),QString("111"),true,1);
//insertGrid(QString("111"),QString("111"),1);
//insertZone(1,QString("111"),QString("111"),1);
//insertTopologic(1,QUuid::createUuid(),QUuid::createUuid(),1,QString("111"),1);
//updateId();
//deleteComponentById(1);
}
DataBase::~DataBase()
{
QString connectionName = QSqlDatabase::database().connectionName();
QSqlDatabase::removeDatabase(connectionName);
}
void DataBase::initial()
{
readXML();
if (QSqlDatabase::contains("qt_sql_default_connection"))
db = QSqlDatabase::database("qt_sql_default_connection");
else
db = QSqlDatabase::addDatabase(_DataBaseType);
//db = QSqlDatabase::addDatabase(_DataBaseType);
db.setDatabaseName(_DataBaseName);
db.setHostName(_HostName);
db.setPort(_Port);
// 需要改成自己的用户名和密码
db.setUserName(_UserName);
db.setPassword(_PassWord);
if (db.open()) {
qDebug()<<"success";
} else {
qDebug()<<"failed";
}
}
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();
if (tpe == QString("PostgreSQL"))
{
_DataBaseType = QString("QPSQL");
_DataBaseName = sName;
}
}
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;
}