QGIS API Documentation 3.32.0-Lima (311a8cb8a6)
qgssqlcomposerdialog.h
Go to the documentation of this file.
1/***************************************************************************
2 qgssqlcomposerdialog.cpp
3 Dialog to compose SQL queries
4
5begin : Apr 2016
6copyright : (C) 2016 Even Rouault
7email : even.rouault at spatialys.com
8 ***************************************************************************/
9
10/***************************************************************************
11 * *
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; either version 2 of the License, or *
15 * (at your option) any later version. *
16 * *
17 ***************************************************************************/
18
19#ifndef QGSSQLCOMPOSERDIALOG_H
20#define QGSSQLCOMPOSERDIALOG_H
21
22#include "ui_qgssqlcomposerdialogbase.h"
23#include "qgis_sip.h"
24#include "qgsguiutils.h"
25
26#include <QPair>
27#include <QStringList>
28#include <QSet>
29#include "qgis_gui.h"
31
33
34class QgsVectorLayer;
35
41class GUI_EXPORT QgsSQLComposerDialog : public QgsSubsetStringEditorInterface, private Ui::QgsSQLComposerDialogBase
42{
43 Q_OBJECT
44
45 public:
46
48 typedef QPair<QString, QString> PairNameTitle;
49
51 typedef QPair<QString, QString> PairNameType;
52
58 class GUI_EXPORT TableSelectedCallback
59 {
60 public:
61 virtual ~TableSelectedCallback() = default;
63 virtual void tableSelected( const QString &name ) = 0;
64 };
65
71 class GUI_EXPORT SQLValidatorCallback
72 {
73 public:
74 virtual ~SQLValidatorCallback() = default;
76 virtual bool isValid( const QString &sql, QString &errorReason, QString &warningMsg ) = 0;
77 };
78
80 struct Argument
81 {
83 QString name;
85 QString type;
86
88 Argument( const QString &nameIn = QString(), const QString &typeIn = QString() ) : name( nameIn ), type( typeIn ) {}
89 };
90
92 struct Function
93 {
95 QString name;
97 QString returnType;
99 int minArgs = -1;
101 int maxArgs = -1;
103 QList<Argument> argumentList;
104
106 Function( const QString &nameIn, int args ) : name( nameIn ), minArgs( args ), maxArgs( args ) {}
108 Function( const QString &nameIn, int minArgs, int maxArgsIn ) : name( nameIn ), minArgs( minArgs ), maxArgs( maxArgsIn ) {}
110 Function() = default;
111 };
112
114 explicit QgsSQLComposerDialog( QWidget *parent = nullptr, Qt::WindowFlags fl = QgsGuiUtils::ModalDialogFlags );
115
122 QgsSQLComposerDialog( QgsVectorLayer *layer, QWidget *parent = nullptr, Qt::WindowFlags fl = QgsGuiUtils::ModalDialogFlags );
123
124 ~QgsSQLComposerDialog() override;
125
127 void setSql( const QString &sql );
128
130 QString sql() const;
131
132 QString subsetString() const override { return sql(); }
133 void setSubsetString( const QString &subsetString ) override { setSql( subsetString ); }
134
136 void addTableNames( const QStringList &list );
138 void addTableNames( const QList<PairNameTitle> &listNameTitle );
140 void addColumnNames( const QStringList &list, const QString &tableName );
142 void addColumnNames( const QList<PairNameType> &list, const QString &tableName );
144 void addOperators( const QStringList &list );
146 void addSpatialPredicates( const QStringList &list );
148 void addSpatialPredicates( const QList<Function> &list );
150 void addFunctions( const QStringList &list );
152 void addFunctions( const QList<Function> &list );
154 void addApis( const QStringList &list );
155
157 void setSupportMultipleTables( bool bMultipleTables, const QString &mainTypename = QString() );
158
164 void setTableSelectedCallback( TableSelectedCallback *tableSelectedCallback );
165
170 void setSQLValidatorCallback( SQLValidatorCallback *sqlValidatorCallback );
171
172 protected:
173 bool eventFilter( QObject *obj, QEvent *event ) override;
174
175 private slots:
176 void accept() override;
177
178 void mTablesCombo_currentIndexChanged( int );
179 void mColumnsCombo_currentIndexChanged( int );
180 void mSpatialPredicatesCombo_currentIndexChanged( int );
181 void mFunctionsCombo_currentIndexChanged( int );
182 void mOperatorsCombo_currentIndexChanged( int );
183 void mAddJoinButton_clicked();
184 void mRemoveJoinButton_clicked();
185 void mTableJoins_itemSelectionChanged();
186 void showHelp();
187 void reset();
188 void buildSQLFromFields();
189 void splitSQLIntoFields();
190
191 private:
192 QgsVectorLayer *mLayer = nullptr;
193 QStringList mApiList;
194 QSet<QString> mAlreadySelectedTables;
195 TableSelectedCallback *mTableSelectedCallback = nullptr;
196 SQLValidatorCallback *mSQLValidatorCallback = nullptr;
197 QObject *mFocusedObject = nullptr;
198 bool mAlreadyModifyingFields = false;
199 bool mDistinct = false;
200 QString mResetSql;
201 QMap<QString, QString> mapTableEntryTextToName;
202 QMap<QString, QString> mapColumnEntryTextToName;
203 QMap<QString, QString> mapSpatialPredicateEntryTextToName;
204 QMap<QString, QString> mapFunctionEntryTextToName;
205 QString lastSearchedText;
206
207
208 void loadTableColumns( const QString &table );
209 void functionCurrentIndexChanged( QComboBox *combo,
210 const QMap<QString, QString> &mapEntryTextToName );
211 void getFunctionList( const QList<Function> &list,
212 QStringList &listApi,
213 QStringList &listCombo,
214 QMap<QString, QString> &mapEntryTextToName );
215};
216
217#endif
Callback to do validation check on dialog validation.
virtual bool isValid(const QString &sql, QString &errorReason, QString &warningMsg)=0
method should return true if the SQL is valid. Otherwise return false and set the errorReason
Callback to do actions on table selection.
virtual void tableSelected(const QString &name)=0
method called when a table is selected
SQL composer dialog.
QString subsetString() const override
Returns the subset string entered in the dialog.
QPair< QString, QString > PairNameType
pair (name, type)
QPair< QString, QString > PairNameTitle
pair (name, title)
void setSubsetString(const QString &subsetString) override
Sets a subset string into the dialog.
Interface for a dialog that can edit subset strings.
Represents a vector layer which manages a vector based data sets.
#define SIP_NO_FILE
Argument(const QString &nameIn=QString(), const QString &typeIn=QString())
constructor
QString type
type, or empty if unknown
description of server functions
QString returnType
Returns type, or empty if unknown.
Function()=default
default constructor
Function(const QString &nameIn, int args)
constructor with name and fixed number of arguments
QList< Argument > argumentList
list of arguments. May be empty despite minArgs > 0
Function(const QString &nameIn, int minArgs, int maxArgsIn)
constructor with name and min,max number of arguments