#include "mainwindow.h" #include "ui_mainwindow.h" #include "QueryResultModel.h" #include "sqlhighlighter.h" #include //#include namespace pg = Pgsql; const char * test_query = //"SELECT id, program, version, lic_bedrijf, lic_plaats, " //"lic_number, callstack_crc_1, callstack_crc_2, callstack_crc_3, exception_class, " //"exception_message \nFROM foutrapport" "SELECT f1.id, f1.program, f1.version, f1.lic_number, f1.callstack_crc_1, f1.callstack_crc_2, array_agg(f2.id) \n" "FROM foutrapport f1 JOIN foutrapport f2 USING (callstack_crc_2) \n" "WHERE f1.actief \n" "GROUP BY f1.id" ; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); QFont font; font.setFamily("Source Code Pro"); font.setFixedPitch(true); font.setPointSize(10); ui->queryEdit->setFont(font); highlighter.reset(new SqlHighlighter(ui->queryEdit->document())); ui->queryEdit->setPlainText(test_query); ui->connectionStringEdit->setText("user=postgres dbname=foutrapport password=admin"); QAction *action; action = ui->mainToolBar->addAction("connect"); connect(action, &QAction::triggered, this, &MainWindow::startConnect); action = ui->mainToolBar->addAction("execute"); connect(action, &QAction::triggered, this, &MainWindow::performQuery); } MainWindow::~MainWindow() {} void MainWindow::startConnect() { if (connection == nullptr) { connection = std::make_unique(); } QString connstr = ui->connectionStringEdit->text(); bool ok = connection->connectStart(connstr + " application_name=Ivory client_encoding=utf8"); if (ok && connection->status() != CONNECTION_BAD) { // Start polling int s = connection->socket(); connectingState.notifier = std::make_unique(s, QSocketNotifier::Write); connect(connectingState.notifier.get(), &QSocketNotifier::activated, this, &MainWindow::socket_activate_connect); connectingState.poll_state = PGRES_POLLING_WRITING; statusBar()->showMessage(tr("Connecting")); } else { statusBar()->showMessage(tr("Connecting fail")); } } void MainWindow::socket_activate_connect(int ) { connectingState.poll_state = connection->connectPoll(); if (connectingState.poll_state == PGRES_POLLING_OK) { statusBar()->showMessage(tr("Connected")); connectingState.notifier.reset(); } else if (connectingState.poll_state = PGRES_POLLING_FAILED) { statusBar()->showMessage(tr("Connection failed")); connectingState.notifier.reset(); } else if (connectingState.poll_state == PGRES_POLLING_READING) { statusBar()->showMessage(tr("Connecting..")); connectingState.notifier = std::make_unique(connection->socket(), QSocketNotifier::Read); connect(connectingState.notifier.get(), &QSocketNotifier::activated, this, &MainWindow::socket_activate_connect); } else if (connectingState.poll_state == PGRES_POLLING_WRITING) { statusBar()->showMessage(tr("Connecting...")); connectingState.notifier = std::make_unique(connection->socket(), QSocketNotifier::Write); connect(connectingState.notifier.get(), &QSocketNotifier::activated, this, &MainWindow::socket_activate_connect); } } void MainWindow::performQuery() { ui->ResultView->setModel(nullptr); resultModel.reset(); QString command = ui->queryEdit->toPlainText(); queryFuture = std::async(std::launch::async, [this,command]()-> Pgsql::Result { auto res = connection->query(command); QMetaObject::invokeMethod(this, "query_ready", Qt::QueuedConnection); // queues on main thread return res; }); } void MainWindow::query_ready() { pg::Result dbres(std::move(queryFuture.get())); if (dbres.resultStatus() == PGRES_TUPLES_OK) { resultModel.reset(new QueryResultModel(nullptr , std::move(dbres))); ui->ResultView->setModel(resultModel.get()); } else { statusBar()->showMessage(tr("No tuples returned, possibly an error...")); } }