Improve sorting

This commit is contained in:
eelke 2022-04-02 08:33:48 +02:00
parent 06504ecc1f
commit 2c5a42f45d
4 changed files with 76 additions and 30 deletions

View file

@ -23,6 +23,7 @@ enum CustomDataRole {
CustomDataTypeRole = Qt::UserRole, ///< Requist the basic type of the value CustomDataTypeRole = Qt::UserRole, ///< Requist the basic type of the value
CustomReferencedTypeRole, ///< CustomReferencedTypeRole, ///<
CustomDataMeaningRole, CustomDataMeaningRole,
CustomSortRole, // return data in a form that will sort correctly
// Add other enum before this one as we might want to have multiple hidden values // Add other enum before this one as we might want to have multiple hidden values
FirstHiddenValue, ///< Used to request value from a model which is not handed to the view FirstHiddenValue, ///< Used to request value from a model which is not handed to the view
}; };

View file

@ -3,6 +3,7 @@
#include "Pgsql_declare.h" #include "Pgsql_declare.h"
#include "Pgsql_oids.h" #include "Pgsql_oids.h"
#include "catalog/PgDatabaseCatalog.h" #include "catalog/PgDatabaseCatalog.h"
#include "CustomDataRole.h"
#include <QBrush> #include <QBrush>
#include <QColor> #include <QColor>
@ -52,7 +53,7 @@ QVariant QueryResultModel::data(const QModelIndex &index, int role) const
} }
} }
} }
else if (role == Qt::UserRole) { else if (role == CustomDataTypeRole) {
v = oid; v = oid;
} }

View file

@ -6,6 +6,7 @@
#include "catalog/PgConstraintContainer.h" #include "catalog/PgConstraintContainer.h"
#include "SqlFormattingUtils.h" #include "SqlFormattingUtils.h"
#include "Pgsql_oids.h" #include "Pgsql_oids.h"
#include "CustomDataRole.h"
#include <QtConcurrent> #include <QtConcurrent>
#include <QFuture> #include <QFuture>
#include <QFutureWatcher> #include <QFutureWatcher>
@ -124,37 +125,79 @@ CrudModel::Value CrudModel::getSavedData(const RowMapping &row_mapping, int colu
return val.value_or(Value()); return val.value_or(Value());
} }
QVariant CrudModel::data(const QModelIndex &index, int role) const QVariant StringToVariant(const std::string &str, Oid oid)
{ {
QVariant v; Pgsql::Value v(str.c_str(), oid);
if (role == Qt::EditRole) { switch (oid) {
auto value = getLatestData(index); default:
if (value) { return QString::fromStdString(str);
QString s = QString::fromUtf8(value->c_str());
v = s; case Pgsql::timestamp_oid:
case Pgsql::timestamptz_oid:
return v.operator QDateTime();
case Pgsql::date_oid:
return v.operator QDate();
case Pgsql::time_oid:
case Pgsql::timetz_oid:
return v.operator QTime();
case Pgsql::int2_oid:
return v.operator int16_t();
case Pgsql::int4_oid:
return v.operator int32_t();
case Pgsql::oid_oid:
case Pgsql::int8_oid:
return v.operator int64_t();
case Pgsql::bool_oid:
return v.operator bool();
case Pgsql::float4_oid:
return v.operator float();
case Pgsql::float8_oid:
return v.operator double();
} }
} }
else if (role == Qt::DisplayRole) {
QVariant CrudModel::data(const QModelIndex &index, int role) const
{
Oid typ = getType(index.column());
if (role == Qt::EditRole || role == Qt::DisplayRole)
{
auto value = getLatestData(index); auto value = getLatestData(index);
if (value) { if (value) {
Oid o = m_roData->type(index.column()); if (role == Qt::EditRole)
if (o == Pgsql::bool_oid) { return QString::fromUtf8(value->c_str());
v = *value == "t"; //s = (s == "t") ? "TRUE" : "FALSE"; else
} {
else { if (typ == Pgsql::bool_oid)
return *value == "t"; //s = (s == "t") ? "TRUE" : "FALSE";
else
{
QString s = QString::fromUtf8(value->c_str()); QString s = QString::fromUtf8(value->c_str());
if (s.length() > 256) {
s.truncate(256); s.truncate(256);
} return s;
v = s;
} }
} }
} }
else if (role == Qt::UserRole) {
v = getType(index.column());
} }
return v; else if (role == CustomDataTypeRole)
return typ;
else if (role == CustomSortRole)
{
auto value = getLatestData(index);
if (value) {
return StringToVariant(value->c_str(), typ);
}
}
return {};
} }
void CrudModel::loadData() void CrudModel::loadData()
@ -367,7 +410,7 @@ std::tuple<QString, Pgsql::Params> CrudModel::createInsertQuery(const PendingRow
for (size_t p = 2; p <= data.size(); ++p) for (size_t p = 2; p <= data.size(); ++p)
q << ",$" << p; q << ",$" << p;
q << ") RETURNING *"; q << ") RETURNING *";
for (auto e : data) { for (auto& e : data) {
params.add(e.second, getType(e.first)); params.add(e.second, getType(e.first));
} }
q.flush(); q.flush();
@ -480,7 +523,6 @@ std::tuple<bool, QString> CrudModel::removeRows(const std::set<IntegerRange<int>
// First delete rows in table // First delete rows in table
QString delete_statement = createDeleteStatement(); QString delete_statement = createDeleteStatement();
{ {
db_update_conn.query("BEGIN;");
auto tx = Pgsql::Transaction::startTransaction(db_update_conn); auto tx = Pgsql::Transaction::startTransaction(db_update_conn);
for (auto range : row_ranges) { for (auto range : row_ranges) {
for (int current_row = range.start(); current_row < range.end(); ++current_row) { for (int current_row = range.start(); current_row < range.end(); ++current_row) {

View file

@ -1,6 +1,7 @@
#include "CrudTab.h" #include "CrudTab.h"
#include "ui_CrudTab.h" #include "ui_CrudTab.h"
#include "CrudModel.h" #include "CrudModel.h"
#include "CustomDataRole.h"
#include "ResultTableModelUtil.h" #include "ResultTableModelUtil.h"
#include "PgLabItemDelegate.h" #include "PgLabItemDelegate.h"
#include "IntegerRange.h" #include "IntegerRange.h"
@ -32,6 +33,7 @@ CrudTab::CrudTab(IDatabaseWindow *context, QWidget *parent)
m_SortFilterProxy = new QSortFilterProxyModel(this); m_SortFilterProxy = new QSortFilterProxyModel(this);
m_SortFilterProxy->setSourceModel(m_crudModel); m_SortFilterProxy->setSourceModel(m_crudModel);
m_SortFilterProxy->setSortRole(CustomSortRole);
ui->tableView->setModel(m_SortFilterProxy); ui->tableView->setModel(m_SortFilterProxy);
ui->tableView->setSortingEnabled(true); ui->tableView->setSortingEnabled(true);