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 <QStandardPaths>
#include <QFileDialog>
#include <QTextDocumentFragment>
#include <QTextStream>
#include <QTextTable>
#include <QTimer>
@ -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<Pgsql::Result> 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<Pgsql::Result> 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);
}
}

View file

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

View file

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

View file

@ -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);
}

View file

@ -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 &regex, const QTextCharFormat &f)
// : pattern(regex), format(f)
// {}
// };
HighlightingRule(const QRegExp &regex, const QTextCharFormat &f)
: pattern(regex), format(f)
{}
};
//QVector<HighlightingRule> highlightingRules;
// std::vector<HighlightingRule> highlightingRules;
std::vector<HighlightingRule> highlightingRules;
// QRegExp commentStartExpression;
// QRegExp commentEndExpression;
// QTextCharFormat keywordFormat;
QTextCharFormat keywordFormat;
// QTextCharFormat classFormat;
// QTextCharFormat singleLineCommentFormat;
// QTextCharFormat multiLineCommentFormat;