Repository navigation
Expand file tree
/
Copy pathQueryWorker.h
More file actions
48 lines (39 loc) · 1.44 KB
/
Copy pathQueryWorker.h
File metadata and controls
48 lines (39 loc) · 1.44 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
47
48
#pragma once
#include <QObject>
#include <QString>
#include <QStringList>
#include <QVector>
#include "VecDatabase.h"
// 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
// SQLite connection - never the one MainWindow uses on the GUI thread.
//
// The connection is a vec::Database (the project's own static SQLite with
// sqlite-vec registered), NOT Qt's QSQLITE plugin, so vec0 virtual tables
// and the vec_* functions work in user queries.
//
// 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.
void succeeded(QStringList headers, ResultRows rows);
void failed(QString message);
private:
bool ensureConnected();
QString m_dbPath;
vec::Database m_db;
};