/
qgsqueryresultwidget.h
193 lines (148 loc) · 5.62 KB
/
qgsqueryresultwidget.h
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/***************************************************************************
qgsqueryresultwidget.h - QgsQueryResultWidget
---------------------
begin : 14.1.2021
copyright : (C) 2021 by elpaso
email : elpaso@itopen.it
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSQUERYRESULTWIDGET_H
#define QGSQUERYRESULTWIDGET_H
#include "qgis_gui.h"
#include "qgis_sip.h"
#include "ui_qgsqueryresultwidgetbase.h"
#include "qgsabstractdatabaseproviderconnection.h"
#include "qgsqueryresultmodel.h"
#include "qgsfeedback.h"
#include <QWidget>
#include <QThread>
#include <QtConcurrent>
///@cond private
#ifndef SIP_RUN
/**
* \brief The QgsConnectionsApiFetcher class fetches tokens (table and field names) of a connection from a separate thread.
*/
class GUI_EXPORT QgsConnectionsApiFetcher: public QObject
{
Q_OBJECT
public:
//! Constructs a result fetcher from \a queryResult
QgsConnectionsApiFetcher( const QgsAbstractDatabaseProviderConnection *conn )
: mConnection( conn )
{}
//! Start fetching
void fetchTokens();
//! Stop fetching
void stopFetching();
signals:
//!! Emitted when \a newTokens have been fetched
void tokensReady( const QStringList &newTokens );
//! Emitted when fetching of tokes has finished or has been interrupted.
void fetchingFinished();
private:
const QgsAbstractDatabaseProviderConnection *mConnection;
QAtomicInt mStopFetching = 0;
};
#endif
///@endcond private
/**
* \brief The QgsQueryResultWidget class allow users to enter and run an SQL query on a
* DB connection (an instance of QgsAbstractDatabaseProviderConnection).
*
* Query results are displayed in a table view.
* Query execution and result fetching can be interrupted by pressing the "Stop" push button.
*
* \note the ownership of the connection is transferred to the widget.
*
* \since QGIS 3.20
*/
class GUI_EXPORT QgsQueryResultWidget: public QWidget, private Ui::QgsQueryResultWidgetBase
{
Q_OBJECT
public:
/**
* Creates a QgsQueryResultWidget with the given \a connection, ownership is transferred to the widget.
*/
QgsQueryResultWidget( QWidget *parent = nullptr, QgsAbstractDatabaseProviderConnection *connection SIP_TRANSFER = nullptr );
virtual ~QgsQueryResultWidget();
/**
* Set the connection to \a connection, ownership is transferred to the widget.
*/
void setConnection( QgsAbstractDatabaseProviderConnection *connection SIP_TRANSFER );
/**
* Convenience method to set the SQL editor text to \a sql.
*/
void setQuery( const QString &sql );
/**
* Set the SQL layer \a options. This method automatically populates and shows the "Load as new layer" panel.
*/
void setSqlVectorLayerOptions( const QgsAbstractDatabaseProviderConnection::SqlVectorLayerOptions &options );
/**
* Returns the sqlVectorLayerOptions
*/
QgsAbstractDatabaseProviderConnection::SqlVectorLayerOptions sqlVectorLayerOptions() const;
public slots:
/**
* Executes the query
*/
void executeQuery();
/**
* Hides the result table and shows the error \a title and \a message in the message bar or
* in the SQL error panel is \a isSqlError is set.
*/
void showError( const QString &title, const QString &message, bool isSqlError = false );
/**
* Triggered when the threaded API fetcher has new \a tokens to add.
*/
void tokensReady( const QStringList &tokens );
private slots:
void syncSqlOptions();
signals:
/**
* Emitted when a new vector SQL (query) layer must be created.
* \param providerKey name of the data provider
* \param connectionUri the connection URI as returned by QgsAbstractProviderConnection::uri()
* \param options
*/
void createSqlVectorLayer( const QString &providerKey, const QString &connectionUri, const QgsAbstractDatabaseProviderConnection::SqlVectorLayerOptions &options );
/**
* Emitted when the first batch of results has been fetched.
* \note If the query returns no results this signal is not emitted.
*/
void firstResultBatchFetched();
private:
std::unique_ptr<QgsAbstractDatabaseProviderConnection> mConnection;
std::unique_ptr<QgsQueryResultModel> mModel;
std::unique_ptr<QgsFeedback> mFeedback;
QgsConnectionsApiFetcher *mApiFetcher = nullptr;
QThread mWorkerThread;
bool mWasCanceled = false;
QgsAbstractDatabaseProviderConnection::SqlVectorLayerOptions mSqlVectorLayerOptions;
bool mFirstRowFetched = false;
QFutureWatcher<QgsAbstractDatabaseProviderConnection::QueryResult> mQueryResultWatcher;
QString mSqlErrorMessage;
/**
* Updates buttons status
*/
void updateButtons();
/**
* Updates SQL layer columns
*/
void updateSqlLayerColumns();
/**
* Cancel and wait for finish.
*/
void cancelRunningQuery();
/**
* Starts the model population after initial query run
*/
void startFetching();
friend class TestQgsQueryResultWidget;
};
#endif // QGSQUERYRESULTWIDGET_H