diff --git a/pglab/CustomDataRole.h b/pglab/CustomDataRole.h index 84f19c3..04dc447 100644 --- a/pglab/CustomDataRole.h +++ b/pglab/CustomDataRole.h @@ -23,6 +23,7 @@ enum CustomDataRole { CustomDataTypeRole = Qt::UserRole, ///< Requist the basic type of the value CustomReferencedTypeRole, ///< 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 FirstHiddenValue, ///< Used to request value from a model which is not handed to the view }; diff --git a/pglab/QueryResultModel.cpp b/pglab/QueryResultModel.cpp index 624b809..360a00c 100644 --- a/pglab/QueryResultModel.cpp +++ b/pglab/QueryResultModel.cpp @@ -3,6 +3,7 @@ #include "Pgsql_declare.h" #include "Pgsql_oids.h" #include "catalog/PgDatabaseCatalog.h" +#include "CustomDataRole.h" #include #include @@ -52,7 +53,7 @@ QVariant QueryResultModel::data(const QModelIndex &index, int role) const } } } - else if (role == Qt::UserRole) { + else if (role == CustomDataTypeRole) { v = oid; } diff --git a/pglab/crud/CrudModel.cpp b/pglab/crud/CrudModel.cpp index fec8ab5..7ef5cac 100644 --- a/pglab/crud/CrudModel.cpp +++ b/pglab/crud/CrudModel.cpp @@ -6,6 +6,7 @@ #include "catalog/PgConstraintContainer.h" #include "SqlFormattingUtils.h" #include "Pgsql_oids.h" +#include "CustomDataRole.h" #include #include #include @@ -124,37 +125,79 @@ CrudModel::Value CrudModel::getSavedData(const RowMapping &row_mapping, int colu return val.value_or(Value()); } +QVariant StringToVariant(const std::string &str, Oid oid) +{ + Pgsql::Value v(str.c_str(), oid); + switch (oid) { + default: + return QString::fromStdString(str); + + 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(); + } + +} + QVariant CrudModel::data(const QModelIndex &index, int role) const { - QVariant v; - if (role == Qt::EditRole) { + Oid typ = getType(index.column()); + if (role == Qt::EditRole || role == Qt::DisplayRole) + { auto value = getLatestData(index); - if (value) { - QString s = QString::fromUtf8(value->c_str()); - v = s; - } + if (value) { + if (role == Qt::EditRole) + return QString::fromUtf8(value->c_str()); + else + { + if (typ == Pgsql::bool_oid) + return *value == "t"; //s = (s == "t") ? "TRUE" : "FALSE"; + else + { + QString s = QString::fromUtf8(value->c_str()); + s.truncate(256); + return s; + } + } + } + } + else if (role == CustomDataTypeRole) + return typ; + else if (role == CustomSortRole) + { + auto value = getLatestData(index); + if (value) { + return StringToVariant(value->c_str(), typ); + } + } - } - else if (role == Qt::DisplayRole) { - auto value = getLatestData(index); - 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()); - if (s.length() > 256) { - s.truncate(256); - } - v = s; - } - } - } - else if (role == Qt::UserRole) { - v = getType(index.column()); - } - return v; + return {}; } void CrudModel::loadData() @@ -367,7 +410,7 @@ std::tuple CrudModel::createInsertQuery(const PendingRow for (size_t p = 2; p <= data.size(); ++p) q << ",$" << p; q << ") RETURNING *"; - for (auto e : data) { + for (auto& e : data) { params.add(e.second, getType(e.first)); } q.flush(); @@ -480,7 +523,6 @@ std::tuple CrudModel::removeRows(const std::set // First delete rows in table QString delete_statement = createDeleteStatement(); { - db_update_conn.query("BEGIN;"); auto tx = Pgsql::Transaction::startTransaction(db_update_conn); for (auto range : row_ranges) { for (int current_row = range.start(); current_row < range.end(); ++current_row) { diff --git a/pglab/crud/CrudTab.cpp b/pglab/crud/CrudTab.cpp index 03091fc..6329535 100644 --- a/pglab/crud/CrudTab.cpp +++ b/pglab/crud/CrudTab.cpp @@ -1,6 +1,7 @@ #include "CrudTab.h" #include "ui_CrudTab.h" #include "CrudModel.h" +#include "CustomDataRole.h" #include "ResultTableModelUtil.h" #include "PgLabItemDelegate.h" #include "IntegerRange.h" @@ -32,6 +33,7 @@ CrudTab::CrudTab(IDatabaseWindow *context, QWidget *parent) m_SortFilterProxy = new QSortFilterProxyModel(this); m_SortFilterProxy->setSourceModel(m_crudModel); + m_SortFilterProxy->setSortRole(CustomSortRole); ui->tableView->setModel(m_SortFilterProxy); ui->tableView->setSortingEnabled(true);