From df2c9ff93177b830278b9541065dcfb64602033b Mon Sep 17 00:00:00 2001 From: Maxim Rylov Date: Thu, 14 Jan 2021 09:47:56 +0100 Subject: [PATCH] More detailed warnings when HANA driver not found --- src/providers/hana/qgshanadriver.cpp | 28 ++++++++++++--- src/providers/hana/qgshanadriver.h | 2 ++ src/providers/hana/qgshananewconnection.cpp | 39 +++++++++++++++++++-- 3 files changed, 62 insertions(+), 7 deletions(-) diff --git a/src/providers/hana/qgshanadriver.cpp b/src/providers/hana/qgshanadriver.cpp index 3b264c02c2a0..21255a4774f2 100644 --- a/src/providers/hana/qgshanadriver.cpp +++ b/src/providers/hana/qgshanadriver.cpp @@ -19,6 +19,7 @@ #include #include +#include #include "odbc/Connection.h" #include "odbc/Environment.h" @@ -69,18 +70,37 @@ QgsHanaDriver::~QgsHanaDriver() QgsDebugCall; } +ConnectionRef QgsHanaDriver::createConnection() +{ + return mEnv->createConnection(); +} + +const QString &QgsHanaDriver::driver() const +{ + return mDriver; +} + QgsHanaDriver *QgsHanaDriver::instance() { static QgsHanaDriver instance; return &instance; } -ConnectionRef QgsHanaDriver::createConnection() +bool QgsHanaDriver::isInstalled( const QString &name ) { - return mEnv->createConnection(); + EnvironmentRef env = Environment::create(); + return env->isDriverInstalled( name.toStdString().c_str() ); } -const QString &QgsHanaDriver::driver() const +bool QgsHanaDriver::isValidPath( const QString &path ) { - return mDriver; + if ( !QLibrary::isLibrary( path ) ) + return false; + + QLibrary lib( path ); + if ( !lib.load() ) + return false; + bool ret = lib.resolve( "SQLConnect" ) != nullptr; + lib.unload(); + return ret; } diff --git a/src/providers/hana/qgshanadriver.h b/src/providers/hana/qgshanadriver.h index ea216a24d9f3..3047b259027e 100644 --- a/src/providers/hana/qgshanadriver.h +++ b/src/providers/hana/qgshanadriver.h @@ -32,6 +32,8 @@ class QgsHanaDriver const QString &driver() const; static QgsHanaDriver *instance(); + static bool isInstalled( const QString &name ); + static bool isValidPath( const QString &path ); protected: Q_DISABLE_COPY( QgsHanaDriver ) diff --git a/src/providers/hana/qgshananewconnection.cpp b/src/providers/hana/qgshananewconnection.cpp index 4a5aedf454e3..ac4dc1395e96 100644 --- a/src/providers/hana/qgshananewconnection.cpp +++ b/src/providers/hana/qgshananewconnection.cpp @@ -21,6 +21,7 @@ #include "qgshanasettings.h" #include "qgssettings.h" +#include #include #include @@ -56,6 +57,17 @@ QgsHanaNewConnection::QgsHanaNewConnection( connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsHanaNewConnection::showHelp ); txtDriver->setText( QgsHanaDriver::instance()->driver() ); +#if defined(Q_OS_WIN) + txtDriver->setToolTip( tr( "The name of the HANA ODBC driver.\r\n\r\n" + "The HANA ODBC driver is a part of the SAP HANA Client,\r\n" + "which can be found at https://tools.hana.ondemand.com/#hanatools." ) ); +#else + txtDriver->setToolTip( tr( "The name or path to the HANA ODBC driver.\r\n\r\n" + "If the driver is registered in odbcinst.ini, enter the driver's name.\r\n" + "Otherwise, enter the path to the driver (libodbcHDB.so).\r\n\r\n" + "The HANA ODBC driver is a part of the SAP HANA Client,\r\n" + "which can be found at https://tools.hana.ondemand.com/#hanatools." ) ); +#endif cbxCryptoProvider->addItem( QStringLiteral( "openssl" ), QStringLiteral( "openssl" ) ); cbxCryptoProvider->addItem( QStringLiteral( "commoncrypto" ), QStringLiteral( "commoncrypto" ) ); @@ -296,9 +308,7 @@ void QgsHanaNewConnection::updateControlsFromSettings( const QgsHanaSettings &se void QgsHanaNewConnection::testConnection() { QString warningMsg; - if ( txtDriver->text().isEmpty() ) - warningMsg = tr( "Driver name has not been specified." ); - else if ( txtHost->text().isEmpty() ) + if ( txtHost->text().isEmpty() ) warningMsg = tr( "Host name has not been specified." ); else if ( rbtnMultipleContainers->isChecked() && rbtnTenantDatabase->isChecked() && txtTenantDatabaseName->text().isEmpty() ) @@ -309,6 +319,29 @@ void QgsHanaNewConnection::testConnection() warningMsg = tr( "Password has not been specified." ); else if ( txtIdentifier->text().isEmpty() ) warningMsg = tr( "Identifier has not been specified." ); + else + { + QString driver = txtDriver->text(); + if ( driver.isEmpty() ) + warningMsg = tr( "Driver name/path has not been specified." ); + else + { + if ( !QgsHanaDriver::isInstalled( driver ) ) + { +#if defined(Q_OS_WIN) + warningMsg = tr( "Driver with name '%1' is not installed." ).arg( driver ); +#else + if ( !QgsHanaDriver::isValidPath( driver ) ) + { + if ( QFileInfo::exists( driver ) ) + warningMsg = tr( "Specified driver '%1' cannot be used to connect to HANA." ).arg( driver ); + else + warningMsg = tr( "Driver with name/path '%1' was not found." ).arg( driver ); + } +#endif + } + } + } if ( !warningMsg.isEmpty() ) {