From 3407f6cc52763008cc995234d5d66de14eadf8f7 Mon Sep 17 00:00:00 2001 From: "Juergen E. Fischer" Date: Fri, 30 May 2025 11:28:12 +0200 Subject: [PATCH] add support for connections via service --- libs/libconnector/src/connection.cpp | 27 +- libs/libconnector/src/connection.h | 1 + .../src/settings/connectionsconfigwidget.cpp | 24 +- .../src/settings/connectionsconfigwidget.h | 8 +- .../ui/settings/connectionsconfigwidget.ui | 572 ++++++++++-------- libs/libutils/src/exception.cpp | 2 +- 6 files changed, 345 insertions(+), 289 deletions(-) diff --git a/libs/libconnector/src/connection.cpp b/libs/libconnector/src/connection.cpp index 0256b9298e..60b959116b 100644 --- a/libs/libconnector/src/connection.cpp +++ b/libs/libconnector/src/connection.cpp @@ -31,6 +31,7 @@ bool Connection::ignore_db_version {false}; const QString Connection::ParamAlias {"alias"}; const QString Connection::ParamApplicationName {"application_name"}; +const QString Connection::ParamService {"service"}; const QString Connection::ParamServerFqdn {"host"}; const QString Connection::ParamServerIp {"hostaddr"}; const QString Connection::ParamPort {"port"}; @@ -158,11 +159,6 @@ void Connection::generateConnectionString() connection_str += value; } } - - if(!connection_str.contains(ParamDbName) || - (!connection_str.contains(ParamServerFqdn) && - !connection_str.contains(ParamServerIp))) - connection_str.clear(); } void Connection::noticeProcessor(void *, const char *message) @@ -186,8 +182,10 @@ void Connection::validateConnectionStatus() if(PQstatus(connection)==CONNECTION_BAD) throw Exception(Exception::getErrorMessage(ErrorCode::ConnectionBroken) - .arg(connection_params[ParamServerFqdn].isEmpty() ? connection_params[ParamServerIp] : connection_params[ParamServerFqdn]) - .arg(connection_params[ParamPort]), + .arg(connection_params[ParamService].isEmpty() + ? QString(QT_TR_NOOP("`%1` at port `%2`")).arg(connection_params[ParamServerFqdn].isEmpty() ? connection_params[ParamServerIp] : connection_params[ParamServerFqdn], connection_params[ParamPort]) + : QString(QT_TR_NOOP("server %1")).arg(connection_params[ParamService]) + ), ErrorCode::ConnectionBroken, __PRETTY_FUNCTION__, __FILE__, __LINE__); } @@ -349,13 +347,18 @@ QString Connection::getConnectionId(bool host_port_only, bool incl_db_name, bool if(!isConfigured()) return ""; - if(!connection_params[ParamServerFqdn].isEmpty()) - addr=connection_params[ParamServerFqdn]; + if(!connection_params[ParamService].isEmpty()) + addr = connection_params[ParamService]; else - addr=connection_params[ParamServerIp]; + { + if(!connection_params[ParamServerFqdn].isEmpty()) + addr=connection_params[ParamServerFqdn]; + else + addr=connection_params[ParamServerIp]; - if(!connection_params[ParamPort].isEmpty()) - port = QString(":%1").arg(connection_params[ParamPort]); + if(!connection_params[ParamPort].isEmpty()) + port = QString(":%1").arg(connection_params[ParamPort]); + } if(incl_db_name) db_name = QString("%1@").arg(connection_params[ParamDbName]); diff --git a/libs/libconnector/src/connection.h b/libs/libconnector/src/connection.h index 4a037b2a01..0a560cccac 100644 --- a/libs/libconnector/src/connection.h +++ b/libs/libconnector/src/connection.h @@ -101,6 +101,7 @@ class __libconnector Connection { //! \brief Constants used to reference the connections parameters static const QString ParamAlias, ParamApplicationName, + ParamService, ParamServerFqdn, ParamServerIp, ParamPort, diff --git a/libs/libgui/src/settings/connectionsconfigwidget.cpp b/libs/libgui/src/settings/connectionsconfigwidget.cpp index 9f81ca680c..fadc2645e0 100644 --- a/libs/libgui/src/settings/connectionsconfigwidget.cpp +++ b/libs/libgui/src/settings/connectionsconfigwidget.cpp @@ -47,6 +47,7 @@ ConnectionsConfigWidget::ConnectionsConfigWidget(QWidget * parent) : BaseConfigW connect(add_tb, &QPushButton::clicked, this, __slot(this, ConnectionsConfigWidget::handleConnection)); connect(alias_edt, &QLineEdit::textChanged, this, &ConnectionsConfigWidget::enableConnectionTest); + connect(service_edt, &QLineEdit::textChanged, this, &ConnectionsConfigWidget::enableConnectionTest); connect(host_edt, &QLineEdit::textChanged, this, &ConnectionsConfigWidget::enableConnectionTest); connect(user_edt, &QLineEdit::textChanged, this, &ConnectionsConfigWidget::enableConnectionTest); connect(passwd_edt, &QLineEdit::textChanged, this, &ConnectionsConfigWidget::enableConnectionTest); @@ -67,6 +68,7 @@ void ConnectionsConfigWidget::hideEvent(QHideEvent *event) { newConnection(); one_time_conn_edit = false; + service_edt->setEnabled(true); host_edt->setEnabled(true); port_sbp->setEnabled(true); conn_btns_wgt->setVisible(true); @@ -160,14 +162,16 @@ void ConnectionsConfigWidget::loadConfiguration() } } -void ConnectionsConfigWidget::setOneTimeEditMode(bool one_time_edit, const QString &conn_alias, const QString &dbname, const QString &host, int port, const QString &username, const QString &password) +void ConnectionsConfigWidget::setOneTimeEditMode(bool one_time_edit, const QString &conn_alias, const QString &service, const QString &host, int port, const QString &dbname, const QString &username, const QString &password) { one_time_conn_edit = one_time_edit; conn_btns_wgt->setVisible(!one_time_edit); add_tb->setVisible(!one_time_edit); + service_edt->setDisabled(one_time_edit && !service.isEmpty()); host_edt->setDisabled(one_time_edit && !host.isEmpty()); port_sbp->setDisabled(one_time_edit && port > 0); alias_edt->setText(conn_alias); + service_edt->setText(service); conn_db_edt->setText(dbname); host_edt->setText(host); port_sbp->setValue(port); @@ -189,10 +193,7 @@ void ConnectionsConfigWidget::enableCertificates() void ConnectionsConfigWidget::enableConnectionTest() { - test_tb->setEnabled(!alias_edt->text().isEmpty() && - !host_edt->text().isEmpty() && - !user_edt->text().isEmpty() && - !conn_db_edt->text().isEmpty()); + test_tb->setEnabled(!alias_edt->text().isEmpty()); add_tb->setEnabled(test_tb->isEnabled()); update_tb->setEnabled(test_tb->isEnabled()); @@ -211,6 +212,7 @@ void ConnectionsConfigWidget::newConnection() conn_db_edt->clear(); alias_edt->clear(); user_edt->clear(); + service_edt->clear(); host_edt->clear(); port_sbp->setValue(5432); passwd_edt->clear(); @@ -336,6 +338,8 @@ void ConnectionsConfigWidget::editConnection() import_chk->setChecked(conn->isDefaultForOperation(Connection::OpImport)); validation_chk->setChecked(conn->isDefaultForOperation(Connection::OpValidation)); + service_edt->setText(conn->getConnectionParam(Connection::ParamService)); + if(!conn->getConnectionParam(Connection::ParamServerFqdn).isEmpty()) host_edt->setText(conn->getConnectionParam(Connection::ParamServerFqdn)); else @@ -400,6 +404,7 @@ void ConnectionsConfigWidget::configureConnection(Connection *conn, bool is_upda } conn->setConnectionParam(Connection::ParamAlias, alias); + conn->setConnectionParam(Connection::ParamService, service_edt->text()); conn->setConnectionParam(Connection::ParamServerIp, ""); conn->setConnectionParam(Connection::ParamServerFqdn, host_edt->text()); conn->setConnectionParam(Connection::ParamPort, QString("%1").arg(port_sbp->value())); @@ -630,8 +635,11 @@ void ConnectionsConfigWidget::fillConnectionsComboBox(QComboBox *combo, bool inc } bool ConnectionsConfigWidget::openConnectionsConfiguration(bool one_time_edit, - const QString &conn_alias, const QString &dbname, const QString &host, - int port, const QString &username, const QString &password) + const QString &conn_alias, + const QString &service, + const QString &host, int port, + const QString &dbname, + const QString &username, const QString &password) { BaseForm parent_form; ConnectionsConfigWidget conn_cfg_wgt; @@ -656,7 +664,7 @@ bool ConnectionsConfigWidget::openConnectionsConfiguration(bool one_time_edit, ) }); - conn_cfg_wgt.setOneTimeEditMode(one_time_edit, conn_alias, dbname, host, port, username, password); + conn_cfg_wgt.setOneTimeEditMode(one_time_edit, conn_alias, service, host, port, dbname, username, password); parent_form.setWindowTitle(tr("Edit database connections")); parent_form.setWindowFlags(Qt::Dialog | Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint); parent_form.setMainWidget(&conn_cfg_wgt); diff --git a/libs/libgui/src/settings/connectionsconfigwidget.h b/libs/libgui/src/settings/connectionsconfigwidget.h index 4de1556c21..27a9b4af24 100644 --- a/libs/libgui/src/settings/connectionsconfigwidget.h +++ b/libs/libgui/src/settings/connectionsconfigwidget.h @@ -65,8 +65,10 @@ class __libgui ConnectionsConfigWidget: public BaseConfigWidget, public Ui::Conn * for them. Once the user accepts or rejects the editions the form * returns to the original operation mode */ void setOneTimeEditMode(bool one_time_edit, - const QString &conn_alias, const QString &dbname, + const QString &conn_alias, + const QString &service, const QString &host, int port, + const QString &dbname, const QString &username, const QString &password); static std::map getConfigurationParams(); @@ -96,8 +98,10 @@ class __libgui ConnectionsConfigWidget: public BaseConfigWidget, public Ui::Conn * Returns true when the connection were changed somehow, either by restoring the defaults, adding new connections * or removing current ones. Default values for connection alias, dbname, host, port and user can be provided. */ static bool openConnectionsConfiguration(bool one_time_edit = false, - const QString &conn_alias = "", const QString &dbname = "", + const QString &conn_alias = "", + const QString &service= "", const QString &host = "", int port = 5432, + const QString &dbname = "", const QString &username = "", const QString &password = ""); //! \brief Returns the first connection found which is defined as the default for the specified operation diff --git a/libs/libgui/ui/settings/connectionsconfigwidget.ui b/libs/libgui/ui/settings/connectionsconfigwidget.ui index 705cf645fe..36d4a9bf94 100644 --- a/libs/libgui/ui/settings/connectionsconfigwidget.ui +++ b/libs/libgui/ui/settings/connectionsconfigwidget.ui @@ -70,7 +70,266 @@ 5 - + + + + + 0 + 0 + + + + + + + + + + true + + + + + + + When successfully connecting to the database the current user will be switched to the one specified in this field via <strong>SET ROLE [role_name]</strong>. + + + + + + true + + + + + + + + 0 + 0 + + + + Set role: + + + + + + + + 0 + 0 + + + + Host/Port: + + + + + + + + 0 + 0 + + + + Timeout: + + + + + + + Qt::Orientation::Vertical + + + + 20 + 40 + + + + + + + + 5 + + + + + + + + Diff + + + + + + + Export + + + + + + + Import + + + + + + + Validation + + + + + + + Qt::Orientation::Horizontal + + + + 40 + 20 + + + + + + + + + + + 0 + 0 + + + + Other params: + + + + + + + QLineEdit::EchoMode::Password + + + true + + + + + + + + 0 + 0 + + + + Connection DB: + + + + + + + 5 + + + + + Specify additional connection parameters in the form [param]=[value]. These parameters are described in the <strong>libpq</strong> chapter at PostgreSQL docs. + + + + + + true + + + + + + + + + Qt::Orientation::Vertical + + + QSizePolicy::Policy::Fixed + + + + 20 + 10 + + + + + + + + + 0 + 0 + + + + User: + + + + + + + 5 + + + + + + 0 + 0 + + + + + + + + + + true + + + + + + + Automatically browses the named database when using this connection to manage databases on <strong>Manage</strong> view. + + + + + + Auto browse + + + + + + 5 @@ -186,33 +445,7 @@ - - - - Qt::Orientation::Vertical - - - - 20 - 40 - - - - - - - - - 0 - 0 - - - - User: - - - - + @@ -228,20 +461,23 @@ - - + + 0 0 + + <p>Indicates in which operations (diff, export, import or validation) the connection is used if none is explicitly specified by the user.</p> + - Host/Port: + Default for: - + @@ -254,132 +490,7 @@ - - - - - 0 - 0 - - - - - - - - - - true - - - - - - - Qt::Orientation::Vertical - - - QSizePolicy::Policy::Fixed - - - - 20 - 10 - - - - - - - - 5 - - - - - - - - Diff - - - - - - - Export - - - - - - - Import - - - - - - - Validation - - - - - - - Qt::Orientation::Horizontal - - - - 40 - 20 - - - - - - - - - - 5 - - - - - - 0 - 0 - - - - - - - - - - true - - - - - - - 1 - - - 65535 - - - 5432 - - - - - - + 5 @@ -406,52 +517,6 @@ - - - - - 0 - 0 - - - - Connection DB: - - - - - - - 5 - - - - - Specify additional connection parameters in the form [param]=[value]. These parameters are described in the <strong>libpq</strong> chapter at PostgreSQL docs. - - - - - - true - - - - - - - - - - 0 - 0 - - - - Timeout: - - - @@ -465,44 +530,18 @@ - - - - QLineEdit::EchoMode::Password - - - true - - - - - - - - 0 - 0 - - - - <p>Indicates in which operations (diff, export, import or validation) the connection is used if none is explicitly specified by the user.</p> - - - Default for: - - - - - + + 5 - - - - 0 - 0 - + + + + 0 + 0 + @@ -516,26 +555,32 @@ - - - Automatically browses the named database when using this connection to manage databases on <strong>Manage</strong> view. + + + 1 - - + + 65535 - - Auto browse + + 5432 - - - - When successfully connecting to the database the current user will be switched to the one specified in this field via <strong>SET ROLE [role_name]</strong>. + + + + + 0 + 0 + - + + + + @@ -543,8 +588,8 @@ - - + + 0 @@ -552,20 +597,7 @@ - Other params: - - - - - - - - 0 - 0 - - - - Set role: + Service @@ -1046,6 +1078,7 @@ conn_attribs_tbw alias_edt conn_db_edt + service_edt auto_browse_chk host_edt port_sbp @@ -1067,6 +1100,13 @@ crl_edt krb_server_edt gssapi_auth_chk + connections_cmb + new_tb + cancel_tb + duplicate_tb + edit_tb + remove_tb + set_role_edt diff --git a/libs/libutils/src/exception.cpp b/libs/libutils/src/exception.cpp index 5cf710c5bd..4ff8b3b0b7 100644 --- a/libs/libutils/src/exception.cpp +++ b/libs/libutils/src/exception.cpp @@ -245,7 +245,7 @@ QString Exception::messages[Exception::ErrorCount][2]={ {"AsgEnumInvalidChars", QT_TR_NOOP("The enumeration `%1' can't be assigned to the type `%2' because contains invalid characters!")}, {"AsgEnumLongName", QT_TR_NOOP("The enumeration `%1' can't be assigned to the type `%2' because is too long!")}, {"ConnectionTimeout", QT_TR_NOOP("The connection was idle for too long and was automatically closed!")}, - {"ConnectionBroken", QT_TR_NOOP("The connection was unexpectedly closed by the database server `%1' at port `%2'!")}, + {"ConnectionBroken", QT_TR_NOOP("The connection was unexpectedly closed by the database server %1!")}, {"DropCurrentDBDefault", QT_TR_NOOP("Failed to drop the database `%1' because it is defined as the default database for the connection `%2'!")}, {"NullPrimaryKeyColumn", QT_TR_NOOP("The column `%1' must be `NOT NULL' because it composes the primary key of the table `%2'. You need to remove the column from the mentioned contraint in order to disable the `NOT NULL' on it!")}, {"InvalidIdentityColumn", QT_TR_NOOP("The identity column `%1' has an invalid data type! The data type must be `smallint', `integer' or `bigint'.")},