Generalise CrudModel to mapping-based editing
This commit is contained in:
parent
35eac713ff
commit
bbe52c9db8
2 changed files with 174 additions and 91 deletions
|
|
@ -4,6 +4,7 @@
|
||||||
#include "catalog/PgAttribute.h"
|
#include "catalog/PgAttribute.h"
|
||||||
#include "catalog/PgAttributeContainer.h"
|
#include "catalog/PgAttributeContainer.h"
|
||||||
#include "catalog/PgConstraintContainer.h"
|
#include "catalog/PgConstraintContainer.h"
|
||||||
|
#include "catalog/PgClassContainer.h"
|
||||||
#include "SqlFormattingUtils.h"
|
#include "SqlFormattingUtils.h"
|
||||||
#include "Pgsql_oids.h"
|
#include "Pgsql_oids.h"
|
||||||
#include "CustomDataRole.h"
|
#include "CustomDataRole.h"
|
||||||
|
|
@ -39,12 +40,23 @@ void CrudModel::setConfig(std::shared_ptr<OpenDatabase> db, const PgClass &table
|
||||||
{
|
{
|
||||||
m_database = db;
|
m_database = db;
|
||||||
m_table = table;
|
m_table = table;
|
||||||
m_primaryKey = db->catalog()->constraints()->getPrimaryForRelation(table.oid());
|
m_wholeTable = true;
|
||||||
callLoadData = true;
|
callLoadData = true;
|
||||||
auto dbconfig = m_database->config();
|
auto dbconfig = m_database->config();
|
||||||
m_dbConn.setupConnection(dbconfig);
|
m_dbConn.setupConnection(dbconfig);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CrudModel::setResult(std::shared_ptr<OpenDatabase> db, std::shared_ptr<Pgsql::Result> result)
|
||||||
|
{
|
||||||
|
m_database = db;
|
||||||
|
m_table.reset();
|
||||||
|
m_wholeTable = false;
|
||||||
|
callLoadData = false;
|
||||||
|
auto dbconfig = m_database->config();
|
||||||
|
m_dbConn.setupConnection(dbconfig);
|
||||||
|
loadIntoModel(result);
|
||||||
|
}
|
||||||
|
|
||||||
QVariant CrudModel::headerData(int section, Qt::Orientation orientation, int role) const
|
QVariant CrudModel::headerData(int section, Qt::Orientation orientation, int role) const
|
||||||
{
|
{
|
||||||
if (section == 0)
|
if (section == 0)
|
||||||
|
|
@ -232,9 +244,11 @@ void CrudModel::loadIntoModel(std::shared_ptr<Pgsql::Result> data)
|
||||||
m_pendingRowList.clear();
|
m_pendingRowList.clear();
|
||||||
m_roData = data;
|
m_roData = data;
|
||||||
initializeColumnList();
|
initializeColumnList();
|
||||||
|
m_mapping.emplace(createResultSourceMapping(*m_roData, *m_database->catalog()));
|
||||||
lastRowKey = data->rows() - 1;
|
lastRowKey = data->rows() - 1;
|
||||||
initRowMapping();
|
initRowMapping();
|
||||||
appendNewRowInternal();
|
if (m_wholeTable)
|
||||||
|
appendNewRowInternal();
|
||||||
endResetModel();
|
endResetModel();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -277,7 +291,7 @@ Qt::ItemFlags CrudModel::flags(const QModelIndex &index) const
|
||||||
if (index.column() < PreColumnCount)
|
if (index.column() < PreColumnCount)
|
||||||
return flags;
|
return flags;
|
||||||
|
|
||||||
if (m_primaryKey && !columnIsReadOnly(index.column()))
|
if (!columnIsReadOnly(index.column()))
|
||||||
flags |= Qt::ItemIsEditable;
|
flags |= Qt::ItemIsEditable;
|
||||||
|
|
||||||
return flags;
|
return flags;
|
||||||
|
|
@ -312,22 +326,17 @@ bool CrudModel::setData(const QModelIndex &index, const QVariant &value, int rol
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::tuple<bool, std::vector<CrudModel::Value>> CrudModel::saveRow(const PendingRow &pending_row)
|
std::tuple<bool, std::vector<CrudModel::Value>> CrudModel::saveRow(const PendingRow &pending_row, const RowMapping &row_mapping)
|
||||||
{
|
{
|
||||||
auto data = pending_row.data();
|
auto data = pending_row.data();
|
||||||
RowMapping& rowmapping = m_rowMapping[pending_row.row()];
|
if (data.empty())
|
||||||
|
return { true, {} };
|
||||||
|
|
||||||
if (!data.empty())
|
if (row_mapping.isNew())
|
||||||
{
|
{
|
||||||
QString buffer;
|
QString buffer;
|
||||||
Pgsql::Params params;
|
Pgsql::Params params;
|
||||||
if (rowmapping.isNew())
|
std::tie(buffer, params) = createInsertQuery(pending_row);
|
||||||
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;
|
Pgsql::Connection db_update_conn;
|
||||||
auto dbconfig = m_database->config();
|
auto dbconfig = m_database->config();
|
||||||
|
|
@ -356,59 +365,120 @@ std::tuple<bool, std::vector<CrudModel::Value>> CrudModel::saveRow(const Pending
|
||||||
msgBox.setText(ex.what());
|
msgBox.setText(ex.what());
|
||||||
msgBox.exec();
|
msgBox.exec();
|
||||||
}
|
}
|
||||||
|
return { false, {} };
|
||||||
|
}
|
||||||
|
|
||||||
|
return saveUpdate(pending_row, row_mapping);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::tuple<bool, std::vector<CrudModel::Value>> CrudModel::saveUpdate(const PendingRow &pending_row, const RowMapping &row_mapping)
|
||||||
|
{
|
||||||
|
std::set<Oid> tables;
|
||||||
|
for (const auto &e : pending_row.data())
|
||||||
|
tables.insert(m_mapping->tableOid(e.first));
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Pgsql::Connection db_update_conn;
|
||||||
|
auto dbconfig = m_database->config();
|
||||||
|
db_update_conn.connect(dbconfig.connectionString());
|
||||||
|
auto tx = Pgsql::Transaction::startTransaction(db_update_conn);
|
||||||
|
for (Oid table_oid : tables)
|
||||||
|
{
|
||||||
|
QString buffer;
|
||||||
|
Pgsql::Params params;
|
||||||
|
std::tie(buffer, params) = createUpdateQuery(table_oid, pending_row, row_mapping);
|
||||||
|
auto result = tx.queryParam(buffer, params);
|
||||||
|
if (!result || result.tuplesAffected() != 1)
|
||||||
|
{
|
||||||
|
QMessageBox::warning(nullptr, tr("Update failed"), tr("The change did not update exactly one row in %1, the row may have changed or is no longer unique.").arg(tableName(table_oid)));
|
||||||
|
return { false, {} };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tx.commit();
|
||||||
|
|
||||||
|
std::vector<Value> values;
|
||||||
|
values.reserve(m_roData->cols());
|
||||||
|
for (int col = 0; col < m_roData->cols(); ++col)
|
||||||
|
{
|
||||||
|
auto &&pending = pending_row.data();
|
||||||
|
auto found = pending.find(col);
|
||||||
|
values.push_back(found != pending.end() ? found->second : getSavedData(row_mapping, col));
|
||||||
|
}
|
||||||
|
return { true, values };
|
||||||
|
}
|
||||||
|
catch (const Pgsql::PgResultError &ex)
|
||||||
|
{
|
||||||
|
QMessageBox msgBox;
|
||||||
|
msgBox.setText(ex.what());
|
||||||
|
msgBox.exec();
|
||||||
}
|
}
|
||||||
return { false, {} };
|
return { false, {} };
|
||||||
}
|
}
|
||||||
|
|
||||||
Pgsql::Params CrudModel::getPKeyParamsForRow(int row) const
|
QString CrudModel::tableName(Oid table_oid) const
|
||||||
{
|
{
|
||||||
Pgsql::Params params;
|
auto table = m_database->catalog()->classes()->getByKey(table_oid);
|
||||||
for (auto attnum : m_primaryKey->key)
|
return table ? table->fullyQualifiedQuotedObjectName() : QString();
|
||||||
{
|
|
||||||
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
|
std::vector<int> CrudModel::resultColumnsOfTable(Oid table_oid) const
|
||||||
{
|
{
|
||||||
return m_roData->getColName(col);
|
std::vector<int> cols;
|
||||||
|
for (int col = 0; col < m_roData->cols(); ++col)
|
||||||
|
if (m_roData->ftable(col) == table_oid && m_roData->ftableCol(col) > 0)
|
||||||
|
cols.push_back(col);
|
||||||
|
return cols;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::tuple<QString, Pgsql::Params> CrudModel::createUpdateQuery(const Pgsql::Params &pkey_params, const PendingRow &pending_row)
|
QString CrudModel::attributeName(int col) const
|
||||||
{
|
{
|
||||||
Pgsql::Params params;
|
if (col < static_cast<int>(columnList.size()) && columnList[col])
|
||||||
auto data = pending_row.data();
|
return columnList[col]->name;
|
||||||
QString table_name = m_table->fullyQualifiedQuotedObjectName();
|
return QString::fromUtf8(m_roData->getColName(col));
|
||||||
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;
|
|
||||||
params.add(e.second, getType(e.first));
|
|
||||||
}
|
|
||||||
|
|
||||||
q << "\nWHERE ";
|
Pgsql::Params CrudModel::getPKeyParamsForRow(const RowMapping &row_mapping, Oid table_oid) const
|
||||||
int i = 0;
|
{
|
||||||
for (auto attnum : m_primaryKey->key)
|
Pgsql::Params params;
|
||||||
|
for (int col : m_mapping->primaryKeyColumns(table_oid))
|
||||||
{
|
{
|
||||||
int col = attNumToCol(attnum);
|
auto s = getSavedData(row_mapping, col);
|
||||||
if (i > 0)
|
params.add(s, getType(col));
|
||||||
q << " AND ";
|
}
|
||||||
q << quoteIdent(columnName(col)) << "=$" << ++param;
|
return params;
|
||||||
++i;
|
}
|
||||||
}
|
|
||||||
params.addParams(pkey_params);
|
std::tuple<QString, Pgsql::Params> CrudModel::createUpdateQuery(Oid table_oid, const PendingRow &pending_row, const RowMapping &row_mapping)
|
||||||
q << "\nRETURNING *";
|
{
|
||||||
q.flush();
|
Pgsql::Params params;
|
||||||
return { buffer, params };
|
QString table_name = tableName(table_oid);
|
||||||
|
QString buffer;
|
||||||
|
QTextStream q(&buffer);
|
||||||
|
q << "UPDATE " << table_name << " AS d\n SET ";
|
||||||
|
int param = 0;
|
||||||
|
for (auto& e : pending_row.data())
|
||||||
|
{
|
||||||
|
if (m_mapping->tableOid(e.first) != table_oid)
|
||||||
|
continue;
|
||||||
|
if (param > 0)
|
||||||
|
q << ",";
|
||||||
|
q << quoteIdent(attributeName(e.first)) << "=$" << ++param;
|
||||||
|
params.add(e.second, getType(e.first));
|
||||||
|
}
|
||||||
|
|
||||||
|
q << "\nWHERE ";
|
||||||
|
int i = 0;
|
||||||
|
for (int col : m_mapping->primaryKeyColumns(table_oid))
|
||||||
|
{
|
||||||
|
if (i > 0)
|
||||||
|
q << " AND ";
|
||||||
|
q << quoteIdent(attributeName(col)) << "=$" << ++param;
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
params.addParams(getPKeyParamsForRow(row_mapping, table_oid));
|
||||||
|
q.flush();
|
||||||
|
return { buffer, params };
|
||||||
}
|
}
|
||||||
|
|
||||||
std::tuple<QString, Pgsql::Params> CrudModel::createInsertQuery(const PendingRow &pending_row)
|
std::tuple<QString, Pgsql::Params> CrudModel::createInsertQuery(const PendingRow &pending_row)
|
||||||
|
|
@ -424,12 +494,11 @@ std::tuple<QString, Pgsql::Params> CrudModel::createInsertQuery(const PendingRow
|
||||||
for (const auto& e : data)
|
for (const auto& e : data)
|
||||||
{
|
{
|
||||||
int num = e.first;
|
int num = e.first;
|
||||||
auto&& column = columnList[num];
|
|
||||||
if (first)
|
if (first)
|
||||||
first = false;
|
first = false;
|
||||||
else
|
else
|
||||||
q << ",";
|
q << ",";
|
||||||
q << quoteIdent(column.name);
|
q << quoteIdent(attributeName(num));
|
||||||
}
|
}
|
||||||
q << ") VALUES ($1";
|
q << ") VALUES ($1";
|
||||||
for (size_t p = 2; p <= data.size(); ++p)
|
for (size_t p = 2; p <= data.size(); ++p)
|
||||||
|
|
@ -442,19 +511,6 @@ std::tuple<QString, Pgsql::Params> CrudModel::createInsertQuery(const PendingRow
|
||||||
return { buffer, params };
|
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
|
QString CrudModel::createDeleteStatement() const
|
||||||
{
|
{
|
||||||
|
|
@ -465,12 +521,11 @@ QString CrudModel::createDeleteStatement() const
|
||||||
q << "DELETE FROM " << table_name;
|
q << "DELETE FROM " << table_name;
|
||||||
q << "\nWHERE ";
|
q << "\nWHERE ";
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for (auto attnum : m_primaryKey->key)
|
for (int col : m_mapping->primaryKeyColumns(m_table->oid()))
|
||||||
{
|
{
|
||||||
const int col = attNumToCol(attnum);
|
|
||||||
if (i > 0)
|
if (i > 0)
|
||||||
q << " AND ";
|
q << " AND ";
|
||||||
q << quoteIdent(columnName(col)) << "=$" << ++i;
|
q << quoteIdent(attributeName(col)) << "=$" << ++i;
|
||||||
}
|
}
|
||||||
|
|
||||||
q.flush();
|
q.flush();
|
||||||
|
|
@ -483,7 +538,13 @@ bool CrudModel::savePendingChanges()
|
||||||
while (!m_pendingRowList.m_rows.empty())
|
while (!m_pendingRowList.m_rows.empty())
|
||||||
{
|
{
|
||||||
auto iter = m_pendingRowList.m_rows.begin();
|
auto iter = m_pendingRowList.m_rows.begin();
|
||||||
auto [ok, modified_row] = saveRow(iter->second);
|
int pendingKey = iter->first;
|
||||||
|
auto mapping_iter = std::find_if(m_rowMapping.begin(), m_rowMapping.end(),
|
||||||
|
[pendingKey](const RowMapping &rhs) -> bool { return rhs.rowKey == pendingKey; });
|
||||||
|
if (mapping_iter == m_rowMapping.end())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
auto [ok, modified_row] = saveRow(iter->second, *mapping_iter);
|
||||||
if (ok)
|
if (ok)
|
||||||
{
|
{
|
||||||
int rowKey = iter->first;
|
int rowKey = iter->first;
|
||||||
|
|
@ -516,9 +577,13 @@ bool CrudModel::columnIsReadOnly(int column_index) const
|
||||||
if (m_roData == nullptr)
|
if (m_roData == nullptr)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
auto&& column = columnList[column_index - PreColumnCount];
|
int col = column_index - PreColumnCount;
|
||||||
return column.getIdentity() == PgAttribute::Identity::Always
|
if (!m_mapping || !m_mapping->isEditable(col))
|
||||||
|| column.getGenerated() != PgAttribute::Generated::None;
|
return true;
|
||||||
|
|
||||||
|
const auto &column = columnList[col];
|
||||||
|
return !column || column->getIdentity() == PgAttribute::Identity::Always
|
||||||
|
|| column->getGenerated() != PgAttribute::Generated::None;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CrudModel::initializeColumnList()
|
void CrudModel::initializeColumnList()
|
||||||
|
|
@ -526,13 +591,20 @@ void CrudModel::initializeColumnList()
|
||||||
columnList.clear();
|
columnList.clear();
|
||||||
columnList.reserve(m_roData->cols());
|
columnList.reserve(m_roData->cols());
|
||||||
auto atts = m_database->catalog()->attributes();
|
auto atts = m_database->catalog()->attributes();
|
||||||
auto columns = atts->getColumnsForRelation(m_table->oid());
|
|
||||||
for (int col = 0; col < m_roData->cols(); ++col)
|
for (int col = 0; col < m_roData->cols(); ++col)
|
||||||
{
|
{
|
||||||
int attnum = m_roData->ftableCol(col);
|
int attnum = m_roData->ftableCol(col);
|
||||||
|
if (attnum <= 0)
|
||||||
|
{
|
||||||
|
columnList.emplace_back();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
auto columns = atts->getColumnsForRelation(m_roData->ftable(col));
|
||||||
auto find_result = std::find_if(columns.begin(), columns.end(), [attnum](const PgAttribute &att) { return att.num == attnum; });
|
auto find_result = std::find_if(columns.begin(), columns.end(), [attnum](const PgAttribute &att) { return att.num == attnum; });
|
||||||
assert(find_result != columns.end());
|
if (find_result != columns.end())
|
||||||
columnList.push_back(*find_result);
|
columnList.emplace_back(*find_result);
|
||||||
|
else
|
||||||
|
columnList.emplace_back();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -548,6 +620,9 @@ void CrudModel::revert()
|
||||||
|
|
||||||
void CrudModel::appendNewRow()
|
void CrudModel::appendNewRow()
|
||||||
{
|
{
|
||||||
|
if (!m_wholeTable)
|
||||||
|
return;
|
||||||
|
|
||||||
int row = static_cast<int>(m_rowMapping.size());
|
int row = static_cast<int>(m_rowMapping.size());
|
||||||
beginInsertRows(QModelIndex(), row, row);
|
beginInsertRows(QModelIndex(), row, row);
|
||||||
appendNewRowInternal();
|
appendNewRowInternal();
|
||||||
|
|
@ -565,6 +640,8 @@ std::tuple<bool, QString> CrudModel::removeRows(const std::set<IntegerRange<int>
|
||||||
return { true, "" };
|
return { true, "" };
|
||||||
if (row_ranges.rbegin()->end() > static_cast<int>(m_rowMapping.size()))
|
if (row_ranges.rbegin()->end() > static_cast<int>(m_rowMapping.size()))
|
||||||
return { false, "Range error" };
|
return { false, "Range error" };
|
||||||
|
if (!m_wholeTable || !m_table || m_mapping->primaryKeyColumns(m_table->oid()).empty())
|
||||||
|
return { false, "Rows can only be removed from a table with a primary key" };
|
||||||
// When removing rows there is no direct mapping anymore between the rows in the grid
|
// When removing rows there is no direct mapping anymore between the rows in the grid
|
||||||
// and the rows in m_roData
|
// and the rows in m_roData
|
||||||
|
|
||||||
|
|
@ -589,7 +666,7 @@ std::tuple<bool, QString> CrudModel::removeRows(const std::set<IntegerRange<int>
|
||||||
for (int current_row = range.start(); current_row < range.end(); ++current_row)
|
for (int current_row = range.start(); current_row < range.end(); ++current_row)
|
||||||
{
|
{
|
||||||
auto&& mapping = m_rowMapping[static_cast<size_t>(current_row)];
|
auto&& mapping = m_rowMapping[static_cast<size_t>(current_row)];
|
||||||
auto params = getPKeyParamsForRow(mapping.rowKey);
|
auto params = getPKeyParamsForRow(mapping, m_table->oid());
|
||||||
if (!params.empty())
|
if (!params.empty())
|
||||||
db_update_conn.queryParam(delete_statement, params);
|
db_update_conn.queryParam(delete_statement, params);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,8 +5,10 @@
|
||||||
#include "Pgsql_Connection.h"
|
#include "Pgsql_Connection.h"
|
||||||
|
|
||||||
#include "IntegerRange.h"
|
#include "IntegerRange.h"
|
||||||
|
#include "ResultSourceMapping.h"
|
||||||
#include "catalog/PgClass.h"
|
#include "catalog/PgClass.h"
|
||||||
#include "catalog/PgConstraint.h"
|
#include "catalog/PgConstraint.h"
|
||||||
|
#include "catalog/PgAttribute.h"
|
||||||
#include <QAbstractTableModel>
|
#include <QAbstractTableModel>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
@ -26,6 +28,8 @@ public:
|
||||||
~CrudModel() override;
|
~CrudModel() override;
|
||||||
|
|
||||||
void setConfig(std::shared_ptr<OpenDatabase> db, const PgClass &table);
|
void setConfig(std::shared_ptr<OpenDatabase> db, const PgClass &table);
|
||||||
|
/// Show an arbitrary query result, cells are editable where the result maps them to a table with a primary key
|
||||||
|
void setResult(std::shared_ptr<OpenDatabase> db, std::shared_ptr<Pgsql::Result> result);
|
||||||
|
|
||||||
|
|
||||||
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
|
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
|
||||||
|
|
@ -50,7 +54,6 @@ public slots:
|
||||||
virtual void revert() override;
|
virtual void revert() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
using PKeyValues = std::vector<std::string>;
|
|
||||||
|
|
||||||
const int PreColumnCount = 1;
|
const int PreColumnCount = 1;
|
||||||
|
|
||||||
|
|
@ -76,6 +79,7 @@ private:
|
||||||
res += " NULLS FIRST";
|
res += " NULLS FIRST";
|
||||||
else if (nulls == NullSorting::Last)
|
else if (nulls == NullSorting::Last)
|
||||||
res += " NULLS LAST";
|
res += " NULLS LAST";
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
@ -205,10 +209,11 @@ private:
|
||||||
|
|
||||||
std::shared_ptr<OpenDatabase> m_database;
|
std::shared_ptr<OpenDatabase> m_database;
|
||||||
std::optional<PgClass> m_table;
|
std::optional<PgClass> m_table;
|
||||||
std::optional<PgConstraint> m_primaryKey;
|
std::optional<ResultSourceMapping> m_mapping;
|
||||||
ASyncDBConnection m_dbConn;
|
ASyncDBConnection m_dbConn;
|
||||||
|
|
||||||
bool callLoadData = false;
|
bool callLoadData = false;
|
||||||
|
bool m_wholeTable = false; ///< true when showing a whole table (CRUD), false for an arbitrary query result
|
||||||
|
|
||||||
std::shared_ptr<Pgsql::Result> m_roData;
|
std::shared_ptr<Pgsql::Result> m_roData;
|
||||||
|
|
||||||
|
|
@ -218,7 +223,7 @@ private:
|
||||||
// identity allways or generated
|
// identity allways or generated
|
||||||
};
|
};
|
||||||
|
|
||||||
std::vector<PgAttribute> columnList; // list of columnMeta 1 to 1 with columns in m_roData.
|
std::vector<std::optional<PgAttribute>> columnList; // attribute per column in m_roData, empty for expression columns
|
||||||
|
|
||||||
PendingRowList m_pendingRowList;
|
PendingRowList m_pendingRowList;
|
||||||
|
|
||||||
|
|
@ -238,29 +243,30 @@ private:
|
||||||
|
|
||||||
Oid getType(int column) const;
|
Oid getType(int column) const;
|
||||||
|
|
||||||
QString columnName(int col) const;
|
|
||||||
|
|
||||||
/// Get the PKey values from when the row was last restored or retrieved.
|
/// Get the PKey values from when the row was last restored or retrieved.
|
||||||
Pgsql::Params getPKeyParamsForRow(int row) const;
|
Pgsql::Params getPKeyParamsForRow(const RowMapping &row_mapping, Oid table_oid) const;
|
||||||
|
|
||||||
bool savePendingChanges();
|
bool savePendingChanges();
|
||||||
|
|
||||||
std::tuple<QString, Pgsql::Params> createUpdateQuery(const Pgsql::Params &pkey_params, const PendingRow &pending_row);
|
std::tuple<QString, Pgsql::Params> createUpdateQuery(Oid table_oid, const PendingRow &pending_row, const RowMapping &row_mapping);
|
||||||
std::tuple<QString, Pgsql::Params> createInsertQuery(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;
|
QString createDeleteStatement() const;
|
||||||
std::tuple<bool, std::vector<Value>> saveRow(const PendingRow &pending_row);
|
std::tuple<bool, std::vector<Value>> saveRow(const PendingRow &pending_row, const RowMapping &row_mapping);
|
||||||
|
std::tuple<bool, std::vector<Value>> saveUpdate(const PendingRow &pending_row, const RowMapping &row_mapping);
|
||||||
|
|
||||||
void appendNewRow();
|
void appendNewRow();
|
||||||
void appendNewRowInternal();
|
void appendNewRowInternal();
|
||||||
|
|
||||||
int lastRowKey = -1;
|
int lastRowKey = -1;
|
||||||
int allocNewRowKey() { return ++lastRowKey; }
|
int allocNewRowKey() { return ++lastRowKey; }
|
||||||
/// Convert an attnum from the database catalog to the corresponding column in the model
|
/// Name of the table with the given oid, quoted and schema qualified
|
||||||
///
|
QString tableName(Oid table_oid) const;
|
||||||
/// \todo still assumes columns are in order, all being shown and no special column like oid shown.
|
/// Result columns that are user columns (attnum > 0) of the given table, in result order
|
||||||
int attNumToCol(int attnum) const { return attnum - 1; }
|
std::vector<int> resultColumnsOfTable(Oid table_oid) const;
|
||||||
|
/// Catalog name of the attribute behind a result column, falls back to the result column name
|
||||||
|
QString attributeName(int col) const;
|
||||||
|
|
||||||
void RemoveRangesOfRowsFromModel(const std::set<IntegerRange<int>> &row_ranges);
|
void RemoveRangesOfRowsFromModel(const std::set<IntegerRange<int>> &row_ranges);
|
||||||
void RemoveRangeOfRowsFromModel(IntegerRange<int> row_range);
|
void RemoveRangeOfRowsFromModel(IntegerRange<int> row_range);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue