2019-01-05 19:58:23 +01:00
|
|
|
|
#include "QueryTool.h"
|
2017-08-23 13:27:23 +02:00
|
|
|
|
#include "ui_QueryTab.h"
|
2017-02-07 21:39:45 +01:00
|
|
|
|
#include "SqlSyntaxHighlighter.h"
|
2017-01-21 18:16:57 +01:00
|
|
|
|
#include <QStandardPaths>
|
2017-02-19 11:12:43 +01:00
|
|
|
|
#include <QPushButton>
|
2018-08-05 09:07:12 +02:00
|
|
|
|
#include <QAction>
|
2017-01-21 18:16:57 +01:00
|
|
|
|
#include <QFileDialog>
|
|
|
|
|
|
#include <QMessageBox>
|
2019-08-16 08:29:27 +02:00
|
|
|
|
#include <QStatusBar>
|
2017-01-22 08:50:41 +01:00
|
|
|
|
#include <QTabWidget>
|
|
|
|
|
|
#include <QTextCodec>
|
2017-01-21 18:16:57 +01:00
|
|
|
|
#include <QTextDocumentFragment>
|
|
|
|
|
|
#include <QTextStream>
|
2017-02-05 08:23:06 +01:00
|
|
|
|
#include <QClipboard>
|
2017-08-23 13:27:23 +02:00
|
|
|
|
#include "ExplainTreeModelItem.h"
|
2017-01-21 18:16:57 +01:00
|
|
|
|
#include "json/json.h"
|
2017-02-04 11:55:49 +01:00
|
|
|
|
#include "OpenDatabase.h"
|
2018-12-16 10:17:59 +01:00
|
|
|
|
#include "catalog/PgDatabaseCatalog.h"
|
2017-12-09 14:16:47 +01:00
|
|
|
|
#include "QueryParamListController.h"
|
2017-01-21 18:16:57 +01:00
|
|
|
|
#include "util.h"
|
2018-09-18 11:53:19 +02:00
|
|
|
|
#include "UserConfiguration.h"
|
2019-08-16 08:29:27 +02:00
|
|
|
|
#include "IDatabaseWindow.h"
|
2017-01-21 18:16:57 +01:00
|
|
|
|
|
2019-01-05 19:58:23 +01:00
|
|
|
|
|
2019-08-16 08:29:27 +02:00
|
|
|
|
QueryTool::QueryTool(IDatabaseWindow *context, QWidget *parent)
|
2019-08-15 16:18:47 +02:00
|
|
|
|
: QWidget(parent)
|
2019-08-16 08:29:27 +02:00
|
|
|
|
, m_context(context)
|
2018-12-29 18:59:54 +01:00
|
|
|
|
, ui(new Ui::QueryTab)
|
2019-11-06 20:03:27 +01:00
|
|
|
|
, m_dbConnection()
|
2017-01-21 08:09:12 +01:00
|
|
|
|
{
|
|
|
|
|
|
ui->setupUi(this);
|
2017-01-21 18:16:57 +01:00
|
|
|
|
|
2019-08-16 08:29:27 +02:00
|
|
|
|
auto db = context->openDatabase();
|
|
|
|
|
|
m_config = db->config();
|
|
|
|
|
|
m_catalog = db->catalog();
|
2018-12-30 15:44:05 +01:00
|
|
|
|
|
2019-01-05 19:58:23 +01:00
|
|
|
|
connect(&m_dbConnection, &ASyncDBConnection::onStateChanged, this, &QueryTool::connectionStateChanged);
|
|
|
|
|
|
connect(&m_dbConnection, &ASyncDBConnection::onNotice, this, &QueryTool::receiveNotice);
|
2018-01-08 20:54:03 +01:00
|
|
|
|
|
2018-09-18 11:53:19 +02:00
|
|
|
|
ui->queryEdit->setFont(UserConfiguration::instance()->codeFont());
|
2017-01-21 18:16:57 +01:00
|
|
|
|
|
2017-02-10 21:01:09 +01:00
|
|
|
|
highlighter = new SqlSyntaxHighlighter(ui->queryEdit->document());
|
2019-11-02 14:28:48 +01:00
|
|
|
|
auto types = m_catalog->types();
|
|
|
|
|
|
if (types) {
|
|
|
|
|
|
highlighter->setTypes(*types);
|
|
|
|
|
|
}
|
2017-02-04 11:55:49 +01:00
|
|
|
|
|
2019-01-05 19:58:23 +01:00
|
|
|
|
connect(ui->queryEdit, &QPlainTextEdit::textChanged, this, &QueryTool::queryTextChanged);
|
2017-02-19 11:12:43 +01:00
|
|
|
|
|
2019-08-16 08:29:27 +02:00
|
|
|
|
m_queryParamListController = new QueryParamListController(ui->paramTableView, m_context->openDatabase(), this);
|
2017-09-01 21:07:37 +02:00
|
|
|
|
connect(ui->addButton, &QPushButton::clicked, m_queryParamListController,
|
|
|
|
|
|
&QueryParamListController::on_addParam);
|
|
|
|
|
|
connect(ui->removeButton, &QPushButton::clicked, m_queryParamListController,
|
|
|
|
|
|
&QueryParamListController::on_removeParam);
|
2018-12-30 15:44:05 +01:00
|
|
|
|
|
2018-12-31 11:38:54 +01:00
|
|
|
|
startConnect();
|
2017-01-21 08:09:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-05 19:58:23 +01:00
|
|
|
|
QueryTool::~QueryTool()
|
2017-01-21 08:09:12 +01:00
|
|
|
|
{
|
|
|
|
|
|
delete ui;
|
|
|
|
|
|
}
|
2017-01-21 18:16:57 +01:00
|
|
|
|
|
2019-01-05 19:58:23 +01:00
|
|
|
|
bool QueryTool::canClose()
|
2017-01-21 18:16:57 +01:00
|
|
|
|
{
|
2017-01-22 08:50:41 +01:00
|
|
|
|
bool can_close;
|
|
|
|
|
|
if (m_queryTextChanged) {
|
|
|
|
|
|
can_close = continueWithoutSavingWarning();
|
|
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
can_close = true;
|
2017-01-21 18:16:57 +01:00
|
|
|
|
}
|
2017-01-22 08:50:41 +01:00
|
|
|
|
return can_close;
|
|
|
|
|
|
}
|
2017-01-21 18:16:57 +01:00
|
|
|
|
|
2019-01-05 19:58:23 +01:00
|
|
|
|
void QueryTool::newdoc()
|
2017-01-22 08:50:41 +01:00
|
|
|
|
{
|
|
|
|
|
|
ui->queryEdit->clear();
|
2017-02-19 17:40:43 +01:00
|
|
|
|
setFileName(tr("new"));
|
2017-01-22 08:50:41 +01:00
|
|
|
|
m_queryTextChanged = false;
|
2017-02-19 17:40:43 +01:00
|
|
|
|
m_new = true;
|
2017-01-22 08:50:41 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-05 19:58:23 +01:00
|
|
|
|
bool QueryTool::load(const QString &filename)
|
2017-01-22 08:50:41 +01:00
|
|
|
|
{
|
|
|
|
|
|
bool result = false;
|
|
|
|
|
|
QFile file(filename);
|
|
|
|
|
|
if (file.open(QIODevice::ReadOnly)) {
|
|
|
|
|
|
QByteArray ba = file.readAll();
|
|
|
|
|
|
const char *ptr = ba.constData();
|
|
|
|
|
|
|
|
|
|
|
|
QTextCodec *codec = QTextCodec::codecForUtfText(ba, QTextCodec::codecForName("utf-8"));
|
|
|
|
|
|
QTextCodec::ConverterState state;
|
|
|
|
|
|
QString text = codec->toUnicode(ptr, ba.size(), &state);
|
|
|
|
|
|
if (state.invalidChars > 0) {
|
|
|
|
|
|
file.reset();
|
2017-01-21 18:16:57 +01:00
|
|
|
|
QTextStream stream(&file);
|
2017-01-22 08:50:41 +01:00
|
|
|
|
text = stream.readAll();
|
2017-01-21 18:16:57 +01:00
|
|
|
|
}
|
2017-01-22 08:50:41 +01:00
|
|
|
|
|
|
|
|
|
|
ui->queryEdit->setPlainText(text);
|
|
|
|
|
|
m_queryTextChanged = false;
|
|
|
|
|
|
setFileName(filename);
|
2017-02-19 17:40:43 +01:00
|
|
|
|
m_new = false;
|
2017-01-22 08:50:41 +01:00
|
|
|
|
result = true;
|
2017-01-21 18:16:57 +01:00
|
|
|
|
}
|
2017-01-22 08:50:41 +01:00
|
|
|
|
return result;
|
2017-01-21 18:16:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-05 19:58:23 +01:00
|
|
|
|
bool QueryTool::save()
|
2017-01-21 18:16:57 +01:00
|
|
|
|
{
|
2017-02-19 14:46:01 +01:00
|
|
|
|
bool result;
|
2017-02-19 17:40:43 +01:00
|
|
|
|
if (m_fileName.isEmpty() || m_new) {
|
2017-02-19 14:46:01 +01:00
|
|
|
|
result = saveAs();
|
2017-01-21 18:16:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
else {
|
2017-02-19 14:46:01 +01:00
|
|
|
|
result = saveSqlTo(m_fileName);
|
2017-01-21 18:16:57 +01:00
|
|
|
|
}
|
2017-02-19 14:46:01 +01:00
|
|
|
|
return result;
|
2017-01-21 18:16:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-05 19:58:23 +01:00
|
|
|
|
bool QueryTool::saveAs()
|
2017-01-21 18:16:57 +01:00
|
|
|
|
{
|
2017-02-19 14:46:01 +01:00
|
|
|
|
bool result = false;
|
2017-01-21 18:16:57 +01:00
|
|
|
|
QString filename = promptUserForSaveSqlFilename();
|
|
|
|
|
|
if (!filename.isEmpty()) {
|
2017-02-19 14:46:01 +01:00
|
|
|
|
result = saveSqlTo(filename);
|
|
|
|
|
|
if (result) {
|
|
|
|
|
|
setFileName(filename);
|
2017-02-19 17:40:43 +01:00
|
|
|
|
m_new = false;
|
2017-02-19 14:46:01 +01:00
|
|
|
|
}
|
2017-01-21 18:16:57 +01:00
|
|
|
|
}
|
2017-02-19 14:46:01 +01:00
|
|
|
|
return result;
|
2017-01-21 18:16:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-05 19:58:23 +01:00
|
|
|
|
void QueryTool::saveCopyAs()
|
2017-01-21 18:16:57 +01:00
|
|
|
|
{
|
|
|
|
|
|
QString filename = promptUserForSaveSqlFilename();
|
|
|
|
|
|
if (!filename.isEmpty()) {
|
|
|
|
|
|
saveSqlTo(filename);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-05 19:58:23 +01:00
|
|
|
|
void QueryTool::execute()
|
2017-01-21 18:16:57 +01:00
|
|
|
|
{
|
|
|
|
|
|
if (m_dbConnection.state() == ASyncDBConnection::State::Connected) {
|
|
|
|
|
|
addLog("Query clicked");
|
|
|
|
|
|
|
2017-01-22 08:50:41 +01:00
|
|
|
|
clearResult();
|
2017-01-21 18:16:57 +01:00
|
|
|
|
ui->messagesEdit->clear();
|
|
|
|
|
|
|
2017-10-05 16:02:06 +02:00
|
|
|
|
std::string cmd = getCommandUtf8();
|
2017-01-22 08:50:41 +01:00
|
|
|
|
m_stopwatch.start();
|
2017-09-03 14:37:12 +02:00
|
|
|
|
|
2017-12-12 20:15:07 +01:00
|
|
|
|
auto cb = [this](Expected<std::shared_ptr<Pgsql::Result>> res, qint64 elapsedms)
|
|
|
|
|
|
{
|
2018-12-31 11:38:54 +01:00
|
|
|
|
if (res.valid()) {
|
|
|
|
|
|
auto && dbresult = res.get();
|
|
|
|
|
|
QMetaObject::invokeMethod(this, "query_ready",
|
|
|
|
|
|
Q_ARG(std::shared_ptr<Pgsql::Result>, dbresult),
|
|
|
|
|
|
Q_ARG(qint64, elapsedms));
|
|
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
/// \todo handle error
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2017-12-12 20:15:07 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
2019-09-26 20:14:57 +02:00
|
|
|
|
try {
|
|
|
|
|
|
if (m_queryParamListController->empty())
|
|
|
|
|
|
m_dbConnection.send(cmd, cb);
|
|
|
|
|
|
else
|
|
|
|
|
|
m_dbConnection.send(cmd, m_queryParamListController->params(), cb);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (const std::exception &ex) {
|
|
|
|
|
|
QMessageBox msgBox;
|
|
|
|
|
|
msgBox.setIcon(QMessageBox::Critical);
|
|
|
|
|
|
msgBox.setText(QString("Error executing query: %1").arg(QString::fromUtf8(ex.what())));
|
|
|
|
|
|
msgBox.setStandardButtons(QMessageBox::Close);
|
|
|
|
|
|
msgBox.setDefaultButton(QMessageBox::Close);
|
|
|
|
|
|
msgBox.exec();
|
|
|
|
|
|
}
|
2017-01-21 18:16:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-05 19:58:23 +01:00
|
|
|
|
void QueryTool::explain(bool analyze)
|
2017-01-21 18:16:57 +01:00
|
|
|
|
{
|
|
|
|
|
|
ui->explainTreeView->setModel(nullptr);
|
|
|
|
|
|
explainModel.reset();
|
|
|
|
|
|
ui->messagesEdit->clear();
|
|
|
|
|
|
|
|
|
|
|
|
addLog("Explain clicked");
|
|
|
|
|
|
|
2017-01-25 06:54:21 +01:00
|
|
|
|
std::string analyze_str;
|
|
|
|
|
|
if (analyze) {
|
2017-12-09 10:45:13 +01:00
|
|
|
|
analyze_str = "ANALYZE, BUFFERS, ";
|
2017-01-25 06:54:21 +01:00
|
|
|
|
}
|
2017-01-22 08:50:41 +01:00
|
|
|
|
m_stopwatch.start();
|
2017-12-09 10:45:13 +01:00
|
|
|
|
std::string cmd = "EXPLAIN (" + analyze_str + "VERBOSE, FORMAT JSON) " + getCommandUtf8();
|
2017-12-12 20:15:07 +01:00
|
|
|
|
|
|
|
|
|
|
auto cb = [this](Expected<std::shared_ptr<Pgsql::Result>> exp_res, qint64 )
|
|
|
|
|
|
{
|
|
|
|
|
|
if (exp_res.valid()) {
|
|
|
|
|
|
// Process explain data seperately
|
|
|
|
|
|
auto res = exp_res.get();
|
|
|
|
|
|
if (res) {
|
|
|
|
|
|
std::thread([this,res]()
|
|
|
|
|
|
{
|
|
|
|
|
|
std::shared_ptr<ExplainRoot> explain;
|
|
|
|
|
|
if (res->cols() == 1 && res->rows() == 1) {
|
|
|
|
|
|
std::string s = res->val(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);
|
2017-01-21 18:16:57 +01:00
|
|
|
|
}
|
2017-12-12 20:15:07 +01:00
|
|
|
|
}
|
2018-12-31 11:38:54 +01:00
|
|
|
|
QMetaObject::invokeMethod(this, "explain_ready",
|
|
|
|
|
|
Q_ARG(ExplainRoot::SPtr, explain));
|
2017-12-12 20:15:07 +01:00
|
|
|
|
}).detach();
|
2017-01-21 18:16:57 +01:00
|
|
|
|
}
|
2017-12-12 20:15:07 +01:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if (m_queryParamListController->empty())
|
|
|
|
|
|
m_dbConnection.send(cmd, cb);
|
|
|
|
|
|
else
|
|
|
|
|
|
m_dbConnection.send(cmd, m_queryParamListController->params(), cb);
|
2017-01-21 18:16:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-05 19:58:23 +01:00
|
|
|
|
void QueryTool::cancel()
|
2017-01-21 18:16:57 +01:00
|
|
|
|
{
|
|
|
|
|
|
m_dbConnection.cancel();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-12-03 19:08:13 +01:00
|
|
|
|
QString QueryTool::title() const
|
|
|
|
|
|
{
|
|
|
|
|
|
QFileInfo fileInfo(m_fileName);
|
|
|
|
|
|
QString fn(fileInfo.fileName());
|
|
|
|
|
|
return fn;
|
|
|
|
|
|
}
|
2017-01-21 18:16:57 +01:00
|
|
|
|
|
2019-01-05 19:58:23 +01:00
|
|
|
|
void QueryTool::setFileName(const QString &filename)
|
2017-01-21 18:16:57 +01:00
|
|
|
|
{
|
|
|
|
|
|
m_fileName = filename;
|
2017-01-25 06:54:21 +01:00
|
|
|
|
QFileInfo fileInfo(filename);
|
|
|
|
|
|
QString fn(fileInfo.fileName());
|
2019-08-16 08:29:27 +02:00
|
|
|
|
m_context->setTitleForWidget(this, fn, m_fileName);
|
2017-01-21 18:16:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-05 19:58:23 +01:00
|
|
|
|
bool QueryTool::continueWithoutSavingWarning()
|
2017-01-21 18:16:57 +01:00
|
|
|
|
{
|
|
|
|
|
|
QMessageBox msgBox;
|
|
|
|
|
|
msgBox.setIcon(QMessageBox::Warning);
|
2017-02-19 14:46:01 +01:00
|
|
|
|
msgBox.setText(QString("Save changes in document \"%1\" before closing?").arg(m_fileName));
|
|
|
|
|
|
msgBox.setInformativeText("The changes will be lost when you choose Discard.");
|
|
|
|
|
|
msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
|
|
|
|
|
|
msgBox.setDefaultButton(QMessageBox::Cancel);
|
2017-01-21 18:16:57 +01:00
|
|
|
|
int ret = msgBox.exec();
|
2017-02-19 14:46:01 +01:00
|
|
|
|
if (ret == QMessageBox::Save) {
|
|
|
|
|
|
if (!save()) {
|
|
|
|
|
|
// save failed or was a saveAs and was cancelled, don't close!
|
|
|
|
|
|
ret = QMessageBox::Cancel;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return ret != QMessageBox::Cancel;
|
2017-01-21 18:16:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-05 19:58:23 +01:00
|
|
|
|
bool QueryTool::saveSqlTo(const QString &filename)
|
2017-01-21 18:16:57 +01:00
|
|
|
|
{
|
2017-01-22 08:50:41 +01:00
|
|
|
|
bool result = false;
|
2017-12-09 14:16:47 +01:00
|
|
|
|
QFileInfo fileinfo(filename);
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-01-21 18:16:57 +01:00
|
|
|
|
QFile file(filename);
|
2017-01-22 08:50:41 +01:00
|
|
|
|
if (file.open(QIODevice::WriteOnly)) {
|
2017-01-21 18:16:57 +01:00
|
|
|
|
QTextStream stream(&file);
|
2017-01-22 08:50:41 +01:00
|
|
|
|
stream.setCodec("utf-8");
|
2017-01-21 18:16:57 +01:00
|
|
|
|
QString text = ui->queryEdit->toPlainText();
|
|
|
|
|
|
stream << text;
|
2017-01-22 08:50:41 +01:00
|
|
|
|
|
2017-02-19 14:46:01 +01:00
|
|
|
|
stream.flush();
|
2017-01-22 08:50:41 +01:00
|
|
|
|
if (stream.status() == QTextStream::Ok) {
|
|
|
|
|
|
m_queryTextChanged = false;
|
|
|
|
|
|
result = true;
|
|
|
|
|
|
}
|
2017-01-21 18:16:57 +01:00
|
|
|
|
}
|
2017-01-22 08:50:41 +01:00
|
|
|
|
return result;
|
2017-01-21 18:16:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-09 14:16:47 +01:00
|
|
|
|
|
2019-01-05 19:58:23 +01:00
|
|
|
|
QString QueryTool::promptUserForSaveSqlFilename()
|
2017-01-21 18:16:57 +01:00
|
|
|
|
{
|
|
|
|
|
|
QString home_dir = QStandardPaths::locate(QStandardPaths::HomeLocation, "", QStandardPaths::LocateDirectory);
|
|
|
|
|
|
return QFileDialog::getSaveFileName(this, tr("Save query"), home_dir, tr("SQL file (*.sql)"));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-05 19:58:23 +01:00
|
|
|
|
void QueryTool::queryTextChanged()
|
2017-01-21 18:16:57 +01:00
|
|
|
|
{
|
|
|
|
|
|
m_queryTextChanged = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-05 19:58:23 +01:00
|
|
|
|
void QueryTool::connectionStateChanged(ASyncDBConnection::State state)
|
2017-01-21 18:16:57 +01:00
|
|
|
|
{
|
2018-12-29 18:59:54 +01:00
|
|
|
|
QString iconname;
|
|
|
|
|
|
switch (state) {
|
|
|
|
|
|
case ASyncDBConnection::State::NotConnected:
|
|
|
|
|
|
case ASyncDBConnection::State::Connecting:
|
2019-08-16 08:29:27 +02:00
|
|
|
|
iconname = "red.png";
|
2018-12-29 18:59:54 +01:00
|
|
|
|
break;
|
|
|
|
|
|
case ASyncDBConnection::State::Connected:
|
2019-08-16 08:29:27 +02:00
|
|
|
|
iconname = "green.png";
|
2018-12-29 18:59:54 +01:00
|
|
|
|
break;
|
|
|
|
|
|
case ASyncDBConnection::State::QuerySend:
|
|
|
|
|
|
case ASyncDBConnection::State::CancelSend:
|
2019-08-16 08:29:27 +02:00
|
|
|
|
iconname = "yellow.png";
|
2018-12-29 18:59:54 +01:00
|
|
|
|
break;
|
|
|
|
|
|
case ASyncDBConnection::State::Terminating:
|
|
|
|
|
|
break;
|
2017-01-21 18:16:57 +01:00
|
|
|
|
}
|
2019-08-16 08:29:27 +02:00
|
|
|
|
m_context->setIconForWidget(this, QIcon(":/icons/16x16/document_" + iconname));
|
2017-01-21 18:16:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-05 19:58:23 +01:00
|
|
|
|
void QueryTool::addLog(QString s)
|
2017-01-21 18:16:57 +01:00
|
|
|
|
{
|
|
|
|
|
|
QTextCursor text_cursor = QTextCursor(ui->edtLog->document());
|
|
|
|
|
|
text_cursor.movePosition(QTextCursor::End);
|
|
|
|
|
|
text_cursor.insertText(s + "\r\n");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-05 19:58:23 +01:00
|
|
|
|
void QueryTool::receiveNotice(Pgsql::ErrorDetails notice)
|
2017-01-21 18:16:57 +01:00
|
|
|
|
{
|
|
|
|
|
|
ui->messagesEdit->append(QString::fromStdString(notice.errorMessage));
|
|
|
|
|
|
|
|
|
|
|
|
ui->messagesEdit->append(QString::fromStdString(notice.severity));
|
|
|
|
|
|
ui->messagesEdit->append(QString("At position: %1").arg(notice.statementPosition));
|
|
|
|
|
|
ui->messagesEdit->append(QString::fromStdString("State: " + notice.state));
|
|
|
|
|
|
ui->messagesEdit->append(QString::fromStdString("Primary: " + notice.messagePrimary));
|
|
|
|
|
|
ui->messagesEdit->append(QString::fromStdString("Detail: " + notice.messageDetail));
|
|
|
|
|
|
ui->messagesEdit->append(QString::fromStdString("Hint: " + notice.messageHint));
|
|
|
|
|
|
ui->messagesEdit->append(QString::fromStdString("Context: " + notice.context));
|
|
|
|
|
|
// std::string state; ///< PG_DIAG_SQLSTATE Error code as listed in https://www.postgresql.org/docs/9.5/static/errcodes-appendix.html
|
|
|
|
|
|
// std::string severity;
|
|
|
|
|
|
// std::string messagePrimary;
|
|
|
|
|
|
// std::string messageDetail;
|
|
|
|
|
|
// std::string messageHint;
|
|
|
|
|
|
// int statementPosition; ///< First character is one, measured in characters not bytes!
|
|
|
|
|
|
// std::string context;
|
|
|
|
|
|
|
|
|
|
|
|
// int internalPosition;
|
|
|
|
|
|
// std::string internalQuery;
|
|
|
|
|
|
// std::string schemaName;
|
|
|
|
|
|
// std::string tableName;
|
|
|
|
|
|
// std::string columnName;
|
|
|
|
|
|
// std::string datatypeName;
|
|
|
|
|
|
// std::string constraintName;
|
|
|
|
|
|
// std::string sourceFile;
|
|
|
|
|
|
// std::string sourceLine;
|
|
|
|
|
|
// std::string sourceFunction;
|
|
|
|
|
|
|
|
|
|
|
|
// QTextCursor cursor = ui->messagesEdit->textCursor();
|
|
|
|
|
|
// cursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
|
|
|
|
|
|
|
|
|
|
|
|
// QTextTable *table = cursor.insertTable(4, 2);
|
|
|
|
|
|
// if (table) {
|
|
|
|
|
|
// table->cellAt(1, 0).firstCursorPosition().insertText("State");
|
|
|
|
|
|
// table->cellAt(1, 1).firstCursorPosition().insertText(QString::fromStdString(notice.state));
|
|
|
|
|
|
// table->cellAt(2, 0).firstCursorPosition().insertText("Primary");
|
|
|
|
|
|
// table->cellAt(2, 1).firstCursorPosition().insertText(QString::fromStdString(notice.messagePrimary));
|
|
|
|
|
|
// table->cellAt(3, 0).firstCursorPosition().insertText("Detail");
|
|
|
|
|
|
// table->cellAt(3, 1).firstCursorPosition().insertText(QString::fromStdString(notice.messageDetail));
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// syntax error at or near "limit
|
|
|
|
|
|
// statementPosition
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-05 19:58:23 +01:00
|
|
|
|
void QueryTool::startConnect()
|
2017-01-21 18:16:57 +01:00
|
|
|
|
{
|
|
|
|
|
|
m_dbConnection.setupConnection(m_config);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-05 19:58:23 +01:00
|
|
|
|
void QueryTool::explain_ready(ExplainRoot::SPtr explain)
|
2017-01-21 18:16:57 +01:00
|
|
|
|
{
|
2017-01-22 08:50:41 +01:00
|
|
|
|
m_stopwatch.stop();
|
2017-01-21 18:16:57 +01:00
|
|
|
|
if (explain) {
|
|
|
|
|
|
addLog("Explain ready");
|
2017-02-18 12:03:52 +01:00
|
|
|
|
QString times_str;
|
|
|
|
|
|
if (explain->totalRuntime > 0.f)
|
|
|
|
|
|
times_str = QString("Total time: %1").arg(
|
|
|
|
|
|
msfloatToHumanReadableString(explain->totalRuntime));
|
|
|
|
|
|
else
|
|
|
|
|
|
times_str = QString("Execution time: %1, Planning time: %2").arg(
|
2017-01-21 18:16:57 +01:00
|
|
|
|
msfloatToHumanReadableString(explain->executionTime)
|
|
|
|
|
|
, msfloatToHumanReadableString(explain->planningTime));
|
|
|
|
|
|
ui->lblTimes->setText(times_str);
|
|
|
|
|
|
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);
|
|
|
|
|
|
ui->tabWidget->setCurrentWidget(ui->explainTab);
|
2017-12-30 12:57:55 +01:00
|
|
|
|
|
2019-08-16 08:29:27 +02:00
|
|
|
|
m_context->showStatusBarMessage(tr("Explain ready."));
|
|
|
|
|
|
|
2017-01-21 18:16:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
else {
|
2019-08-16 08:29:27 +02:00
|
|
|
|
addLog(tr("Explain no result"));
|
2017-01-21 18:16:57 +01:00
|
|
|
|
ui->tabWidget->setCurrentWidget(ui->messageTab);
|
2019-08-16 08:29:27 +02:00
|
|
|
|
m_context->showStatusBarMessage(tr("Explain no result"));
|
2017-01-21 18:16:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-05 19:58:23 +01:00
|
|
|
|
QString QueryTool::getCommand() const
|
2017-01-21 18:16:57 +01:00
|
|
|
|
{
|
|
|
|
|
|
QString command;
|
|
|
|
|
|
QTextCursor cursor = ui->queryEdit->textCursor();
|
|
|
|
|
|
if (cursor.hasSelection()) {
|
|
|
|
|
|
command = cursor.selection().toPlainText();
|
|
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
command = ui->queryEdit->toPlainText();
|
|
|
|
|
|
}
|
2017-10-05 16:02:06 +02:00
|
|
|
|
return command;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-05 19:58:23 +01:00
|
|
|
|
std::string QueryTool::getCommandUtf8() const
|
2017-10-05 16:02:06 +02:00
|
|
|
|
{
|
|
|
|
|
|
return getCommand().toUtf8().data();
|
2017-01-21 18:16:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-05 19:58:23 +01:00
|
|
|
|
void QueryTool::query_ready(std::shared_ptr<Pgsql::Result> dbres, qint64 elapsedms)
|
2017-09-10 10:11:58 +02:00
|
|
|
|
{
|
2018-12-31 11:38:54 +01:00
|
|
|
|
if (dbres) {
|
|
|
|
|
|
addLog("query_ready with result");
|
|
|
|
|
|
auto st = dbres->resultStatus();
|
|
|
|
|
|
if (st == PGRES_TUPLES_OK) {
|
|
|
|
|
|
//int n_rows = dbres->getRows();
|
|
|
|
|
|
//QString rowcount_str = QString("rows: %1").arg(dbres->getRows());
|
|
|
|
|
|
|
|
|
|
|
|
auto result_model = std::make_shared<QueryResultModel>(nullptr , dbres,
|
|
|
|
|
|
m_catalog);
|
|
|
|
|
|
TuplesResultWidget *trw = new TuplesResultWidget;
|
|
|
|
|
|
trw->setResult(result_model, elapsedms);
|
|
|
|
|
|
resultList.push_back(trw);
|
|
|
|
|
|
ui->tabWidget->addTab(trw, "Data");
|
|
|
|
|
|
if (resultList.size() == 1)
|
|
|
|
|
|
ui->tabWidget->setCurrentWidget(trw);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
if (st == PGRES_COMMAND_OK) {
|
|
|
|
|
|
int tuples_affected = dbres->tuplesAffected();
|
|
|
|
|
|
QString msg;
|
|
|
|
|
|
if (tuples_affected >= 0)
|
|
|
|
|
|
msg = tr("Query returned succesfully: %1 rows affected, execution time %2")
|
|
|
|
|
|
.arg(QString::number(tuples_affected))
|
|
|
|
|
|
.arg(msfloatToHumanReadableString(elapsedms));
|
|
|
|
|
|
else
|
|
|
|
|
|
msg = tr("Query returned succesfully, execution time %1")
|
|
|
|
|
|
.arg(msfloatToHumanReadableString(elapsedms));
|
|
|
|
|
|
|
|
|
|
|
|
ui->messagesEdit->append(msg);
|
|
|
|
|
|
|
|
|
|
|
|
ui->tabWidget->setCurrentWidget(ui->messageTab);
|
|
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
// if (st == PGRES_EMPTY_QUERY) {
|
|
|
|
|
|
// statusBar()->showMessage(tr("Empty query."));
|
|
|
|
|
|
// }
|
|
|
|
|
|
// 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..."));
|
|
|
|
|
|
// }
|
|
|
|
|
|
ui->tabWidget->setCurrentWidget(ui->messageTab);
|
|
|
|
|
|
auto details = dbres->diagDetails();
|
|
|
|
|
|
markError(details);
|
|
|
|
|
|
receiveNotice(details);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
m_stopwatch.stop();
|
|
|
|
|
|
addLog("query_ready with NO result");
|
|
|
|
|
|
}
|
2017-01-21 18:16:57 +01:00
|
|
|
|
}
|
2017-01-22 08:50:41 +01:00
|
|
|
|
|
2019-01-05 19:58:23 +01:00
|
|
|
|
void QueryTool::markError(const Pgsql::ErrorDetails &details)
|
2018-04-09 21:44:00 +02:00
|
|
|
|
{
|
|
|
|
|
|
if (details.statementPosition > 0) {
|
|
|
|
|
|
QTextCursor cursor = ui->queryEdit->textCursor();
|
2018-11-10 10:59:52 +01:00
|
|
|
|
|
|
|
|
|
|
// Following finds out the start of the current selection
|
|
|
|
|
|
// theoreticallly the selection might have changed however
|
|
|
|
|
|
// theoretically all the text might have changed also so we ignore
|
|
|
|
|
|
// both issues for know and we solve both when we decide it is to much of
|
|
|
|
|
|
// a problem but in practice syntax errors come back very quickly...
|
|
|
|
|
|
int position_offset = 0;
|
|
|
|
|
|
if (cursor.hasSelection()) {
|
|
|
|
|
|
position_offset = cursor.selectionStart();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
cursor.setPosition(details.statementPosition - 1 + position_offset);
|
2018-04-09 21:44:00 +02:00
|
|
|
|
ui->queryEdit->setTextCursor(cursor);
|
|
|
|
|
|
|
|
|
|
|
|
int length = 0;
|
|
|
|
|
|
if (details.state == "42703") {
|
2018-08-05 09:05:24 +02:00
|
|
|
|
std::size_t pos = details.messagePrimary.find('"');
|
2018-04-09 21:44:00 +02:00
|
|
|
|
if (pos != std::string::npos) {
|
2018-08-25 18:11:12 +02:00
|
|
|
|
std::size_t pos2 = details.messagePrimary.find('"', pos+1);
|
2018-04-09 21:44:00 +02:00
|
|
|
|
if (pos2 != std::string::npos) {
|
2018-08-25 18:11:12 +02:00
|
|
|
|
length = static_cast<int>(pos2 - pos);
|
2018-04-09 21:44:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (details.state == "42P01") {
|
2018-08-05 09:05:24 +02:00
|
|
|
|
std::size_t pos = details.messagePrimary.find('"');
|
2018-04-09 21:44:00 +02:00
|
|
|
|
if (pos != std::string::npos) {
|
2018-08-25 18:11:12 +02:00
|
|
|
|
std::size_t pos2 = details.messagePrimary.find('"', pos+1);
|
2018-04-09 21:44:00 +02:00
|
|
|
|
if (pos2 != std::string::npos) {
|
2018-08-25 18:11:12 +02:00
|
|
|
|
length = static_cast<int>(pos2 - pos);
|
2018-04-09 21:44:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2018-11-10 10:59:52 +01:00
|
|
|
|
ui->queryEdit->addErrorMarker(details.statementPosition - 1 + position_offset, length);
|
2018-04-09 21:44:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-05 19:58:23 +01:00
|
|
|
|
void QueryTool::clearResult()
|
2017-01-22 08:50:41 +01:00
|
|
|
|
{
|
2017-01-25 06:54:21 +01:00
|
|
|
|
for (auto e : resultList)
|
|
|
|
|
|
delete e;
|
|
|
|
|
|
resultList.clear();
|
2017-01-22 08:50:41 +01:00
|
|
|
|
}
|
2017-02-05 08:23:06 +01:00
|
|
|
|
|
2019-01-05 19:58:23 +01:00
|
|
|
|
void QueryTool::copyQueryAsCString()
|
2017-02-05 08:23:06 +01:00
|
|
|
|
{
|
2017-10-05 16:02:06 +02:00
|
|
|
|
QString command = getCommand();
|
2017-02-05 08:23:06 +01:00
|
|
|
|
QString cs = ConvertToMultiLineCString(command);
|
|
|
|
|
|
QApplication::clipboard()->setText(cs);
|
|
|
|
|
|
}
|
2017-02-05 13:35:41 +01:00
|
|
|
|
|
2018-09-18 11:53:19 +02:00
|
|
|
|
#include <codebuilder/CodeBuilder.h>
|
|
|
|
|
|
#include <codebuilder/DefaultConfigs.h>
|
|
|
|
|
|
|
2019-01-05 19:58:23 +01:00
|
|
|
|
void QueryTool::copyQueryAsRawCppString()
|
2017-10-05 16:02:06 +02:00
|
|
|
|
{
|
|
|
|
|
|
QString command = getCommand();
|
|
|
|
|
|
QString cs = ConvertToMultiLineRawCppString(command);
|
|
|
|
|
|
QApplication::clipboard()->setText(cs);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-08-19 11:38:04 +02:00
|
|
|
|
void QueryTool::pasteLangString()
|
|
|
|
|
|
{
|
|
|
|
|
|
QString s = QApplication::clipboard()->text();
|
|
|
|
|
|
s = ConvertLangToSqlString(s);
|
|
|
|
|
|
ui->queryEdit->insertPlainText(s);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-05 19:58:23 +01:00
|
|
|
|
void QueryTool::generateCode()
|
2018-09-18 11:53:19 +02:00
|
|
|
|
{
|
|
|
|
|
|
QString command = getCommand();
|
|
|
|
|
|
if (resultList.empty()) {
|
|
|
|
|
|
QMessageBox::question(this, "pglab", tr("Please execute the query first"), QMessageBox::Ok);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (resultList.size() == 1) {
|
|
|
|
|
|
std::shared_ptr<const Pgsql::Result> dbres = resultList[0]->GetPgsqlResult();
|
2019-08-19 10:05:05 +02:00
|
|
|
|
m_context->newCodeGenPage(command, dbres);
|
2018-09-18 11:53:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-02-08 10:10:11 +01:00
|
|
|
|
void QueryTool::exportDataToFilename(const QString &file_name)
|
2017-02-05 13:35:41 +01:00
|
|
|
|
{
|
|
|
|
|
|
auto widget = ui->tabWidget->currentWidget();
|
|
|
|
|
|
auto fi = std::find(resultList.begin(), resultList.end(), widget);
|
|
|
|
|
|
if (fi != resultList.end()) {
|
|
|
|
|
|
TuplesResultWidget* rw = *fi;
|
|
|
|
|
|
rw->exportData(file_name);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2018-04-21 14:36:33 +02:00
|
|
|
|
|
2019-01-05 19:58:23 +01:00
|
|
|
|
void QueryTool::focusEditor()
|
2018-04-21 14:36:33 +02:00
|
|
|
|
{
|
|
|
|
|
|
ui->queryEdit->setFocus();
|
|
|
|
|
|
}
|
2019-01-06 16:34:50 +01:00
|
|
|
|
|
|
|
|
|
|
void QueryTool::exportData()
|
|
|
|
|
|
{
|
|
|
|
|
|
QString home_dir = QStandardPaths::locate(QStandardPaths::HomeLocation, "", QStandardPaths::LocateDirectory);
|
|
|
|
|
|
QString file_name = QFileDialog::getSaveFileName(this,
|
|
|
|
|
|
tr("Export data"), home_dir, tr("CSV file (*.csv)"));
|
2019-02-08 10:10:11 +01:00
|
|
|
|
exportDataToFilename(file_name);
|
|
|
|
|
|
}
|