Bezig met crudmodel/tab
This commit is contained in:
parent
ee321b3fb1
commit
14ab400ccb
15 changed files with 308 additions and 33 deletions
31
pglab/CrudModel.cpp
Normal file
31
pglab/CrudModel.cpp
Normal 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
67
pglab/CrudModel.h
Normal 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
14
pglab/CrudTab.cpp
Normal 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
22
pglab/CrudTab.h
Normal 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
24
pglab/CrudTab.ui
Normal 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>
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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; });
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue