Improvements to the CrudModel
The new data of modified rows is now stored directly within the row_mapping also changed how new rows are handled so the new empty row for inserting is not a special case but is part of the list.
This commit is contained in:
parent
a7f247bdee
commit
06504ecc1f
9 changed files with 213 additions and 317 deletions
532
pglab/crud/CrudModel.cpp
Normal file
532
pglab/crud/CrudModel.cpp
Normal file
|
|
@ -0,0 +1,532 @@
|
|||
#include "CrudModel.h"
|
||||
#include "OpenDatabase.h"
|
||||
#include "catalog/PgDatabaseCatalog.h"
|
||||
#include "catalog/PgAttribute.h"
|
||||
#include "catalog/PgAttributeContainer.h"
|
||||
#include "catalog/PgConstraintContainer.h"
|
||||
#include "SqlFormattingUtils.h"
|
||||
#include "Pgsql_oids.h"
|
||||
#include <QtConcurrent>
|
||||
#include <QFuture>
|
||||
#include <QFutureWatcher>
|
||||
#include <QMessageBox>
|
||||
#include "Pgsql_oids.h"
|
||||
#include "Pgsql_PgException.h"
|
||||
#include "Pgsql_Params.h"
|
||||
#include "Pgsql_Transaction.h"
|
||||
#include <string>
|
||||
#include "ScopeGuard.h"
|
||||
|
||||
CrudModel::CrudModel(QObject *parent)
|
||||
: QAbstractTableModel(parent)
|
||||
, m_dbConn()
|
||||
{
|
||||
qDebug("CrudModel created");
|
||||
connect(&m_dbConn, &ASyncDBConnection::onStateChanged, this, &CrudModel::connectionStateChanged);
|
||||
}
|
||||
|
||||
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::setConfig(std::shared_ptr<OpenDatabase> db, const PgClass &table)
|
||||
{
|
||||
m_database = db;
|
||||
m_table = table;
|
||||
m_primaryKey = db->catalog()->constraints()->getPrimaryForRelation(table.oid());
|
||||
//cat->attributes()->getColumnsForRelation()
|
||||
callLoadData = true;
|
||||
auto dbconfig = m_database->config();
|
||||
m_dbConn.setupConnection(dbconfig);
|
||||
}
|
||||
|
||||
QVariant CrudModel::headerData(int section, Qt::Orientation orientation, int role) const
|
||||
{
|
||||
QVariant r;
|
||||
if (role == Qt::DisplayRole) {
|
||||
if (orientation == Qt::Horizontal) {
|
||||
QString s(m_roData->getColName(section));
|
||||
s += "\n";
|
||||
s += getTypeDisplayString(*m_database->catalog(), getType(section));
|
||||
r = s;
|
||||
}
|
||||
else {
|
||||
r = QString::number(section + 1);
|
||||
}
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
int CrudModel::rowCount(const QModelIndex &/*parent*/) const
|
||||
{
|
||||
return (int)m_rowMapping.size();
|
||||
}
|
||||
|
||||
int CrudModel::columnCount(const QModelIndex &/*parent*/) const
|
||||
{
|
||||
int col_count = m_roData ? m_roData->cols() : 0;
|
||||
return col_count;
|
||||
}
|
||||
|
||||
Oid CrudModel::getType(int column) const
|
||||
{
|
||||
return m_roData ? m_roData->type(column) : InvalidOid;
|
||||
}
|
||||
|
||||
CrudModel::Value CrudModel::getLatestData(int columnIndex, int rowIndex) const
|
||||
{
|
||||
if (m_roData) {
|
||||
auto row_mapping = m_rowMapping[rowIndex];
|
||||
|
||||
std::optional<Value> val;
|
||||
if (row_mapping.pending) {
|
||||
val = m_pendingRowList.getValue(columnIndex, row_mapping.rowKey);
|
||||
}
|
||||
if (!val.has_value())
|
||||
{
|
||||
val = getSavedData(row_mapping, columnIndex);
|
||||
}
|
||||
return val.value_or(Value());
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
CrudModel::Value CrudModel::getSavedData(int columnIndex, int rowIndex) const
|
||||
{
|
||||
if (m_roData) {
|
||||
auto row_mapping = m_rowMapping[rowIndex];
|
||||
return getSavedData(row_mapping, columnIndex);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
CrudModel::Value CrudModel::getSavedData(const RowMapping &row_mapping, int columnIndex) const
|
||||
{
|
||||
// First see if we have buffered editted values that still need saving
|
||||
std::optional<Value> val;
|
||||
if (!val.has_value() && row_mapping.isModified()) {
|
||||
val = row_mapping.modifiedValue(columnIndex);
|
||||
}
|
||||
|
||||
// If we did not have pending or modified data
|
||||
if (!val.has_value() && row_mapping.rowKey < m_roData->rows()) {
|
||||
// Then we are going to read the original data.
|
||||
if (!m_roData->null(columnIndex, row_mapping.rowKey)) {
|
||||
val = std::string(m_roData->val(columnIndex, row_mapping.rowKey));
|
||||
}
|
||||
}
|
||||
return val.value_or(Value());
|
||||
}
|
||||
|
||||
QVariant CrudModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
QVariant v;
|
||||
if (role == Qt::EditRole) {
|
||||
auto value = getLatestData(index);
|
||||
if (value) {
|
||||
QString s = QString::fromUtf8(value->c_str());
|
||||
v = s;
|
||||
}
|
||||
|
||||
}
|
||||
else if (role == Qt::DisplayRole) {
|
||||
auto value = getLatestData(index);
|
||||
if (value) {
|
||||
Oid o = m_roData->type(index.column());
|
||||
if (o == Pgsql::bool_oid) {
|
||||
v = *value == "t"; //s = (s == "t") ? "TRUE" : "FALSE";
|
||||
}
|
||||
else {
|
||||
QString s = QString::fromUtf8(value->c_str());
|
||||
if (s.length() > 256) {
|
||||
s.truncate(256);
|
||||
}
|
||||
v = s;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (role == Qt::UserRole) {
|
||||
v = getType(index.column());
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
void CrudModel::loadData()
|
||||
{
|
||||
QString table_name = m_table->fullyQualifiedQuotedObjectName(); // genFQTableName(*m_database->catalog(), *m_table);
|
||||
std::string q = "SELECT * FROM ";
|
||||
q += std::string(table_name.toUtf8().data());
|
||||
m_dbConn.send(q, [this] (Expected<std::shared_ptr<Pgsql::Result>> res, qint64) {
|
||||
if (res.valid()) {
|
||||
auto dbres = res.get();
|
||||
if (dbres && *dbres) {
|
||||
QMetaObject::invokeMethod(this, "loadIntoModel", Qt::QueuedConnection,
|
||||
Q_ARG(std::shared_ptr<Pgsql::Result>, dbres));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void CrudModel::loadIntoModel(std::shared_ptr<Pgsql::Result> data)
|
||||
{
|
||||
beginResetModel();
|
||||
m_pendingRowList.clear();
|
||||
m_roData = data;
|
||||
lastRowKey = data->rows() - 1;
|
||||
initRowMapping();
|
||||
appendNewRow();
|
||||
endResetModel();
|
||||
}
|
||||
|
||||
void CrudModel::initRowMapping()
|
||||
{
|
||||
size_t cnt = m_roData->rows();
|
||||
m_rowMapping.reserve(cnt + 1);
|
||||
for (int i = 0; i < cnt; ++i)
|
||||
m_rowMapping.emplace_back(i);
|
||||
}
|
||||
|
||||
void CrudModel::connectionStateChanged(ASyncDBConnection::State state)
|
||||
{
|
||||
switch (state) {
|
||||
case ASyncDBConnection::State::NotConnected:
|
||||
break;
|
||||
case ASyncDBConnection::State::Connecting:
|
||||
break;
|
||||
case ASyncDBConnection::State::Connected:
|
||||
if (callLoadData) {
|
||||
callLoadData = false;
|
||||
loadData();
|
||||
}
|
||||
break;
|
||||
case ASyncDBConnection::State::QuerySend:
|
||||
|
||||
break;
|
||||
case ASyncDBConnection::State::CancelSend:
|
||||
break;
|
||||
case ASyncDBConnection::State::Terminating:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Qt::ItemFlags CrudModel::flags(const QModelIndex &) const
|
||||
{
|
||||
Qt::ItemFlags flags = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
|
||||
if (m_primaryKey) {
|
||||
flags |= Qt::ItemIsEditable;
|
||||
}
|
||||
return flags;
|
||||
}
|
||||
|
||||
bool CrudModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
||||
{
|
||||
if (role == Qt::EditRole) {
|
||||
int grid_row = index.row();
|
||||
int col = index.column();
|
||||
|
||||
auto& row_mapping = m_rowMapping[grid_row];
|
||||
row_mapping.pending = true;
|
||||
|
||||
Value val;
|
||||
std::string s = value.toString().toUtf8().data();
|
||||
if (!s.empty()) {
|
||||
if (s == "''")
|
||||
s.clear();
|
||||
val = s;
|
||||
}
|
||||
m_pendingRowList.setValue(col, row_mapping.rowKey, val);
|
||||
|
||||
emit dataChanged(index, index, QVector<int>() << role);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
std::tuple<bool, std::vector<CrudModel::Value>> CrudModel::saveRow(const PendingRow &pending_row)
|
||||
{
|
||||
auto data = pending_row.data();
|
||||
RowMapping& rowmapping = m_rowMapping[pending_row.row()];
|
||||
|
||||
if (!data.empty()) {
|
||||
QString buffer;
|
||||
Pgsql::Params params;
|
||||
if (rowmapping.isNew()){
|
||||
std::tie(buffer, params) = createInsertQuery(pending_row);
|
||||
}
|
||||
else {
|
||||
Pgsql::Params pkey_params = getPKeyParamsForRow(pending_row.row());
|
||||
std::tie(buffer, params) = createUpdateQuery(pkey_params, pending_row);
|
||||
}
|
||||
|
||||
Pgsql::Connection db_update_conn;
|
||||
auto dbconfig = m_database->config();
|
||||
db_update_conn.connect(dbconfig.connectionString());
|
||||
try {
|
||||
auto result = db_update_conn.queryParam(buffer, params);
|
||||
if (result && result.rows() == 1) {
|
||||
|
||||
std::vector<Value> values;
|
||||
auto row = *result.begin();
|
||||
for (auto v : row) {
|
||||
if (v.null())
|
||||
values.push_back(Value());
|
||||
else
|
||||
values.push_back(std::string(v.c_str()));
|
||||
}
|
||||
|
||||
return { true, values };
|
||||
}
|
||||
}
|
||||
catch (const Pgsql::PgResultError &ex) {
|
||||
QMessageBox msgBox;
|
||||
msgBox.setText(ex.what());
|
||||
msgBox.exec();
|
||||
}
|
||||
}
|
||||
return { false, {} };
|
||||
}
|
||||
|
||||
Pgsql::Params CrudModel::getPKeyParamsForRow(int row) const
|
||||
{
|
||||
Pgsql::Params params;
|
||||
for (auto attnum : m_primaryKey->key) {
|
||||
const int col = attNumToCol(attnum);
|
||||
Oid t = getType(col);
|
||||
auto s = getSavedData(col, row);
|
||||
params.add(s, t);
|
||||
}
|
||||
return params;
|
||||
}
|
||||
|
||||
QString CrudModel::columnName(int col) const
|
||||
{
|
||||
return m_roData->getColName(col);
|
||||
}
|
||||
|
||||
std::tuple<QString, Pgsql::Params> CrudModel::createUpdateQuery(const Pgsql::Params &pkey_params, const PendingRow &pending_row)
|
||||
{
|
||||
Pgsql::Params params;
|
||||
auto data = pending_row.data();
|
||||
QString table_name = m_table->fullyQualifiedQuotedObjectName(); //genFQTableName(*m_database->catalog(), *m_table);
|
||||
QString buffer;
|
||||
QTextStream q(&buffer);
|
||||
q << "UPDATE " << table_name << " AS d\n SET ";
|
||||
int param = 0;
|
||||
for (auto& e : data) {
|
||||
if (param > 0)
|
||||
q << ",";
|
||||
q << quoteIdent(columnName(e.first)) << "=$" << ++param;
|
||||
|
||||
// Add value to paramlist
|
||||
params.add(e.second, getType(e.first));
|
||||
}
|
||||
|
||||
q << "\nWHERE ";
|
||||
int i = 0;
|
||||
for (auto attnum : m_primaryKey->key) {
|
||||
int col = attNumToCol(attnum);
|
||||
if (i > 0)
|
||||
q << " AND ";
|
||||
q << quoteIdent(columnName(col)) << "=$" << ++param;
|
||||
++i;
|
||||
}
|
||||
params.addParams(pkey_params);
|
||||
q << "\nRETURNING *";
|
||||
q.flush();
|
||||
return { buffer, params };
|
||||
}
|
||||
|
||||
std::tuple<QString, Pgsql::Params> CrudModel::createInsertQuery(const PendingRow &pending_row)
|
||||
{
|
||||
Pgsql::Params params;
|
||||
auto data = pending_row.data();
|
||||
QString table_name = m_table->fullyQualifiedQuotedObjectName(); // genFQTableName(*m_database->catalog(), *m_table);
|
||||
QString buffer;
|
||||
QTextStream q(&buffer);
|
||||
q << "INSERT INTO " << table_name << "(";
|
||||
|
||||
auto columns = m_database->catalog()->attributes()->getColumnsForRelation(m_table->oid());
|
||||
bool first = true;
|
||||
for (const auto& e : data) {
|
||||
int num = e.first + 1;
|
||||
auto find_res = std::find_if(columns.begin(), columns.end(),
|
||||
[num] (const auto &elem) -> bool { return num == elem.num; });
|
||||
if (find_res != columns.end()) {
|
||||
if (first) first = false;
|
||||
else q << ",";
|
||||
q << find_res->name;
|
||||
}
|
||||
}
|
||||
q << ") VALUES ($1";
|
||||
for (size_t p = 2; p <= data.size(); ++p)
|
||||
q << ",$" << p;
|
||||
q << ") RETURNING *";
|
||||
for (auto e : data) {
|
||||
params.add(e.second, getType(e.first));
|
||||
}
|
||||
q.flush();
|
||||
return { buffer, params };
|
||||
}
|
||||
|
||||
std::tuple<QString, Pgsql::Params> CrudModel::createDeleteStatement(const PKeyValues &pkey_values)
|
||||
{
|
||||
Pgsql::Params params;
|
||||
size_t i = 0;
|
||||
for (auto attnum : m_primaryKey->key) {
|
||||
const int col = attNumToCol(attnum);
|
||||
params.add(pkey_values[i].c_str(), getType(col));
|
||||
++i;
|
||||
}
|
||||
|
||||
return { createDeleteStatement(), params };
|
||||
}
|
||||
|
||||
QString CrudModel::createDeleteStatement() const
|
||||
{
|
||||
Pgsql::Params params;
|
||||
QString table_name = m_table->fullyQualifiedQuotedObjectName();
|
||||
QString buffer;
|
||||
QTextStream q(&buffer);
|
||||
q << "DELETE FROM " << table_name;
|
||||
q << "\nWHERE ";
|
||||
int i = 0;
|
||||
for (auto attnum : m_primaryKey->key) {
|
||||
const int col = attNumToCol(attnum);
|
||||
if (i > 0)
|
||||
q << " AND ";
|
||||
q << quoteIdent(columnName(col)) << "=$" << ++i;
|
||||
}
|
||||
|
||||
q.flush();
|
||||
return buffer;
|
||||
}
|
||||
|
||||
|
||||
bool CrudModel::savePendingChanges()
|
||||
{
|
||||
while (!m_pendingRowList.m_rows.empty()) {
|
||||
auto iter = m_pendingRowList.m_rows.begin();
|
||||
auto [ok, modified_row] = saveRow(iter->second);
|
||||
if (ok) {
|
||||
int rowKey = iter->first;
|
||||
m_pendingRowList.m_rows.erase(iter);
|
||||
|
||||
auto iter = std::find_if(m_rowMapping.begin(), m_rowMapping.end(),
|
||||
[rowKey](const RowMapping &rhs) -> bool { return rhs.rowKey == rowKey; });
|
||||
if (iter != m_rowMapping.end()) {
|
||||
iter->setModifiedRowData(modified_row);
|
||||
iter->pending = false;
|
||||
if (IsLastRow(iter))
|
||||
{
|
||||
appendNewRow();
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CrudModel::IsLastRow(RowMappingVector::iterator mapping_iter) const
|
||||
{
|
||||
return mapping_iter == --m_rowMapping.end();
|
||||
}
|
||||
|
||||
bool CrudModel::submit()
|
||||
{
|
||||
return savePendingChanges();
|
||||
}
|
||||
|
||||
void CrudModel::revert()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void CrudModel::appendNewRow()
|
||||
{
|
||||
int row = m_rowMapping.size();
|
||||
beginInsertRows(QModelIndex(), row, row);
|
||||
m_rowMapping.emplace_back(allocNewRowKey(), std::vector<Value>(m_roData->cols()));
|
||||
endInsertRows();
|
||||
}
|
||||
|
||||
std::tuple<bool, QString> CrudModel::removeRows(const std::set<IntegerRange<int>> &row_ranges)
|
||||
{
|
||||
if (row_ranges.empty()) return { true, "" };
|
||||
if (row_ranges.rbegin()->end() > static_cast<int>(m_rowMapping.size())) return { false, "Range error" };
|
||||
// When removing rows there is no direct mapping anymore between the rows in the grid
|
||||
// and the rows in m_roData
|
||||
|
||||
// Therefor we need an indirection to keep track of which rows are visible and which
|
||||
// grid row maps to what data row. Maybe we can also keep track where the current data
|
||||
// of the row is located original data, pending data or modified data
|
||||
|
||||
// 1. Get PKEY and remove that row from table
|
||||
|
||||
try {
|
||||
Pgsql::Connection db_update_conn;
|
||||
auto dbconfig = m_database->config();
|
||||
db_update_conn.connect(dbconfig.connectionString());
|
||||
|
||||
// First delete rows in table
|
||||
QString delete_statement = createDeleteStatement();
|
||||
{
|
||||
db_update_conn.query("BEGIN;");
|
||||
auto tx = Pgsql::Transaction::startTransaction(db_update_conn);
|
||||
for (auto range : row_ranges) {
|
||||
for (int current_row = range.start(); current_row < range.end(); ++current_row) {
|
||||
auto&& mapping = m_rowMapping[static_cast<size_t>(current_row)];
|
||||
auto params = getPKeyParamsForRow(mapping.rowKey);
|
||||
if (!params.empty()) {
|
||||
// Execute DELETE
|
||||
db_update_conn.queryParam(delete_statement, params);
|
||||
}
|
||||
}
|
||||
}
|
||||
tx.commit();
|
||||
// If something goes wrong after this commit we should reload contents of model
|
||||
}
|
||||
// Then from model
|
||||
RemoveRangesOfRowsFromModel(row_ranges);
|
||||
return { true, "" };
|
||||
} catch (const Pgsql::PgResultError &error) {
|
||||
return { false, QString::fromUtf8(error.details().messageDetail.c_str()) };
|
||||
}
|
||||
}
|
||||
|
||||
bool CrudModel::removeRows(int row, int count, const QModelIndex &)
|
||||
{
|
||||
if (m_rowMapping.empty()) return false;
|
||||
|
||||
IntegerRange<int> range(row, count);
|
||||
auto [res, message] = removeRows({ range });
|
||||
return res;
|
||||
}
|
||||
|
||||
void CrudModel::RemoveRangesOfRowsFromModel(const std::set<IntegerRange<int>> &row_ranges)
|
||||
{
|
||||
int rows_deleted = 0;
|
||||
for (auto range : row_ranges) {
|
||||
range.setStart(range.start() - rows_deleted);
|
||||
RemoveRangeOfRowsFromModel(range);
|
||||
rows_deleted += range.length();
|
||||
}
|
||||
}
|
||||
|
||||
void CrudModel::RemoveRangeOfRowsFromModel(IntegerRange<int> range)
|
||||
{
|
||||
beginRemoveRows(QModelIndex(), range.start(), range.end() - 1);
|
||||
SCOPE_EXIT { endRemoveRows(); };
|
||||
auto first = m_rowMapping.begin() + range.start();
|
||||
m_rowMapping.erase(first, first + range.length());
|
||||
}
|
||||
|
||||
263
pglab/crud/CrudModel.h
Normal file
263
pglab/crud/CrudModel.h
Normal file
|
|
@ -0,0 +1,263 @@
|
|||
#ifndef CRUDMODEL_H
|
||||
#define CRUDMODEL_H
|
||||
|
||||
#include "ASyncDBConnection.h"
|
||||
#include "Pgsql_Connection.h"
|
||||
|
||||
#include "IntegerRange.h"
|
||||
#include "catalog/PgClass.h"
|
||||
#include "catalog/PgConstraint.h"
|
||||
#include <QAbstractTableModel>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <set>
|
||||
#include <tuple>
|
||||
#include <vector>
|
||||
#include <optional>
|
||||
|
||||
class PgConstraint;
|
||||
class OpenDatabase;
|
||||
|
||||
class CrudModel: public QAbstractTableModel {
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit CrudModel(QObject *parent = nullptr);
|
||||
~CrudModel() override;
|
||||
|
||||
void setConfig(std::shared_ptr<OpenDatabase> db, const PgClass &table);
|
||||
|
||||
|
||||
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
|
||||
|
||||
|
||||
// Basic functionality:
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
|
||||
virtual QVariant data(const QModelIndex &index, int role) const override;
|
||||
virtual Qt::ItemFlags flags(const QModelIndex &) const override;
|
||||
|
||||
virtual bool setData(const QModelIndex &index, const QVariant &value, int role) override;
|
||||
|
||||
void loadData();
|
||||
|
||||
std::tuple<bool, QString> removeRows(const std::set<IntegerRange<int>> &row_ranges);
|
||||
bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
|
||||
|
||||
public slots:
|
||||
virtual bool submit() override;
|
||||
virtual void revert() override;
|
||||
|
||||
private:
|
||||
using PKeyValues = std::vector<std::string>;
|
||||
|
||||
|
||||
class ColumnSort {
|
||||
public:
|
||||
enum Direction { Ascending, Descending };
|
||||
enum NullSorting {
|
||||
Default, ///< Behaves like NULL values are larger then non NULL values ASC NULLS LAST or DESC NULLS FIRST
|
||||
First,
|
||||
Last };
|
||||
|
||||
std::string columnName;
|
||||
Direction direction = Direction::Ascending;
|
||||
NullSorting nulls = NullSorting::Default;
|
||||
|
||||
std::string toSql() const
|
||||
{
|
||||
std::string res = columnName;
|
||||
if (direction == Direction::Descending)
|
||||
res += " DESC";
|
||||
if (nulls == NullSorting::First)
|
||||
res += " NULLS FIRST";
|
||||
else if (nulls == NullSorting::Last)
|
||||
res += " NULLS LAST";
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
using Value = std::optional<std::string>;
|
||||
|
||||
/** Similar to a modified row but it only stores values for columns that actually have been edited.
|
||||
*
|
||||
*/
|
||||
class PendingRow {
|
||||
public:
|
||||
using ValueMap = std::map<int, Value>;
|
||||
|
||||
explicit PendingRow(int row)
|
||||
: m_row(row)
|
||||
{}
|
||||
|
||||
const auto& data() const { return m_values; }
|
||||
|
||||
int row() const { return m_row; }
|
||||
|
||||
void setValue(int col, const Value &val)
|
||||
{
|
||||
m_values.insert_or_assign(col, val);
|
||||
}
|
||||
|
||||
private:
|
||||
int m_row;
|
||||
ValueMap m_values;
|
||||
};
|
||||
|
||||
|
||||
class PendingRowList {
|
||||
public:
|
||||
using Map = std::map<int, PendingRow>;
|
||||
|
||||
PendingRow& getRow(int row)
|
||||
{
|
||||
auto iter = m_rows.lower_bound(row);
|
||||
if (iter != m_rows.end() && iter->first == row) {
|
||||
return iter->second;
|
||||
}
|
||||
else {
|
||||
return m_rows.insert(iter, {row, PendingRow(row)})->second;
|
||||
}
|
||||
}
|
||||
|
||||
void setValue(int col, int row, const Value &value)
|
||||
{
|
||||
auto iter = m_rows.find(row);
|
||||
if (iter == m_rows.end()) {
|
||||
iter = m_rows.insert({row, PendingRow(row)}).first;
|
||||
}
|
||||
iter->second.setValue(col, value);
|
||||
}
|
||||
|
||||
std::optional<Value> getValue(int col, int row) const
|
||||
{
|
||||
auto iter = m_rows.find(row);
|
||||
if (iter != m_rows.end()) {
|
||||
auto &r = iter->second;
|
||||
auto cell = r.data().find(col);
|
||||
if (cell != r.data().end())
|
||||
return cell->second;
|
||||
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
auto begin() { return m_rows.begin(); }
|
||||
auto end() { return m_rows.end(); }
|
||||
|
||||
void clear() { m_rows.clear(); }
|
||||
|
||||
Map m_rows;
|
||||
};
|
||||
|
||||
class RowMapping {
|
||||
public:
|
||||
bool pending = false; ///< Row has pending changes, look first in pending for current data
|
||||
|
||||
int rowKey = -1; ///< value to use as key/index into the data lists
|
||||
|
||||
RowMapping() = default;
|
||||
explicit RowMapping(int row_key, std::vector<Value> data)
|
||||
: rowKey(row_key)
|
||||
, modifiedRow(std::move(data))
|
||||
{}
|
||||
|
||||
RowMapping(int row_key)
|
||||
: rowKey(row_key)
|
||||
{}
|
||||
|
||||
bool isModified() const
|
||||
{
|
||||
return !modifiedRow.empty();
|
||||
}
|
||||
|
||||
Value modifiedValue(int columnIndex) const
|
||||
{
|
||||
return modifiedRow[columnIndex];
|
||||
}
|
||||
|
||||
void setModifiedRowData(std::vector<Value> data)
|
||||
{
|
||||
modifiedRow = std::move(data);
|
||||
}
|
||||
|
||||
bool isNew() const
|
||||
{
|
||||
// row in m_roData, thus it is not new
|
||||
if (modifiedRow.empty())
|
||||
return false;
|
||||
|
||||
// if all elements are empty then this is a new row
|
||||
for (auto &c : modifiedRow)
|
||||
if (c.has_value())
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
private:
|
||||
std::vector<Value> modifiedRow;
|
||||
};
|
||||
|
||||
using RowMappingVector = std::vector<RowMapping>;
|
||||
|
||||
std::shared_ptr<OpenDatabase> m_database;
|
||||
std::optional<PgClass> m_table;
|
||||
std::optional<PgConstraint> m_primaryKey;
|
||||
ASyncDBConnection m_dbConn;
|
||||
|
||||
bool callLoadData = false;
|
||||
|
||||
std::shared_ptr<Pgsql::Result> m_roData;
|
||||
|
||||
PendingRowList m_pendingRowList;
|
||||
|
||||
/// Keeps track of mapping grid rows to data rows
|
||||
RowMappingVector m_rowMapping;
|
||||
|
||||
/// call on initial load to fill in the mappings
|
||||
void initRowMapping();
|
||||
|
||||
Value getLatestData(int column, int row) const;
|
||||
Value getLatestData(const QModelIndex &index) const
|
||||
{
|
||||
return getLatestData(index.column(), index.row());
|
||||
}
|
||||
Value getSavedData(int columnIndex, int rowIndex) const;
|
||||
Value getSavedData(const RowMapping &row_mapping, int columnIndex) const;
|
||||
|
||||
Oid getType(int column) const;
|
||||
|
||||
QString columnName(int col) const;
|
||||
|
||||
/// Get the PKey values from when the row was last restored or retrieved.
|
||||
Pgsql::Params getPKeyParamsForRow(int row) const;
|
||||
|
||||
bool savePendingChanges();
|
||||
|
||||
std::tuple<QString, Pgsql::Params> createUpdateQuery(const Pgsql::Params &pkey_params, const PendingRow &pending_row);
|
||||
std::tuple<QString, Pgsql::Params> createInsertQuery(const PendingRow &pending_row);
|
||||
std::tuple<QString, Pgsql::Params> createDeleteStatement(const PKeyValues &pkey_values);
|
||||
|
||||
QString createDeleteStatement() const;
|
||||
std::tuple<bool, std::vector<Value>> saveRow(const PendingRow &pending_row);
|
||||
|
||||
void appendNewRow();
|
||||
|
||||
int lastRowKey = -1;
|
||||
int allocNewRowKey() { return ++lastRowKey; }
|
||||
/// Convert an attnum from the database catalog to the corresponding column in the model
|
||||
///
|
||||
/// \todo still assumes columns are in order, all being shown and no special column like oid shown.
|
||||
int attNumToCol(int attnum) const { return attnum - 1; }
|
||||
|
||||
void RemoveRangesOfRowsFromModel(const std::set<IntegerRange<int>> &row_ranges);
|
||||
void RemoveRangeOfRowsFromModel(IntegerRange<int> row_range);
|
||||
|
||||
bool IsLastRow(RowMappingVector::iterator mapping_iter) const;
|
||||
private slots:
|
||||
|
||||
void loadIntoModel(std::shared_ptr<Pgsql::Result> data);
|
||||
void connectionStateChanged(ASyncDBConnection::State state);
|
||||
};
|
||||
|
||||
#endif // CRUDMODEL_H
|
||||
113
pglab/crud/CrudTab.cpp
Normal file
113
pglab/crud/CrudTab.cpp
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
#include "CrudTab.h"
|
||||
#include "ui_CrudTab.h"
|
||||
#include "CrudModel.h"
|
||||
#include "ResultTableModelUtil.h"
|
||||
#include "PgLabItemDelegate.h"
|
||||
#include "IntegerRange.h"
|
||||
#include "OpenDatabase.h"
|
||||
#include "catalog/PgClassContainer.h"
|
||||
#include "catalog/PgDatabaseCatalog.h"
|
||||
#include <QDebug>
|
||||
#include <QMenu>
|
||||
#include <QMessageBox>
|
||||
#include <iterator>
|
||||
#include <set>
|
||||
|
||||
|
||||
CrudTab::CrudTab(IDatabaseWindow *context, QWidget *parent)
|
||||
: QWidget(parent)
|
||||
, ui(new Ui::CrudTab)
|
||||
, m_context(context)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
|
||||
m_db = context->openDatabase();
|
||||
|
||||
SetTableViewDefault(ui->tableView);
|
||||
|
||||
auto delegate = new PgLabItemDelegate(ui->tableView);
|
||||
ui->tableView->setItemDelegate(delegate);
|
||||
|
||||
m_crudModel = new CrudModel(nullptr);
|
||||
|
||||
m_SortFilterProxy = new QSortFilterProxyModel(this);
|
||||
m_SortFilterProxy->setSourceModel(m_crudModel);
|
||||
ui->tableView->setModel(m_SortFilterProxy);
|
||||
ui->tableView->setSortingEnabled(true);
|
||||
|
||||
ui->tableView->setSelectionMode(QAbstractItemView::ExtendedSelection);
|
||||
ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
|
||||
ui->tableView->addAction(ui->actionRemove_rows);
|
||||
|
||||
auto horizontal_header = ui->tableView->horizontalHeader();
|
||||
horizontal_header->setContextMenuPolicy(Qt::ContextMenuPolicy::CustomContextMenu);
|
||||
connect(horizontal_header, &QHeaderView::customContextMenuRequested,
|
||||
this, &CrudTab::headerCustomContextMenu);
|
||||
|
||||
}
|
||||
|
||||
CrudTab::~CrudTab()
|
||||
{
|
||||
delete m_crudModel;
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void CrudTab::setConfig(Oid oid) //std::shared_ptr<OpenDatabase> db, const PgClass &table)
|
||||
{
|
||||
m_table = *m_db->catalog()->classes()->getByKey(oid);
|
||||
m_crudModel->setConfig(m_db, *m_table);
|
||||
m_context->setTitleForWidget(this, m_table->objectName(), "");
|
||||
}
|
||||
|
||||
void CrudTab::refresh()
|
||||
{
|
||||
m_crudModel->loadData();
|
||||
}
|
||||
|
||||
void CrudTab::on_actionRemove_rows_triggered()
|
||||
{
|
||||
std::set<IntegerRange<int>> row_ranges;
|
||||
auto selection = m_SortFilterProxy->mapSelectionToSource(ui->tableView->selectionModel()->selection());
|
||||
for (auto range : selection) {
|
||||
row_ranges.emplace(range.top(), range.height());
|
||||
}
|
||||
std::set<IntegerRange<int>> merged_ranges;
|
||||
merge_ranges(row_ranges.begin(), row_ranges.end(), std::inserter(merged_ranges, merged_ranges.begin()));
|
||||
|
||||
QString msg = tr("Are you certain you want to remove the following row(s)?");
|
||||
msg += "\n";
|
||||
bool first = true;
|
||||
for (auto range : merged_ranges) {
|
||||
if (first) first = false;
|
||||
else msg += ", ";
|
||||
|
||||
auto s = range.start() + 1, e = range.end();
|
||||
if (s == e)
|
||||
msg += QString("%1").arg(s);
|
||||
else
|
||||
msg += QString("%1 through %2").arg(s).arg(e);
|
||||
|
||||
msg += " ";
|
||||
}
|
||||
auto res = QMessageBox::question(this, "pgLab", msg, QMessageBox::Yes, QMessageBox::No);
|
||||
|
||||
if (res == QMessageBox::Yes) {
|
||||
auto [res, msg] = m_crudModel->removeRows(merged_ranges);
|
||||
if (!res) {
|
||||
QMessageBox::critical(this, "pgLab", msg, QMessageBox::Close);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CrudTab::headerCustomContextMenu(const QPoint &pos)
|
||||
{
|
||||
auto menu = new QMenu(this);
|
||||
|
||||
QAction *action = new QAction(QIcon(":/icons/script_go.png"), tr("Refresh"), this);
|
||||
action->setShortcut(QKeySequence(Qt::Key_F5));
|
||||
connect(action, &QAction::triggered, this, &CrudTab::refresh);
|
||||
menu->addAction(action);
|
||||
|
||||
auto horizontal_header = ui->tableView->horizontalHeader();
|
||||
menu->popup(horizontal_header->mapToGlobal(pos));
|
||||
}
|
||||
45
pglab/crud/CrudTab.h
Normal file
45
pglab/crud/CrudTab.h
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
#ifndef CRUDTAB_H
|
||||
#define CRUDTAB_H
|
||||
|
||||
#include <QSortFilterProxyModel>
|
||||
#include "catalog/PgClass.h"
|
||||
#include "IDatabaseWindow.h"
|
||||
#include <QWidget>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
|
||||
namespace Ui {
|
||||
class CrudTab;
|
||||
}
|
||||
|
||||
class OpenDatabase;
|
||||
class CrudModel;
|
||||
|
||||
class CrudTab : public QWidget {
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit CrudTab(IDatabaseWindow *context, QWidget *parent = nullptr);
|
||||
~CrudTab() override;
|
||||
|
||||
void setConfig(Oid oid);
|
||||
public slots:
|
||||
void refresh();
|
||||
private:
|
||||
Ui::CrudTab *ui;
|
||||
IDatabaseWindow *m_context;
|
||||
|
||||
std::shared_ptr<OpenDatabase> m_db;
|
||||
std::optional<PgClass> m_table;
|
||||
|
||||
CrudModel *m_crudModel = nullptr;
|
||||
QSortFilterProxyModel *m_SortFilterProxy = nullptr;
|
||||
|
||||
void initActions();
|
||||
|
||||
private slots:
|
||||
void on_actionRemove_rows_triggered();
|
||||
void headerCustomContextMenu(const QPoint &pos);
|
||||
};
|
||||
|
||||
|
||||
#endif // CRUDTAB_H
|
||||
35
pglab/crud/CrudTab.ui
Normal file
35
pglab/crud/CrudTab.ui
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
<?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>
|
||||
<action name="actionRemove_rows">
|
||||
<property name="text">
|
||||
<string>Remove rows</string>
|
||||
</property>
|
||||
<property name="shortcut">
|
||||
<string>Ctrl+D</string>
|
||||
</property>
|
||||
<property name="shortcutContext">
|
||||
<enum>Qt::WidgetWithChildrenShortcut</enum>
|
||||
</property>
|
||||
</action>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
||||
Loading…
Add table
Add a link
Reference in a new issue