-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathQueryWorker.h
More file actions
46 lines (38 loc) · 1.42 KB
/
Copy pathQueryWorker.h
File metadata and controls
46 lines (38 loc) · 1.42 KB
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
#pragma once
#include <QObject>
#include <QString>
#include <QStringList>
#include <QVector>
// One row of query results, already converted to display strings.
using ResultRow = QStringList;
using ResultRows = QVector<ResultRow>;
// Executes SQL on a background thread so a slow or accidentally
// expensive query (large JOIN, missing index, etc.) never freezes the
// UI. Lives in its own QThread (see MainWindow), and owns its own
// QSqlDatabase connection - Qt requires a distinct connection per
// thread, so this never touches the connection MainWindow uses on the
// GUI thread.
//
// The connection is opened lazily, on first execute(), because it must
// be created on the thread that will use it - not in the constructor,
// which still runs on the GUI thread before moveToThread() takes effect.
class QueryWorker : public QObject
{
Q_OBJECT
public:
explicit QueryWorker(QString dbPath, QObject* parent = nullptr);
~QueryWorker() override;
public slots:
void execute(const QString& sql);
signals:
// Headers + rows are plain value types so they can safely cross the
// thread boundary via a queued connection - a QSqlQuery itself is
// tied to its originating thread and must never be shared.
void succeeded(QStringList headers, ResultRows rows);
void failed(QString message);
private:
bool ensureConnected();
QString m_dbPath;
QString m_connectionName;
bool m_connected = false;
};