pgLab/mainwindow.cpp

106 lines
3.2 KiB
C++
Raw Normal View History

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QueryResultModel.h"
#include "sqlhighlighter.h"
#include <windows.h>
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();
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<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);
}
}
void MainWindow::performQuery()
{
ui->ResultView->setModel(nullptr);
resultModel.reset();
if (connection->status() == CONNECTION_OK) {
pg::Result dbres = connection->query(ui->queryEdit->toPlainText());
resultModel.reset(new QueryResultModel(this , std::move(dbres)));
ui->ResultView->setModel(resultModel.get());
}
}