Added ability to test the connection in the dialog where the connection details are entered.

This commit is contained in:
eelke 2021-07-04 16:45:50 +02:00
parent 45795333f2
commit c00a0452d1
7 changed files with 201 additions and 43 deletions

View file

@ -51,25 +51,25 @@ public:
/** This version of send uses the signal onQueryResult and onQueryError to report back
* the completion of the query.
*/
bool send(const std::string &command, Pgsql::Params params = Pgsql::Params())
{
return send(command, params, [this] (Expected<std::shared_ptr<Pgsql::Result>> res, qint64) {
if (res.valid()) {
emit onQueryResult(res.get());
}
else {
emit onQueryError();
}
});
}
// bool send(const std::string &command, Pgsql::Params params = Pgsql::Params())
// {
// return send(command, params, [this] (Expected<std::shared_ptr<Pgsql::Result>> res, qint64) {
// if (res.valid()) {
// emit onQueryResult(res.get());
// }
// else {
// emit onQueryError();
// }
// });
// }
bool cancel();
signals:
void onStateChanged(ASyncDBConnection::State state);
void onNotice(Pgsql::ErrorDetails notice);
void onQueryResult(std::shared_ptr<Pgsql::Result> result);
void onQueryError();
// void onQueryResult(std::shared_ptr<Pgsql::Result> result);
// void onQueryError();
private:
Pgsql::Connection m_connection;

View file

@ -0,0 +1,58 @@
#include "TestConnection.h"
#include "ConnectionConfig.h"
#include "Pgsql_Connection.h"
#include "Pgsql_PgException.h"
TestConnectionResult TestConnectionResult::Failed(QString msg)
{
return TestConnectionResult(false, msg, {});
}
TestConnectionResult TestConnectionResult::Success(QStringList databases)
{
return TestConnectionResult(true, "Test succesfull", databases);
}
bool TestConnectionResult::ok() const
{
return Ok;
}
QString TestConnectionResult::message() const
{
return Message;
}
const QStringList &TestConnectionResult::databases() const
{
return Databases;
}
TestConnectionResult::TestConnectionResult(bool ok, QString msg, QStringList databases)
: Ok(ok)
, Message(msg)
, Databases(databases)
{}
TestConnectionResult TestConnection(const ConnectionConfig &cc)
{
try {
Pgsql::Connection conn;
conn.connect(cc.connectionString());
auto result = conn.query(
"SELECT datname\n"
" FROM pg_database\n"
" WHERE datallowconn AND has_database_privilege(datname, 'connect')\n"
" ORDER BY 1");
QStringList dbs;
for (auto row : result)
dbs.append(row.get(0));
return TestConnectionResult::Success(dbs);
}
catch (const Pgsql::PgException &ex) {
return TestConnectionResult::Failed(QString::fromUtf8(ex.what()));
}
}

32
pglablib/TestConnection.h Normal file
View file

@ -0,0 +1,32 @@
#pragma once
#include <QString>
#include <QStringList>
class ConnectionConfig;
class TestConnectionResult {
public:
/// Creates an instance signalling failure
static TestConnectionResult Failed(QString msg);
/// Creates a result instance signalling success
static TestConnectionResult Success(QStringList databases);
TestConnectionResult() = default;
bool ok() const;
QString message() const;
const QStringList& databases() const;
private:
bool Ok;
QString Message;
QStringList Databases;
TestConnectionResult(bool ok, QString msg, QStringList databases);
};
/// Test if a connection can be opened using the details in cc
///
/// It returns either the error message from the string on failure
/// or a list of databases the details give access to.
TestConnectionResult TestConnection(const ConnectionConfig &cc);

View file

@ -23,6 +23,7 @@ SOURCES += \
Pglablib.cpp \
ASyncDBConnection.cpp \
ConnectionConfig.cpp \
TestConnection.cpp \
WaitHandleList.cpp \
catalog/PgType.cpp \
catalog/PgTypeContainer.cpp \
@ -89,6 +90,7 @@ HEADERS += \
Pglablib.h \
ASyncDBConnection.h \
ConnectionConfig.h \
TestConnection.h \
WaitHandleList.h \
Win32Event.h \
catalog/PgType.h \