Query, Explain and Cancel are going throught the asyncdbconnection now.

Todo: Notice processing and error reporting.
This commit is contained in:
Eelke Klein 2017-01-08 09:58:34 +01:00
parent fce51a7b7e
commit a36bf5f7f4
11 changed files with 335 additions and 217 deletions

View file

@ -46,14 +46,14 @@ MainWindow::MainWindow(QWidget *parent)
action = ui->mainToolBar->addAction("connect");
connect(action, &QAction::triggered, this, &MainWindow::startConnect);
// action = ui->mainToolBar->addAction("execute");
// connect(action, &QAction::triggered, this, &MainWindow::performQuery);
action = ui->mainToolBar->addAction("execute");
connect(action, &QAction::triggered, this, &MainWindow::performQuery);
// action = ui->mainToolBar->addAction("explain");
// connect(action, &QAction::triggered, this, &MainWindow::performExplain);
action = ui->mainToolBar->addAction("explain");
connect(action, &QAction::triggered, this, &MainWindow::performExplain);
// action = ui->mainToolBar->addAction("cancel");
// connect(action, &QAction::triggered, this, &MainWindow::cancel_query);
action = ui->mainToolBar->addAction("cancel");
connect(action, &QAction::triggered, this, &MainWindow::cancel_query);
m_dbConnection.setStateCallback([this](ASyncDBConnection::State st)
{
@ -112,174 +112,129 @@ void MainWindow::connectionStateChanged(ASyncDBConnection::State state)
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"));
// }
std::string connstr = ui->connectionStringEdit->text().toUtf8().data();
m_dbConnection.setupConnection(connstr);
}
//void MainWindow::socket_activate_connect(int )
//{
// connectingState.poll_state = connection->connectPoll();
void MainWindow::performQuery()
{
ui->ResultView->setModel(nullptr);
resultModel.reset();
ui->messagesEdit->clear();
QString command = ui->queryEdit->toPlainText();
std::string cmd = command.toUtf8().data();
m_dbConnection.send(cmd,
[this](std::shared_ptr<Pgsql::Result> res)
{
QueueTask([this, res]() { query_ready(res); });
});
}
// if (connectingState.poll_state == PGRES_POLLING_OK) {
// connection->setNoticeReceiver(
// std::bind(&MainWindow::processNotice, this, std::placeholders::_1));
// 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::query_ready(std::shared_ptr<Pgsql::Result> dbres)
{
if (dbres) {
auto st = dbres->resultStatus();
if (st == PGRES_TUPLES_OK) {
resultModel.reset(new QueryResultModel(nullptr , dbres));
ui->ResultView->setModel(resultModel.get());
statusBar()->showMessage(tr("Query ready."));
}
else {
if (st == PGRES_EMPTY_QUERY) {
statusBar()->showMessage(tr("Empty query."));
}
else if (st == PGRES_COMMAND_OK) {
statusBar()->showMessage(tr("Command OK."));
}
else if (st == PGRES_COPY_OUT) {
statusBar()->showMessage(tr("COPY OUT."));
}
else if (st == PGRES_COPY_IN) {
statusBar()->showMessage(tr("COPY IN."));
}
else if (st == PGRES_BAD_RESPONSE) {
statusBar()->showMessage(tr("BAD RESPONSE."));
}
else if (st == PGRES_NONFATAL_ERROR) {
statusBar()->showMessage(tr("NON FATAL ERROR."));
}
else if (st == PGRES_FATAL_ERROR) {
statusBar()->showMessage(tr("FATAL ERROR."));
}
else if (st == PGRES_COPY_BOTH) {
statusBar()->showMessage(tr("COPY BOTH shouldn't happen is for replication."));
}
else if (st == PGRES_SINGLE_TUPLE) {
statusBar()->showMessage(tr("SINGLE TUPLE result."));
}
else {
statusBar()->showMessage(tr("No tuples returned, possibly an error..."));
}
//receiveNotice(dbres->diagDetails());
}
}
else {
statusBar()->showMessage(tr("Query cancelled."));
}
}
void MainWindow::performExplain()
{
ui->ResultView->setModel(nullptr);
resultModel.reset();
ui->messagesEdit->clear();
//void MainWindow::performQuery()
//{
// ui->ResultView->setModel(nullptr);
// resultModel.reset();
// ui->messagesEdit->clear();
QString command = "EXPLAIN (ANALYZE, VERBOSE, BUFFERS, FORMAT JSON) ";
command += ui->queryEdit->toPlainText();
std::string cmd = command.toUtf8().data();
m_dbConnection.send(cmd,
[this](std::shared_ptr<Pgsql::Result> res)
{
if (res) {
// Process explain data seperately
std::thread([this,res]()
{
std::shared_ptr<ExplainRoot> explain;
if (res->getCols() == 1 && res->getRows() == 1) {
std::string s = res->getVal(0, 0);
Json::Value root; // will contains the root value after parsing.
Json::Reader reader;
bool parsingSuccessful = reader.parse(s, root);
if (parsingSuccessful) {
explain = ExplainRoot::createFromJson(root);
}
}
QueueTask([this, explain]() { explain_ready(explain); });
}).detach();
}
});
}
// queryCancel = std::move(connection->getCancel());
void MainWindow::explain_ready(ExplainRoot::SPtr explain)
{
if (explain) {
explainModel.reset(new QueryExplainModel(nullptr, explain));
ui->explainTreeView->setModel(explainModel.get());
ui->explainTreeView->expandAll();
ui->explainTreeView->setColumnWidth(0, 200);
ui->explainTreeView->setColumnWidth(1, 80);
ui->explainTreeView->setColumnWidth(2, 80);
ui->explainTreeView->setColumnWidth(3, 80);
ui->explainTreeView->setColumnWidth(4, 80);
ui->explainTreeView->setColumnWidth(5, 80);
ui->explainTreeView->setColumnWidth(6, 600);
statusBar()->showMessage(tr("Explain ready."));
}
else {
statusBar()->showMessage(tr("Explain failed."));
}
}
// QString command = ui->queryEdit->toPlainText();
// std::thread([this,command]()
// {
// auto res = std::make_shared<Pgsql::Result>(connection->query(command));
// QueueTask([this, res]() { query_ready(std::move(*res)); });
// }).detach();
//}
//void MainWindow::query_ready(Pgsql::Result dbres)
//{
// if (dbres) {
// auto st = dbres.resultStatus();
// if (st == PGRES_TUPLES_OK) {
// resultModel.reset(new QueryResultModel(nullptr , std::move(dbres)));
// ui->ResultView->setModel(resultModel.get());
// statusBar()->showMessage(tr("Query ready."));
// }
// else {
// if (st == PGRES_EMPTY_QUERY) {
// statusBar()->showMessage(tr("Empty query."));
// }
// else if (st == PGRES_COMMAND_OK) {
// statusBar()->showMessage(tr("Command OK."));
// }
// else if (st == PGRES_COPY_OUT) {
// statusBar()->showMessage(tr("COPY OUT."));
// }
// else if (st == PGRES_COPY_IN) {
// statusBar()->showMessage(tr("COPY IN."));
// }
// else if (st == PGRES_BAD_RESPONSE) {
// statusBar()->showMessage(tr("BEAD RESPONSE."));
// }
// else if (st == PGRES_NONFATAL_ERROR) {
// statusBar()->showMessage(tr("NON FATAL ERROR."));
// }
// else if (st == PGRES_FATAL_ERROR) {
// statusBar()->showMessage(tr("FATAL ERROR."));
// }
// else if (st == PGRES_COPY_BOTH) {
// statusBar()->showMessage(tr("COPY BOTH shouldn't happen is for replication."));
// }
// else if (st == PGRES_SINGLE_TUPLE) {
// statusBar()->showMessage(tr("SINGLE TUPLE result."));
// }
// else {
// statusBar()->showMessage(tr("No tuples returned, possibly an error..."));
// }
// receiveNotice(dbres.diagDetails());
// }
// }
// else {
// statusBar()->showMessage(tr("Query cancelled."));
// }
//}
//void MainWindow::performExplain()
//{
// ui->ResultView->setModel(nullptr);
// resultModel.reset();
// ui->messagesEdit->clear();
// queryCancel = std::move(connection->getCancel());
// QString command = "EXPLAIN (ANALYZE, VERBOSE, BUFFERS, FORMAT JSON) ";
// command += ui->queryEdit->toPlainText();
//// explainFuture = std::async(std::launch::async, [this,command]()-> std::unique_ptr<ExplainRoot>
// std::thread([this,command]()
// {
// std::shared_ptr<ExplainRoot> explain;
// auto res = connection->query(command);
// if (res.getCols() == 1 && res.getRows() == 1) {
// std::string s = res.getVal(0, 0);
// Json::Value root; // will contains the root value after parsing.
// Json::Reader reader;
// bool parsingSuccessful = reader.parse(s, root);
// if (parsingSuccessful) {
// explain = ExplainRoot::createFromJson(root);
// }
// }
// QueueTask([this, explain]() { explain_ready(explain); });
// }).detach();
//}
//void MainWindow::explain_ready(ExplainRoot::SPtr explain)
//{
// if (explain) {
// explainModel.reset(new QueryExplainModel(nullptr, explain));
// ui->explainTreeView->setModel(explainModel.get());
// ui->explainTreeView->expandAll();
// ui->explainTreeView->setColumnWidth(0, 200);
// ui->explainTreeView->setColumnWidth(1, 80);
// ui->explainTreeView->setColumnWidth(2, 80);
// ui->explainTreeView->setColumnWidth(3, 80);
// ui->explainTreeView->setColumnWidth(4, 80);
// ui->explainTreeView->setColumnWidth(5, 80);
// ui->explainTreeView->setColumnWidth(6, 600);
// statusBar()->showMessage(tr("Explain ready."));
// }
// else {
// statusBar()->showMessage(tr("Explain failed."));
// }
//}
//void MainWindow::cancel_query()
//{
// queryCancel.cancel();
//}
void MainWindow::cancel_query()
{
m_dbConnection.cancel();
}
//void MainWindow::processNotice(const PGresult *result)
//{