QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgssqlcomposerdialog.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgssqlcomposerdialog.cpp
3  Dialog to compose SQL queries
4 
5 begin : Apr 2016
6 copyright : (C) 2016 Even Rouault
7 email : 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"
30 
32 
38 class GUI_EXPORT QgsSQLComposerDialog : public QDialog, private Ui::QgsSQLComposerDialogBase
39 {
40  Q_OBJECT
41 
42  public:
43 
45  typedef QPair<QString, QString> PairNameTitle;
46 
48  typedef QPair<QString, QString> PairNameType;
49 
55  class GUI_EXPORT TableSelectedCallback
56  {
57  public:
58  virtual ~TableSelectedCallback() = default;
60  virtual void tableSelected( const QString &name ) = 0;
61  };
62 
68  class GUI_EXPORT SQLValidatorCallback
69  {
70  public:
71  virtual ~SQLValidatorCallback() = default;
73  virtual bool isValid( const QString &sql, QString &errorReason, QString &warningMsg ) = 0;
74  };
75 
77  struct Argument
78  {
80  QString name;
82  QString type;
83 
85  Argument( const QString &nameIn = QString(), const QString &typeIn = QString() ) : name( nameIn ), type( typeIn ) {}
86  };
87 
89  struct Function
90  {
92  QString name;
94  QString returnType;
96  int minArgs = -1;
98  int maxArgs = -1;
100  QList<Argument> argumentList;
101 
103  Function( const QString &nameIn, int args ) : name( nameIn ), minArgs( args ), maxArgs( args ) {}
105  Function( const QString &nameIn, int minArgs, int maxArgsIn ) : name( nameIn ), minArgs( minArgs ), maxArgs( maxArgsIn ) {}
107  Function() = default;
108  };
109 
111  explicit QgsSQLComposerDialog( QWidget *parent = nullptr, Qt::WindowFlags fl = QgsGuiUtils::ModalDialogFlags );
112  ~QgsSQLComposerDialog() override;
113 
115  void setSql( const QString &sql );
116 
118  QString sql() const;
119 
121  void addTableNames( const QStringList &list );
123  void addTableNames( const QList<PairNameTitle> &listNameTitle );
125  void addColumnNames( const QStringList &list, const QString &tableName );
127  void addColumnNames( const QList<PairNameType> &list, const QString &tableName );
129  void addOperators( const QStringList &list );
131  void addSpatialPredicates( const QStringList &list );
133  void addSpatialPredicates( const QList<Function> &list );
135  void addFunctions( const QStringList &list );
137  void addFunctions( const QList<Function> &list );
139  void addApis( const QStringList &list );
140 
142  void setSupportMultipleTables( bool bMultipleTables, const QString &mainTypename = QString() );
143 
149  void setTableSelectedCallback( TableSelectedCallback *tableSelectedCallback );
150 
155  void setSQLValidatorCallback( SQLValidatorCallback *sqlValidatorCallback );
156 
157  protected:
158  bool eventFilter( QObject *obj, QEvent *event ) override;
159 
160  private slots:
161  void accept() override;
162 
163  void mTablesCombo_currentIndexChanged( int );
164  void mColumnsCombo_currentIndexChanged( int );
165  void mSpatialPredicatesCombo_currentIndexChanged( int );
166  void mFunctionsCombo_currentIndexChanged( int );
167  void mOperatorsCombo_currentIndexChanged( int );
168  void mAddJoinButton_clicked();
169  void mRemoveJoinButton_clicked();
170  void mTableJoins_itemSelectionChanged();
171  void showHelp();
172  void reset();
173  void buildSQLFromFields();
174  void splitSQLIntoFields();
175 
176  private:
177  QStringList mApiList;
178  QSet<QString> mAlreadySelectedTables;
179  TableSelectedCallback *mTableSelectedCallback = nullptr;
180  SQLValidatorCallback *mSQLValidatorCallback = nullptr;
181  QObject *mFocusedObject = nullptr;
182  bool mAlreadyModifyingFields = false;
183  bool mDistinct = false;
184  QString mResetSql;
185  QMap<QString, QString> mapTableEntryTextToName;
186  QMap<QString, QString> mapColumnEntryTextToName;
187  QMap<QString, QString> mapSpatialPredicateEntryTextToName;
188  QMap<QString, QString> mapFunctionEntryTextToName;
189  QString lastSearchedText;
190 
191 
192  void loadTableColumns( const QString &table );
193  void functionCurrentIndexChanged( QComboBox *combo,
194  const QMap<QString, QString> &mapEntryTextToName );
195  void getFunctionList( const QList<Function> &list,
196  QStringList &listApi,
197  QStringList &listCombo,
198  QMap<QString, QString> &mapEntryTextToName );
199 };
200 
201 #endif
QgsSQLComposerDialog::TableSelectedCallback
Callback to do actions on table selection.
Definition: qgssqlcomposerdialog.h:56
QgsSQLComposerDialog::SQLValidatorCallback::isValid
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
QgsSQLComposerDialog::Function::returnType
QString returnType
Returns type, or empty if unknown.
Definition: qgssqlcomposerdialog.h:94
QgsSQLComposerDialog::Function::Function
Function()=default
default constructor
QgsSQLComposerDialog::Argument::name
QString name
name
Definition: qgssqlcomposerdialog.h:80
QgsSQLComposerDialog::PairNameType
QPair< QString, QString > PairNameType
pair (name, type)
Definition: qgssqlcomposerdialog.h:48
QgsSQLComposerDialog::Function::Function
Function(const QString &nameIn, int args)
constructor with name and fixed number of arguments
Definition: qgssqlcomposerdialog.h:103
QgsSQLComposerDialog::PairNameTitle
QPair< QString, QString > PairNameTitle
pair (name, title)
Definition: qgssqlcomposerdialog.h:45
qgis_sip.h
QgsSQLComposerDialog::Argument::Argument
Argument(const QString &nameIn=QString(), const QString &typeIn=QString())
constructor
Definition: qgssqlcomposerdialog.h:85
QgsSQLComposerDialog::Argument
argument of a function
Definition: qgssqlcomposerdialog.h:78
QgsSQLComposerDialog::TableSelectedCallback::~TableSelectedCallback
virtual ~TableSelectedCallback()=default
QgsSQLComposerDialog::Function::name
QString name
name
Definition: qgssqlcomposerdialog.h:92
QgsSQLComposerDialog
SQL composer dialog.
Definition: qgssqlcomposerdialog.h:39
QgsSQLComposerDialog::TableSelectedCallback::tableSelected
virtual void tableSelected(const QString &name)=0
method called when a table is selected
QgsSQLComposerDialog::Function
description of server functions
Definition: qgssqlcomposerdialog.h:90
SIP_NO_FILE
#define SIP_NO_FILE
Definition: qgis_sip.h:23
QgsSQLComposerDialog::Function::Function
Function(const QString &nameIn, int minArgs, int maxArgsIn)
constructor with name and min,max number of arguments
Definition: qgssqlcomposerdialog.h:105
QgsSQLComposerDialog::SQLValidatorCallback::~SQLValidatorCallback
virtual ~SQLValidatorCallback()=default
qgsguiutils.h
QgsSQLComposerDialog::Argument::type
QString type
type, or empty if unknown
Definition: qgssqlcomposerdialog.h:82
QgsSQLComposerDialog::SQLValidatorCallback
Callback to do validation check on dialog validation.
Definition: qgssqlcomposerdialog.h:69
QgsSQLComposerDialog::Function::argumentList
QList< Argument > argumentList
list of arguments. May be empty despite minArgs > 0
Definition: qgssqlcomposerdialog.h:100