2016-12-26 16:06:55 +01:00
|
|
|
|
#include "mainwindow.h"
|
|
|
|
|
|
#include "ui_mainwindow.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include "QueryResultModel.h"
|
|
|
|
|
|
#include "sqlhighlighter.h"
|
|
|
|
|
|
|
2016-12-27 15:41:11 +01:00
|
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
2016-12-26 16:06:55 +01:00
|
|
|
|
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";
|
|
|
|
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
|
|
|
|
|
|
|
//performQuery();
|
|
|
|
|
|
|
2016-12-27 15:41:11 +01:00
|
|
|
|
QAction *action;
|
|
|
|
|
|
action = ui->mainToolBar->addAction("connect");
|
|
|
|
|
|
connect(action, &QAction::triggered, this, &MainWindow::startConnect);
|
|
|
|
|
|
|
|
|
|
|
|
action = ui->mainToolBar->addAction("execute");
|
2016-12-26 16:06:55 +01:00
|
|
|
|
connect(action, &QAction::triggered, this, &MainWindow::performQuery);
|
2016-12-27 15:41:11 +01:00
|
|
|
|
|
2016-12-26 16:06:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MainWindow::~MainWindow()
|
|
|
|
|
|
{}
|
|
|
|
|
|
|
2016-12-27 15:41:11 +01:00
|
|
|
|
void MainWindow::startConnect()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (connection == nullptr) {
|
|
|
|
|
|
connection = std::make_unique<pg::Connection>();
|
|
|
|
|
|
}
|
|
|
|
|
|
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<QSocketNotifier>(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<QSocketNotifier>(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<QSocketNotifier>(connection->socket(), QSocketNotifier::Write);
|
|
|
|
|
|
connect(connectingState.notifier.get(), &QSocketNotifier::activated, this, &MainWindow::socket_activate_connect);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-12-26 16:06:55 +01:00
|
|
|
|
void MainWindow::performQuery()
|
|
|
|
|
|
{
|
|
|
|
|
|
ui->ResultView->setModel(nullptr);
|
|
|
|
|
|
resultModel.reset();
|
|
|
|
|
|
|
2016-12-27 15:41:11 +01:00
|
|
|
|
if (connection->status() == CONNECTION_OK) {
|
|
|
|
|
|
pg::Result dbres = connection->query(ui->queryEdit->toPlainText());
|
2016-12-26 16:06:55 +01:00
|
|
|
|
resultModel.reset(new QueryResultModel(this , std::move(dbres)));
|
|
|
|
|
|
ui->ResultView->setModel(resultModel.get());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|