@@ -4666,18 +4666,28 @@ bool QgsSpatiaLiteProvider::checkLayerType()
4666
4666
.arg ( sql ), tr ( " SpatiaLite" ), Qgis::MessageLevel::Warning );
4667
4667
}
4668
4668
4669
- QString pk { QStringLiteral ( " %1.%2" ).arg ( quotedIdentifier ( alias ) ).arg ( pks.first () ) };
4670
- QString newSql ( mQuery .replace ( injectionRe,
4671
- QStringLiteral ( R"re( SELECT %1.%2, \1)re" )
4672
- .arg ( quotedIdentifier ( tableIdentifier ) )
4673
- .arg ( pks.first () ) ) );
4674
- sql = QStringLiteral ( " SELECT %1 FROM %2 LIMIT 1" ).arg ( pk ).arg ( newSql );
4669
+ // Try first without any injection or manipulation
4670
+ sql = QStringLiteral ( " SELECT %1, %2 FROM %3 LIMIT 1" ).arg ( quotedIdentifier ( pks.first ( ) ), quotedIdentifier ( mGeometryColumn ), mQuery );
4675
4671
ret = sqlite3_get_table ( mSqliteHandle , sql.toUtf8 ().constData (), &results, &rows, &columns, &errMsg );
4676
4672
if ( ret == SQLITE_OK && rows == 1 )
4677
4673
{
4678
- mQuery = newSql;
4679
4674
mPrimaryKey = pks.first ( );
4680
4675
}
4676
+ else // if that does not work, try injection with table name/alias
4677
+ {
4678
+ QString pk { QStringLiteral ( " %1.%2" ).arg ( quotedIdentifier ( alias ) ).arg ( pks.first () ) };
4679
+ QString newSql ( mQuery .replace ( injectionRe,
4680
+ QStringLiteral ( R"re( SELECT %1.%2, \1)re" )
4681
+ .arg ( quotedIdentifier ( tableIdentifier ) )
4682
+ .arg ( pks.first () ) ) );
4683
+ sql = QStringLiteral ( " SELECT %1 FROM %2 LIMIT 1" ).arg ( pk ).arg ( newSql );
4684
+ ret = sqlite3_get_table ( mSqliteHandle , sql.toUtf8 ().constData (), &results, &rows, &columns, &errMsg );
4685
+ if ( ret == SQLITE_OK && rows == 1 )
4686
+ {
4687
+ mQuery = newSql;
4688
+ mPrimaryKey = pks.first ( );
4689
+ }
4690
+ }
4681
4691
}
4682
4692
4683
4693
// If there is still no primary key, check if we can get use the ROWID from the table that provides the geometry
0 commit comments