2018-01-08 20:45:52 +01:00
|
|
|
|
#include "CrudModel.h"
|
|
|
|
|
|
#include "OpenDatabase.h"
|
2018-12-16 10:17:59 +01:00
|
|
|
|
#include "catalog/PgDatabaseCatalog.h"
|
|
|
|
|
|
#include "catalog/PgAttribute.h"
|
|
|
|
|
|
#include "catalog/PgAttributeContainer.h"
|
|
|
|
|
|
#include "catalog/PgConstraintContainer.h"
|
2018-01-09 20:39:43 +01:00
|
|
|
|
#include "SqlFormattingUtils.h"
|
2018-02-05 21:42:54 +01:00
|
|
|
|
#include "Pgsql_oids.h"
|
2018-01-09 20:39:43 +01:00
|
|
|
|
#include <QtConcurrent>
|
|
|
|
|
|
#include <QFuture>
|
|
|
|
|
|
#include <QFutureWatcher>
|
2022-01-22 16:22:29 +01:00
|
|
|
|
#include <QMessageBox>
|
2018-01-09 20:39:43 +01:00
|
|
|
|
#include "Pgsql_oids.h"
|
2018-12-15 11:24:58 +01:00
|
|
|
|
#include "Pgsql_PgException.h"
|
2018-02-05 21:42:54 +01:00
|
|
|
|
#include "Pgsql_Params.h"
|
2018-12-15 11:24:58 +01:00
|
|
|
|
#include "Pgsql_Transaction.h"
|
2018-01-08 20:45:52 +01:00
|
|
|
|
#include <string>
|
2018-12-08 13:55:43 +01:00
|
|
|
|
#include "ScopeGuard.h"
|
2018-01-08 20:45:52 +01:00
|
|
|
|
|
2018-12-30 15:46:15 +01:00
|
|
|
|
CrudModel::CrudModel(QObject *parent)
|
|
|
|
|
|
: QAbstractTableModel(parent)
|
2022-01-22 16:22:29 +01:00
|
|
|
|
, m_dbConn()
|
2018-01-09 20:39:43 +01:00
|
|
|
|
{
|
2018-02-05 21:42:54 +01:00
|
|
|
|
qDebug("CrudModel created");
|
2018-01-09 20:39:43 +01:00
|
|
|
|
connect(&m_dbConn, &ASyncDBConnection::onStateChanged, this, &CrudModel::connectionStateChanged);
|
|
|
|
|
|
}
|
2018-01-08 20:45:52 +01:00
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
*/
|
2018-01-09 20:39:43 +01:00
|
|
|
|
void CrudModel::setConfig(std::shared_ptr<OpenDatabase> db, const PgClass &table)
|
2018-01-08 20:45:52 +01:00
|
|
|
|
{
|
|
|
|
|
|
m_database = db;
|
|
|
|
|
|
m_table = table;
|
2018-11-25 19:45:06 +01:00
|
|
|
|
m_primaryKey = db->catalog()->constraints()->getPrimaryForRelation(table.oid());
|
2018-01-08 20:45:52 +01:00
|
|
|
|
//cat->attributes()->getColumnsForRelation()
|
2018-01-09 20:39:43 +01:00
|
|
|
|
callLoadData = true;
|
2018-02-18 07:15:43 +01:00
|
|
|
|
auto dbconfig = m_database->config();
|
|
|
|
|
|
m_dbConn.setupConnection(dbconfig);
|
2018-01-09 20:39:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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";
|
2018-11-25 09:05:01 +01:00
|
|
|
|
s += getTypeDisplayString(*m_database->catalog(), getType(section));
|
2018-01-09 20:39:43 +01:00
|
|
|
|
r = s;
|
|
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
r = QString::number(section + 1);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return r;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-02-05 21:42:54 +01:00
|
|
|
|
int CrudModel::rowCount(const QModelIndex &/*parent*/) const
|
2018-01-09 20:39:43 +01:00
|
|
|
|
{
|
2022-01-22 16:22:29 +01:00
|
|
|
|
return (int)m_rowMapping.size();
|
2018-01-09 20:39:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-02-05 21:42:54 +01:00
|
|
|
|
int CrudModel::columnCount(const QModelIndex &/*parent*/) const
|
2018-01-09 20:39:43 +01:00
|
|
|
|
{
|
2018-02-18 08:37:59 +01:00
|
|
|
|
int col_count = m_roData ? m_roData->cols() : 0;
|
|
|
|
|
|
return col_count;
|
2018-01-09 20:39:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Oid CrudModel::getType(int column) const
|
|
|
|
|
|
{
|
|
|
|
|
|
return m_roData ? m_roData->type(column) : InvalidOid;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-01-22 16:22:29 +01:00
|
|
|
|
CrudModel::Value CrudModel::getLatestData(int columnIndex, int rowIndex) const
|
2018-01-09 20:39:43 +01:00
|
|
|
|
{
|
|
|
|
|
|
if (m_roData) {
|
2022-01-22 16:22:29 +01:00
|
|
|
|
auto row_mapping = m_rowMapping[rowIndex];
|
2018-02-18 08:37:59 +01:00
|
|
|
|
|
2018-11-08 21:50:49 +01:00
|
|
|
|
std::optional<Value> val;
|
2018-04-08 09:02:22 +02:00
|
|
|
|
if (row_mapping.pending) {
|
2022-01-22 16:22:29 +01:00
|
|
|
|
val = m_pendingRowList.getValue(columnIndex, row_mapping.rowKey);
|
2018-02-05 21:42:54 +01:00
|
|
|
|
}
|
2022-01-22 16:22:29 +01:00
|
|
|
|
if (!val.has_value())
|
|
|
|
|
|
{
|
|
|
|
|
|
val = getSavedData(row_mapping, columnIndex);
|
|
|
|
|
|
}
|
|
|
|
|
|
return val.value_or(Value());
|
2018-01-09 20:39:43 +01:00
|
|
|
|
}
|
2022-01-22 16:22:29 +01:00
|
|
|
|
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());
|
2018-01-09 20:39:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QVariant CrudModel::data(const QModelIndex &index, int role) const
|
|
|
|
|
|
{
|
2018-01-15 13:30:30 +01:00
|
|
|
|
QVariant v;
|
2018-02-05 21:42:54 +01:00
|
|
|
|
if (role == Qt::EditRole) {
|
2022-01-22 16:22:29 +01:00
|
|
|
|
auto value = getLatestData(index);
|
2018-02-05 21:42:54 +01:00
|
|
|
|
if (value) {
|
2018-02-18 07:15:43 +01:00
|
|
|
|
QString s = QString::fromUtf8(value->c_str());
|
|
|
|
|
|
v = s;
|
2018-02-05 21:42:54 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (role == Qt::DisplayRole) {
|
2022-01-22 16:22:29 +01:00
|
|
|
|
auto value = getLatestData(index);
|
2018-02-05 21:42:54 +01:00
|
|
|
|
if (value) {
|
|
|
|
|
|
Oid o = m_roData->type(index.column());
|
|
|
|
|
|
if (o == Pgsql::bool_oid) {
|
2018-02-18 08:32:38 +01:00
|
|
|
|
v = *value == "t"; //s = (s == "t") ? "TRUE" : "FALSE";
|
2018-02-05 21:42:54 +01:00
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
QString s = QString::fromUtf8(value->c_str());
|
|
|
|
|
|
if (s.length() > 256) {
|
|
|
|
|
|
s.truncate(256);
|
|
|
|
|
|
}
|
|
|
|
|
|
v = s;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2018-01-09 20:39:43 +01:00
|
|
|
|
}
|
2018-01-15 13:30:30 +01:00
|
|
|
|
else if (role == Qt::UserRole) {
|
|
|
|
|
|
v = getType(index.column());
|
2018-01-09 20:39:43 +01:00
|
|
|
|
}
|
2018-01-15 13:30:30 +01:00
|
|
|
|
return v;
|
2018-01-09 20:39:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CrudModel::loadData()
|
|
|
|
|
|
{
|
2018-11-25 19:45:06 +01:00
|
|
|
|
QString table_name = m_table->fullyQualifiedQuotedObjectName(); // genFQTableName(*m_database->catalog(), *m_table);
|
2018-01-09 20:39:43 +01:00
|
|
|
|
std::string q = "SELECT * FROM ";
|
2018-11-10 11:37:17 +01:00
|
|
|
|
q += std::string(table_name.toUtf8().data());
|
2018-01-09 20:39:43 +01:00
|
|
|
|
m_dbConn.send(q, [this] (Expected<std::shared_ptr<Pgsql::Result>> res, qint64) {
|
|
|
|
|
|
if (res.valid()) {
|
|
|
|
|
|
auto dbres = res.get();
|
|
|
|
|
|
if (dbres && *dbres) {
|
2018-12-31 09:33:34 +01:00
|
|
|
|
QMetaObject::invokeMethod(this, "loadIntoModel", Qt::QueuedConnection,
|
|
|
|
|
|
Q_ARG(std::shared_ptr<Pgsql::Result>, dbres));
|
2018-01-09 20:39:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CrudModel::loadIntoModel(std::shared_ptr<Pgsql::Result> data)
|
|
|
|
|
|
{
|
|
|
|
|
|
beginResetModel();
|
2018-11-14 19:17:29 +01:00
|
|
|
|
m_pendingRowList.clear();
|
2018-01-09 20:39:43 +01:00
|
|
|
|
m_roData = data;
|
2022-01-22 16:22:29 +01:00
|
|
|
|
lastRowKey = data->rows() - 1;
|
2018-04-08 09:02:22 +02:00
|
|
|
|
initRowMapping();
|
|
|
|
|
|
appendNewRow();
|
2018-01-09 20:39:43 +01:00
|
|
|
|
endResetModel();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-04-08 09:02:22 +02:00
|
|
|
|
void CrudModel::initRowMapping()
|
|
|
|
|
|
{
|
2022-01-22 16:22:29 +01:00
|
|
|
|
size_t cnt = m_roData->rows();
|
|
|
|
|
|
m_rowMapping.reserve(cnt + 1);
|
|
|
|
|
|
for (int i = 0; i < cnt; ++i)
|
|
|
|
|
|
m_rowMapping.emplace_back(i);
|
2018-04-08 09:02:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-01-09 20:39:43 +01:00
|
|
|
|
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
|
|
|
|
|
|
{
|
2018-12-15 11:24:58 +01:00
|
|
|
|
Qt::ItemFlags flags = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
|
2018-02-18 12:10:09 +01:00
|
|
|
|
if (m_primaryKey) {
|
2018-04-08 09:02:22 +02:00
|
|
|
|
flags |= Qt::ItemIsEditable;
|
2018-02-18 12:10:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
return flags;
|
2018-01-08 20:45:52 +01:00
|
|
|
|
}
|
2018-02-05 21:42:54 +01:00
|
|
|
|
|
|
|
|
|
|
bool CrudModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (role == Qt::EditRole) {
|
2018-04-08 09:02:22 +02:00
|
|
|
|
int grid_row = index.row();
|
2018-02-05 21:42:54 +01:00
|
|
|
|
int col = index.column();
|
|
|
|
|
|
|
2018-04-08 09:02:22 +02:00
|
|
|
|
auto& row_mapping = m_rowMapping[grid_row];
|
|
|
|
|
|
row_mapping.pending = true;
|
|
|
|
|
|
|
2018-02-05 21:42:54 +01:00
|
|
|
|
Value val;
|
2018-02-18 08:37:59 +01:00
|
|
|
|
std::string s = value.toString().toUtf8().data();
|
|
|
|
|
|
if (!s.empty()) {
|
|
|
|
|
|
if (s == "''")
|
|
|
|
|
|
s.clear();
|
|
|
|
|
|
val = s;
|
|
|
|
|
|
}
|
2018-04-08 09:02:22 +02:00
|
|
|
|
m_pendingRowList.setValue(col, row_mapping.rowKey, val);
|
2018-02-05 21:42:54 +01:00
|
|
|
|
|
|
|
|
|
|
emit dataChanged(index, index, QVector<int>() << role);
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-01-22 16:22:29 +01:00
|
|
|
|
std::tuple<bool, std::vector<CrudModel::Value>> CrudModel::saveRow(const PendingRow &pending_row)
|
2018-02-05 21:42:54 +01:00
|
|
|
|
{
|
2022-01-22 16:22:29 +01:00
|
|
|
|
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, {} };
|
2018-02-05 21:42:54 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-12-08 13:55:43 +01:00
|
|
|
|
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);
|
2022-01-22 16:22:29 +01:00
|
|
|
|
auto s = getSavedData(col, row);
|
|
|
|
|
|
params.add(s, t);
|
2018-12-08 13:55:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
return params;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-02-05 21:42:54 +01:00
|
|
|
|
QString CrudModel::columnName(int col) const
|
|
|
|
|
|
{
|
|
|
|
|
|
return m_roData->getColName(col);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-01-22 16:22:29 +01:00
|
|
|
|
std::tuple<QString, Pgsql::Params> CrudModel::createUpdateQuery(const Pgsql::Params &pkey_params, const PendingRow &pending_row)
|
2018-02-18 08:37:59 +01:00
|
|
|
|
{
|
|
|
|
|
|
Pgsql::Params params;
|
|
|
|
|
|
auto data = pending_row.data();
|
2018-11-25 19:45:06 +01:00
|
|
|
|
QString table_name = m_table->fullyQualifiedQuotedObjectName(); //genFQTableName(*m_database->catalog(), *m_table);
|
2018-02-18 08:37:59 +01:00
|
|
|
|
QString buffer;
|
|
|
|
|
|
QTextStream q(&buffer);
|
|
|
|
|
|
q << "UPDATE " << table_name << " AS d\n SET ";
|
|
|
|
|
|
int param = 0;
|
2022-01-22 16:22:29 +01:00
|
|
|
|
for (auto& e : data) {
|
2018-02-18 08:37:59 +01:00
|
|
|
|
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) {
|
2018-12-08 13:55:43 +01:00
|
|
|
|
int col = attNumToCol(attnum);
|
2018-02-18 08:37:59 +01:00
|
|
|
|
if (i > 0)
|
|
|
|
|
|
q << " AND ";
|
|
|
|
|
|
q << quoteIdent(columnName(col)) << "=$" << ++param;
|
|
|
|
|
|
++i;
|
|
|
|
|
|
}
|
2022-01-22 16:22:29 +01:00
|
|
|
|
params.addParams(pkey_params);
|
2018-02-18 08:37:59 +01:00
|
|
|
|
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();
|
2018-11-25 19:45:06 +01:00
|
|
|
|
QString table_name = m_table->fullyQualifiedQuotedObjectName(); // genFQTableName(*m_database->catalog(), *m_table);
|
2018-02-18 08:37:59 +01:00
|
|
|
|
QString buffer;
|
|
|
|
|
|
QTextStream q(&buffer);
|
2018-04-08 09:02:22 +02:00
|
|
|
|
q << "INSERT INTO " << table_name << "(";
|
|
|
|
|
|
|
2018-11-25 19:45:06 +01:00
|
|
|
|
auto columns = m_database->catalog()->attributes()->getColumnsForRelation(m_table->oid());
|
2018-04-08 09:02:22 +02:00
|
|
|
|
bool first = true;
|
2022-01-22 16:22:29 +01:00
|
|
|
|
for (const auto& e : data) {
|
2018-04-08 09:02:22 +02:00
|
|
|
|
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";
|
2018-02-18 08:37:59 +01:00
|
|
|
|
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 };
|
|
|
|
|
|
}
|
2018-02-05 21:42:54 +01:00
|
|
|
|
|
2018-04-08 09:02:22 +02:00
|
|
|
|
std::tuple<QString, Pgsql::Params> CrudModel::createDeleteStatement(const PKeyValues &pkey_values)
|
|
|
|
|
|
{
|
|
|
|
|
|
Pgsql::Params params;
|
2018-12-08 13:55:43 +01:00
|
|
|
|
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();
|
2018-04-08 09:02:22 +02:00
|
|
|
|
QString buffer;
|
|
|
|
|
|
QTextStream q(&buffer);
|
|
|
|
|
|
q << "DELETE FROM " << table_name;
|
|
|
|
|
|
q << "\nWHERE ";
|
2018-12-08 13:55:43 +01:00
|
|
|
|
int i = 0;
|
2018-04-08 09:02:22 +02:00
|
|
|
|
for (auto attnum : m_primaryKey->key) {
|
2018-12-08 13:55:43 +01:00
|
|
|
|
const int col = attNumToCol(attnum);
|
2018-04-08 09:02:22 +02:00
|
|
|
|
if (i > 0)
|
|
|
|
|
|
q << " AND ";
|
2018-12-08 13:55:43 +01:00
|
|
|
|
q << quoteIdent(columnName(col)) << "=$" << ++i;
|
2018-04-08 09:02:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
q.flush();
|
2018-12-08 13:55:43 +01:00
|
|
|
|
return buffer;
|
2018-04-08 09:02:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-02-05 21:42:54 +01:00
|
|
|
|
bool CrudModel::savePendingChanges()
|
|
|
|
|
|
{
|
2018-02-18 07:15:43 +01:00
|
|
|
|
while (!m_pendingRowList.m_rows.empty()) {
|
|
|
|
|
|
auto iter = m_pendingRowList.m_rows.begin();
|
2022-01-22 16:22:29 +01:00
|
|
|
|
auto [ok, modified_row] = saveRow(iter->second);
|
2018-02-18 07:15:43 +01:00
|
|
|
|
if (ok) {
|
2018-04-08 09:02:22 +02:00
|
|
|
|
int rowKey = iter->first;
|
2018-02-18 07:15:43 +01:00
|
|
|
|
m_pendingRowList.m_rows.erase(iter);
|
2018-02-18 08:37:59 +01:00
|
|
|
|
|
2018-04-08 09:02:22 +02:00
|
|
|
|
auto iter = std::find_if(m_rowMapping.begin(), m_rowMapping.end(),
|
2022-01-22 16:22:29 +01:00
|
|
|
|
[rowKey](const RowMapping &rhs) -> bool { return rhs.rowKey == rowKey; });
|
2018-04-08 09:02:22 +02:00
|
|
|
|
if (iter != m_rowMapping.end()) {
|
2022-01-22 16:22:29 +01:00
|
|
|
|
iter->setModifiedRowData(modified_row);
|
2018-04-08 09:02:22 +02:00
|
|
|
|
iter->pending = false;
|
2022-01-22 16:22:29 +01:00
|
|
|
|
if (IsLastRow(iter))
|
|
|
|
|
|
{
|
2018-04-08 09:02:22 +02:00
|
|
|
|
appendNewRow();
|
2022-01-22 16:22:29 +01:00
|
|
|
|
}
|
2018-04-08 09:02:22 +02:00
|
|
|
|
}
|
2018-02-18 08:37:59 +01:00
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
return false;
|
2018-02-18 07:15:43 +01:00
|
|
|
|
}
|
2018-02-05 21:42:54 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-01-22 16:22:29 +01:00
|
|
|
|
bool CrudModel::IsLastRow(RowMappingVector::iterator mapping_iter) const
|
|
|
|
|
|
{
|
|
|
|
|
|
return mapping_iter == --m_rowMapping.end();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-02-05 21:42:54 +01:00
|
|
|
|
bool CrudModel::submit()
|
|
|
|
|
|
{
|
|
|
|
|
|
return savePendingChanges();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CrudModel::revert()
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2018-02-18 08:37:59 +01:00
|
|
|
|
|
|
|
|
|
|
void CrudModel::appendNewRow()
|
|
|
|
|
|
{
|
2022-01-22 16:22:29 +01:00
|
|
|
|
int row = m_rowMapping.size();
|
2018-02-18 08:37:59 +01:00
|
|
|
|
beginInsertRows(QModelIndex(), row, row);
|
2022-01-22 16:22:29 +01:00
|
|
|
|
m_rowMapping.emplace_back(allocNewRowKey(), std::vector<Value>(m_roData->cols()));
|
|
|
|
|
|
endInsertRows();
|
2018-02-18 08:37:59 +01:00
|
|
|
|
}
|
2018-04-08 09:02:22 +02:00
|
|
|
|
|
2018-12-15 11:24:58 +01:00
|
|
|
|
std::tuple<bool, QString> CrudModel::removeRows(const std::set<IntegerRange<int>> &row_ranges)
|
2018-04-08 09:02:22 +02:00
|
|
|
|
{
|
2018-12-15 11:24:58 +01:00
|
|
|
|
if (row_ranges.empty()) return { true, "" };
|
2018-12-25 13:17:04 +01:00
|
|
|
|
if (row_ranges.rbegin()->end() > static_cast<int>(m_rowMapping.size())) return { false, "Range error" };
|
2018-04-08 09:02:22 +02:00
|
|
|
|
// 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
|
|
|
|
|
|
|
2018-12-15 11:24:58 +01:00
|
|
|
|
try {
|
|
|
|
|
|
Pgsql::Connection db_update_conn;
|
|
|
|
|
|
auto dbconfig = m_database->config();
|
2019-11-03 07:58:48 +01:00
|
|
|
|
db_update_conn.connect(dbconfig.connectionString());
|
2018-12-15 11:24:58 +01:00
|
|
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2018-04-08 09:02:22 +02:00
|
|
|
|
}
|
2018-12-15 11:24:58 +01:00
|
|
|
|
tx.commit();
|
|
|
|
|
|
// If something goes wrong after this commit we should reload contents of model
|
|
|
|
|
|
}
|
|
|
|
|
|
// Then from model
|
2022-01-22 16:22:29 +01:00
|
|
|
|
RemoveRangesOfRowsFromModel(row_ranges);
|
2018-12-15 11:24:58 +01:00
|
|
|
|
return { true, "" };
|
|
|
|
|
|
} catch (const Pgsql::PgResultError &error) {
|
|
|
|
|
|
return { false, QString::fromUtf8(error.details().messageDetail.c_str()) };
|
2018-04-08 09:02:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2018-12-15 11:24:58 +01:00
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-01-22 16:22:29 +01:00
|
|
|
|
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());
|
|
|
|
|
|
}
|
2018-12-15 11:24:58 +01:00
|
|
|
|
|