修改通过右键打开数据库时无法取到链接名称的bug
This commit is contained in:
parent
d0bd059ffe
commit
933f051bff
|
|
@ -31,6 +31,7 @@ private:
|
||||||
QString m_curConnection; //用来记录当前链接,只能存在一个链接
|
QString m_curConnection; //用来记录当前链接,只能存在一个链接
|
||||||
|
|
||||||
void initView();
|
void initView();
|
||||||
|
void openConnection(const QString&);
|
||||||
void connectToDB(const QString&);
|
void connectToDB(const QString&);
|
||||||
void disconnectToDB(const QString&);
|
void disconnectToDB(const QString&);
|
||||||
void removeNode(DBStructureNode*);
|
void removeNode(DBStructureNode*);
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,8 @@ bool DatabaseManager::addDatabase(const DatabaseConfig& config)
|
||||||
for(auto i = m_configs.constBegin(); i != m_configs.constEnd(); i++)
|
for(auto i = m_configs.constBegin(); i != m_configs.constEnd(); i++)
|
||||||
{
|
{
|
||||||
DatabaseConfig c = i.value();
|
DatabaseConfig c = i.value();
|
||||||
if(config.strHost == c.strHost && config.nPort == c.nPort)
|
if(config.strHost == c.strHost && config.nPort == c.nPort && config.strDBType == c.strDBType
|
||||||
|
&& config.strDBName == c.strDBName)
|
||||||
{
|
{
|
||||||
QSqlError error(QString::fromWCharArray(L"配置信息错误"),
|
QSqlError error(QString::fromWCharArray(L"配置信息错误"),
|
||||||
QString::fromWCharArray(L"'%1' 具有相同配置信息").arg(c.strConnectionName),
|
QString::fromWCharArray(L"'%1' 具有相同配置信息").arg(c.strConnectionName),
|
||||||
|
|
@ -77,10 +78,11 @@ bool DatabaseManager::connect(const QString& strConnectionName)
|
||||||
|
|
||||||
if(!db.open())
|
if(!db.open())
|
||||||
{
|
{
|
||||||
|
QString errorText = db.lastError().databaseText();
|
||||||
LOG_ERROR("DB", QString("DB: %1 open failed. connectionName: %2. error: %3")
|
LOG_ERROR("DB", QString("DB: %1 open failed. connectionName: %2. error: %3")
|
||||||
.arg(config.strDBName)
|
.arg(config.strDBName)
|
||||||
.arg(strConnectionName)
|
.arg(strConnectionName)
|
||||||
.arg(db.lastError().databaseText()));
|
.arg(QString::fromLocal8Bit(errorText.toLatin1())));
|
||||||
emit errorOccurred(strConnectionName, QString::fromWCharArray(L"数据库打开失败,详情可见日志文件"));
|
emit errorOccurred(strConnectionName, QString::fromWCharArray(L"数据库打开失败,详情可见日志文件"));
|
||||||
QSqlDatabase::removeDatabase(strConnectionName);
|
QSqlDatabase::removeDatabase(strConnectionName);
|
||||||
return false;
|
return false;
|
||||||
|
|
|
||||||
|
|
@ -52,6 +52,12 @@ void DBStructureView::initView()
|
||||||
connect(this, &QTreeView::customContextMenuRequested, this ,&DBStructureView::showContextMenu);
|
connect(this, &QTreeView::customContextMenuRequested, this ,&DBStructureView::showContextMenu);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void DBStructureView::openConnection(const QString& connection)
|
||||||
|
{
|
||||||
|
connectToDB(connection);
|
||||||
|
m_curConnection = connection;
|
||||||
|
}
|
||||||
|
|
||||||
void DBStructureView::connectToDB(const QString& connName)
|
void DBStructureView::connectToDB(const QString& connName)
|
||||||
{
|
{
|
||||||
DBStructureModel* model = dynamic_cast<DBStructureModel*>(this->model());
|
DBStructureModel* model = dynamic_cast<DBStructureModel*>(this->model());
|
||||||
|
|
@ -154,17 +160,10 @@ void DBStructureView::itemDoubleClick(const QModelIndex& index)
|
||||||
DBStructureNode* node = static_cast<DBStructureNode*>(index.internalPointer());
|
DBStructureNode* node = static_cast<DBStructureNode*>(index.internalPointer());
|
||||||
if(node->type() == ConnectionNode)
|
if(node->type() == ConnectionNode)
|
||||||
{
|
{
|
||||||
if(!m_curConnection.isEmpty()) //先断掉当前链接
|
//先断掉当前链接
|
||||||
{
|
disconnectCurConnection();
|
||||||
disconnectToDB(m_curConnection);
|
//打开新链接
|
||||||
m_curConnection = "";
|
openConnection(node->name());
|
||||||
}
|
|
||||||
|
|
||||||
if(node->status() == Disconnect)
|
|
||||||
{
|
|
||||||
connectToDB(node->name());
|
|
||||||
m_curConnection = node->name();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else if(node->type() == GroupNode)
|
else if(node->type() == GroupNode)
|
||||||
{
|
{
|
||||||
|
|
@ -188,14 +187,17 @@ void DBStructureView::showContextMenu(const QPoint& pos)
|
||||||
QString connName = node->name();
|
QString connName = node->name();
|
||||||
CustomMenu menu;
|
CustomMenu menu;
|
||||||
menu.addAction(QString::fromWCharArray(L"链接"), [this, &connName]{
|
menu.addAction(QString::fromWCharArray(L"链接"), [this, &connName]{
|
||||||
connectToDB(connName);
|
//先断掉当前链接
|
||||||
|
disconnectCurConnection();
|
||||||
|
//打开新链接
|
||||||
|
openConnection(connName);
|
||||||
//展开链接
|
//展开链接
|
||||||
DBStructureModel* model = dynamic_cast<DBStructureModel*>(this->model());
|
DBStructureModel* model = dynamic_cast<DBStructureModel*>(this->model());
|
||||||
QModelIndex index = model->getConnNodeIndex(connName);
|
QModelIndex index = model->getConnNodeIndex(connName);
|
||||||
if(model && index.isValid())
|
if(model && index.isValid())
|
||||||
expand(index);
|
expand(index);
|
||||||
})->setEnabled(!isConnected);
|
})->setEnabled(!isConnected);
|
||||||
menu.addAction(QString::fromWCharArray(L"断开链接"), [this, &connName]{disconnectToDB(connName);})->setEnabled(isConnected);
|
menu.addAction(QString::fromWCharArray(L"断开链接"), [this, &connName]{disconnectCurConnection();})->setEnabled(isConnected);
|
||||||
menu.addAction(QString::fromWCharArray(L"刷新"), [this, &connName]{
|
menu.addAction(QString::fromWCharArray(L"刷新"), [this, &connName]{
|
||||||
DBStructureModel* model = dynamic_cast<DBStructureModel*>(this->model());
|
DBStructureModel* model = dynamic_cast<DBStructureModel*>(this->model());
|
||||||
if(model)
|
if(model)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue