2017-12-09 20:21:22 +01:00
|
|
|
|
#include "OpenDatabase.h"
|
2017-12-10 10:35:46 +01:00
|
|
|
|
#include "PgDatabaseCatalog.h"
|
2017-08-26 11:45:50 +02:00
|
|
|
|
#include "Pgsql_Connection.h"
|
2017-08-23 13:27:23 +02:00
|
|
|
|
#include "TypeSelectionItemModel.h"
|
2017-02-01 18:01:02 +01:00
|
|
|
|
|
2017-12-25 10:31:58 +01:00
|
|
|
|
Expected<OpenDatabase::OpenDatabaseSPtr> OpenDatabase::createOpenDatabase(const ConnectionConfig &cfg)
|
2017-02-01 18:01:02 +01:00
|
|
|
|
{
|
2017-12-25 10:31:58 +01:00
|
|
|
|
OpenDatabaseSPtr odb(new OpenDatabase(cfg));
|
2017-02-01 18:01:02 +01:00
|
|
|
|
if (odb->Init()) {
|
|
|
|
|
|
|
2017-09-01 21:07:37 +02:00
|
|
|
|
return odb;
|
2017-02-02 07:22:54 +01:00
|
|
|
|
|
2017-02-01 18:01:02 +01:00
|
|
|
|
}
|
|
|
|
|
|
//return Expected<ConnectionConfig>::fromException(std::out_of_range("Invalid row"));
|
2017-12-25 10:31:58 +01:00
|
|
|
|
return Expected<OpenDatabaseSPtr>::fromException(
|
2017-09-01 21:07:37 +02:00
|
|
|
|
std::runtime_error("Failed to get database information"));
|
2017-02-01 18:01:02 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-25 10:31:58 +01:00
|
|
|
|
OpenDatabase::OpenDatabase(const ConnectionConfig& cfg)
|
|
|
|
|
|
: m_config(cfg)
|
2017-12-10 10:35:46 +01:00
|
|
|
|
, m_catalogue(std::make_shared<PgDatabaseCatalog>())
|
2017-02-01 18:01:02 +01:00
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
OpenDatabase::~OpenDatabase()
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool OpenDatabase::Init()
|
|
|
|
|
|
{
|
2017-02-04 11:55:49 +01:00
|
|
|
|
Pgsql::Connection conn;
|
|
|
|
|
|
auto kw = m_config.getKeywords();
|
|
|
|
|
|
auto vals = m_config.getValues();
|
|
|
|
|
|
if (conn.connect(kw, vals, 0)) {
|
2017-12-28 07:29:07 +01:00
|
|
|
|
m_catalogue->loadAll(conn, nullptr);
|
2017-09-01 21:07:37 +02:00
|
|
|
|
return true;
|
2017-02-04 11:55:49 +01:00
|
|
|
|
}
|
2017-09-01 21:07:37 +02:00
|
|
|
|
return false;
|
2017-02-01 18:01:02 +01:00
|
|
|
|
}
|
2017-02-04 11:55:49 +01:00
|
|
|
|
|
2017-12-10 10:35:46 +01:00
|
|
|
|
std::shared_ptr<PgDatabaseCatalog> OpenDatabase::catalogue()
|
2017-02-04 11:55:49 +01:00
|
|
|
|
{
|
|
|
|
|
|
return m_catalogue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-02-07 21:39:45 +01:00
|
|
|
|
TypeSelectionItemModel* OpenDatabase::typeSelectionModel()
|
2017-02-04 11:55:49 +01:00
|
|
|
|
{
|
|
|
|
|
|
if (m_typeSelectionModel == nullptr) {
|
|
|
|
|
|
m_typeSelectionModel = new TypeSelectionItemModel(nullptr);
|
2017-02-07 21:39:45 +01:00
|
|
|
|
m_typeSelectionModel->setTypeList(m_catalogue->types());
|
2017-02-04 11:55:49 +01:00
|
|
|
|
}
|
|
|
|
|
|
return m_typeSelectionModel;
|
|
|
|
|
|
}
|