460 lines
11 KiB
C++
460 lines
11 KiB
C++
#include "CrudModel.h"
|
|
#include "ASyncWindow.h"
|
|
#include "OpenDatabase.h"
|
|
#include "PgDatabaseCatalog.h"
|
|
#include "PgAttributeContainer.h"
|
|
#include "PgConstraintContainer.h"
|
|
#include "GlobalIoService.h"
|
|
#include "SqlFormattingUtils.h"
|
|
#include "WorkManager.h"
|
|
#include "Pgsql_oids.h"
|
|
#include <QtConcurrent>
|
|
#include <QFuture>
|
|
#include <QFutureWatcher>
|
|
#include "Pgsql_oids.h"
|
|
#include "Pgsql_Params.h"
|
|
#include <string>
|
|
|
|
CrudModel::CrudModel(ASyncWindow *async_window)
|
|
: m_asyncWindow(async_window)
|
|
, m_dbConn(*getGlobalAsioIoService())
|
|
{
|
|
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->catalogue()->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->catalogue(), getType(section));
|
|
r = s;
|
|
}
|
|
else {
|
|
r = QString::number(section + 1);
|
|
}
|
|
}
|
|
return r;
|
|
}
|
|
|
|
|
|
// Basic functionality:
|
|
int CrudModel::rowCount(const QModelIndex &/*parent*/) const
|
|
{
|
|
// int row_count = m_roData ? m_roData->rows() : 0;
|
|
|
|
// todo there will be rownumbers that are not in m_roData
|
|
|
|
// ++row_count; // one empty new row at the end
|
|
return m_rowCount;
|
|
}
|
|
|
|
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::getData(const QModelIndex &index) const
|
|
{
|
|
Value value;
|
|
if (m_roData) {
|
|
int row = index.row();
|
|
int col = index.column();
|
|
|
|
const int last_row = rowCount() - 1;
|
|
|
|
//Oid o = m_roData->type(col);
|
|
// First see if we have buffered editted values that still need saving
|
|
boost::optional<Value> val = m_pendingRowList.getValue(col, row);
|
|
if (!val && row < last_row) { // last_row should never be in modified list, when it is put in modified list a new last row should be created
|
|
// No pending save have a look if we have modified saved data in the modified list
|
|
auto find_res = m_modifiedRowList.find(row);
|
|
if (find_res != m_modifiedRowList.end())
|
|
val = find_res->second.data()[col];
|
|
|
|
}
|
|
|
|
//Value value;
|
|
// If we did not have pending or modified data
|
|
if (!val && row < m_roData->rows()) {
|
|
// Then we are going to read the original data.
|
|
if (!m_roData->null(col, row))
|
|
value = std::string(m_roData->val(col, row));
|
|
}
|
|
else {
|
|
if (val)
|
|
value = *val;
|
|
}
|
|
}
|
|
return value;
|
|
}
|
|
|
|
QVariant CrudModel::data(const QModelIndex &index, int role) const
|
|
{
|
|
QVariant v;
|
|
if (role == Qt::EditRole) {
|
|
auto value = getData(index);
|
|
if (value) {
|
|
QString s = QString::fromUtf8(value->c_str());
|
|
v = s;
|
|
}
|
|
|
|
}
|
|
else if (role == Qt::DisplayRole) {
|
|
auto value = getData(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 = genFQTableName(*m_database->catalogue(), m_table);
|
|
std::string q = "SELECT * FROM ";
|
|
q += table_name.toUtf8();
|
|
m_dbConn.send(q, [this] (Expected<std::shared_ptr<Pgsql::Result>> res, qint64) {
|
|
if (res.valid()) {
|
|
auto dbres = res.get();
|
|
if (dbres && *dbres) {
|
|
m_asyncWindow->QueueTask([this, dbres]() { loadIntoModel(dbres); });
|
|
}
|
|
}
|
|
else {
|
|
// emit onQueryError();
|
|
}
|
|
});
|
|
}
|
|
|
|
void CrudModel::loadIntoModel(std::shared_ptr<Pgsql::Result> data)
|
|
{
|
|
beginResetModel();
|
|
m_roData = data;
|
|
m_rowCount = data->rows() + 1;
|
|
endResetModel();
|
|
}
|
|
|
|
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 row = index.row();
|
|
int col = index.column();
|
|
|
|
Value val;
|
|
std::string s = value.toString().toUtf8().data();
|
|
if (!s.empty()) {
|
|
if (s == "''")
|
|
s.clear();
|
|
val = s;
|
|
}
|
|
m_pendingRowList.setValue(col, row, val);
|
|
|
|
emit dataChanged(index, index, QVector<int>() << role);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
const CrudModel::ModifiedRow* CrudModel::getModifiedRow(int row) const
|
|
{
|
|
auto iter = m_modifiedRowList.find(row);
|
|
if (iter == m_modifiedRowList.end())
|
|
return nullptr;
|
|
else
|
|
return &iter->second;
|
|
}
|
|
|
|
CrudModel::PKeyValues CrudModel::getPKeyForRow(int row) const
|
|
{
|
|
PKeyValues values;
|
|
values.reserve(m_primaryKey->fkey.size());
|
|
|
|
auto mod_row = getModifiedRow(row);
|
|
if (mod_row){
|
|
for (auto attnum : m_primaryKey->key) {
|
|
int col = attnum - 1; // Assume column ordering matches table, also we assume know special columns like oid are shown
|
|
values.push_back(*(mod_row->data()[col]));
|
|
}
|
|
}
|
|
else if (row < m_roData->rows()){
|
|
for (auto attnum : m_primaryKey->key) {
|
|
int col = attnum - 1; // Assume column ordering matches table, also we assume know special columns like oid are shown
|
|
values.push_back(m_roData->get(col, row).c_str());
|
|
}
|
|
}
|
|
|
|
return values;
|
|
}
|
|
|
|
QString CrudModel::columnName(int col) const
|
|
{
|
|
return m_roData->getColName(col);
|
|
}
|
|
|
|
std::tuple<QString, Pgsql::Params> CrudModel::createUpdateQuery(const PKeyValues &pkey_values, const PendingRow &pending_row)
|
|
{
|
|
Pgsql::Params params;
|
|
auto data = pending_row.data();
|
|
QString table_name = genFQTableName(*m_database->catalogue(), 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 = attnum - 1; // Assume column ordering matches table, also we assume know special columns like oid are shown
|
|
if (i > 0)
|
|
q << " AND ";
|
|
q << quoteIdent(columnName(col)) << "=$" << ++param;
|
|
|
|
params.add(pkey_values[i].c_str(), getType(col));
|
|
++i;
|
|
}
|
|
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 = genFQTableName(*m_database->catalogue(), m_table);
|
|
QString buffer;
|
|
QTextStream q(&buffer);
|
|
q << "INSERT INTO " << table_name << " VALUES ($1";
|
|
for (size_t p = 2; p <= data.size(); ++p)
|
|
q << ",$" << p;
|
|
q << ") RETURNING *";
|
|
for (auto e : data) {
|
|
// Add value to paramlist
|
|
params.add(e.second, getType(e.first));
|
|
}
|
|
q.flush();
|
|
return { buffer, params };
|
|
}
|
|
|
|
std::tuple<bool, CrudModel::ModifiedRow> CrudModel::updateRow(const PendingRow &pending_row)
|
|
{
|
|
auto data = pending_row.data();
|
|
|
|
if (!data.empty()) {
|
|
auto pkey_values = getPKeyForRow(pending_row.row());
|
|
|
|
// QString table_name = genFQTableName(*m_database->catalogue(), 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 = attnum - 1; // Assume column ordering matches table, also we assume know special columns like oid are shown
|
|
// if (i > 0)
|
|
// q << " AND ";
|
|
// q << quoteIdent(columnName(col)) << "=$" << ++param;
|
|
|
|
// params.add(pkey_values[i].c_str(), getType(col));
|
|
// ++i;
|
|
// }
|
|
// q << "\nRETURNING *";
|
|
// q.flush();
|
|
|
|
QString buffer;
|
|
Pgsql::Params params;
|
|
if (pkey_values.empty()){
|
|
// todo insert query
|
|
std::tie(buffer, params) = createInsertQuery(pending_row);
|
|
}
|
|
else {
|
|
std::tie(buffer, params) = createUpdateQuery(pkey_values, pending_row);
|
|
}
|
|
|
|
|
|
int row_number = pending_row.row();
|
|
Pgsql::Connection db_update_conn;
|
|
auto dbconfig = m_database->config();
|
|
bool res = db_update_conn.connect(dbconfig.getKeywords(), dbconfig.getValues(), false);
|
|
if (res) {
|
|
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()));
|
|
}
|
|
|
|
ModifiedRow modified_row(row_number, values);
|
|
|
|
return { true, modified_row };
|
|
}
|
|
}
|
|
|
|
// m_dbConn.send(buffer.toUtf8().data(), params,
|
|
// [row_number, this] (Expected<std::shared_ptr<Pgsql::Result>> result, qint64) {
|
|
|
|
// } );
|
|
}
|
|
return { false, {} };
|
|
}
|
|
|
|
bool CrudModel::savePendingChanges()
|
|
{
|
|
// need to start async store of changes
|
|
|
|
// modified results are only known after updates/inserts have returned the new values
|
|
// (new values can be modified by triggers and such things)
|
|
|
|
// do we leave panding changes in the list until async task complete? But then we do not know which
|
|
// ones we are storing because user can add new ones.
|
|
|
|
// We could also clear the pending list and keep a copy in the async task but then display would revert back
|
|
// to original values until save has completed.
|
|
|
|
// Moving them to modified list doesn't seem a good idea either because we would have to undo the changes
|
|
// when the save fails.
|
|
|
|
|
|
|
|
// PendingRowList copy = m_pendingRowList;
|
|
//
|
|
|
|
|
|
while (!m_pendingRowList.m_rows.empty()) {
|
|
auto iter = m_pendingRowList.m_rows.begin();
|
|
auto [ok, modified_row] = updateRow(iter->second);
|
|
if (ok) {
|
|
int row = iter->first;
|
|
m_modifiedRowList.insert_or_assign(row, modified_row);
|
|
m_pendingRowList.m_rows.erase(iter);
|
|
|
|
if (row == m_rowCount - 1)
|
|
appendNewRow();
|
|
|
|
}
|
|
else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
bool CrudModel::submit()
|
|
{
|
|
return savePendingChanges();
|
|
}
|
|
|
|
void CrudModel::revert()
|
|
{
|
|
|
|
}
|
|
|
|
void CrudModel::appendNewRow()
|
|
{
|
|
int row = m_rowCount;
|
|
beginInsertRows(QModelIndex(), row, row);
|
|
++m_rowCount;
|
|
endInsertRows();
|
|
}
|