ServerWindow shows list of databases.
This commit is contained in:
parent
e71ef2e6df
commit
612b524151
15 changed files with 307 additions and 172 deletions
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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";
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
#include "PgsqlConn.h"
|
||||
|
||||
#include "Pgsql_declare.h"
|
||||
#include <stdexcept>
|
||||
|
||||
|
||||
|
|
@ -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<const char *> 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);
|
||||
|
|
|
|||
|
|
@ -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 <typename T>
|
||||
// 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<const char *>;
|
||||
|
||||
void deleteValues()
|
||||
{
|
||||
for (auto e : m_paramValues)
|
||||
delete[] e;
|
||||
}
|
||||
|
||||
/* Assumes other lists already have been copied */
|
||||
void copyValues(const t_paramValues &r);
|
||||
|
||||
std::vector<Oid> m_paramTypes;
|
||||
t_paramValues m_paramValues;
|
||||
std::vector<int> m_paramLengths; ///< postgresql ignores lengths for text parameters but we will it anyway for efficient copying
|
||||
std::vector<int> m_paramFormats;
|
||||
};
|
||||
|
||||
class Connection;
|
||||
/*
|
||||
This library has multiple layers.
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
84
src/Pgsql_Params.cpp
Normal file
84
src/Pgsql_Params.cpp
Normal file
|
|
@ -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<const char *> 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);
|
||||
}
|
||||
}
|
||||
49
src/Pgsql_Params.h
Normal file
49
src/Pgsql_Params.h
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
#ifndef PGSQL_PARAMS_H
|
||||
#define PGSQL_PARAMS_H
|
||||
|
||||
#include <vector>
|
||||
#include <QString>
|
||||
#include <pgsql/libpq-fe.h>
|
||||
#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<const char *>;
|
||||
|
||||
void deleteValues()
|
||||
{
|
||||
for (auto e : m_paramValues)
|
||||
delete[] e;
|
||||
}
|
||||
|
||||
/* Assumes other lists already have been copied */
|
||||
void copyValues(const t_paramValues &r);
|
||||
|
||||
std::vector<Oid> m_paramTypes;
|
||||
t_paramValues m_paramValues;
|
||||
std::vector<int> m_paramLengths; ///< postgresql ignores lengths for text parameters but we will it anyway for efficient copying
|
||||
std::vector<int> m_paramFormats;
|
||||
};
|
||||
|
||||
} // end namespace Pgsql
|
||||
|
||||
#endif // PGSQL_PARAMS_H
|
||||
27
src/Pgsql_declare.h
Normal file
27
src/Pgsql_declare.h
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
#ifndef PGSQL_DECLARE_H
|
||||
#define PGSQL_DECLARE_H
|
||||
|
||||
#include <pgsql/libpq-fe.h>
|
||||
|
||||
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
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
#include "queryresultmodel.h"
|
||||
#include "Pgsql_declare.h"
|
||||
#include <QBrush>
|
||||
#include <QColor>
|
||||
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -15,8 +15,120 @@
|
|||
</property>
|
||||
<widget class="QWidget" name="centralwidget">
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
<property name="leftMargin">
|
||||
<number>2</number>
|
||||
</property>
|
||||
<property name="topMargin">
|
||||
<number>2</number>
|
||||
</property>
|
||||
<property name="rightMargin">
|
||||
<number>2</number>
|
||||
</property>
|
||||
<property name="bottomMargin">
|
||||
<number>2</number>
|
||||
</property>
|
||||
<item>
|
||||
<widget class="QTableView" name="tableView"/>
|
||||
<widget class="QTabWidget" name="tabWidget">
|
||||
<property name="currentIndex">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<widget class="QWidget" name="databasesTab">
|
||||
<attribute name="title">
|
||||
<string>Databases</string>
|
||||
</attribute>
|
||||
<layout class="QVBoxLayout" name="verticalLayout_2">
|
||||
<property name="leftMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="topMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="rightMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="bottomMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<item>
|
||||
<widget class="QTableView" name="tableView">
|
||||
<property name="alternatingRowColors">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
<attribute name="verticalHeaderDefaultSectionSize">
|
||||
<number>20</number>
|
||||
</attribute>
|
||||
<attribute name="verticalHeaderMinimumSectionSize">
|
||||
<number>16</number>
|
||||
</attribute>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<widget class="QWidget" name="tablespacesTab">
|
||||
<attribute name="title">
|
||||
<string>Tablespaces</string>
|
||||
</attribute>
|
||||
<layout class="QVBoxLayout" name="verticalLayout_3">
|
||||
<property name="leftMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="topMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="rightMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="bottomMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<item>
|
||||
<widget class="QTableView" name="tableView_2">
|
||||
<property name="alternatingRowColors">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
<attribute name="verticalHeaderDefaultSectionSize">
|
||||
<number>20</number>
|
||||
</attribute>
|
||||
<attribute name="verticalHeaderMinimumSectionSize">
|
||||
<number>16</number>
|
||||
</attribute>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<widget class="QWidget" name="rolesTab">
|
||||
<attribute name="title">
|
||||
<string>Roles/users</string>
|
||||
</attribute>
|
||||
<layout class="QVBoxLayout" name="verticalLayout_4">
|
||||
<property name="leftMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="topMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="rightMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="bottomMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<item>
|
||||
<widget class="QTableView" name="tableView_3">
|
||||
<property name="alternatingRowColors">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
<attribute name="verticalHeaderDefaultSectionSize">
|
||||
<number>20</number>
|
||||
</attribute>
|
||||
<attribute name="verticalHeaderMinimumSectionSize">
|
||||
<number>16</number>
|
||||
</attribute>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
|
|
|
|||
|
|
@ -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 \
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue