Skip to content

Commit 56ca46e

Browse files
authoredDec 19, 2018
Merge pull request #8706 from elpaso/bugfix-20674-dbmanager-aliased-3
Fix views query tables in spatialite
2 parents 6bb0720 + 07d9d1d commit 56ca46e

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed
 

‎src/providers/spatialite/qgsspatialiteprovider.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4666,18 +4666,28 @@ bool QgsSpatiaLiteProvider::checkLayerType()
46664666
.arg( sql ), tr( "SpatiaLite" ), Qgis::MessageLevel::Warning );
46674667
}
46684668

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 );
46754671
ret = sqlite3_get_table( mSqliteHandle, sql.toUtf8().constData(), &results, &rows, &columns, &errMsg );
46764672
if ( ret == SQLITE_OK && rows == 1 )
46774673
{
4678-
mQuery = newSql;
46794674
mPrimaryKey = pks.first( );
46804675
}
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+
}
46814691
}
46824692

46834693
// If there is still no primary key, check if we can get use the ROWID from the table that provides the geometry

‎tests/src/python/test_provider_spatialite.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,8 @@ def testLoadStyle(self):
791791

792792
def _aliased_sql_helper(self, dbname):
793793
queries = (
794+
'(SELECT * FROM (SELECT * from \\"some view\\"))',
795+
'(SELECT * FROM \\"some view\\")',
794796
'(select sd.* from somedata as sd left join somedata as sd2 on ( sd2.name = sd.name ))',
795797
'(select sd.* from \\"somedata\\" as sd left join \\"somedata\\" as sd2 on ( sd2.name = sd.name ))',
796798
"(SELECT * FROM somedata as my_alias1\n)",
@@ -814,6 +816,8 @@ def _aliased_sql_helper(self, dbname):
814816
for sql in queries:
815817
vl = QgsVectorLayer('dbname=\'{}\' table="{}" (geom) sql='.format(dbname, sql), 'test', 'spatialite')
816818
self.assertTrue(vl.isValid(), 'dbname: {} - sql: {}'.format(dbname, sql))
819+
self.assertTrue(vl.featureCount() > 1)
820+
self.assertTrue(vl.isSpatial())
817821

818822
def testPkLessQuery(self):
819823
"""Test if features in queries with/without pk can be retrieved by id"""
@@ -855,6 +859,9 @@ def _make_table(table_name):
855859
_make_table("somedata")
856860
_make_table("some data")
857861

862+
sql = "CREATE VIEW \"some view\" AS SELECT * FROM \"somedata\""
863+
cur.execute(sql)
864+
858865
cur.execute("COMMIT")
859866
con.close()
860867

‎tests/testdata/provider/spatialite.db

3 KB
Binary file not shown.

0 commit comments

Comments
 (0)
Please sign in to comment.