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.
This commit is contained in:
Eelke Klein 2017-01-16 17:31:46 +01:00
parent c555182ddd
commit 5831f18008
5 changed files with 44 additions and 39 deletions

View file

@ -6,6 +6,7 @@
#include "sqlhighlighter.h" #include "sqlhighlighter.h"
#include <QStandardPaths> #include <QStandardPaths>
#include <QFileDialog> #include <QFileDialog>
#include <QTextDocumentFragment>
#include <QTextStream> #include <QTextStream>
#include <QTextTable> #include <QTextTable>
#include <QTimer> #include <QTimer>
@ -93,6 +94,7 @@ MainWindow::MainWindow(QWidget *parent)
font.setPointSize(10); font.setPointSize(10);
ui->queryEdit->setFont(font); ui->queryEdit->setFont(font);
highlighter.reset(new SqlHighlighter(ui->queryEdit->document())); highlighter.reset(new SqlHighlighter(ui->queryEdit->document()));
// ui->queryEdit->setPlainText(test_query); // ui->queryEdit->setPlainText(test_query);
// ui->connectionStringEdit->setText("user=postgres dbname=foutrapport password=admin"); // ui->connectionStringEdit->setText("user=postgres dbname=foutrapport password=admin");
@ -191,6 +193,19 @@ void MainWindow::startConnect()
m_dbConnection.setupConnection(m_config); 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() void MainWindow::performQuery()
{ {
if (m_dbConnection.state() == ASyncDBConnection::State::Connected) { if (m_dbConnection.state() == ASyncDBConnection::State::Connected) {
@ -200,9 +215,7 @@ void MainWindow::performQuery()
resultModel.reset(); resultModel.reset();
ui->messagesEdit->clear(); ui->messagesEdit->clear();
std::string cmd = getCommand();
QString command = ui->queryEdit->toPlainText();
std::string cmd = command.toUtf8().data();
startTimer(); startTimer();
m_dbConnection.send(cmd, m_dbConnection.send(cmd,
[this](std::shared_ptr<Pgsql::Result> res) [this](std::shared_ptr<Pgsql::Result> res)
@ -274,9 +287,7 @@ void MainWindow::performExplain()
addLog("Explain clicked"); addLog("Explain clicked");
startTimer(); startTimer();
QString command = "EXPLAIN (ANALYZE, VERBOSE, BUFFERS, FORMAT JSON) "; std::string cmd = "EXPLAIN (ANALYZE, VERBOSE, BUFFERS, FORMAT JSON) " + getCommand();
command += ui->queryEdit->toPlainText();
std::string cmd = command.toUtf8().data();
m_dbConnection.send(cmd, m_dbConnection.send(cmd,
[this](std::shared_ptr<Pgsql::Result> res) [this](std::shared_ptr<Pgsql::Result> res)
{ {
@ -410,7 +421,7 @@ void MainWindow::on_actionLoad_SQL_triggered()
ui->queryEdit->clear(); ui->queryEdit->clear();
while (!stream.atEnd()){ while (!stream.atEnd()){
QString line = stream.readLine(); QString line = stream.readLine();
ui->queryEdit->append(line); ui->queryEdit->appendPlainText(line);
} }
} }

View file

@ -64,6 +64,7 @@ private:
void query_ready(std::shared_ptr<Pgsql::Result> res); void query_ready(std::shared_ptr<Pgsql::Result> res);
void explain_ready(std::shared_ptr<ExplainRoot> explain); void explain_ready(std::shared_ptr<ExplainRoot> explain);
std::string getCommand() const;
private slots: private slots:
void startConnect(); void startConnect();

View file

@ -32,14 +32,7 @@
<property name="orientation"> <property name="orientation">
<enum>Qt::Vertical</enum> <enum>Qt::Vertical</enum>
</property> </property>
<widget class="QTextEdit" name="queryEdit"> <widget class="QPlainTextEdit" name="queryEdit"/>
<property name="lineWrapMode">
<enum>QTextEdit::NoWrap</enum>
</property>
<property name="acceptRichText">
<bool>false</bool>
</property>
</widget>
<widget class="QTabWidget" name="tabWidget"> <widget class="QTabWidget" name="tabWidget">
<property name="currentIndex"> <property name="currentIndex">
<number>1</number> <number>1</number>

View file

@ -22,7 +22,7 @@ SqlHighlighter::SqlHighlighter(QTextDocument *parent)
: QSyntaxHighlighter(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 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)-"; // static auto err = R"-(left|right|inner|outer)-";
@ -31,10 +31,10 @@ SqlHighlighter::SqlHighlighter(QTextDocument *parent)
// errFormat.setFontWeight(QFont::Bold); // errFormat.setFontWeight(QFont::Bold);
// highlightingRules.emplace_back(QRegExp(err, Qt::CaseInsensitive), errFormat); // highlightingRules.emplace_back(QRegExp(err, Qt::CaseInsensitive), errFormat);
// QTextCharFormat keywordFormat; QTextCharFormat keywordFormat;
// keywordFormat.setForeground(QColor(128, 128, 255)); keywordFormat.setForeground(QColor(128, 128, 255));
// keywordFormat.setFontWeight(QFont::Bold); keywordFormat.setFontWeight(QFont::Bold);
// highlightingRules.emplace_back(QRegExp(keywords, Qt::CaseInsensitive), keywordFormat); highlightingRules.emplace_back(QRegExp(keywords, Qt::CaseInsensitive), keywordFormat);
// QTextCharFormat typesFormat; // QTextCharFormat typesFormat;
// typesFormat.setForeground(QColor(128, 255, 128)); // typesFormat.setForeground(QColor(128, 255, 128));
@ -104,15 +104,15 @@ namespace {
void SqlHighlighter::highlightBlock(const QString &text) void SqlHighlighter::highlightBlock(const QString &text)
{ {
// foreach (const HighlightingRule &rule, highlightingRules) { foreach (const HighlightingRule &rule, highlightingRules) {
// QRegExp expression(rule.pattern); QRegExp expression(rule.pattern);
// int index = expression.indexIn(text); int index = expression.indexIn(text);
// while (index >= 0) { while (index >= 0) {
// int length = expression.matchedLength(); int length = expression.matchedLength();
// setFormat(index, length, rule.format); setFormat(index, length, rule.format);
// index = expression.indexIn(text, index + length); index = expression.indexIn(text, index + length);
// } }
// } }
setCurrentBlockState(0); setCurrentBlockState(0);
} }

View file

@ -16,22 +16,22 @@ protected:
void highlightBlock(const QString &text) Q_DECL_OVERRIDE; void highlightBlock(const QString &text) Q_DECL_OVERRIDE;
private: private:
// struct HighlightingRule struct HighlightingRule
// { {
// QRegExp pattern; QRegExp pattern;
// QTextCharFormat format; QTextCharFormat format;
// HighlightingRule(const QRegExp &regex, const QTextCharFormat &f) HighlightingRule(const QRegExp &regex, const QTextCharFormat &f)
// : pattern(regex), format(f) : pattern(regex), format(f)
// {} {}
// }; };
//QVector<HighlightingRule> highlightingRules; //QVector<HighlightingRule> highlightingRules;
// std::vector<HighlightingRule> highlightingRules; std::vector<HighlightingRule> highlightingRules;
// QRegExp commentStartExpression; // QRegExp commentStartExpression;
// QRegExp commentEndExpression; // QRegExp commentEndExpression;
// QTextCharFormat keywordFormat; QTextCharFormat keywordFormat;
// QTextCharFormat classFormat; // QTextCharFormat classFormat;
// QTextCharFormat singleLineCommentFormat; // QTextCharFormat singleLineCommentFormat;
// QTextCharFormat multiLineCommentFormat; // QTextCharFormat multiLineCommentFormat;