/
qgsqueryresultwidget.h
242 lines (188 loc) · 7.33 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/***************************************************************************
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>
#include <QStyledItemDelegate>
///@cond private
#ifndef SIP_RUN
/**
* The QgsQueryResultItemDelegate class shows results truncated to 255 characters and using current locale
*/
class GUI_EXPORT QgsQueryResultItemDelegate: public QStyledItemDelegate
{
Q_OBJECT
// QStyledItemDelegate interface
public:
explicit QgsQueryResultItemDelegate( QObject *parent = nullptr );
QString displayText( const QVariant &value, const QLocale &locale ) const override;
};
/**
* The QgsConnectionsApiFetcher class fetches tokens (schema, table and field names) of a connection from a separate thread.
* WARNING: this class is an implementation detail and it is not part of public API!
*/
class GUI_EXPORT QgsConnectionsApiFetcher: public QObject
{
Q_OBJECT
public:
//! Constructs a result fetcher from \a connection.
QgsConnectionsApiFetcher( const QgsAbstractDatabaseProviderConnection *connection )
: mConnection( connection )
{}
//! 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 = nullptr;
QAtomicInt mStopFetching = 0;
};
#endif
///@endcond private
/**
* \ingroup gui
* \brief The QgsQueryResultWidget class allows 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.
*
* The widget supports a few QueryWidgetMode modes that pre-configure the widget appearance to
* be used in different contexts like when updating the SQL of an existing query layer.
*
* \note the ownership of the connection is transferred to the widget.
*
* \since QGIS 3.22
*/
class GUI_EXPORT QgsQueryResultWidget: public QWidget, private Ui::QgsQueryResultWidgetBase
{
Q_OBJECT
public:
/**
* \brief The QueryWidgetMode enum represents various modes for the widget appearance.
*/
enum class QueryWidgetMode : int
{
SqlQueryMode = 1 << 0, //!< Defaults widget mode for SQL execution and SQL query layer creation.
QueryLayerUpdateMode = 1 << 1, //!< SQL query layer update mode: the create SQL layer button is renamed to 'Update' and the SQL layer creation group box is expanded.
};
Q_ENUM( QueryWidgetMode )
/**
* 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();
/**
* Initializes the widget from \a options.
*/
void setSqlVectorLayerOptions( const QgsAbstractDatabaseProviderConnection::SqlVectorLayerOptions &options );
/**
* Sets the widget mode to \a widgetMode, default is SqlQueryMode.
*/
void setWidgetMode( QueryWidgetMode widgetMode );
/**
* Sets 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 );
public slots:
/**
* Displays a message with \a text \a title and \a level in the widget's message bar.
*/
void notify( const QString &title, const QString &text, Qgis::MessageLevel level = Qgis::MessageLevel::Info );
/**
* Starts executing 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 );
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 slots:
/**
* Updates buttons status.
*/
void updateButtons();
private:
std::unique_ptr<QgsAbstractDatabaseProviderConnection> mConnection;
std::unique_ptr<QgsQueryResultModel> mModel;
std::unique_ptr<QgsFeedback> mFeedback;
std::unique_ptr<QgsConnectionsApiFetcher> mApiFetcher;
QThread mApiFetcherWorkerThread;
bool mWasCanceled = false;
mutable QgsAbstractDatabaseProviderConnection::SqlVectorLayerOptions mSqlVectorLayerOptions;
bool mFirstRowFetched = false;
QFutureWatcher<QgsAbstractDatabaseProviderConnection::QueryResult> mQueryResultWatcher;
QString mSqlErrorMessage;
long long mActualRowCount = -1;
long long mFetchedRowsBatchCount = 0;
QueryWidgetMode mQueryWidgetMode = QueryWidgetMode::SqlQueryMode;
/**
* Updates SQL layer columns.
*/
void updateSqlLayerColumns();
/**
* Cancel and wait for finish.
*/
void cancelRunningQuery();
/**
* Cancel API fetching.
*/
void cancelApiFetcher();
/**
* Starts the model population after initial query run.
*/
void startFetching();
/**
* Returns the sqlVectorLayerOptions
*/
QgsAbstractDatabaseProviderConnection::SqlVectorLayerOptions sqlVectorLayerOptions() const;
friend class TestQgsQueryResultWidget;
};
#endif // QGSQUERYRESULTWIDGET_H