2017-08-23 13:27:23 +02:00
|
|
|
|
#include "QueryResultModel.h"
|
2017-12-09 10:45:13 +01:00
|
|
|
|
#include "ResultTableModelUtil.h"
|
2017-02-13 19:51:19 +01:00
|
|
|
|
#include "Pgsql_declare.h"
|
2018-01-15 12:23:41 +01:00
|
|
|
|
#include "Pgsql_oids.h"
|
2018-12-16 10:17:59 +01:00
|
|
|
|
#include "catalog/PgDatabaseCatalog.h"
|
2022-04-02 08:33:48 +02:00
|
|
|
|
#include "CustomDataRole.h"
|
2017-01-18 20:50:53 +01:00
|
|
|
|
#include <QBrush>
|
|
|
|
|
|
#include <QColor>
|
2016-12-26 16:06:55 +01:00
|
|
|
|
|
2017-12-13 18:05:25 +01:00
|
|
|
|
using namespace Pgsql;
|
|
|
|
|
|
|
2017-12-13 18:49:58 +01:00
|
|
|
|
QueryResultModel::QueryResultModel(QObject *parent, std::shared_ptr<Pgsql::Result> r
|
|
|
|
|
|
, std::shared_ptr<PgDatabaseCatalog> cat)
|
2018-01-15 12:23:41 +01:00
|
|
|
|
: QAbstractTableModel(parent)
|
2016-12-26 16:06:55 +01:00
|
|
|
|
, result(std::move(r))
|
2017-12-13 18:49:58 +01:00
|
|
|
|
, catalog(cat)
|
2016-12-26 16:06:55 +01:00
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int QueryResultModel::rowCount(const QModelIndex &) const
|
|
|
|
|
|
{
|
2017-02-18 12:05:48 +01:00
|
|
|
|
int r = result->rows();
|
2016-12-26 16:06:55 +01:00
|
|
|
|
return r;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int QueryResultModel::columnCount(const QModelIndex &) const
|
|
|
|
|
|
{
|
2017-02-18 12:05:48 +01:00
|
|
|
|
int r = result->cols();
|
2016-12-26 16:06:55 +01:00
|
|
|
|
return r;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-09 19:47:33 +01:00
|
|
|
|
QVariant QueryResultModel::data(const QModelIndex &index, int role) const
|
|
|
|
|
|
{
|
2018-01-15 12:23:41 +01:00
|
|
|
|
// static const QString null_str("null");
|
|
|
|
|
|
QVariant v;
|
|
|
|
|
|
|
|
|
|
|
|
const int col = index.column();
|
|
|
|
|
|
const int rij = index.row();
|
|
|
|
|
|
const Oid oid = result->type(col);
|
2019-02-08 10:10:11 +01:00
|
|
|
|
if (role == Qt::DisplayRole || role == Qt::EditRole) {
|
2017-01-18 20:50:53 +01:00
|
|
|
|
if (result->null(col, rij)) {
|
2018-01-15 12:23:41 +01:00
|
|
|
|
//v = nullptr;
|
2016-12-26 16:06:55 +01:00
|
|
|
|
}
|
2018-01-15 12:23:41 +01:00
|
|
|
|
else {
|
2018-02-05 21:47:21 +01:00
|
|
|
|
Value value = result->get(col, rij);
|
2018-01-15 12:23:41 +01:00
|
|
|
|
if (oid == bool_oid) {
|
|
|
|
|
|
bool b = value;
|
|
|
|
|
|
v = b;
|
|
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
QString s = value;
|
|
|
|
|
|
v = s;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-04-02 08:33:48 +02:00
|
|
|
|
else if (role == CustomDataTypeRole) {
|
2018-01-15 12:23:41 +01:00
|
|
|
|
v = oid;
|
2016-12-26 16:06:55 +01:00
|
|
|
|
}
|
2018-01-15 12:23:41 +01:00
|
|
|
|
|
2019-02-07 16:48:30 +01:00
|
|
|
|
|
2018-01-15 12:23:41 +01:00
|
|
|
|
return v;
|
2016-12-26 16:06:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QVariant QueryResultModel::headerData(int section, Qt::Orientation orientation, int role) const
|
|
|
|
|
|
{
|
|
|
|
|
|
QVariant r;
|
|
|
|
|
|
if (role == Qt::DisplayRole) {
|
|
|
|
|
|
if (orientation == Qt::Horizontal) {
|
2017-12-13 18:49:58 +01:00
|
|
|
|
QString s(result->getColName(section));
|
|
|
|
|
|
s += "\n";
|
2018-01-15 12:23:41 +01:00
|
|
|
|
s += getTypeDisplayString(*catalog, result->type(section));
|
2017-12-13 18:49:58 +01:00
|
|
|
|
r = s;
|
2016-12-26 16:06:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
r = QString::number(section + 1);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-02-08 10:10:11 +01:00
|
|
|
|
Qt::ItemFlags f;
|
2016-12-26 16:06:55 +01:00
|
|
|
|
return r;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-02-08 10:10:11 +01:00
|
|
|
|
Qt::ItemFlags QueryResultModel::flags(const QModelIndex &) const
|
|
|
|
|
|
{
|
|
|
|
|
|
return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
|
|
|
|
|
|
}
|