Bezig met crudmodel/tab

This commit is contained in:
eelke 2018-01-08 20:45:52 +01:00
parent ee321b3fb1
commit 14ab400ccb
15 changed files with 308 additions and 33 deletions

31
pglab/CrudModel.cpp Normal file
View file

@ -0,0 +1,31 @@
#include "CrudModel.h"
#include "OpenDatabase.h"
#include "PgDatabaseCatalog.h"
#include "PgAttributeContainer.h"
#include "PgConstraintContainer.h"
#include <string>
CrudModel::CrudModel()
: m_dbConn(*getGlobalAsioIoService())
{}
CrudModel::~CrudModel()
{
m_dbConn.closeConnection();
}
/*
* Strategy
* when ordered by primary key, offset and limit work very quickly so we can get away with not loading
* everything.
*/
void CrudModel::setData(std::shared_ptr<OpenDatabase> db, const PgClass &table)
{
m_database = db;
m_table = table;
m_primaryKey = db->catalogue()->constraints()->getPrimaryForRelation(table.oid);
//cat->attributes()->getColumnsForRelation()
m_dbConn.setupConnection(m_database.config());
}

67
pglab/CrudModel.h Normal file
View file

@ -0,0 +1,67 @@
#ifndef CRUDMODEL_H
#define CRUDMODEL_H
#include "BaseTableModel.h"
#include "ASyncDBConnection.h"
#include "PgClass.h"
#include "Pgsql_Connection.h"
#include <memory>
#include <vector>
#include <boost/optional.hpp>
class PgConstraint;
class OpenDatase;
/**
* @brief The CrudModel class
*
* Features
* - order by one or more user selectable columns
* - user filter condition
* - user limit and offset
* - hide columns (will not be retrieved can greatly speed up things when you disable wide columns)
* - can use foreign keys to display dropdown
*
* How to load data?
* - 2D array of QVariants won't be efficient
* - 2D array of strings
* - We do know the type of a single column is fixed
* - Keep data in Result and only load data in replacement rows
* std::string has short string optimization so this probably means
* that a two dimensional array of std::string objects could actually be quite efficient!
*/
class CrudModel: public BaseTableModel {
Q_OBJECT
public:
CrudModel();
~CrudModel();
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
void setData(std::shared_ptr<OpenDatabase> db, const PgClass &table);
// Basic functionality:
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
protected:
virtual Oid getType(int column) const override;
virtual QVariant getData(const QModelIndex &index) const override;
private:
std::shared_ptr<OpenDatabase> m_database;
PgClass m_table;
boost::optional<PgConstraint> m_primaryKey;
ASyncDBConnection m_dbConn;
using RowData = std::vector<std::string>;
using RowDataPtr = std::unique_ptr<RowData>;
/** Choosen for a vector of pointers while vector is relatively slow with insertion and removal in the middle
* I hope the small size of the elements will make this not much of an issue while the simple linear and sequential
* nature of a vector will keep memory allocation fairly efficient.
using RowList = std::vector<RowDataPtr>;
};
#endif // CRUDMODEL_H

14
pglab/CrudTab.cpp Normal file
View file

@ -0,0 +1,14 @@
#include "CrudTab.h"
#include "ui_CrudTab.h"
CrudTab::CrudTab(QWidget *parent) :
QWidget(parent),
ui(new Ui::CrudTab)
{
ui->setupUi(this);
}
CrudTab::~CrudTab()
{
delete ui;
}

22
pglab/CrudTab.h Normal file
View file

@ -0,0 +1,22 @@
#ifndef CRUDTAB_H
#define CRUDTAB_H
#include <QWidget>
namespace Ui {
class CrudTab;
}
class CrudTab : public QWidget
{
Q_OBJECT
public:
explicit CrudTab(QWidget *parent = 0);
~CrudTab();
private:
Ui::CrudTab *ui;
};
#endif // CRUDTAB_H

24
pglab/CrudTab.ui Normal file
View file

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>CrudTab</class>
<widget class="QWidget" name="CrudTab">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>718</width>
<height>581</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QTableView" name="tableView"/>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

View file

@ -26,6 +26,7 @@ public:
std::shared_ptr<PgDatabaseCatalog> catalogue();
TypeSelectionItemModel* typeSelectionModel();
const ConnectionConfig& config() const { return m_config; }
private:
ConnectionConfig m_config;
std::shared_ptr<PgDatabaseCatalog> m_catalogue;

View file

@ -24,12 +24,12 @@ void TablesTableModel::setCatalog(std::shared_ptr<const PgDatabaseCatalog> cat)
// How many?
int n = 0;
for (const auto &e : *classes)
if (e.kind == RelKind::Table) ++n;
if (e.kind == RelKind::Table && !e.system_namespace) ++n;
m_tables.clear();
m_tables.reserve(n); // reserve space
for (const auto &e : *classes) {
if (e.kind == RelKind::Table) {
if (e.kind == RelKind::Table && !e.system_namespace) {
m_tables.push_back(e);
}
}
@ -49,8 +49,11 @@ void TablesTableModel::doSort(int so)
{
if (so == 1)
std::sort(m_tables.begin(), m_tables.end(),
[] (auto l, auto r) -> bool { return l.relnamespace < r.relnamespace
|| (l.relnamespace == r.relnamespace && l.name < r.name); });
[] (auto l, auto r) -> bool { return l.relnamespace_name < r.relnamespace_name
|| (l.relnamespace_name == r.relnamespace_name && l.name < r.name); });
else
std::sort(m_tables.begin(), m_tables.end(),
[] (auto l, auto r) -> bool { return l.name < r.name; });
}

View file

@ -66,7 +66,9 @@ SOURCES += main.cpp\
ApplicationWindow.cpp \
ConstraintModel.cpp \
IconColumnDelegate.cpp \
IndexModel.cpp
IndexModel.cpp \
CrudTab.cpp \
CrudModel.cpp
HEADERS += \
QueryResultModel.h \
@ -104,7 +106,9 @@ HEADERS += \
ApplicationWindow.h \
ConstraintModel.h \
IconColumnDelegate.h \
IndexModel.h
IndexModel.h \
CrudTab.h \
CrudModel.h
FORMS += mainwindow.ui \
ConnectionManagerWindow.ui \
@ -116,7 +120,8 @@ FORMS += mainwindow.ui \
ProcessStdioWidget.ui \
TablesPage.ui \
NamespaceFilterWidget.ui \
ApplicationWindow.ui
ApplicationWindow.ui \
CrudTab.ui
RESOURCES += \
resources.qrc

View file

@ -26,12 +26,15 @@ enum class RelKind {
void operator<<(RelKind &s, const Pgsql::Value &v);
class PgClass {
public:
Oid oid = InvalidOid;
QString name;
Oid relnamespace = InvalidOid;
QString relnamespace_name; // Transient, cached value from relnamespace
bool system_namespace = false; // Transient, cached value from relnamespace
Oid type = InvalidOid;
Oid oftype = InvalidOid;
Oid owner = InvalidOid;
@ -56,6 +59,7 @@ public:
bool operator<(Oid _oid) const { return oid < _oid; }
bool operator<(const PgClass &rhs) const { return oid < rhs.oid; }
private:
};
#endif // PGCLASS_H

View file

@ -1,6 +1,8 @@
#include "PgClassContainer.h"
#include "Pgsql_Connection.h"
#include "Pgsql_Col.h"
#include "PgDatabaseCatalog.h"
#include "PgNamespaceContainer.h"
std::string PgClassContainer::getLoadQuery() const
{
@ -21,5 +23,10 @@ PgClass PgClassContainer::loadElem(const Pgsql::Row &row)
>> v.tuples_est >> v.toastrelid >> v.isshared >> v.persistence
>> v.kind >> v.hasoids >> v.ispopulated >> v.frozenxid >> v.minmxid
>> v.acl >> v.options;
auto cat = m_catalogue.lock();
auto ns = cat->namespaces()->getByKey(v.relnamespace);
v.relnamespace_name = ns.name;
v.system_namespace = ns.isSystemCatalog();
return v;
}

View file

@ -72,3 +72,15 @@ std::vector<PgConstraint> PgConstraintContainer::getConstraintsForRelation(Oid r
result.push_back(e);
return result;
}
boost::optional<PgConstraint> PgConstraintContainer::getPrimaryForRelation(Oid relid) const
{
boost::optional<PgConstraint> result;
for (const auto &e : m_container) {
if (e.relid == relid && e.type == ConstraintType::PrimaryKey) {
result = e;
break;
}
}
return result;
}

View file

@ -5,6 +5,7 @@
#include "PgConstraint.h"
#include "Pgsql_declare.h"
#include <vector>
#include <boost/optional.hpp>
class PgConstraintContainer : public PgContainer<PgConstraint> {
public:
@ -15,6 +16,7 @@ public:
const PgConstraint* getFKeyForTableColumn(Oid relid, int16_t attnum) const;
std::vector<PgConstraint> getConstraintsForRelation(Oid relid) const;
boost::optional<PgConstraint> getPrimaryForRelation(Oid relid) const;
protected:
virtual PgConstraint loadElem(const Pgsql::Row &row) override;
};

View file

@ -101,4 +101,89 @@ private:
};
template<typename T, typename K=Oid>
class PgSPtrContainer: public IPgContainter {
public:
using t_Elem = std::shared_ptr<T>;
using t_Container = std::vector<t_Elem>; ///< Do not assume it will stay a vector only expect bidirectional access
explicit PgSPtrContainer(std::weak_ptr<PgDatabaseCatalog> cat)
: m_catalogue(cat)
{}
typename t_Container::const_iterator begin() const
{
return m_container.begin();
}
typename t_Container::const_iterator end() const
{
return m_container.end();
}
void clear()
{
m_container.clear();
}
int count() const
{
return (int)m_container.size();
}
const t_Elem getByKey(const K &key) const
{
auto lb_result = std::lower_bound(m_container.begin(), m_container.end(), key);
if (lb_result != m_container.end() && **lb_result == key)
return *lb_result;
return m_invalidInstance;
}
const t_Elem getByName(const QString name) const
{
auto find_res = std::find_if(m_container.begin(), m_container.end(),
[name](auto e) -> bool { return *e = name; } );
if (find_res != m_container.end())
return *find_res;
return m_invalidInstance;
}
const t_Elem getByIdx(int idx) const
{
return m_container.at(idx);
}
/** Override to implement complete loading logic.
*
* Do not override this function if you only want to implement
* the loading of a single element. Override loadElem instead.
*/
virtual void load(const Pgsql::Result &res) override
{
m_container.clear();
m_container.reserve(res.rows());
for (auto row : res)
m_container.push_back(loadElem(row));
std::sort(m_container.begin(), m_container.end());
}
protected:
std::weak_ptr<PgDatabaseCatalog> m_catalogue;
t_Container m_container;
/** Override the implementation for this function to implement loading of single row.
*
* When overriding this function there is no need to override load.
*/
virtual t_Elem loadElem(const Pgsql::Row &) = 0;
private:
};
#endif // PGCONTAINER_H

View file

@ -121,31 +121,32 @@ void PgDatabaseCatalog::loadAll(Pgsql::Connection &conn,
std::function<bool(int, int)> progress_callback)
{
loadInfo(conn);
if (progress_callback && !progress_callback(1, 9))
return;
load2(m_attributes, conn);
if (progress_callback && !progress_callback(2, 9))
return;
load2(m_authIds, conn);
if (progress_callback && !progress_callback(3, 9))
return;
load2(m_classes, conn);
if (progress_callback && !progress_callback(4, 9))
return;
load2(m_constraints, conn);
if (progress_callback && !progress_callback(5, 9))
return;
load2(m_databases, conn);
if (progress_callback && !progress_callback(6, 9))
return;
load2(m_indexes, conn);
if (progress_callback && !progress_callback(7, 9))
int n = 0;
if (progress_callback && !progress_callback(++n, 9))
return;
load2(m_namespaces, conn);
if (progress_callback && !progress_callback(8, 9))
if (progress_callback && !progress_callback(++n, 9))
return;
load2(m_classes, conn); // needs namespaces
if (progress_callback && !progress_callback(++n, 9))
return;
load2(m_attributes, conn);
if (progress_callback && !progress_callback(++n, 9))
return;
load2(m_authIds, conn);
if (progress_callback && !progress_callback(++n, 9))
return;
load2(m_constraints, conn);
if (progress_callback && !progress_callback(++n, 9))
return;
load2(m_databases, conn);
if (progress_callback && !progress_callback(++n, 9))
return;
load2(m_indexes, conn);
if (progress_callback && !progress_callback(++n, 9))
return;
load2(m_types, conn);
progress_callback && progress_callback(9, 9);
progress_callback && progress_callback(++n, 9);
}
void PgDatabaseCatalog::loadInfo(Pgsql::Connection &conn)
@ -167,7 +168,7 @@ void load(Pgsql::Connection &conn, IPgContainter &pg_cont)
std::string q = pg_cont.getLoadQuery();
Pgsql::Result result = conn.query(q.c_str());
if (result && result.resultStatus() == PGRES_TUPLES_OK) {
boost::timer::auto_cpu_timer t;
//boost::timer::auto_cpu_timer t;
pg_cont.load(result);
}
else {

View file

@ -4,9 +4,6 @@ PgNamespace::PgNamespace() = default;
bool PgNamespace::isSystemCatalog() const
{
return name == "pg_catalog"
|| name == "pg_toast"
|| name == "pg_temp_1"
|| name == "pg_toast_temp_1"
return name.startsWith("pg_")
|| name == "information_schema";
}