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

@ -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()));
}
}