Basic support for update in Crud implementation, error reporting and transfering data to modified set is still missing.
This commit is contained in:
parent
3fb32f1200
commit
d626c19e14
15 changed files with 530 additions and 83 deletions
|
|
@ -8,6 +8,7 @@
|
|||
#include "Pgsql_Connection.h"
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <tuple>
|
||||
#include <vector>
|
||||
#include <boost/optional.hpp>
|
||||
|
||||
|
|
@ -25,13 +26,27 @@ class ASyncWindow;
|
|||
* - hide columns (will not be retrieved can greatly speed up things when you disable wide columns)
|
||||
* - can use foreign keys to display dropdown
|
||||
*
|
||||
* How to load data?
|
||||
* - 2D array of QVariants won't be efficient
|
||||
* - 2D array of strings
|
||||
* - We do know the type of a single column is fixed
|
||||
* - Keep data in Result and only load data in replacement rows
|
||||
* std::string has short string optimization so this probably means
|
||||
* that a two dimensional array of std::string objects could actually be quite efficient!
|
||||
*
|
||||
* Need to keep track of changes from the original. Things to track
|
||||
* - new data being entered for existing row
|
||||
* - new data for new row
|
||||
* - current data of modified row
|
||||
* - current data of new row
|
||||
* - which rows are deleted
|
||||
* We need to be able to:
|
||||
* - determine row count easily
|
||||
* - find specific row from table easily
|
||||
* - support resorting (without requerying)
|
||||
* //- support custom filtering (without requerying)
|
||||
*
|
||||
* Keep data as much in original result
|
||||
* - use redirect list to go from index to actual row in the result
|
||||
* - use an additional map index by original row number containing changed data
|
||||
* - might be beneficial to have vector<bool> in sync with original list to signal changes from the result
|
||||
* this could prevent doing a lot of slower searches in the changed row set by only doing this when the direty markes
|
||||
* has been set vector<bool> takes 1 bit per value making it very efficient and fast. We also could put a bit in the mapping
|
||||
* vector but this probably would double the size of that list taking 32 times the size of vector<bool>.
|
||||
*
|
||||
*/
|
||||
class CrudModel: public QAbstractTableModel {
|
||||
Q_OBJECT
|
||||
|
|
@ -52,13 +67,15 @@ public:
|
|||
virtual QVariant data(const QModelIndex &index, int role) const override;
|
||||
virtual Qt::ItemFlags flags(const QModelIndex &) const override;
|
||||
|
||||
virtual bool setData(const QModelIndex &index, const QVariant &value, int role) override;
|
||||
|
||||
public slots:
|
||||
virtual bool submit() override;
|
||||
virtual void revert() override;
|
||||
|
||||
private:
|
||||
ASyncWindow * m_asyncWindow;
|
||||
std::shared_ptr<OpenDatabase> m_database;
|
||||
PgClass m_table;
|
||||
boost::optional<PgConstraint> m_primaryKey;
|
||||
ASyncDBConnection m_dbConn;
|
||||
bool callLoadData = false;
|
||||
using PKeyValues = std::vector<std::string>;
|
||||
|
||||
|
||||
|
||||
// using RowData = std::vector<std::string>;
|
||||
|
|
@ -69,58 +86,157 @@ private:
|
|||
// */
|
||||
// using RowList = std::vector<RowDataPtr>;
|
||||
|
||||
std::shared_ptr<Pgsql::Result> m_roData;
|
||||
void loadData();
|
||||
void loadIntoModel(std::shared_ptr<Pgsql::Result> data);
|
||||
|
||||
// std::shared_ptr<RowList> resultToRowList(std::shared_ptr<Pgsql::Result> result);
|
||||
|
||||
using Value = boost::optional<std::string>;
|
||||
|
||||
/** Remembers the values that have been applies to a row after the original result was retrieved.
|
||||
/** Used to remember the changes that have been made to rows.
|
||||
*
|
||||
* For simplicity it also holds the values for columns that have not been changed.
|
||||
*/
|
||||
class ModifiedRow {
|
||||
public:
|
||||
|
||||
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;
|
||||
std::vector<Value> m_values;
|
||||
};
|
||||
using ModifiedRowList = std::map<int, ModifiedRow>;
|
||||
|
||||
/** Manages the changes for the current row
|
||||
/** Similar to a modified row but it only stores values for columns that actually have been edited.
|
||||
*
|
||||
*/
|
||||
class PendingChanges {
|
||||
class PendingRow {
|
||||
public:
|
||||
using ValueMap = std::map<int16_t, Value>;
|
||||
|
||||
explicit PendingRow(int row)
|
||||
: m_row(row)
|
||||
{}
|
||||
|
||||
const auto& data() const { return m_values; }
|
||||
|
||||
int row() const { return m_row; }
|
||||
|
||||
void setValue(int col, const Value &val)
|
||||
{
|
||||
m_values.insert_or_assign(col, val);
|
||||
}
|
||||
|
||||
private:
|
||||
int m_currentRow;
|
||||
std::vector<std::string> m_currentPKeyValues; ///< The values to use for updating the row, for new rows this list is empty
|
||||
std::map<int16_t, Value> m_values; ///< values that need to be applied
|
||||
int m_row;
|
||||
ValueMap m_values;
|
||||
};
|
||||
|
||||
using DeletedList = std::vector<int>; // These are the original indexes before there were any modifications
|
||||
|
||||
PendingChanges m_pendingChanges;
|
||||
class PendingRowList {
|
||||
public:
|
||||
using Map = std::map<int, PendingRow>;
|
||||
|
||||
PendingRow& getRow(int row)
|
||||
{
|
||||
auto iter = m_rows.lower_bound(row);
|
||||
if (iter != m_rows.end() && iter->first == row) {
|
||||
return iter->second;
|
||||
}
|
||||
else {
|
||||
return m_rows.insert(iter, {row, PendingRow(row)})->second;
|
||||
}
|
||||
}
|
||||
|
||||
void setValue(int col, int row, const Value &value)
|
||||
{
|
||||
auto iter = m_rows.find(row);
|
||||
if (iter == m_rows.end()) {
|
||||
iter = m_rows.insert({row, PendingRow(row)}).first;
|
||||
}
|
||||
iter->second.setValue(col, value);
|
||||
}
|
||||
|
||||
boost::optional<Value> getValue(int col, int row) const
|
||||
{
|
||||
auto iter = m_rows.find(row);
|
||||
if (iter != m_rows.end()) {
|
||||
auto &r = iter->second;
|
||||
auto cell = r.data().find(col);
|
||||
if (cell != r.data().end())
|
||||
return cell->second;
|
||||
|
||||
}
|
||||
return boost::none;
|
||||
}
|
||||
|
||||
auto begin() { return m_rows.begin(); }
|
||||
auto end() { return m_rows.end(); }
|
||||
|
||||
void clear() { m_rows.clear(); }
|
||||
|
||||
private:
|
||||
Map m_rows;
|
||||
};
|
||||
|
||||
|
||||
|
||||
ASyncWindow * m_asyncWindow;
|
||||
std::shared_ptr<OpenDatabase> m_database;
|
||||
PgClass m_table;
|
||||
boost::optional<PgConstraint> m_primaryKey;
|
||||
ASyncDBConnection m_dbConn;
|
||||
bool callLoadData = false;
|
||||
|
||||
std::shared_ptr<Pgsql::Result> m_roData;
|
||||
|
||||
PendingRowList m_pendingRowList;
|
||||
|
||||
/** Maintains a list of all modified rows.
|
||||
*
|
||||
* The key values are the indexes of the row before any rows were deleted.
|
||||
*/
|
||||
ModifiedRowList m_modifiedRowList;
|
||||
|
||||
|
||||
// Alternative, vector of rows
|
||||
class Row {
|
||||
public:
|
||||
int resultRow; ///< row index in original result, -1 for new rows
|
||||
std::vector<Value> currentValues; ///
|
||||
|
||||
};
|
||||
using RedirectVec = std::vector<int>;
|
||||
/// In sync with the actual table, used to efficiently find the correct row in the result
|
||||
RedirectVec m_redirectVector;
|
||||
|
||||
|
||||
QVariant getData(const QModelIndex &index) const;
|
||||
void loadData();
|
||||
void loadIntoModel(std::shared_ptr<Pgsql::Result> data);
|
||||
|
||||
Value getData(const QModelIndex &index) const;
|
||||
Oid getType(int column) const;
|
||||
|
||||
///
|
||||
/// \brief columnName
|
||||
/// \param col
|
||||
/// \return
|
||||
///
|
||||
QString columnName(int col) const;
|
||||
|
||||
///
|
||||
/// \brief getModifiedRow searches for the specified row in the modified set
|
||||
/// \param row
|
||||
/// \return Pointer to the modified element or nullptr if there is no modification
|
||||
///
|
||||
const ModifiedRow* getModifiedRow(int row) const;
|
||||
|
||||
///
|
||||
/// \brief getPKeyForRow retrieve the primary key of the specified row
|
||||
///
|
||||
/// This function should not be called when there is no primarykey. (Editing should be disabled anyway in that case)
|
||||
///
|
||||
/// \param row Actual result row not intermeddiat model row
|
||||
/// \return
|
||||
///
|
||||
PKeyValues getPKeyForRow(int row) const;
|
||||
|
||||
bool savePendingChanges();
|
||||
|
||||
void updateRow(const PendingRow &pending_row);
|
||||
private slots:
|
||||
|
||||
void connectionStateChanged(ASyncDBConnection::State state);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue