From 612b524151dd50569da23ced6092e6e459178d33 Mon Sep 17 00:00:00 2001 From: eelke Date: Mon, 13 Feb 2017 19:51:19 +0100 Subject: [PATCH] ServerWindow shows list of databases. --- src/DatabasesTableModel.cpp | 10 ++- src/DatabasesTableModel.h | 8 +-- src/MasterController.cpp | 2 +- src/PgDatabaseContainer.cpp | 2 +- src/PgsqlConn.cpp | 85 +------------------------- src/PgsqlConn.h | 60 ------------------ src/PgsqlDatabaseCatalogue.h | 4 +- src/Pgsql_Params.cpp | 84 ++++++++++++++++++++++++++ src/Pgsql_Params.h | 49 +++++++++++++++ src/Pgsql_declare.h | 27 +++++++++ src/QueryResultModel.cpp | 16 +---- src/ServerWindow.cpp | 9 +++ src/ServerWindow.h | 2 + src/ServerWindow.ui | 114 ++++++++++++++++++++++++++++++++++- src/src.pro | 7 ++- 15 files changed, 307 insertions(+), 172 deletions(-) create mode 100644 src/Pgsql_Params.cpp create mode 100644 src/Pgsql_Params.h create mode 100644 src/Pgsql_declare.h diff --git a/src/DatabasesTableModel.cpp b/src/DatabasesTableModel.cpp index 7bb6073..ccd42c6 100644 --- a/src/DatabasesTableModel.cpp +++ b/src/DatabasesTableModel.cpp @@ -6,6 +6,13 @@ DatabasesTableModel::DatabasesTableModel(QObject *parent) { } +void DatabasesTableModel::setDatabaseList(const PgDatabaseContainer* databases) +{ + beginResetModel(); + m_databases = databases; + endResetModel(); +} + QVariant DatabasesTableModel::headerData(int section, Qt::Orientation orientation, int role) const { QVariant v; @@ -53,8 +60,7 @@ int DatabasesTableModel::rowCount(const QModelIndex &parent) const { int result = 0; if (m_databases) { - if (parent.isValid()) - return m_databases->count(); + result = m_databases->count(); } return result; } diff --git a/src/DatabasesTableModel.h b/src/DatabasesTableModel.h index 0717a48..daa19cc 100644 --- a/src/DatabasesTableModel.h +++ b/src/DatabasesTableModel.h @@ -19,7 +19,9 @@ public: - explicit DatabasesTableModel(QObject *parent = 0); + explicit DatabasesTableModel(QObject *parent); + + void setDatabaseList(const PgDatabaseContainer* databases); // Header: QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; @@ -31,9 +33,7 @@ public: QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; private: - - - PgDatabaseContainer *m_databases; + const PgDatabaseContainer *m_databases = nullptr; }; #endif // DATABASESTABLEMODEL_H diff --git a/src/MasterController.cpp b/src/MasterController.cpp index 47f9f23..63a0f3f 100644 --- a/src/MasterController.cpp +++ b/src/MasterController.cpp @@ -48,7 +48,7 @@ void MasterController::openServerWindowForConnection(int connection_index) if (cc.valid()) { auto w = new ServerWindow(this, nullptr); w->setAttribute( Qt::WA_DeleteOnClose ); - //w->setConfig(cc.get()); + w->setConfig(cc.get()); w->show(); } } diff --git a/src/PgDatabaseContainer.cpp b/src/PgDatabaseContainer.cpp index d682888..5cadd52 100644 --- a/src/PgDatabaseContainer.cpp +++ b/src/PgDatabaseContainer.cpp @@ -6,7 +6,7 @@ PgDatabaseContainer::PgDatabaseContainer() std::string PgDatabaseContainer::getLoadQuery() const { - return "SELECT oid,datname,datdba,encoding,datcollate,datctype,datistemplate,datallowcon," + return "SELECT oid,datname,datdba,encoding,datcollate,datctype,datistemplate,datallowconn," "datconnlimit,dattablespace,datacl FROM pg_database"; } diff --git a/src/PgsqlConn.cpp b/src/PgsqlConn.cpp index 75fa1a9..66be446 100644 --- a/src/PgsqlConn.cpp +++ b/src/PgsqlConn.cpp @@ -1,5 +1,5 @@ #include "PgsqlConn.h" - +#include "Pgsql_declare.h" #include @@ -47,76 +47,6 @@ ErrorDetails ErrorDetails::createErrorDetailsFromPGresult(const PGresult *result } -Params::Params() -{} - -Params::Params(const Params& rhs) - : m_paramTypes(rhs.m_paramTypes) - , m_paramLengths(rhs.m_paramLengths) - , m_paramFormats(rhs.m_paramFormats) -{ - //std::vector m_paramValues; - copyValues(rhs.m_paramValues); -} - -Params& Params::operator=(const Params& rhs) -{ - if (&rhs != this) { - m_paramTypes = rhs.m_paramTypes; - m_paramLengths = rhs.m_paramLengths; - m_paramFormats = rhs.m_paramFormats; - copyValues(rhs.m_paramValues); - } - return *this; -} - -Params::Params(const Params&& rhs) - : m_paramTypes(std::move(rhs.m_paramTypes)) - , m_paramValues(std::move(rhs.m_paramValues)) - , m_paramLengths(std::move(rhs.m_paramLengths)) - , m_paramFormats(std::move(rhs.m_paramFormats)) -{} - -Params::~Params() -{ - deleteValues(); -} - -void Params::addText(const char *data, Oid oid) -{ - m_paramTypes.push_back(oid); - m_paramValues.push_back(data); - m_paramLengths.push_back(data ? strlen(data) + 1 : 0); - m_paramFormats.push_back(0); -} - -void Params::add(const QString &s, Oid oid) -{ - auto ba = s.toUtf8(); - const int len = ba.size(); - char * p = new char[len]; - std::memcpy(p, ba.data(), len); - addText(p, oid_varchar); -} - -void Params::addBinary(const char *data, int length, Oid oid) -{ - m_paramTypes.push_back(oid); - m_paramValues.push_back(data); - m_paramLengths.push_back(length); - m_paramFormats.push_back(1); -} - -void Params::clear() -{ - m_paramTypes.clear(); - deleteValues(); - m_paramValues.clear(); - m_paramLengths.clear(); - m_paramFormats.clear(); -} - - bool Row::next() { @@ -127,19 +57,6 @@ bool Row::next() return false; } - -void Params::copyValues(const t_paramValues &r) -{ - const int n = m_paramTypes.size(); - m_paramValues.reserve(n); - for (int i = 0; i < n; ++i) { - const int len = m_paramLengths[i]; - char * p = new char[len]; - std::memcpy(p, r[i], len); - m_paramValues.push_back(p); - } -} - Value Row::get(int col) const { return m_result.get(col, m_row); diff --git a/src/PgsqlConn.h b/src/PgsqlConn.h index f8ef26b..0b07ac6 100644 --- a/src/PgsqlConn.h +++ b/src/PgsqlConn.h @@ -14,66 +14,6 @@ namespace Pgsql { - const Oid oid_bool = 16; - const Oid oid_int2 = 21; - const Oid oid_int4 = 23; - const Oid oid_int8 = 20; - const Oid oid_float4 = 700; - const Oid oid_float8 = 701; - const Oid oid_numeric = 1700; - const Oid oid_oid = 26; - const Oid oid_varchar = 1043; - - - - class Params { - public: - Params(); - Params(const Params& rhs); - Params& operator=(const Params& rhs); - Params(const Params&& rhs); - ~Params(); - -// template -// Params& add(const T& val, Oid oid = InvalidOid) -// { - -// fmt::FormatInt( -// int idx = i-1; -// m_strings[idx] = value_to_dbstring(val); -// m_paramValues[idx] = m_strings[idx].c_str(); - -// return *this; -// } - - - /** \brief Add a parameter to the list. - * - * The class takes ownership of data and will try to delete[] it. - */ - void addText(const char *data, Oid oid); - void add(const QString &s, Oid oid); - void addBinary(const char *data, int length, Oid oid); - void clear(); - - private: - using t_paramValues = std::vector; - - void deleteValues() - { - for (auto e : m_paramValues) - delete[] e; - } - - /* Assumes other lists already have been copied */ - void copyValues(const t_paramValues &r); - - std::vector m_paramTypes; - t_paramValues m_paramValues; - std::vector m_paramLengths; ///< postgresql ignores lengths for text parameters but we will it anyway for efficient copying - std::vector m_paramFormats; - }; - class Connection; /* This library has multiple layers. diff --git a/src/PgsqlDatabaseCatalogue.h b/src/PgsqlDatabaseCatalogue.h index 9652bcb..bcf5cd7 100644 --- a/src/PgsqlDatabaseCatalogue.h +++ b/src/PgsqlDatabaseCatalogue.h @@ -25,8 +25,8 @@ public: void loadTypes(Pgsql::Connection &conn); void loadDatabases(Pgsql::Connection &conn); - const PgTypeContainer* types() const { return m_types; } - const PgDatabaseContainer *databases() const { return m_databases; } + const PgTypeContainer* types() const; + const PgDatabaseContainer *databases() const; private: PgTypeContainer *m_types = nullptr; PgDatabaseContainer *m_databases = nullptr; diff --git a/src/Pgsql_Params.cpp b/src/Pgsql_Params.cpp new file mode 100644 index 0000000..02dfa48 --- /dev/null +++ b/src/Pgsql_Params.cpp @@ -0,0 +1,84 @@ +#include "Pgsql_Params.h" + +using namespace Pgsql; + +Params::Params() +{} + +Params::Params(const Params& rhs) + : m_paramTypes(rhs.m_paramTypes) + , m_paramLengths(rhs.m_paramLengths) + , m_paramFormats(rhs.m_paramFormats) +{ + //std::vector m_paramValues; + copyValues(rhs.m_paramValues); +} + +Params& Params::operator=(const Params& rhs) +{ + if (&rhs != this) { + m_paramTypes = rhs.m_paramTypes; + m_paramLengths = rhs.m_paramLengths; + m_paramFormats = rhs.m_paramFormats; + copyValues(rhs.m_paramValues); + } + return *this; +} + +Params::Params(const Params&& rhs) + : m_paramTypes(std::move(rhs.m_paramTypes)) + , m_paramValues(std::move(rhs.m_paramValues)) + , m_paramLengths(std::move(rhs.m_paramLengths)) + , m_paramFormats(std::move(rhs.m_paramFormats)) +{} + +Params::~Params() +{ + deleteValues(); +} + +void Params::addText(const char *data, Oid oid) +{ + m_paramTypes.push_back(oid); + m_paramValues.push_back(data); + m_paramLengths.push_back(data ? strlen(data) + 1 : 0); + m_paramFormats.push_back(0); +} + +void Params::add(const QString &s, Oid oid) +{ + auto ba = s.toUtf8(); + const int len = ba.size(); + char * p = new char[len]; + std::memcpy(p, ba.data(), len); + addText(p, oid); +} + +void Params::addBinary(const char *data, int length, Oid oid) +{ + m_paramTypes.push_back(oid); + m_paramValues.push_back(data); + m_paramLengths.push_back(length); + m_paramFormats.push_back(1); +} + +void Params::clear() +{ + m_paramTypes.clear(); + deleteValues(); + m_paramValues.clear(); + m_paramLengths.clear(); + m_paramFormats.clear(); +} + +void Params::copyValues(const t_paramValues &r) +{ + const int n = m_paramTypes.size(); + m_paramValues.reserve(n); + for (int i = 0; i < n; ++i) { + const int len = m_paramLengths[i]; + char * p = new char[len]; + std::memcpy(p, r[i], len); + m_paramValues.push_back(p); + } +} diff --git a/src/Pgsql_Params.h b/src/Pgsql_Params.h new file mode 100644 index 0000000..a3cd7d7 --- /dev/null +++ b/src/Pgsql_Params.h @@ -0,0 +1,49 @@ +#ifndef PGSQL_PARAMS_H +#define PGSQL_PARAMS_H + +#include +#include +#include +#include "Pgsql_declare.h" + +namespace Pgsql { + + class Params { + public: + Params(); + Params(const Params& rhs); + Params& operator=(const Params& rhs); + Params(const Params&& rhs); + ~Params(); + + + /** \brief Add a parameter to the list. + * + * The class takes ownership of data and will try to delete[] it. + */ + void addText(const char *data, Oid oid=oid_varchar); + void add(const QString &s, Oid oid=oid_varchar); + void addBinary(const char *data, int length, Oid oid); + void clear(); + + private: + using t_paramValues = std::vector; + + void deleteValues() + { + for (auto e : m_paramValues) + delete[] e; + } + + /* Assumes other lists already have been copied */ + void copyValues(const t_paramValues &r); + + std::vector m_paramTypes; + t_paramValues m_paramValues; + std::vector m_paramLengths; ///< postgresql ignores lengths for text parameters but we will it anyway for efficient copying + std::vector m_paramFormats; + }; + +} // end namespace Pgsql + +#endif // PGSQL_PARAMS_H diff --git a/src/Pgsql_declare.h b/src/Pgsql_declare.h new file mode 100644 index 0000000..9454641 --- /dev/null +++ b/src/Pgsql_declare.h @@ -0,0 +1,27 @@ +#ifndef PGSQL_DECLARE_H +#define PGSQL_DECLARE_H + +#include + +namespace Pgsql { + + const Oid oid_bool = 16; + const Oid oid_int2 = 21; + const Oid oid_int4 = 23; + const Oid oid_int8 = 20; + const Oid oid_float4 = 700; + const Oid oid_float8 = 701; + const Oid oid_numeric = 1700; + const Oid oid_oid = 26; + const Oid oid_varchar = 1043; + + + class Params; + class Connection; + class Result; + class Value; + class Row; + +} // END namespace Pgsql + +#endif // PGSQL_DECLARE_H diff --git a/src/QueryResultModel.cpp b/src/QueryResultModel.cpp index 33732d7..3fc45a3 100644 --- a/src/QueryResultModel.cpp +++ b/src/QueryResultModel.cpp @@ -1,4 +1,5 @@ #include "queryresultmodel.h" +#include "Pgsql_declare.h" #include #include @@ -20,21 +21,6 @@ int QueryResultModel::columnCount(const QModelIndex &) const return r; } -//DisplayRole = 0, -//DecorationRole = 1, -//EditRole = 2, -//ToolTipRole = 3, -//StatusTipRole = 4, -//WhatsThisRole = 5, -//// Metadata -//FontRole = 6, -//TextAlignmentRole = 7, -//BackgroundColorRole = 8, -//BackgroundRole = 8, -//TextColorRole = 9, -//ForegroundRole = 9, -//CheckStateRole = 10, - QVariant QueryResultModel::data(const QModelIndex &index, int role) const { using namespace Pgsql; diff --git a/src/ServerWindow.cpp b/src/ServerWindow.cpp index 285d920..759adbe 100644 --- a/src/ServerWindow.cpp +++ b/src/ServerWindow.cpp @@ -1,6 +1,8 @@ #include "ServerWindow.h" #include "ui_ServerWindow.h" #include "OpenDatabase.h" +#include "DatabasesTableModel.h" +#include "PgsqlDatabaseCatalogue.h" ServerWindow::ServerWindow(MasterController *master, QWidget *parent) : ASyncWindow(parent) @@ -8,6 +10,9 @@ ServerWindow::ServerWindow(MasterController *master, QWidget *parent) , ui(new Ui::ServerWindow) { ui->setupUi(this); + + m_databasesModel = new DatabasesTableModel(this); + ui->tableView->setModel(m_databasesModel); } ServerWindow::~ServerWindow() @@ -21,6 +26,10 @@ void ServerWindow::setConfig(const ConnectionConfig &config) auto res = OpenDatabase::createOpenDatabase(config); if (res.valid()) { m_database = res.get(); + auto cat = m_database->catalogue(); + if (cat) { + m_databasesModel->setDatabaseList(cat->databases()); + } } QString title = "pglab - "; title += m_config.name().c_str(); diff --git a/src/ServerWindow.h b/src/ServerWindow.h index 83aa885..35e6b21 100644 --- a/src/ServerWindow.h +++ b/src/ServerWindow.h @@ -10,6 +10,7 @@ class ServerWindow; class MasterController; class OpenDatabase; +class DatabasesTableModel; class ServerWindow : public ASyncWindow { Q_OBJECT @@ -24,6 +25,7 @@ private: MasterController *m_masterController; ConnectionConfig m_config; OpenDatabase *m_database; + DatabasesTableModel *m_databasesModel; }; #endif // SERVERWINDOW_H diff --git a/src/ServerWindow.ui b/src/ServerWindow.ui index 2f7495c..d9a0adb 100644 --- a/src/ServerWindow.ui +++ b/src/ServerWindow.ui @@ -15,8 +15,120 @@ + + 2 + + + 2 + + + 2 + + + 2 + - + + + 0 + + + + Databases + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + true + + + 20 + + + 16 + + + + + + + + Tablespaces + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + true + + + 20 + + + 16 + + + + + + + + Roles/users + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + true + + + 20 + + + 16 + + + + + + diff --git a/src/src.pro b/src/src.pro index 4d7de12..31d5564 100644 --- a/src/src.pro +++ b/src/src.pro @@ -56,7 +56,8 @@ SOURCES += main.cpp\ ASyncWindow.cpp \ DatabasesTableModel.cpp \ PgDatabase.cpp \ - PgDatabaseContainer.cpp + PgDatabaseContainer.cpp \ + Pgsql_Params.cpp HEADERS += \ sqlparser.h \ @@ -100,7 +101,9 @@ HEADERS += \ DatabasesTableModel.h \ PgDatabase.h \ PgDatabaseContainer.h \ - PgContainer.h + PgContainer.h \ + Pgsql_Params.h \ + Pgsql_declare.h FORMS += mainwindow.ui \ DatabaseWindow.ui \