add db reconnect
This commit is contained in:
parent
ba2fb8211f
commit
3ee42e2eed
|
|
@ -29,7 +29,7 @@ public:
|
||||||
QSqlQuery executeBatchSQL(const QStringList& sqlStatements, bool createOrDrop = false,
|
QSqlQuery executeBatchSQL(const QStringList& sqlStatements, bool createOrDrop = false,
|
||||||
const QList<QVariantList>& paramsList = QList<QVariantList>(), bool useTranscation = false);
|
const QList<QVariantList>& paramsList = QList<QVariantList>(), bool useTranscation = false);
|
||||||
static DataBase* GetInstance();
|
static DataBase* GetInstance();
|
||||||
|
QSqlDatabase getDB(); // ✅ 线程安全入口
|
||||||
public:
|
public:
|
||||||
bool insertPage(QString tag,QString name,QJsonObject label,QJsonObject context,QString description,int op);
|
bool insertPage(QString tag,QString name,QJsonObject label,QJsonObject context,QString description,int op);
|
||||||
bool insertStation(int zoneId,QString name,QString description,bool isLocal,int op);
|
bool insertStation(int zoneId,QString name,QString description,bool isLocal,int op);
|
||||||
|
|
@ -183,17 +183,13 @@ private:
|
||||||
QMap<int,ComponentTypeInfo> _componentType; //存储系统支持的类型列表
|
QMap<int,ComponentTypeInfo> _componentType; //存储系统支持的类型列表
|
||||||
private:
|
private:
|
||||||
void initial();
|
void initial();
|
||||||
//bool createProjectDB();
|
bool isDbValid(QSqlDatabase& db);
|
||||||
//void initialProjectDB();
|
|
||||||
void readXML();
|
void readXML();
|
||||||
static DataBase* dbInstance;
|
static DataBase* dbInstance;
|
||||||
static int _id;
|
static int _id;
|
||||||
QSqlDatabase db;
|
|
||||||
//QSqlDatabase prodb;
|
|
||||||
QString m_sFileName;
|
QString m_sFileName;
|
||||||
QString _DataBaseType;
|
QString _DataBaseType;
|
||||||
QString _DataBaseName;
|
QString _DataBaseName;
|
||||||
//QString _ProjectDB; //工程模数据库名
|
|
||||||
QString _HostName;
|
QString _HostName;
|
||||||
int _Port;
|
int _Port;
|
||||||
QString _UserName;
|
QString _UserName;
|
||||||
|
|
|
||||||
|
|
@ -10,17 +10,26 @@
|
||||||
#include <QSqlDriver>
|
#include <QSqlDriver>
|
||||||
#include <QJsonDocument>
|
#include <QJsonDocument>
|
||||||
#include <QJsonArray>
|
#include <QJsonArray>
|
||||||
|
#include <QMutex>
|
||||||
|
#include <QThread>
|
||||||
|
|
||||||
DataBase* DataBase::dbInstance = nullptr;
|
DataBase* DataBase::dbInstance = nullptr;
|
||||||
int DataBase::_id = 0;
|
int DataBase::_id = 0;
|
||||||
|
|
||||||
DataBase::DataBase()
|
DataBase::DataBase()
|
||||||
{
|
{
|
||||||
m_sFileName = QString("setting.xml");
|
m_sFileName = "setting.xml";
|
||||||
|
readXML(); // 读数据库配置
|
||||||
|
|
||||||
|
QSqlDatabase db = getDB();
|
||||||
|
|
||||||
|
if (!db.isOpen()) {
|
||||||
|
LOG_ERROR("DB", "Database not available");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ✅ 全局只执行一次的业务初始化
|
||||||
initial();
|
initial();
|
||||||
//createProjectDB();
|
|
||||||
//initialProjectDB();
|
|
||||||
createProjectManager();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
DataBase::~DataBase()
|
DataBase::~DataBase()
|
||||||
|
|
@ -31,26 +40,6 @@ DataBase::~DataBase()
|
||||||
|
|
||||||
void DataBase::initial()
|
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(); //获取属性组信息
|
getAttributeGroup(); //获取属性组信息
|
||||||
getDataType(); //获取数据类型信息
|
getDataType(); //获取数据类型信息
|
||||||
getModelType(); //获取模型类型
|
getModelType(); //获取模型类型
|
||||||
|
|
@ -59,20 +48,71 @@ void DataBase::initial()
|
||||||
getModelAttribute();
|
getModelAttribute();
|
||||||
getModelAttributePublic(); //获取公共属性组
|
getModelAttributePublic(); //获取公共属性组
|
||||||
getModelConnectivity(); //获取连接性
|
getModelConnectivity(); //获取连接性
|
||||||
|
|
||||||
|
createProjectManager();
|
||||||
|
}
|
||||||
|
|
||||||
|
QSqlDatabase DataBase::getDB()
|
||||||
|
{
|
||||||
|
QString connName =
|
||||||
|
QString("db_%1").arg((quintptr)QThread::currentThreadId());
|
||||||
|
|
||||||
|
QSqlDatabase db;
|
||||||
|
|
||||||
|
if (QSqlDatabase::contains(connName)) {
|
||||||
|
db = QSqlDatabase::database(connName);
|
||||||
|
} else {
|
||||||
|
db = QSqlDatabase::addDatabase(_DataBaseType, connName);
|
||||||
|
db.setDatabaseName(_DataBaseName);
|
||||||
|
db.setHostName(_HostName);
|
||||||
|
db.setPort(_Port);
|
||||||
|
db.setUserName(_UserName);
|
||||||
|
db.setPassword(_PassWord);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ✅ 只重连,不删
|
||||||
|
if (!isDbValid(db)) {
|
||||||
|
if (db.isOpen())
|
||||||
|
db.close();
|
||||||
|
|
||||||
|
if (!db.open()) {
|
||||||
|
LOG_ERROR("DB", db.lastError().text());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return db;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool DataBase::isDbValid(QSqlDatabase& db)
|
||||||
|
{
|
||||||
|
if (!db.isOpen())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
QSqlQuery query(db);
|
||||||
|
return query.exec("SELECT 1");
|
||||||
}
|
}
|
||||||
|
|
||||||
DataBase* DataBase::GetInstance()
|
DataBase* DataBase::GetInstance()
|
||||||
{
|
{
|
||||||
if(dbInstance == nullptr)
|
static QMutex mutex;
|
||||||
{
|
static DataBase* instance = nullptr;
|
||||||
dbInstance = new DataBase();
|
|
||||||
|
if (!instance) {
|
||||||
|
QMutexLocker locker(&mutex);
|
||||||
|
if (!instance) {
|
||||||
|
instance = new DataBase();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return dbInstance;
|
// ✅ 在这里触发一次数据库连接检查
|
||||||
|
instance->getDB();
|
||||||
|
|
||||||
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
QSqlQuery DataBase::executeSQL(const QString& strSQL,bool isDDL,const QVariantList& params, bool useTranscation)
|
QSqlQuery DataBase::executeSQL(const QString& strSQL,bool isDDL,const QVariantList& params, bool useTranscation)
|
||||||
{
|
{
|
||||||
//事务
|
//事务
|
||||||
|
QSqlDatabase db = getDB();
|
||||||
bool transactionStarted = false;
|
bool transactionStarted = false;
|
||||||
if(useTranscation)
|
if(useTranscation)
|
||||||
{
|
{
|
||||||
|
|
@ -144,6 +184,7 @@ QSqlQuery DataBase::executeSQL(const QString& strSQL,bool isDDL,const QVariantLi
|
||||||
QSqlQuery DataBase::executeBatchSQL(const QStringList& sqlStatements, bool createOrDrop,const QList<QVariantList>& paramsList, bool useTranscation)
|
QSqlQuery DataBase::executeBatchSQL(const QStringList& sqlStatements, bool createOrDrop,const QList<QVariantList>& paramsList, bool useTranscation)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
QSqlDatabase db = getDB();
|
||||||
//参数数量校验
|
//参数数量校验
|
||||||
if(!paramsList.isEmpty() && sqlStatements.size() != paramsList.size())
|
if(!paramsList.isEmpty() && sqlStatements.size() != paramsList.size())
|
||||||
{
|
{
|
||||||
|
|
@ -229,6 +270,7 @@ QSqlQuery DataBase::executeBatchSQL(const QStringList& sqlStatements, bool creat
|
||||||
|
|
||||||
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)
|
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)
|
||||||
{
|
{
|
||||||
|
QSqlDatabase db = getDB();
|
||||||
if(db.open())
|
if(db.open())
|
||||||
{
|
{
|
||||||
QSqlQuery qry(db);
|
QSqlQuery qry(db);
|
||||||
|
|
@ -270,6 +312,7 @@ bool DataBase::insertComponent(QUuid uuid,QString modelName,QString nspath,QStri
|
||||||
|
|
||||||
bool DataBase::updateComponent(QUuid uuid,QString tag,QString name,QJsonObject context,bool inService,int state,int status)
|
bool DataBase::updateComponent(QUuid uuid,QString tag,QString name,QJsonObject context,bool inService,int state,int status)
|
||||||
{
|
{
|
||||||
|
QSqlDatabase db = getDB();
|
||||||
if(db.open())
|
if(db.open())
|
||||||
{
|
{
|
||||||
QSqlQuery qry(db);
|
QSqlQuery qry(db);
|
||||||
|
|
@ -373,6 +416,7 @@ bool DataBase::updateDynamicProperty(QUuid uuid,GroupStateValue groupValue)
|
||||||
|
|
||||||
bool DataBase::insertPage(QString tag,QString name,QJsonObject label,QJsonObject context,QString description,int op)
|
bool DataBase::insertPage(QString tag,QString name,QJsonObject label,QJsonObject context,QString description,int op)
|
||||||
{
|
{
|
||||||
|
QSqlDatabase db = getDB();
|
||||||
if(db.open())
|
if(db.open())
|
||||||
{
|
{
|
||||||
QSqlQuery qry(db);
|
QSqlQuery qry(db);
|
||||||
|
|
@ -406,6 +450,7 @@ bool DataBase::insertPage(QString tag,QString name,QJsonObject label,QJsonObject
|
||||||
|
|
||||||
bool DataBase::insertStation(int zoneId,QString name,QString description,bool isLocal,int op)
|
bool DataBase::insertStation(int zoneId,QString name,QString description,bool isLocal,int op)
|
||||||
{
|
{
|
||||||
|
QSqlDatabase db = getDB();
|
||||||
if(db.open())
|
if(db.open())
|
||||||
{
|
{
|
||||||
QSqlQuery qry(db);
|
QSqlQuery qry(db);
|
||||||
|
|
@ -433,6 +478,7 @@ bool DataBase::insertStation(int zoneId,QString name,QString description,bool is
|
||||||
|
|
||||||
bool DataBase::insertGrid(QString name,QString description,int op)
|
bool DataBase::insertGrid(QString name,QString description,int op)
|
||||||
{
|
{
|
||||||
|
QSqlDatabase db = getDB();
|
||||||
if(db.open())
|
if(db.open())
|
||||||
{
|
{
|
||||||
QSqlQuery qry(db);
|
QSqlQuery qry(db);
|
||||||
|
|
@ -456,6 +502,7 @@ bool DataBase::insertGrid(QString name,QString description,int op)
|
||||||
|
|
||||||
bool DataBase::insertZone(int grid_id,QString name,QString description,int op)
|
bool DataBase::insertZone(int grid_id,QString name,QString description,int op)
|
||||||
{
|
{
|
||||||
|
QSqlDatabase db = getDB();
|
||||||
if(db.open())
|
if(db.open())
|
||||||
{
|
{
|
||||||
QSqlQuery qry(db);
|
QSqlQuery qry(db);
|
||||||
|
|
@ -480,6 +527,7 @@ bool DataBase::insertZone(int grid_id,QString name,QString description,int op)
|
||||||
|
|
||||||
bool DataBase::insertTopologic(QUuid uuid_from,QUuid uuid_to,QJsonObject context,int flag,QString description,int op)
|
bool DataBase::insertTopologic(QUuid uuid_from,QUuid uuid_to,QJsonObject context,int flag,QString description,int op)
|
||||||
{
|
{
|
||||||
|
QSqlDatabase db = getDB();
|
||||||
QJsonDocument contextDoc(context);
|
QJsonDocument contextDoc(context);
|
||||||
QString strContext = contextDoc.toJson(QJsonDocument::Compact);
|
QString strContext = contextDoc.toJson(QJsonDocument::Compact);
|
||||||
|
|
||||||
|
|
@ -761,6 +809,7 @@ bool DataBase::deleteTopologic(QUuid fromPin,QUuid toPin)
|
||||||
/************************************************************/
|
/************************************************************/
|
||||||
ComponentInfo DataBase::getComponentInfoByUuid(QString uuid)
|
ComponentInfo DataBase::getComponentInfoByUuid(QString uuid)
|
||||||
{
|
{
|
||||||
|
QSqlDatabase db = getDB();
|
||||||
ComponentInfo inf;
|
ComponentInfo inf;
|
||||||
if(db.open())
|
if(db.open())
|
||||||
{
|
{
|
||||||
|
|
@ -804,6 +853,7 @@ ComponentInfo DataBase::getComponentInfoByUuid(QString uuid)
|
||||||
|
|
||||||
QList<ComponentInfo> DataBase::getAllComponents()
|
QList<ComponentInfo> DataBase::getAllComponents()
|
||||||
{
|
{
|
||||||
|
QSqlDatabase db = getDB();
|
||||||
QList<ComponentInfo> lst;
|
QList<ComponentInfo> lst;
|
||||||
if(db.open())
|
if(db.open())
|
||||||
{
|
{
|
||||||
|
|
@ -847,6 +897,7 @@ QList<ComponentInfo> DataBase::getAllComponents()
|
||||||
|
|
||||||
bool DataBase::componentExist(QString uuid)
|
bool DataBase::componentExist(QString uuid)
|
||||||
{
|
{
|
||||||
|
QSqlDatabase db = getDB();
|
||||||
if(db.open())
|
if(db.open())
|
||||||
{
|
{
|
||||||
QSqlQuery qry(db);
|
QSqlQuery qry(db);
|
||||||
|
|
@ -874,6 +925,7 @@ bool DataBase::componentExist(QString uuid)
|
||||||
|
|
||||||
bool DataBase::deleteComponent(QString uuid)
|
bool DataBase::deleteComponent(QString uuid)
|
||||||
{
|
{
|
||||||
|
QSqlDatabase db = getDB();
|
||||||
if(db.open())
|
if(db.open())
|
||||||
{
|
{
|
||||||
QSqlQuery qry(db);
|
QSqlQuery qry(db);
|
||||||
|
|
@ -894,6 +946,7 @@ bool DataBase::deleteComponent(QString uuid)
|
||||||
|
|
||||||
QMap<int,ComponentTypeInfo> DataBase::getAllComponentType()
|
QMap<int,ComponentTypeInfo> DataBase::getAllComponentType()
|
||||||
{
|
{
|
||||||
|
QSqlDatabase db = getDB();
|
||||||
if(_componentType.empty())
|
if(_componentType.empty())
|
||||||
{
|
{
|
||||||
QMap<int,ComponentTypeInfo> map;
|
QMap<int,ComponentTypeInfo> map;
|
||||||
|
|
@ -932,6 +985,7 @@ QMap<int,ComponentTypeInfo> DataBase::getAllComponentType()
|
||||||
|
|
||||||
bool DataBase::updatePage(QString tag,QString name,QJsonObject context)
|
bool DataBase::updatePage(QString tag,QString name,QJsonObject context)
|
||||||
{
|
{
|
||||||
|
QSqlDatabase db = getDB();
|
||||||
if(db.open())
|
if(db.open())
|
||||||
{
|
{
|
||||||
QSqlQuery qry(db);
|
QSqlQuery qry(db);
|
||||||
|
|
@ -962,6 +1016,7 @@ bool DataBase::updatePage(QString tag,QString name,QJsonObject context)
|
||||||
|
|
||||||
int DataBase::getPageIdByName(QString name)
|
int DataBase::getPageIdByName(QString name)
|
||||||
{
|
{
|
||||||
|
QSqlDatabase db = getDB();
|
||||||
if(db.open())
|
if(db.open())
|
||||||
{
|
{
|
||||||
QSqlQuery qry(db);
|
QSqlQuery qry(db);
|
||||||
|
|
@ -993,6 +1048,7 @@ int DataBase::getPageIdByName(QString name)
|
||||||
|
|
||||||
QJsonObject DataBase::getPageContextByName(QString name)
|
QJsonObject DataBase::getPageContextByName(QString name)
|
||||||
{
|
{
|
||||||
|
QSqlDatabase db = getDB();
|
||||||
if(db.open())
|
if(db.open())
|
||||||
{
|
{
|
||||||
QSqlQuery qry(db);
|
QSqlQuery qry(db);
|
||||||
|
|
@ -1058,6 +1114,7 @@ QList<PageInfo> DataBase::getAllPage()
|
||||||
|
|
||||||
bool DataBase::deleteComponentById(int id)
|
bool DataBase::deleteComponentById(int id)
|
||||||
{
|
{
|
||||||
|
QSqlDatabase db = getDB();
|
||||||
if(db.open())
|
if(db.open())
|
||||||
{
|
{
|
||||||
QSqlQuery qry(db);
|
QSqlQuery qry(db);
|
||||||
|
|
@ -2561,6 +2618,7 @@ bool DataBase::deleteProjectSetting(const QString& baseModel,const QString& mode
|
||||||
|
|
||||||
bool DataBase::createProjectManager()
|
bool DataBase::createProjectManager()
|
||||||
{
|
{
|
||||||
|
QSqlDatabase db = getDB();
|
||||||
QString strSQL = R"(
|
QString strSQL = R"(
|
||||||
CREATE TABLE IF NOT EXISTS project_manager (
|
CREATE TABLE IF NOT EXISTS project_manager (
|
||||||
id SERIAL NOT NULL PRIMARY KEY,
|
id SERIAL NOT NULL PRIMARY KEY,
|
||||||
|
|
@ -3284,6 +3342,7 @@ bool DataBase::createDynamicTable(const QString &tableName, const QStringList &f
|
||||||
|
|
||||||
bool DataBase::deleteProjectModel(const QString& sProject)
|
bool DataBase::deleteProjectModel(const QString& sProject)
|
||||||
{
|
{
|
||||||
|
QSqlDatabase db = getDB();
|
||||||
QStringList lstTable;
|
QStringList lstTable;
|
||||||
QString strSQL = "SELECT name FROM project_manager WHERE tag = ?";
|
QString strSQL = "SELECT name FROM project_manager WHERE tag = ?";
|
||||||
QVariantList params;
|
QVariantList params;
|
||||||
|
|
@ -3459,6 +3518,7 @@ bool DataBase::deleteRecordFromManager(const QString& sProject,const QString& sG
|
||||||
|
|
||||||
bool DataBase::modifyProjectTable(QString sTable,QMap<QString,QString> mOld,QMap<QString,QString> mNew)
|
bool DataBase::modifyProjectTable(QString sTable,QMap<QString,QString> mOld,QMap<QString,QString> mNew)
|
||||||
{
|
{
|
||||||
|
QSqlDatabase db = getDB();
|
||||||
QStringList sqlStatements;
|
QStringList sqlStatements;
|
||||||
for (auto &col : mOld.keys()) {
|
for (auto &col : mOld.keys()) {
|
||||||
if (!mNew.contains(col)) {
|
if (!mNew.contains(col)) {
|
||||||
|
|
|
||||||
|
|
@ -62,7 +62,11 @@ CMainWindow::~CMainWindow()
|
||||||
//if(m_pElectricElementsBox)
|
//if(m_pElectricElementsBox)
|
||||||
//delete m_pElectricElementsBox;
|
//delete m_pElectricElementsBox;
|
||||||
if(m_pPropertiesEditorView){
|
if(m_pPropertiesEditorView){
|
||||||
|
auto pView = m_pPropertiesEditorView->getQuickDetailsView();
|
||||||
|
if(pView){
|
||||||
|
pView->disconnect();
|
||||||
|
delete pView;
|
||||||
|
}
|
||||||
m_pPropertiesEditorView->deleteLater();
|
m_pPropertiesEditorView->deleteLater();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue