Added delete support to the CRUD system.

This commit is contained in:
eelke 2018-04-08 09:02:22 +02:00
parent 36f5153091
commit 8c20bd6a02
8 changed files with 230 additions and 89 deletions

View file

@ -72,6 +72,10 @@ public:
virtual bool setData(const QModelIndex &index, const QVariant &value, int role) override;
void loadData();
bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
/// Removes selected rows
void removeRows();
public slots:
virtual bool submit() override;
virtual void revert() override;
@ -186,6 +190,19 @@ private:
Map m_rows;
};
class RowMapping {
public:
bool modified = false; ///< Row has been modified, look there for actual data
bool pending = false; ///< Row has pending changes, look first in pending for current data
int rowKey = -1; ///< value to use as key/index into the data lists
RowMapping() = default;
explicit RowMapping(int row_key)
: rowKey(row_key)
{}
};
using RowMappingVector = std::vector<RowMapping>;
ASyncWindow * m_asyncWindow;
@ -217,10 +234,14 @@ private:
/// The key values are the indexes of the row before any rows were deleted.
///
ModifiedRowList m_modifiedRowList;
/// Keeps track of mapping grid rows to data rows
RowMappingVector m_rowMapping;
/// call on initial load to fill in the mappings
void initRowMapping();
using RedirectVec = std::vector<int>;
/// In sync with the actual table, used to efficiently find the correct row in the result
RedirectVec m_redirectVector;
// using RedirectVec = std::vector<int>;
// /// In sync with the actual table, used to efficiently find the correct row in the result
// RedirectVec m_redirectVector;
void loadIntoModel(std::shared_ptr<Pgsql::Result> data);
@ -256,9 +277,13 @@ private:
std::tuple<QString, Pgsql::Params> createUpdateQuery(const PKeyValues &pkey_values, const PendingRow &pending_row);
std::tuple<QString, Pgsql::Params> createInsertQuery(const PendingRow &pending_row);
std::tuple<QString, Pgsql::Params> createDeleteStatement(const PKeyValues &pkey_values);
std::tuple<bool, ModifiedRow> updateRow(const PendingRow &pending_row);
void appendNewRow();
int lastRowKey = 0;
int allocNewRowKey() { return ++lastRowKey; }
private slots:
void connectionStateChanged(ASyncDBConnection::State state);