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"
|
2017-12-13 18:49:58 +01:00
|
|
|
|
#include "PgDatabaseCatalog.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)
|
2017-12-09 19:47:33 +01:00
|
|
|
|
: BaseTableModel(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
|
|
|
|
Oid QueryResultModel::getType(int column) const
|
2016-12-26 16:06:55 +01:00
|
|
|
|
{
|
2017-12-09 19:47:33 +01:00
|
|
|
|
return result->type(column);
|
|
|
|
|
|
}
|
2017-01-18 20:50:53 +01:00
|
|
|
|
|
2017-12-09 19:47:33 +01:00
|
|
|
|
QVariant QueryResultModel::getData(const QModelIndex &index) const
|
|
|
|
|
|
{
|
2016-12-26 16:06:55 +01:00
|
|
|
|
QVariant r;
|
|
|
|
|
|
int rij = index.row();
|
|
|
|
|
|
int col = index.column();
|
2017-12-09 19:47:33 +01:00
|
|
|
|
if (result->null(col, rij)) {
|
|
|
|
|
|
r = "null";
|
2016-12-26 16:06:55 +01:00
|
|
|
|
}
|
2017-12-09 19:47:33 +01:00
|
|
|
|
else {
|
2017-12-13 18:05:25 +01:00
|
|
|
|
Oid o = result->type(col);
|
|
|
|
|
|
auto value = result->get(col, rij);
|
|
|
|
|
|
switch (o) {
|
|
|
|
|
|
case BOOLOID:
|
|
|
|
|
|
r = bool(value); //s = (s == "t") ? "TRUE" : "FALSE";
|
|
|
|
|
|
break;
|
|
|
|
|
|
default: {
|
|
|
|
|
|
QString s = value;
|
2017-12-09 19:47:33 +01:00
|
|
|
|
if (s.length() > 256) {
|
|
|
|
|
|
s.truncate(256);
|
|
|
|
|
|
}
|
|
|
|
|
|
r = s;
|
2017-12-13 18:05:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-01-18 20:50:53 +01:00
|
|
|
|
}
|
2017-12-09 19:47:33 +01:00
|
|
|
|
return r;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QVariant QueryResultModel::data(const QModelIndex &index, int role) const
|
|
|
|
|
|
{
|
|
|
|
|
|
if (role == Qt::ForegroundRole) {
|
|
|
|
|
|
int rij = index.row();
|
|
|
|
|
|
int col = index.column();
|
2017-01-18 20:50:53 +01:00
|
|
|
|
if (result->null(col, rij)) {
|
2017-12-09 19:47:33 +01:00
|
|
|
|
return QBrush(Qt::gray);
|
2016-12-26 16:06:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-12-09 19:47:33 +01:00
|
|
|
|
return BaseTableModel::data(index, role);
|
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";
|
|
|
|
|
|
s += getTypeDisplayString(*catalog, getType(section));
|
|
|
|
|
|
r = s;
|
2016-12-26 16:06:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
r = QString::number(section + 1);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return r;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//Qt::ItemFlags QueryResultModel::flags(const QModelIndex &) const
|
|
|
|
|
|
//{
|
|
|
|
|
|
// return Qt::ItemIsUserCheckable;
|
|
|
|
|
|
//}
|