Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[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
  • Loading branch information
rouault committed Jun 6, 2016
1 parent 58bbdcb commit 9b92100
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 14 deletions.
17 changes: 14 additions & 3 deletions src/gui/qgssqlcomposerdialog.cpp
Expand Up @@ -158,14 +158,18 @@ void QgsSQLComposerDialog::accept()
{
if ( mSQLValidatorCallback )
{
QString errorMsg;
if ( !mSQLValidatorCallback->isValid( sql(), errorMsg ) )
QString errorMsg, warningMsg;
if ( !mSQLValidatorCallback->isValid( sql(), errorMsg, warningMsg ) )
{
if ( errorMsg.isEmpty() )
errorMsg = tr( "An error occurred during evaluation of the SQL statement" );
QMessageBox::critical( this, tr( "SQL error" ), errorMsg );
return;
}
if ( !warningMsg.isEmpty() )
{
QMessageBox::warning( this, tr( "SQL warning" ), warningMsg );
}
}
QDialog::accept();
}
Expand Down Expand Up @@ -710,11 +714,18 @@ void QgsSQLComposerDialog::addApis( const QStringList& list )
mQueryEdit->lexer()->setAPIs( apis );
}

void QgsSQLComposerDialog::setSupportMultipleTables( bool on )
void QgsSQLComposerDialog::setSupportMultipleTables( bool on, QString mainTypename )
{
mJoinsLabels->setVisible( on );
mTableJoins->setVisible( on );
mAddJoinButton->setVisible( on );
mRemoveJoinButton->setVisible( on );
mTablesCombo->setVisible( on );

QString mainTypenameFormatted;
if ( !mainTypename.isEmpty() )
mainTypenameFormatted = " (" + mainTypename + ")";
mQueryEdit->setToolTip( tr( "This is the SQL query editor. The SQL statement can select data from several tables, \n"
"but it must compulsory include the main typename%1 in the selected tables, \n"
"and only the geometry column of the main typename can be used as the geometry column of the resulting layer." ).arg( mainTypenameFormatted ) );
}
4 changes: 2 additions & 2 deletions src/gui/qgssqlcomposerdialog.h
Expand Up @@ -62,7 +62,7 @@ class GUI_EXPORT QgsSQLComposerDialog : public QDialog, private Ui::QgsSQLCompos
public:
virtual ~SQLValidatorCallback();
//! method should return true if the SQL is valid. Otherwise return false and set the errorReason
virtual bool isValid( const QString& sql, QString& errorReason ) = 0;
virtual bool isValid( const QString& sql, QString& errorReason, QString& warningMsg ) = 0;
};

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

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

/** Set a callback that will be called when a new table is selected, so
that new column names can be added typically.
Expand Down
20 changes: 15 additions & 5 deletions src/providers/wfs/qgswfsprovider.cpp
Expand Up @@ -86,7 +86,7 @@ QgsWFSProvider::QgsWFSProvider( const QString& uri, const QgsWFSCapabilities::Ca

if ( !mShared->mURI.sql().isEmpty() )
{
if ( !processSQL( mShared->mURI.sql(), mProcessSQLErrorMsg ) )
if ( !processSQL( mShared->mURI.sql(), mProcessSQLErrorMsg, mProcessSQLWarningMsg ) )
{
QgsMessageLog::logMessage( mProcessSQLErrorMsg, tr( "WFS" ) );
mValid = false;
Expand Down Expand Up @@ -270,10 +270,11 @@ void QgsWFSProviderSQLColumnRefValidator::visit( const QgsSQLStatement::NodeColu
}


bool QgsWFSProvider::processSQL( const QString& sqlString, QString& errorMsg )
bool QgsWFSProvider::processSQL( const QString& sqlString, QString& errorMsg, QString& warningMsg )
{
QgsDebugMsg( QString( "Processing SQL: %1" ).arg( sqlString ) );
errorMsg.clear();
warningMsg.clear();
QgsSQLStatement sql( sqlString );
if ( sql.hasParserError() )
{
Expand Down Expand Up @@ -557,8 +558,17 @@ bool QgsWFSProvider::processSQL( const QString& sqlString, QString& errorMsg )
}
}
}
// Geometry field
else if ( mapTypenameToGeometryAttribute[columnTableTypename] == columnRef->name() )
{
if ( columnTableTypename != mShared->mURI.typeName() )
{
warningMsg = tr( "The geometry field of a typename that is not the main typename is ignored in the selected fields" );
QgsDebugMsg( warningMsg );
}
}
// Regular field
else if ( mapTypenameToGeometryAttribute[columnTableTypename] != columnRef->name() )
else
{
const QgsFields tableFields = mapTypenameToFields[columnTableTypename];
int idx = tableFields.fieldNameIndex( columnRef->name() );
Expand Down Expand Up @@ -633,8 +643,8 @@ bool QgsWFSProvider::setSubsetString( const QString& theSQL, bool updateFeatureC
mShared->mDistinctSelect = false;
if ( theSQL.startsWith( "SELECT ", Qt::CaseInsensitive ) )
{
QString errorMsg;
if ( !processSQL( theSQL, errorMsg ) )
QString errorMsg, warningMsg;
if ( !processSQL( theSQL, errorMsg, warningMsg ) )
{
QgsMessageLog::logMessage( errorMsg, tr( "WFS" ) );
return false;
Expand Down
5 changes: 4 additions & 1 deletion src/providers/wfs/qgswfsprovider.h
Expand Up @@ -101,6 +101,8 @@ class QgsWFSProvider : public QgsVectorDataProvider

const QString processSQLErrorMsg() const { return mProcessSQLErrorMsg; }

const QString processSQLWarningMsg() const { return mProcessSQLWarningMsg; }

//Editing operations
/**
* Adds a list of features
Expand Down Expand Up @@ -165,6 +167,7 @@ class QgsWFSProvider : public QgsVectorDataProvider
QgsFields mThisTypenameFields;

QString mProcessSQLErrorMsg;
QString mProcessSQLWarningMsg;

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

bool processSQL( const QString& sqlString, QString& errorMsg );
bool processSQL( const QString& sqlString, QString& errorMsg, QString& warningMsg );
};

#endif /* QGSWFSPROVIDER_H */
7 changes: 4 additions & 3 deletions src/providers/wfs/qgswfssourceselect.cpp
Expand Up @@ -409,7 +409,7 @@ class QgsWFSValidatorCallback: public QObject, public QgsSQLComposerDialog::SQLV
QgsWFSValidatorCallback( QObject* parent,
const QgsWFSDataSourceURI& uri, const QString& allSql,
const QgsWFSCapabilities::Capabilities& caps );
bool isValid( const QString& sql, QString& errorReason ) override;
bool isValid( const QString& sql, QString& errorReason, QString& warningMsg ) override;
private:
QgsWFSDataSourceURI mURI;
QString mAllSql;
Expand All @@ -427,7 +427,7 @@ QgsWFSValidatorCallback::QgsWFSValidatorCallback( QObject* parent,
{
}

bool QgsWFSValidatorCallback::isValid( const QString& sqlStr, QString& errorReason )
bool QgsWFSValidatorCallback::isValid( const QString& sqlStr, QString& errorReason, QString& warningMsg )
{
errorReason.clear();
if ( sqlStr.isEmpty() || sqlStr == mAllSql )
Expand All @@ -441,6 +441,7 @@ bool QgsWFSValidatorCallback::isValid( const QString& sqlStr, QString& errorReas
errorReason = p.processSQLErrorMsg();
return false;
}
warningMsg = p.processSQLWarningMsg();

return true;
}
Expand Down Expand Up @@ -546,7 +547,7 @@ void QgsWFSSourceSelect::buildQuery( const QModelIndex& index )
d->setTableSelectedCallback( tableSelectedCbk );

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

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

0 comments on commit 9b92100

Please sign in to comment.