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);
|
m_primaryKey = db->catalogue()->constraints()->getPrimaryForRelation(table.oid);
|
||||||
//cat->attributes()->getColumnsForRelation()
|
//cat->attributes()->getColumnsForRelation()
|
||||||
callLoadData = true;
|
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
|
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;
|
QVariant v;
|
||||||
if (role == Qt::EditRole) {
|
if (role == Qt::EditRole) {
|
||||||
// int row = index.row();
|
|
||||||
// if (m_pendingChanges.needsToSave(row)) {
|
|
||||||
//// if (savePendingChanges())
|
|
||||||
//// startEditingRow();
|
|
||||||
// }
|
|
||||||
auto value = getData(index);
|
auto value = getData(index);
|
||||||
if (value) {
|
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());
|
QString s = QString::fromUtf8(value->c_str());
|
||||||
v = s;
|
v = s;
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -171,11 +162,6 @@ void CrudModel::loadData()
|
||||||
if (res.valid()) {
|
if (res.valid()) {
|
||||||
auto dbres = res.get();
|
auto dbres = res.get();
|
||||||
if (dbres && *dbres) {
|
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); });
|
m_asyncWindow->QueueTask([this, dbres]() { loadIntoModel(dbres); });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -285,54 +271,73 @@ QString CrudModel::columnName(int col) const
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
std::tuple<bool, CrudModel::ModifiedRow> CrudModel::updateRow(const PendingRow &pending_row)
|
||||||
void 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();
|
auto data = pending_row.data();
|
||||||
Pgsql::Params params;
|
Pgsql::Params params;
|
||||||
if (!data.empty()) {
|
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;
|
int param = 0;
|
||||||
for (auto e : data) {
|
for (auto e : data) {
|
||||||
if (param > 0)
|
if (param > 0)
|
||||||
q << ",";
|
q << ",";
|
||||||
|
q << quoteIdent(columnName(e.first)) << "=$" << ++param;
|
||||||
|
|
||||||
q << quoteIdent(columnName(e.first))
|
// Add value to paramlist
|
||||||
<< "=$" << ++param;
|
params.add(e.second, getType(e.first));
|
||||||
|
|
||||||
if (e.second)
|
|
||||||
params.add(e.second->c_str(), getType(e.first));
|
|
||||||
else
|
|
||||||
params.add(nullptr, getType(e.first));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
q << "\nWHERE ";
|
q << "\nWHERE ";
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for (auto attnum : m_primaryKey->key) {
|
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
|
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)
|
if (i > 0)
|
||||||
q << " AND ";
|
q << " AND ";
|
||||||
q << quoteIdent(columnName(col)) << "=$" << ++param;
|
q << quoteIdent(columnName(col)) << "=$" << ++param;
|
||||||
|
|
||||||
|
params.add(pkey_values[i].c_str(), getType(col));
|
||||||
++i;
|
++i;
|
||||||
}
|
}
|
||||||
q << "\nRETURNING *";
|
q << "\nRETURNING *";
|
||||||
q.flush();
|
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,
|
// and the result should be stored as a modified row
|
||||||
[this] (Expected<std::shared_ptr<Pgsql::Result>> result, qint64) {
|
|
||||||
|
|
||||||
} );
|
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()
|
bool CrudModel::savePendingChanges()
|
||||||
|
|
@ -357,15 +362,14 @@ bool CrudModel::savePendingChanges()
|
||||||
//
|
//
|
||||||
|
|
||||||
|
|
||||||
for (const auto& row : m_pendingRowList) {
|
while (!m_pendingRowList.m_rows.empty()) {
|
||||||
// int row_number = row.first;
|
auto iter = m_pendingRowList.m_rows.begin();
|
||||||
// if (row_number >= 0) {
|
auto [ok, modified_row] = updateRow(iter->second);
|
||||||
//// update
|
if (ok) {
|
||||||
// }
|
m_modifiedRowList.emplace(iter->first, modified_row);
|
||||||
updateRow(row.second);
|
m_pendingRowList.m_rows.erase(iter);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
m_pendingRowList.clear();
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,8 @@
|
||||||
|
|
||||||
#include <QAbstractTableModel>
|
#include <QAbstractTableModel>
|
||||||
#include "ASyncDBConnection.h"
|
#include "ASyncDBConnection.h"
|
||||||
|
#include "Pgsql_Connection.h"
|
||||||
|
|
||||||
#include "PgClass.h"
|
#include "PgClass.h"
|
||||||
#include "PgConstraint.h"
|
#include "PgConstraint.h"
|
||||||
#include "Pgsql_Connection.h"
|
#include "Pgsql_Connection.h"
|
||||||
|
|
@ -98,13 +100,17 @@ private:
|
||||||
*/
|
*/
|
||||||
class ModifiedRow {
|
class ModifiedRow {
|
||||||
public:
|
public:
|
||||||
|
ModifiedRow() = default;
|
||||||
ModifiedRow(int row, const std::vector<Value> &values)
|
ModifiedRow(int row, const std::vector<Value> &values)
|
||||||
: m_row(row), m_values(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; }
|
const auto& data() const { return m_values; }
|
||||||
int row() const { return m_row; }
|
int row() const { return m_row; }
|
||||||
private:
|
private:
|
||||||
int m_row;
|
int m_row = -1;
|
||||||
std::vector<Value> m_values;
|
std::vector<Value> m_values;
|
||||||
};
|
};
|
||||||
using ModifiedRowList = std::map<int, ModifiedRow>;
|
using ModifiedRowList = std::map<int, ModifiedRow>;
|
||||||
|
|
@ -177,7 +183,6 @@ private:
|
||||||
|
|
||||||
void clear() { m_rows.clear(); }
|
void clear() { m_rows.clear(); }
|
||||||
|
|
||||||
private:
|
|
||||||
Map m_rows;
|
Map m_rows;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -188,6 +193,7 @@ private:
|
||||||
PgClass m_table;
|
PgClass m_table;
|
||||||
boost::optional<PgConstraint> m_primaryKey;
|
boost::optional<PgConstraint> m_primaryKey;
|
||||||
ASyncDBConnection m_dbConn;
|
ASyncDBConnection m_dbConn;
|
||||||
|
|
||||||
bool callLoadData = false;
|
bool callLoadData = false;
|
||||||
|
|
||||||
std::shared_ptr<Pgsql::Result> m_roData;
|
std::shared_ptr<Pgsql::Result> m_roData;
|
||||||
|
|
@ -236,7 +242,7 @@ private:
|
||||||
|
|
||||||
bool savePendingChanges();
|
bool savePendingChanges();
|
||||||
|
|
||||||
void updateRow(const PendingRow &pending_row);
|
std::tuple<bool, ModifiedRow> updateRow(const PendingRow &pending_row);
|
||||||
private slots:
|
private slots:
|
||||||
|
|
||||||
void connectionStateChanged(ASyncDBConnection::State state);
|
void connectionStateChanged(ASyncDBConnection::State state);
|
||||||
|
|
|
||||||
|
|
@ -35,18 +35,19 @@ FROM pg_constraint)__";
|
||||||
|
|
||||||
PgConstraint PgConstraintContainer::loadElem(const Pgsql::Row &row)
|
PgConstraint PgConstraintContainer::loadElem(const Pgsql::Row &row)
|
||||||
{
|
{
|
||||||
Pgsql::Col col(row);
|
using namespace Pgsql;
|
||||||
|
Col col(row);
|
||||||
PgConstraint v;
|
PgConstraint v;
|
||||||
col >> v.oid >> v.name >> v.connamespace >> v.type >> v.deferrable
|
col >> v.oid >> v.name >> v.connamespace >> v.type >> v.deferrable
|
||||||
>> v.deferred >> v.validated >> v.relid >> v.typid >> v.indid
|
>> v.deferred >> v.validated >> v.relid >> v.typid >> v.indid
|
||||||
>> v.frelid >> v.fupdtype >> v.fdeltype >> v.fmatchtype
|
>> v.frelid >> v.fupdtype >> v.fdeltype >> v.fmatchtype
|
||||||
>> v.islocal >> v.inhcount >> v.noinherit;
|
>> v.islocal >> v.inhcount >> v.noinherit;
|
||||||
col.getAsArray<int16_t>(std::back_inserter(v.key));
|
col.getAsArray<int16_t>(std::back_inserter(v.key), NullHandling::Ignore);
|
||||||
col.getAsArray<int16_t>(std::back_inserter(v.fkey));
|
col.getAsArray<int16_t>(std::back_inserter(v.fkey), NullHandling::Ignore);
|
||||||
col.getAsArray<Oid>(std::back_inserter(v.pfeqop));
|
col.getAsArray<Oid>(std::back_inserter(v.pfeqop), NullHandling::Ignore);
|
||||||
col.getAsArray<Oid>(std::back_inserter(v.ppeqop));
|
col.getAsArray<Oid>(std::back_inserter(v.ppeqop), NullHandling::Ignore);
|
||||||
col.getAsArray<Oid>(std::back_inserter(v.ffeqop));
|
col.getAsArray<Oid>(std::back_inserter(v.ffeqop), NullHandling::Ignore);
|
||||||
col.getAsArray<Oid>(std::back_inserter(v.exclop));
|
col.getAsArray<Oid>(std::back_inserter(v.exclop), NullHandling::Ignore);
|
||||||
col >> v.bin >> v.src >> v.definition;
|
col >> v.bin >> v.src >> v.definition;
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -153,6 +153,18 @@ Result Connection::query(const char * command)
|
||||||
return Result(result);
|
return Result(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Result Connection::queryParam(const char * command, const Params ¶ms)
|
||||||
|
{
|
||||||
|
PGresult *result = PQexecParams(conn, command, params.size(), params.types(),
|
||||||
|
params.values(), params.lengths(), params.formats(), 0);
|
||||||
|
return Result(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
Result Connection::queryParam(const QString &command, const Params ¶ms)
|
||||||
|
{
|
||||||
|
return queryParam(command.toUtf8().data(), params);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
bool Connection::sendQuery(const char *query)
|
bool Connection::sendQuery(const char *query)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -92,11 +92,15 @@ namespace Pgsql {
|
||||||
std::string getErrorMessage() const;
|
std::string getErrorMessage() const;
|
||||||
|
|
||||||
Result query(const char * command);
|
Result query(const char * command);
|
||||||
|
|
||||||
Result query(const QString &command)
|
Result query(const QString &command)
|
||||||
{
|
{
|
||||||
return query(command.toUtf8().data());
|
return query(command.toUtf8().data());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Result queryParam(const char * command, const Params ¶ms);
|
||||||
|
Result queryParam(const QString &command, const Params ¶ms);
|
||||||
|
|
||||||
bool sendQuery(const char * query);
|
bool sendQuery(const char * query);
|
||||||
bool sendQuery(const std::string &command)
|
bool sendQuery(const std::string &command)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,8 @@
|
||||||
#include <QString>
|
#include <QString>
|
||||||
#include <libpq-fe.h>
|
#include <libpq-fe.h>
|
||||||
#include "Pgsql_declare.h"
|
#include "Pgsql_declare.h"
|
||||||
|
#include "Pgsql_oids.h"
|
||||||
|
#include <boost/optional.hpp>
|
||||||
|
|
||||||
namespace Pgsql {
|
namespace Pgsql {
|
||||||
|
|
||||||
|
|
@ -18,8 +20,12 @@ namespace Pgsql {
|
||||||
~Params();
|
~Params();
|
||||||
|
|
||||||
|
|
||||||
void add(const QString &s, Oid oid=VARCHAROID);
|
void add(const QString &s, Oid oid=varchar_oid);
|
||||||
void add(const char *data, Oid oid=VARCHAROID);
|
void add(const char *data, Oid oid=varchar_oid);
|
||||||
|
void add(boost::optional<std::string> s, Oid oid=varchar_oid)
|
||||||
|
{
|
||||||
|
add(s ? s->c_str() : nullptr, oid);
|
||||||
|
}
|
||||||
//void addBinary(const char *data, int length, Oid oid);
|
//void addBinary(const char *data, int length, Oid oid);
|
||||||
void clear();
|
void clear();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -183,8 +183,10 @@ Value Result::get(int col, int row) const
|
||||||
colRangeCheck(col);
|
colRangeCheck(col);
|
||||||
rowRangeCheck(row);
|
rowRangeCheck(row);
|
||||||
|
|
||||||
|
bool is_null = PQgetisnull(result, row, col);
|
||||||
|
char *ptr = is_null ? nullptr : PQgetvalue(result, row, col);
|
||||||
return Value(
|
return Value(
|
||||||
PQgetvalue(result, row, col),
|
ptr,
|
||||||
PQftype(result, col)
|
PQftype(result, col)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -33,4 +33,7 @@ Value Row::get(int col) const
|
||||||
//{
|
//{
|
||||||
|
|
||||||
//}
|
//}
|
||||||
|
Row::const_iterator Row::end() const
|
||||||
|
{
|
||||||
|
return const_iterator(*this, m_result.cols());
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,51 @@ namespace Pgsql {
|
||||||
*/
|
*/
|
||||||
class Row {
|
class Row {
|
||||||
public:
|
public:
|
||||||
|
class const_iterator {
|
||||||
|
public:
|
||||||
|
const_iterator(const Row &r, int col)
|
||||||
|
: m_row(r)
|
||||||
|
, m_col(col)
|
||||||
|
{}
|
||||||
|
|
||||||
|
const_iterator operator++()
|
||||||
|
{
|
||||||
|
const_iterator t(*this);
|
||||||
|
++m_col;
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
const_iterator& operator++(int)
|
||||||
|
{
|
||||||
|
++m_col;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator==(const const_iterator &rhs)
|
||||||
|
{
|
||||||
|
return m_row == rhs.m_row && m_col == rhs.m_col;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator!=(const const_iterator &rhs)
|
||||||
|
{
|
||||||
|
return !operator==(rhs);
|
||||||
|
}
|
||||||
|
|
||||||
|
const Value operator*()
|
||||||
|
{
|
||||||
|
return m_row.get(m_col);
|
||||||
|
}
|
||||||
|
|
||||||
|
const Value operator->()
|
||||||
|
{
|
||||||
|
return m_row.get(m_col);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
const Row &m_row;
|
||||||
|
int m_col;
|
||||||
|
};
|
||||||
|
|
||||||
Row(const Result &result, int row);
|
Row(const Result &result, int row);
|
||||||
bool next();
|
bool next();
|
||||||
|
|
||||||
|
|
@ -22,6 +67,12 @@ namespace Pgsql {
|
||||||
//Value get(const char *colname) const;
|
//Value get(const char *colname) const;
|
||||||
//bool get(int col, QString &s);
|
//bool get(int col, QString &s);
|
||||||
|
|
||||||
|
const_iterator begin() const
|
||||||
|
{
|
||||||
|
return const_iterator(*this, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
const_iterator end() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const Result& m_result;
|
const Result& m_result;
|
||||||
|
|
|
||||||
|
|
@ -16,9 +16,12 @@ namespace Pgsql {
|
||||||
*/
|
*/
|
||||||
class Value {
|
class Value {
|
||||||
public:
|
public:
|
||||||
|
const char *empty_str = "";
|
||||||
Value(const char *val, Oid typ);
|
Value(const char *val, Oid typ);
|
||||||
QString asQString() const;
|
QString asQString() const;
|
||||||
const char* c_str() const { return m_val; }
|
|
||||||
|
bool null() const { return m_val == nullptr; }
|
||||||
|
const char* c_str() const { return m_val == nullptr ? empty_str : m_val; }
|
||||||
|
|
||||||
operator QString() const;
|
operator QString() const;
|
||||||
operator QDateTime() const;
|
operator QDateTime() const;
|
||||||
|
|
@ -42,6 +45,11 @@ namespace Pgsql {
|
||||||
template <typename E, typename I>
|
template <typename E, typename I>
|
||||||
void getAsArray(I insert_iter, NullHandling nullhandling = NullHandling::Throw) const
|
void getAsArray(I insert_iter, NullHandling nullhandling = NullHandling::Throw) const
|
||||||
{
|
{
|
||||||
|
if (m_val == nullptr) {
|
||||||
|
if (nullhandling == NullHandling::Throw)
|
||||||
|
throw std::runtime_error("Unexpected NULL value in array");
|
||||||
|
}
|
||||||
|
else {
|
||||||
using value_type = E;
|
using value_type = E;
|
||||||
ArrayParser parser(m_val);
|
ArrayParser parser(m_val);
|
||||||
for (;;) {
|
for (;;) {
|
||||||
|
|
@ -63,6 +71,7 @@ namespace Pgsql {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
template <typename E, typename I>
|
template <typename E, typename I>
|
||||||
void getAsArray(I insert_iter, const E &value_for_nulls) const
|
void getAsArray(I insert_iter, const E &value_for_nulls) const
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue