This repository has been archived by the owner on Mar 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquerywidget.cpp
123 lines (103 loc) · 4.11 KB
/
querywidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <QSqlQueryModel>
#include <QSqlQuery>
#include <QThread>
#include <QSqlError>
#include <QStringListModel>
#include <QException>
#include "querywidget.h"
#include "ui_querywidget.h"
#include "utilities/bdatabasemanager.h"
QueryWidget::QueryWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::QueryWidget),
model(new QSqlQueryModel(this)) {
ui->setupUi(this);
ui->examplesComboBox->setCurrentText("Examples...");
exampleQueries.insert("1. Total Count", "SELECT count(*) FROM bz");
exampleQueries.insert("2. Inspect Table", "SELECT * FROM bz LIMIT 1000");
exampleQueries.insert("3. Flow of Station",
"SELECT stationID, count(*) flow FROM bz GROUP BY stationID ORDER BY flow DESC");
exampleQueries.insert("4. 'WHERE' Example", "SELECT * FROM bz WHERE stationID = 15 AND payType = 0");
auto exampleNames = QStringList() << "Examples";
for (const auto &name:exampleQueries.keys()) {
exampleNames << name;
}
auto examplesModel = new QStringListModel(exampleNames);
ui->examplesComboBox->setModel(examplesModel);
connect(ui->examplesComboBox, &QComboBox::currentTextChanged,
this, &QueryWidget::setQueryText);
ui->queryInput->setPlaceholderText(
"AnyExplore supports any standard SQL statements to explore the entire database. "
"Default table name is 'bz'.\n"
"Please see the exampleNames on the right.");
ui->resultTable->setModel(model);
connect(ui->queryButton, &QPushButton::clicked, this, &QueryWidget::doQuery);
connect(ui->queryInput, &QPlainTextEdit::textChanged, this, &QueryWidget::onInputModified);
connect(this, &QueryWidget::success, this, &QueryWidget::onSuccess);
connect(this, &QueryWidget::failed, this, &QueryWidget::onFailed);
}
QueryWidget::~QueryWidget() {
delete ui;
}
void QueryWidget::doQuery() {
auto queryText = ui->queryInput->toPlainText();
if (!queryText.toUpper().startsWith("SELECT")) {
emit statusBarMessage("Sorry, the database is select-only", 3000);
emit failed();
return;
}
auto thread = QThread::create([this, queryText]() {
auto db = BDatabaseManager::readOnlyConnection("query_thread");
model->removeRows(0, model->rowCount());
model->setQuery(queryText, db);
model->query();
while (model->canFetchMore()) model->fetchMore();
if (model->lastError().type() == QSqlError::NoError) {
emit statusBarMessage("Done", 3000);
emit success();
} else {
emit statusBarMessage(QString("Query error: %1").
arg(model->lastError().databaseText()), 8000);
emit failed();
}
});
connect(thread, &QThread::started, this, &QueryWidget::onQueryStarted);
connect(thread, &QThread::finished, this, &QueryWidget::onQueryFinished);
thread->start();
}
void QueryWidget::onQueryFinished() {
ui->resultTable->resizeColumnsToContents();
ui->queryButton->setEnabled(true);
ui->resultTable->setEnabled(true);
}
void QueryWidget::onQueryStarted() {
emit statusBarMessage("Querying...");
ui->queryButton->setEnabled(false);
ui->resultTable->setEnabled(false);
}
void QueryWidget::setBzEnabled(bool enabled) {
ui->queryButton->setEnabled(enabled);
}
void QueryWidget::setQueryText(const QString &name) {
if (name.startsWith("Example")) return;
try {
ui->queryInput->setPlainText(exampleQueries[name]);
ui->examplesComboBox->setCurrentIndex(0);
} catch (QException &) {}
}
void QueryWidget::onSuccess() {
auto palette = ui->queryInput->palette();
palette.setColor(QPalette::Text, Qt::darkGreen);
ui->queryInput->setPalette(palette);
}
void QueryWidget::onFailed() {
auto palette = ui->queryInput->palette();
palette.setColor(QPalette::Text, Qt::darkRed);
ui->queryInput->setPalette(palette);
}
void QueryWidget::onInputModified() {
auto palette = ui->queryInput->palette();
if (palette.color(QPalette::Text) == Qt::black) return;
palette.setColor(QPalette::Text, Qt::black);
ui->queryInput->setPalette(palette);
}