Can use the parameter list in the query window now.

Still requires extensive testing for all possible types.
This commit is contained in:
eelke 2017-02-19 11:12:43 +01:00
parent aefc9eb7ba
commit 3af26d915e
14 changed files with 461 additions and 242 deletions

View file

@ -4,6 +4,7 @@
#include "SqlSyntaxHighlighter.h"
#include <QStandardPaths>
#include <QPushButton>
#include <QFileDialog>
#include <QMessageBox>
#include <QTabWidget>
@ -19,6 +20,44 @@
#include "pgsqldatabasecatalogue.h"
#include "util.h"
QueryParamListController::QueryParamListController(QTableView *tv,
OpenDatabase *opendb, QWidget *parent)
: QObject(parent)
, paramTableView(tv)
, m_openDatabase(opendb)
{
m_typeDelegate.setTypeSelectionModel(opendb->typeSelectionModel());
paramTableView->setModel(&m_paramList);
paramTableView->setItemDelegateForColumn(1, &m_typeDelegate);
}
Pgsql::Params QueryParamListController::params() const
{
Pgsql::Params params;
auto types = m_openDatabase->catalogue()->types();
for (auto e : m_paramList) {
Oid oid = types->getByName(e.type).oid;
params.add(e.value, oid);
}
return params;
}
void QueryParamListController::on_addParam()
{
m_paramList.insertRows(m_paramList.rowCount(), 1);
}
void QueryParamListController::on_removeParam()
{
auto rc = m_paramList.rowCount();
if (rc > 0)
m_paramList.removeRows(rc-1, 1);
}
QueryTab::QueryTab(MainWindow *win, QWidget *parent) :
QWidget(parent),
ui(new Ui::QueryTab),
@ -43,20 +82,16 @@ QueryTab::QueryTab(MainWindow *win, QWidget *parent) :
ui->queryEdit->setFont(font);
OpenDatabase* open_database = m_win->getDatabase();
m_typeDelegate.setTypeSelectionModel(open_database->typeSelectionModel());
auto cat = open_database->catalogue();
highlighter = new SqlSyntaxHighlighter(ui->queryEdit->document());
highlighter->setTypes(cat->types());
ui->paramTableView->setModel(&m_paramList);
ui->paramTableView->setItemDelegateForColumn(1, &m_typeDelegate);
connect(ui->queryEdit, &QPlainTextEdit::textChanged, this, &QueryTab::queryTextChanged);
// m_stopwatch.setOutputLabel(ui->lblElapsedTime);
// ui->lblElapsedTime->clear();
// ui->lblRowCount->clear();
m_queryParamListController = new QueryParamListController(ui->paramTableView, open_database, this);
connect(ui->addButton, &QPushButton::clicked, m_queryParamListController, &QueryParamListController::on_addParam);
connect(ui->removeButton, &QPushButton::clicked, m_queryParamListController, &QueryParamListController::on_removeParam);
}
QueryTab::~QueryTab()
@ -64,16 +99,12 @@ QueryTab::~QueryTab()
m_dbConnection.closeConnection();
m_dbConnection.setStateCallback(nullptr);
// delete m_pgTypes;
delete ui;
}
void QueryTab::setConfig(const ConnectionConfig &config)
{
m_config = config;
// QString title = "pglab - ";
// title += m_config.name().c_str();
// setWindowTitle(title);
m_win->QueueTask([this]() { startConnect(); });
}
@ -89,30 +120,6 @@ bool QueryTab::canClose()
return can_close;
}
//void QueryTab::open()
//{
// if (m_queryTextChanged && !continueWithoutSavingWarning()) {
// return;
// }
// QString home_dir = QStandardPaths::locate(QStandardPaths::HomeLocation, "", QStandardPaths::LocateDirectory);
// QString file_name = QFileDialog::getOpenFileName(this,
// tr("Open sql query"), home_dir, tr("SQL files (*.sql *.txt)"));
// if ( ! file_name.isEmpty()) {
// QFile file(file_name);
// if (file.open(QIODevice::ReadWrite)) {
// QTextStream stream(&file);
// ui->queryEdit->clear();
// while (!stream.atEnd()){
// QString line = stream.readLine();
// ui->queryEdit->appendPlainText(line);
// }
// m_queryTextChanged = false;
// setFileName(file_name);
// }
// }
//}
void QueryTab::newdoc()
{
ui->queryEdit->clear();
@ -183,6 +190,7 @@ void QueryTab::execute()
std::string cmd = getCommand();
m_stopwatch.start();
m_dbConnection.send(cmd,
m_queryParamListController->params(),
[this](std::shared_ptr<Pgsql::Result> res, qint64 elapsedms)
{
m_win->QueueTask([this, res, elapsedms]() { query_ready(res, elapsedms); });
@ -295,26 +303,6 @@ void QueryTab::queryTextChanged()
void QueryTab::connectionStateChanged(ASyncDBConnection::State state)
{
// QString status_str;
// switch (state) {
// case ASyncDBConnection::State::NotConnected:
// status_str = tr("Geen verbinding");
// break;
// case ASyncDBConnection::State::Connecting:
// status_str = tr("Verbinden");
// break;
// case ASyncDBConnection::State::Connected:
// status_str = tr("Verbonden");
// break;
// case ASyncDBConnection::State::QuerySend:
// status_str = tr("Query verstuurd");
// break;
// case ASyncDBConnection::State::CancelSend:
// status_str = tr("Query geannuleerd");
// break;
// }
// addLog(status_str);
QTabWidget *tabwidget = getTabWidget();
if (tabwidget) {
int i = tabwidget->indexOf(this);
@ -334,13 +322,6 @@ void QueryTab::connectionStateChanged(ASyncDBConnection::State state)
}
tabwidget->setTabIcon(i, QIcon(iconname));
}
// statusBar()->showMessage(status_str);
// bool connected = ASyncDBConnection::State::Connected == state;
// ui->actionExecute_SQL->setEnabled(connected);
// ui->actionExplain_Analyze->setEnabled(connected);
// ui->actionCancel->setEnabled(ASyncDBConnection::State::QuerySend == state);
}
@ -488,16 +469,9 @@ void QueryTab::query_ready(std::shared_ptr<Pgsql::Result> dbres, qint64 elapsedm
if (resultList.size() == 1)
ui->tabWidget->setCurrentWidget(trw);
// ui->lblRowCount->setText(rowcount_str);
// resultModel.reset(new QueryResultModel(nullptr , dbres));
// ui->ResultView->setModel(resultModel.get());
// ui->tabWidget->setCurrentWidget(ui->dataTab);
//statusBar()->showMessage(tr("Query ready."));
}
else {
if (st == PGRES_COMMAND_OK) {
// statusBar()->showMessage(tr("Command OK."));
int tuples_affected = dbres->tuplesAffected();
QString msg;
if (tuples_affected >= 0)
@ -548,18 +522,14 @@ void QueryTab::query_ready(std::shared_ptr<Pgsql::Result> dbres, qint64 elapsedm
else {
m_stopwatch.stop();
addLog("query_ready with NO result");
// statusBar()->showMessage(tr("Query cancelled."));
}
}
void QueryTab::clearResult()
{
// ui->ResultView->setModel(nullptr);
// resultModel.reset();
for (auto e : resultList)
delete e;
resultList.clear();
// ui->lblRowCount->clear();
}
void QueryTab::copyQueryAsCString()