2018-01-08 20:45:52 +01:00
|
|
|
|
#ifndef CRUDMODEL_H
|
|
|
|
|
|
#define CRUDMODEL_H
|
|
|
|
|
|
|
|
|
|
|
|
#include "BaseTableModel.h"
|
|
|
|
|
|
#include "ASyncDBConnection.h"
|
|
|
|
|
|
#include "PgClass.h"
|
2018-01-09 20:39:43 +01:00
|
|
|
|
#include "PgConstraint.h"
|
2018-01-08 20:45:52 +01:00
|
|
|
|
#include "Pgsql_Connection.h"
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
#include <boost/optional.hpp>
|
|
|
|
|
|
|
|
|
|
|
|
class PgConstraint;
|
2018-01-09 20:39:43 +01:00
|
|
|
|
class OpenDatabase;
|
|
|
|
|
|
class ASyncWindow;
|
2018-01-08 20:45:52 +01:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief The CrudModel class
|
|
|
|
|
|
*
|
|
|
|
|
|
* Features
|
|
|
|
|
|
* - order by one or more user selectable columns
|
|
|
|
|
|
* - user filter condition
|
|
|
|
|
|
* - user limit and offset
|
|
|
|
|
|
* - 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!
|
|
|
|
|
|
*/
|
|
|
|
|
|
class CrudModel: public BaseTableModel {
|
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
public:
|
2018-01-09 20:39:43 +01:00
|
|
|
|
explicit CrudModel(ASyncWindow *async_win);
|
2018-01-08 20:45:52 +01:00
|
|
|
|
~CrudModel();
|
|
|
|
|
|
|
2018-01-09 20:39:43 +01:00
|
|
|
|
void setConfig(std::shared_ptr<OpenDatabase> db, const PgClass &table);
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-01-08 20:45:52 +01:00
|
|
|
|
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Basic functionality:
|
|
|
|
|
|
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
|
|
|
|
|
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
|
|
|
|
|
|
|
2018-01-09 20:39:43 +01:00
|
|
|
|
virtual QVariant data(const QModelIndex &index, int role) const override;
|
|
|
|
|
|
virtual Qt::ItemFlags flags(const QModelIndex &) const override;
|
2018-01-08 20:45:52 +01:00
|
|
|
|
protected:
|
|
|
|
|
|
virtual Oid getType(int column) const override;
|
|
|
|
|
|
virtual QVariant getData(const QModelIndex &index) const override;
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
2018-01-09 20:39:43 +01:00
|
|
|
|
ASyncWindow * m_asyncWindow;
|
2018-01-08 20:45:52 +01:00
|
|
|
|
std::shared_ptr<OpenDatabase> m_database;
|
|
|
|
|
|
PgClass m_table;
|
|
|
|
|
|
boost::optional<PgConstraint> m_primaryKey;
|
|
|
|
|
|
ASyncDBConnection m_dbConn;
|
2018-01-09 20:39:43 +01:00
|
|
|
|
bool callLoadData = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// using RowData = std::vector<std::string>;
|
|
|
|
|
|
// using RowDataPtr = std::unique_ptr<RowData>;
|
|
|
|
|
|
// /** Choosen for a vector of pointers while vector is relatively slow with insertion and removal in the middle
|
|
|
|
|
|
// * I hope the small size of the elements will make this not much of an issue while the simple linear and sequential
|
|
|
|
|
|
// * nature of a vector will keep memory allocation fairly efficient.
|
|
|
|
|
|
// */
|
|
|
|
|
|
// 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);
|
2018-01-08 20:45:52 +01:00
|
|
|
|
|
2018-01-09 20:39:43 +01:00
|
|
|
|
private slots:
|
2018-01-08 20:45:52 +01:00
|
|
|
|
|
2018-01-09 20:39:43 +01:00
|
|
|
|
void connectionStateChanged(ASyncDBConnection::State state);
|
|
|
|
|
|
// void queryResult(std::shared_ptr<Pgsql::Result> result);
|
|
|
|
|
|
// void queryError();
|
2018-01-08 20:45:52 +01:00
|
|
|
|
|
2018-01-09 20:39:43 +01:00
|
|
|
|
// void dataProcessingFutureFinished();
|
2018-01-08 20:45:52 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif // CRUDMODEL_H
|