Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport release-3_40] Fix query layers from SQL window trailing semicolon #60230

Merged
merged 3 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,8 @@ Checks if ``capability`` is supported.





};

QFlags<QgsAbstractDatabaseProviderConnection::Capability> operator|(QgsAbstractDatabaseProviderConnection::Capability f1, QFlags<QgsAbstractDatabaseProviderConnection::Capability> f2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,8 @@ Checks if ``capability`` is supported.





};

QFlags<QgsAbstractDatabaseProviderConnection::Capability> operator|(QgsAbstractDatabaseProviderConnection::Capability f1, QFlags<QgsAbstractDatabaseProviderConnection::Capability> f2);
Expand Down
2 changes: 1 addition & 1 deletion src/core/providers/ogr/qgsogrproviderconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ QgsVectorLayer *QgsOgrProviderConnection::createSqlVectorLayer( const QgsAbstrac
QgsProviderMetadata *providerMetadata { QgsProviderRegistry::instance()->providerMetadata( QStringLiteral( "ogr" ) ) };
Q_ASSERT( providerMetadata );
QMap<QString, QVariant> decoded = providerMetadata->decodeUri( uri() );
decoded[ QStringLiteral( "subset" ) ] = options.sql;
decoded[ QStringLiteral( "subset" ) ] = sanitizeSqlForQueryLayer( options.sql ) ;
return new QgsVectorLayer( providerMetadata->encodeUri( decoded ), options.layerName.isEmpty() ? QStringLiteral( "QueryLayer" ) : options.layerName, providerKey() );
}

Expand Down
10 changes: 10 additions & 0 deletions src/core/providers/qgsabstractdatabaseproviderconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ void QgsAbstractDatabaseProviderConnection::checkCapability( Qgis::DatabaseProvi
}
}

QString QgsAbstractDatabaseProviderConnection::sanitizeSqlForQueryLayer( const QString &sql ) const
{
QString sanitizedSql { sql.trimmed() };
while ( sanitizedSql.endsWith( ';' ) )
{
sanitizedSql.chop( 1 );
}
return sanitizedSql;
}

///@endcond

QString QgsAbstractDatabaseProviderConnection::providerKey() const
Expand Down
4 changes: 4 additions & 0 deletions src/core/providers/qgsabstractdatabaseproviderconnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -1143,6 +1143,10 @@ class CORE_EXPORT QgsAbstractDatabaseProviderConnection : public QgsAbstractProv
* \throws QgsProviderConnectionException if the capability is not supported
*/
void checkCapability( Qgis::DatabaseProviderConnectionCapability2 capability ) const;

//! Trim and remove any trailing semicolon
QString sanitizeSqlForQueryLayer( const QString &sql ) const SIP_SKIP;

///@endcond

Capabilities mCapabilities = Capabilities() SIP_SKIP;
Expand Down
2 changes: 1 addition & 1 deletion src/providers/hana/qgshanaproviderconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ QgsVectorLayer *QgsHanaProviderConnection::createSqlVectorLayer( const SqlVector
if ( !options.primaryKeyColumns.isEmpty() )
{
tUri.setKeyColumn( QgsHanaPrimaryKeyUtils::buildUriKey( options.primaryKeyColumns ) );
tUri.setTable( QStringLiteral( "(%1)" ).arg( options.sql ) );
tUri.setTable( QStringLiteral( "(%1)" ).arg( sanitizeSqlForQueryLayer( options.sql ) ) );
}
else
{
Expand Down
12 changes: 7 additions & 5 deletions src/providers/oracle/qgsoracleproviderconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ QgsVectorLayer *QgsOracleProviderConnection::createSqlVectorLayer( const QgsAbst
throw QgsProviderConnectionException( QObject::tr( "Could not create a SQL vector layer: SQL expression is empty." ) );
}

const QString optionsSql { sanitizeSqlForQueryLayer( options.sql ) };

QgsDataSourceUri tUri( uri() );

tUri.setSql( options.filter );
Expand All @@ -173,25 +175,25 @@ QgsVectorLayer *QgsOracleProviderConnection::createSqlVectorLayer( const QgsAbst
if ( !options.primaryKeyColumns.isEmpty() )
{
tUri.setKeyColumn( options.primaryKeyColumns.join( ',' ) );
tUri.setTable( QStringLiteral( "(%1)" ).arg( options.sql ) );
tUri.setTable( QStringLiteral( "(%1)" ).arg( optionsSql ) );
}
else
{
// Disable when there is no pk
tUri.setUseEstimatedMetadata( false );
int pkId { 0 };
while ( options.sql.contains( QStringLiteral( "qgis_generated_uid_%1_" ).arg( pkId ), Qt::CaseSensitivity::CaseInsensitive ) )
while ( optionsSql.contains( QStringLiteral( "qgis_generated_uid_%1_" ).arg( pkId ), Qt::CaseSensitivity::CaseInsensitive ) )
{
pkId++;
}
tUri.setKeyColumn( QStringLiteral( "qgis_generated_uid_%1_" ).arg( pkId ) );

int sqlId { 0 };
while ( options.sql.contains( QStringLiteral( "qgis_generated_subq_%1_" ).arg( sqlId ), Qt::CaseSensitivity::CaseInsensitive ) )
while ( optionsSql.contains( QStringLiteral( "qgis_generated_subq_%1_" ).arg( sqlId ), Qt::CaseSensitivity::CaseInsensitive ) )
{
sqlId++;
}
tUri.setTable( QStringLiteral( "(SELECT row_number() over (ORDER BY NULL) AS qgis_generated_uid_%1_, qgis_generated_subq_%3_.* FROM (%2\n) qgis_generated_subq_%3_\n)" ).arg( QString::number( pkId ), options.sql, QString::number( sqlId ) ) );
tUri.setTable( QStringLiteral( "(SELECT row_number() over (ORDER BY NULL) AS qgis_generated_uid_%1_, qgis_generated_subq_%3_.* FROM (%2\n) qgis_generated_subq_%3_\n)" ).arg( QString::number( pkId ), optionsSql, QString::number( sqlId ) ) );
}

if ( !options.geometryColumn.isEmpty() )
Expand All @@ -212,7 +214,7 @@ QgsVectorLayer *QgsOracleProviderConnection::createSqlVectorLayer( const QgsAbst
WHERE a.%1 IS NOT NULL %3
ORDER BY a.%1.SDO_GTYPE
)" )
.arg( options.geometryColumn, options.sql, limit ) };
.arg( options.geometryColumn, optionsSql, limit ) };
const QList<QList<QVariant>> candidates { executeSql( sql ) };
for ( const QList<QVariant> &row : std::as_const( candidates ) )
{
Expand Down
4 changes: 2 additions & 2 deletions src/providers/postgres/qgspostgresproviderconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,7 @@ QgsVectorLayer *QgsPostgresProviderConnection::createSqlVectorLayer( const SqlVe
if ( !options.primaryKeyColumns.isEmpty() )
{
tUri.setKeyColumn( options.primaryKeyColumns.join( ',' ) );
tUri.setTable( QStringLiteral( "(%1)" ).arg( options.sql ) );
tUri.setTable( QStringLiteral( "(%1)" ).arg( sanitizeSqlForQueryLayer( options.sql ) ) );
}
else
{
Expand All @@ -891,7 +891,7 @@ QgsVectorLayer *QgsPostgresProviderConnection::createSqlVectorLayer( const SqlVe
{
sqlId++;
}
tUri.setTable( QStringLiteral( "(SELECT row_number() over () AS _uid%1_, * FROM (%2\n) AS _subq_%3_\n)" ).arg( QString::number( pkId ), options.sql, QString::number( sqlId ) ) );
tUri.setTable( QStringLiteral( "(SELECT row_number() over () AS _uid%1_, * FROM (%2\n) AS _subq_%3_\n)" ).arg( QString::number( pkId ), sanitizeSqlForQueryLayer( options.sql ), QString::number( sqlId ) ) );
}

if ( !options.geometryColumn.isEmpty() )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ QgsVectorLayer *QgsSpatiaLiteProviderConnection::createSqlVectorLayer( const Qgs
QgsDataSourceUri tUri( uri() );

tUri.setSql( options.filter );
tUri.setTable( '(' + options.sql + ')' );
tUri.setTable( '(' + sanitizeSqlForQueryLayer( options.sql ) + ')' );

if ( !options.geometryColumn.isEmpty() )
{
Expand Down
8 changes: 8 additions & 0 deletions tests/src/python/test_qgsproviderconnection_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,14 @@ def testCreateSqlVectorLayer(self):
self.assertEqual(vl.name(), options.layerName)
self.assertEqual(vl.sourceCrs().authid(), crs)

# Test issue https://github.com/qgis/QGIS/issues/56993
options.sql += ";;;"
vl2 = conn.createSqlVectorLayer(options)
self.assertTrue(vl2.isValid())
self.assertTrue(vl2.isSqlQuery())
self.assertTrue(vl2.isSpatial())
del vl2

# Test that a database connection can be retrieved from an existing layer
vlconn = QgsMapLayerUtils.databaseConnection(vl)
self.assertIsNotNone(vlconn)
Expand Down
Loading