Updating rows kinda works.
Blocking calls are still used.
This commit is contained in:
parent
99d738ee65
commit
628c16e2f4
10 changed files with 179 additions and 81 deletions
|
|
@ -40,7 +40,9 @@ void CrudModel::setConfig(std::shared_ptr<OpenDatabase> db, const PgClass &table
|
|||
m_primaryKey = db->catalogue()->constraints()->getPrimaryForRelation(table.oid);
|
||||
//cat->attributes()->getColumnsForRelation()
|
||||
callLoadData = true;
|
||||
m_dbConn.setupConnection(m_database->config());
|
||||
auto dbconfig = m_database->config();
|
||||
m_dbConn.setupConnection(dbconfig);
|
||||
|
||||
}
|
||||
|
||||
QVariant CrudModel::headerData(int section, Qt::Orientation orientation, int role) const
|
||||
|
|
@ -115,21 +117,10 @@ QVariant CrudModel::data(const QModelIndex &index, int role) const
|
|||
{
|
||||
QVariant v;
|
||||
if (role == Qt::EditRole) {
|
||||
// int row = index.row();
|
||||
// if (m_pendingChanges.needsToSave(row)) {
|
||||
//// if (savePendingChanges())
|
||||
//// startEditingRow();
|
||||
// }
|
||||
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());
|
||||
v = s;
|
||||
// }
|
||||
QString s = QString::fromUtf8(value->c_str());
|
||||
v = s;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -171,11 +162,6 @@ void CrudModel::loadData()
|
|||
if (res.valid()) {
|
||||
auto dbres = res.get();
|
||||
if (dbres && *dbres) {
|
||||
// WorkManager::getWorkManager()->addWork(
|
||||
// [dbres, this] () -> void {
|
||||
// std::shared_ptr<RowList> rl = resultToRowList(dbres);
|
||||
// m_asyncWindow->QueueTask([this, rl]() { loadIntoModel(rl); });
|
||||
// });
|
||||
m_asyncWindow->QueueTask([this, dbres]() { loadIntoModel(dbres); });
|
||||
}
|
||||
}
|
||||
|
|
@ -285,54 +271,73 @@ QString CrudModel::columnName(int col) const
|
|||
}
|
||||
|
||||
|
||||
|
||||
void CrudModel::updateRow(const PendingRow &pending_row)
|
||||
std::tuple<bool, CrudModel::ModifiedRow> CrudModel::updateRow(const PendingRow &pending_row)
|
||||
{
|
||||
auto pkey_values = getPKeyForRow(pending_row.row());
|
||||
|
||||
QString buffer;
|
||||
QTextStream q(&buffer);
|
||||
q << "UPDATE ";
|
||||
q << genFQTableName(*m_database->catalogue(), m_table);
|
||||
q << " AS d\n SET ";
|
||||
|
||||
auto data = pending_row.data();
|
||||
Pgsql::Params params;
|
||||
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;
|
||||
|
||||
q << quoteIdent(columnName(e.first))
|
||||
<< "=$" << ++param;
|
||||
|
||||
if (e.second)
|
||||
params.add(e.second->c_str(), getType(e.first));
|
||||
else
|
||||
params.add(nullptr, getType(e.first));
|
||||
// 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
|
||||
params.add(pkey_values[i].c_str(), getType(col));
|
||||
if (i > 0)
|
||||
q << " AND ";
|
||||
q << quoteIdent(columnName(col)) << "=$" << ++param;
|
||||
|
||||
params.add(pkey_values[i].c_str(), getType(col));
|
||||
++i;
|
||||
}
|
||||
q << "\nRETURNING *";
|
||||
q.flush();
|
||||
|
||||
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) {
|
||||
// pending row should be removed
|
||||
|
||||
m_dbConn.send(buffer.toUtf8().data(), params,
|
||||
[this] (Expected<std::shared_ptr<Pgsql::Result>> result, qint64) {
|
||||
// and the result should be stored as a modified row
|
||||
|
||||
} );
|
||||
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()
|
||||
|
|
@ -357,15 +362,14 @@ bool CrudModel::savePendingChanges()
|
|||
//
|
||||
|
||||
|
||||
for (const auto& row : m_pendingRowList) {
|
||||
// int row_number = row.first;
|
||||
// if (row_number >= 0) {
|
||||
//// update
|
||||
// }
|
||||
updateRow(row.second);
|
||||
|
||||
while (!m_pendingRowList.m_rows.empty()) {
|
||||
auto iter = m_pendingRowList.m_rows.begin();
|
||||
auto [ok, modified_row] = updateRow(iter->second);
|
||||
if (ok) {
|
||||
m_modifiedRowList.emplace(iter->first, modified_row);
|
||||
m_pendingRowList.m_rows.erase(iter);
|
||||
}
|
||||
}
|
||||
m_pendingRowList.clear();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,8 @@
|
|||
|
||||
#include <QAbstractTableModel>
|
||||
#include "ASyncDBConnection.h"
|
||||
#include "Pgsql_Connection.h"
|
||||
|
||||
#include "PgClass.h"
|
||||
#include "PgConstraint.h"
|
||||
#include "Pgsql_Connection.h"
|
||||
|
|
@ -98,13 +100,17 @@ private:
|
|||
*/
|
||||
class ModifiedRow {
|
||||
public:
|
||||
ModifiedRow() = default;
|
||||
ModifiedRow(int row, const std::vector<Value> &values)
|
||||
: m_row(row), m_values(values)
|
||||
{}
|
||||
ModifiedRow(int row, const std::vector<Value> &&values)
|
||||
: m_row(row), m_values(values)
|
||||
{}
|
||||
const auto& data() const { return m_values; }
|
||||
int row() const { return m_row; }
|
||||
private:
|
||||
int m_row;
|
||||
int m_row = -1;
|
||||
std::vector<Value> m_values;
|
||||
};
|
||||
using ModifiedRowList = std::map<int, ModifiedRow>;
|
||||
|
|
@ -177,7 +183,6 @@ private:
|
|||
|
||||
void clear() { m_rows.clear(); }
|
||||
|
||||
private:
|
||||
Map m_rows;
|
||||
};
|
||||
|
||||
|
|
@ -188,6 +193,7 @@ private:
|
|||
PgClass m_table;
|
||||
boost::optional<PgConstraint> m_primaryKey;
|
||||
ASyncDBConnection m_dbConn;
|
||||
|
||||
bool callLoadData = false;
|
||||
|
||||
std::shared_ptr<Pgsql::Result> m_roData;
|
||||
|
|
@ -236,7 +242,7 @@ private:
|
|||
|
||||
bool savePendingChanges();
|
||||
|
||||
void updateRow(const PendingRow &pending_row);
|
||||
std::tuple<bool, ModifiedRow> updateRow(const PendingRow &pending_row);
|
||||
private slots:
|
||||
|
||||
void connectionStateChanged(ASyncDBConnection::State state);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue