From 5831f18008d268c0bfc18534b36c3f80b8483f10 Mon Sep 17 00:00:00 2001 From: Eelke Klein Date: Mon, 16 Jan 2017 17:31:46 +0100 Subject: [PATCH] If there is a selection in the query then only that part is Executed or explained. Replaced QTextEdit with QPLainTextEdit as it is more efficient and CAN do syntax highlighting. --- mainwindow.cpp | 25 ++++++++++++++++++------- mainwindow.h | 1 + mainwindow.ui | 9 +-------- sqlhighlighter.cpp | 28 ++++++++++++++-------------- sqlhighlighter.h | 20 ++++++++++---------- 5 files changed, 44 insertions(+), 39 deletions(-) diff --git a/mainwindow.cpp b/mainwindow.cpp index 362c0c7..8f011ed 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -6,6 +6,7 @@ #include "sqlhighlighter.h" #include #include +#include #include #include #include @@ -93,6 +94,7 @@ MainWindow::MainWindow(QWidget *parent) 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"); @@ -191,6 +193,19 @@ void MainWindow::startConnect() m_dbConnection.setupConnection(m_config); } +std::string MainWindow::getCommand() const +{ + QString command; + QTextCursor cursor = ui->queryEdit->textCursor(); + if (cursor.hasSelection()) { + command = cursor.selection().toPlainText(); + } + else { + command = ui->queryEdit->toPlainText(); + } + return command.toUtf8().data(); +} + void MainWindow::performQuery() { if (m_dbConnection.state() == ASyncDBConnection::State::Connected) { @@ -200,9 +215,7 @@ void MainWindow::performQuery() resultModel.reset(); ui->messagesEdit->clear(); - - QString command = ui->queryEdit->toPlainText(); - std::string cmd = command.toUtf8().data(); + std::string cmd = getCommand(); startTimer(); m_dbConnection.send(cmd, [this](std::shared_ptr res) @@ -274,9 +287,7 @@ void MainWindow::performExplain() addLog("Explain clicked"); startTimer(); - QString command = "EXPLAIN (ANALYZE, VERBOSE, BUFFERS, FORMAT JSON) "; - command += ui->queryEdit->toPlainText(); - std::string cmd = command.toUtf8().data(); + std::string cmd = "EXPLAIN (ANALYZE, VERBOSE, BUFFERS, FORMAT JSON) " + getCommand(); m_dbConnection.send(cmd, [this](std::shared_ptr res) { @@ -410,7 +421,7 @@ void MainWindow::on_actionLoad_SQL_triggered() ui->queryEdit->clear(); while (!stream.atEnd()){ QString line = stream.readLine(); - ui->queryEdit->append(line); + ui->queryEdit->appendPlainText(line); } } diff --git a/mainwindow.h b/mainwindow.h index 241b00e..7bde69c 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -64,6 +64,7 @@ private: void query_ready(std::shared_ptr res); void explain_ready(std::shared_ptr explain); + std::string getCommand() const; private slots: void startConnect(); diff --git a/mainwindow.ui b/mainwindow.ui index a9442bf..af68d7d 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -32,14 +32,7 @@ Qt::Vertical - - - QTextEdit::NoWrap - - - false - - + 1 diff --git a/sqlhighlighter.cpp b/sqlhighlighter.cpp index e2d8e10..1aca454 100644 --- a/sqlhighlighter.cpp +++ b/sqlhighlighter.cpp @@ -22,7 +22,7 @@ SqlHighlighter::SqlHighlighter(QTextDocument *parent) : QSyntaxHighlighter(parent) { // { -// static auto keywords = R"-(as|alter|all|and|any|by|column|create|database|from|group|having|in|not|or|order|select|table|where|(?:(?:inner|(?:left|right|full)(\s+outer)?)\s+)?join)-"; + static auto keywords = R"-(as|alter|all|and|any|by|column|create|database|from|group|having|in|not|or|order|select|table|where|(?:(?:inner|(?:left|right|full)(\s+outer)?)\s+)?join)-"; // static auto types = R"-(bigint|boolean|char|character varying|date|int[248]|integer|numeric|smallint|time|timestamp(?:tz)?|timestamp(?:\s+with\s+timezone)?|varchar)-"; // static auto err = R"-(left|right|inner|outer)-"; @@ -31,10 +31,10 @@ SqlHighlighter::SqlHighlighter(QTextDocument *parent) // errFormat.setFontWeight(QFont::Bold); // highlightingRules.emplace_back(QRegExp(err, Qt::CaseInsensitive), errFormat); -// QTextCharFormat keywordFormat; -// keywordFormat.setForeground(QColor(128, 128, 255)); -// keywordFormat.setFontWeight(QFont::Bold); -// highlightingRules.emplace_back(QRegExp(keywords, Qt::CaseInsensitive), keywordFormat); + QTextCharFormat keywordFormat; + keywordFormat.setForeground(QColor(128, 128, 255)); + keywordFormat.setFontWeight(QFont::Bold); + highlightingRules.emplace_back(QRegExp(keywords, Qt::CaseInsensitive), keywordFormat); // QTextCharFormat typesFormat; // typesFormat.setForeground(QColor(128, 255, 128)); @@ -104,15 +104,15 @@ namespace { void SqlHighlighter::highlightBlock(const QString &text) { -// foreach (const HighlightingRule &rule, highlightingRules) { -// QRegExp expression(rule.pattern); -// int index = expression.indexIn(text); -// while (index >= 0) { -// int length = expression.matchedLength(); -// setFormat(index, length, rule.format); -// index = expression.indexIn(text, index + length); -// } -// } + foreach (const HighlightingRule &rule, highlightingRules) { + QRegExp expression(rule.pattern); + int index = expression.indexIn(text); + while (index >= 0) { + int length = expression.matchedLength(); + setFormat(index, length, rule.format); + index = expression.indexIn(text, index + length); + } + } setCurrentBlockState(0); } diff --git a/sqlhighlighter.h b/sqlhighlighter.h index 6cccf3b..4202d54 100644 --- a/sqlhighlighter.h +++ b/sqlhighlighter.h @@ -16,22 +16,22 @@ protected: void highlightBlock(const QString &text) Q_DECL_OVERRIDE; private: -// struct HighlightingRule -// { -// QRegExp pattern; -// QTextCharFormat format; + struct HighlightingRule + { + QRegExp pattern; + QTextCharFormat format; -// HighlightingRule(const QRegExp ®ex, const QTextCharFormat &f) -// : pattern(regex), format(f) -// {} -// }; + HighlightingRule(const QRegExp ®ex, const QTextCharFormat &f) + : pattern(regex), format(f) + {} + }; //QVector highlightingRules; -// std::vector highlightingRules; + std::vector highlightingRules; // QRegExp commentStartExpression; // QRegExp commentEndExpression; -// QTextCharFormat keywordFormat; + QTextCharFormat keywordFormat; // QTextCharFormat classFormat; // QTextCharFormat singleLineCommentFormat; // QTextCharFormat multiLineCommentFormat;