Skip to content

Commit 9b92100

Browse files
committed
[WFS provider] Improve tooltip of SQL statement field when joins are possible, and a warning when specifying a geometry field of another layer in the selected columns
1 parent 58bbdcb commit 9b92100

5 files changed

+39
-14
lines changed

src/gui/qgssqlcomposerdialog.cpp

+14-3
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,18 @@ void QgsSQLComposerDialog::accept()
158158
{
159159
if ( mSQLValidatorCallback )
160160
{
161-
QString errorMsg;
162-
if ( !mSQLValidatorCallback->isValid( sql(), errorMsg ) )
161+
QString errorMsg, warningMsg;
162+
if ( !mSQLValidatorCallback->isValid( sql(), errorMsg, warningMsg ) )
163163
{
164164
if ( errorMsg.isEmpty() )
165165
errorMsg = tr( "An error occurred during evaluation of the SQL statement" );
166166
QMessageBox::critical( this, tr( "SQL error" ), errorMsg );
167167
return;
168168
}
169+
if ( !warningMsg.isEmpty() )
170+
{
171+
QMessageBox::warning( this, tr( "SQL warning" ), warningMsg );
172+
}
169173
}
170174
QDialog::accept();
171175
}
@@ -710,11 +714,18 @@ void QgsSQLComposerDialog::addApis( const QStringList& list )
710714
mQueryEdit->lexer()->setAPIs( apis );
711715
}
712716

713-
void QgsSQLComposerDialog::setSupportMultipleTables( bool on )
717+
void QgsSQLComposerDialog::setSupportMultipleTables( bool on, QString mainTypename )
714718
{
715719
mJoinsLabels->setVisible( on );
716720
mTableJoins->setVisible( on );
717721
mAddJoinButton->setVisible( on );
718722
mRemoveJoinButton->setVisible( on );
719723
mTablesCombo->setVisible( on );
724+
725+
QString mainTypenameFormatted;
726+
if ( !mainTypename.isEmpty() )
727+
mainTypenameFormatted = " (" + mainTypename + ")";
728+
mQueryEdit->setToolTip( tr( "This is the SQL query editor. The SQL statement can select data from several tables, \n"
729+
"but it must compulsory include the main typename%1 in the selected tables, \n"
730+
"and only the geometry column of the main typename can be used as the geometry column of the resulting layer." ).arg( mainTypenameFormatted ) );
720731
}

src/gui/qgssqlcomposerdialog.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class GUI_EXPORT QgsSQLComposerDialog : public QDialog, private Ui::QgsSQLCompos
6262
public:
6363
virtual ~SQLValidatorCallback();
6464
//! method should return true if the SQL is valid. Otherwise return false and set the errorReason
65-
virtual bool isValid( const QString& sql, QString& errorReason ) = 0;
65+
virtual bool isValid( const QString& sql, QString& errorReason, QString& warningMsg ) = 0;
6666
};
6767

6868
//! argument of a function
@@ -131,7 +131,7 @@ class GUI_EXPORT QgsSQLComposerDialog : public QDialog, private Ui::QgsSQLCompos
131131
void addApis( const QStringList& list );
132132

133133
//! set if multiple tables/joins are supported. Default is false
134-
void setSupportMultipleTables( bool );
134+
void setSupportMultipleTables( bool bMultipleTables, QString mainTypename = QString() );
135135

136136
/** Set a callback that will be called when a new table is selected, so
137137
that new column names can be added typically.

src/providers/wfs/qgswfsprovider.cpp

+15-5
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ QgsWFSProvider::QgsWFSProvider( const QString& uri, const QgsWFSCapabilities::Ca
8686

8787
if ( !mShared->mURI.sql().isEmpty() )
8888
{
89-
if ( !processSQL( mShared->mURI.sql(), mProcessSQLErrorMsg ) )
89+
if ( !processSQL( mShared->mURI.sql(), mProcessSQLErrorMsg, mProcessSQLWarningMsg ) )
9090
{
9191
QgsMessageLog::logMessage( mProcessSQLErrorMsg, tr( "WFS" ) );
9292
mValid = false;
@@ -270,10 +270,11 @@ void QgsWFSProviderSQLColumnRefValidator::visit( const QgsSQLStatement::NodeColu
270270
}
271271

272272

273-
bool QgsWFSProvider::processSQL( const QString& sqlString, QString& errorMsg )
273+
bool QgsWFSProvider::processSQL( const QString& sqlString, QString& errorMsg, QString& warningMsg )
274274
{
275275
QgsDebugMsg( QString( "Processing SQL: %1" ).arg( sqlString ) );
276276
errorMsg.clear();
277+
warningMsg.clear();
277278
QgsSQLStatement sql( sqlString );
278279
if ( sql.hasParserError() )
279280
{
@@ -557,8 +558,17 @@ bool QgsWFSProvider::processSQL( const QString& sqlString, QString& errorMsg )
557558
}
558559
}
559560
}
561+
// Geometry field
562+
else if ( mapTypenameToGeometryAttribute[columnTableTypename] == columnRef->name() )
563+
{
564+
if ( columnTableTypename != mShared->mURI.typeName() )
565+
{
566+
warningMsg = tr( "The geometry field of a typename that is not the main typename is ignored in the selected fields" );
567+
QgsDebugMsg( warningMsg );
568+
}
569+
}
560570
// Regular field
561-
else if ( mapTypenameToGeometryAttribute[columnTableTypename] != columnRef->name() )
571+
else
562572
{
563573
const QgsFields tableFields = mapTypenameToFields[columnTableTypename];
564574
int idx = tableFields.fieldNameIndex( columnRef->name() );
@@ -633,8 +643,8 @@ bool QgsWFSProvider::setSubsetString( const QString& theSQL, bool updateFeatureC
633643
mShared->mDistinctSelect = false;
634644
if ( theSQL.startsWith( "SELECT ", Qt::CaseInsensitive ) )
635645
{
636-
QString errorMsg;
637-
if ( !processSQL( theSQL, errorMsg ) )
646+
QString errorMsg, warningMsg;
647+
if ( !processSQL( theSQL, errorMsg, warningMsg ) )
638648
{
639649
QgsMessageLog::logMessage( errorMsg, tr( "WFS" ) );
640650
return false;

src/providers/wfs/qgswfsprovider.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ class QgsWFSProvider : public QgsVectorDataProvider
101101

102102
const QString processSQLErrorMsg() const { return mProcessSQLErrorMsg; }
103103

104+
const QString processSQLWarningMsg() const { return mProcessSQLWarningMsg; }
105+
104106
//Editing operations
105107
/**
106108
* Adds a list of features
@@ -165,6 +167,7 @@ class QgsWFSProvider : public QgsVectorDataProvider
165167
QgsFields mThisTypenameFields;
166168

167169
QString mProcessSQLErrorMsg;
170+
QString mProcessSQLWarningMsg;
168171

169172
/** Collects information about the field types. Is called internally from QgsWFSProvider ctor.
170173
The method gives back the name of
@@ -202,7 +205,7 @@ class QgsWFSProvider : public QgsVectorDataProvider
202205
/** Convert the value to its appropriate XML representation */
203206
QString convertToXML( const QVariant& value );
204207

205-
bool processSQL( const QString& sqlString, QString& errorMsg );
208+
bool processSQL( const QString& sqlString, QString& errorMsg, QString& warningMsg );
206209
};
207210

208211
#endif /* QGSWFSPROVIDER_H */

src/providers/wfs/qgswfssourceselect.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ class QgsWFSValidatorCallback: public QObject, public QgsSQLComposerDialog::SQLV
409409
QgsWFSValidatorCallback( QObject* parent,
410410
const QgsWFSDataSourceURI& uri, const QString& allSql,
411411
const QgsWFSCapabilities::Capabilities& caps );
412-
bool isValid( const QString& sql, QString& errorReason ) override;
412+
bool isValid( const QString& sql, QString& errorReason, QString& warningMsg ) override;
413413
private:
414414
QgsWFSDataSourceURI mURI;
415415
QString mAllSql;
@@ -427,7 +427,7 @@ QgsWFSValidatorCallback::QgsWFSValidatorCallback( QObject* parent,
427427
{
428428
}
429429

430-
bool QgsWFSValidatorCallback::isValid( const QString& sqlStr, QString& errorReason )
430+
bool QgsWFSValidatorCallback::isValid( const QString& sqlStr, QString& errorReason, QString& warningMsg )
431431
{
432432
errorReason.clear();
433433
if ( sqlStr.isEmpty() || sqlStr == mAllSql )
@@ -441,6 +441,7 @@ bool QgsWFSValidatorCallback::isValid( const QString& sqlStr, QString& errorReas
441441
errorReason = p.processSQLErrorMsg();
442442
return false;
443443
}
444+
warningMsg = p.processSQLWarningMsg();
444445

445446
return true;
446447
}
@@ -546,7 +547,7 @@ void QgsWFSSourceSelect::buildQuery( const QModelIndex& index )
546547
d->setTableSelectedCallback( tableSelectedCbk );
547548

548549
const bool bSupportJoins = mCaps.featureTypes.size() > 1 && mCaps.supportsJoins;
549-
d->setSupportMultipleTables( bSupportJoins );
550+
d->setSupportMultipleTables( bSupportJoins, QgsSQLStatement::quotedIdentifierIfNeeded( displayedTypeName ) );
550551

551552
QMap< QString, QString > mapTypenameToTitle;
552553
Q_FOREACH ( const QgsWFSCapabilities::FeatureType f, mCaps.featureTypes )

0 commit comments

Comments
 (0)