Skip to content

Commit 6f725b8

Browse files
committedDec 13, 2018
sqlite accept aliased queries from db manager
Fixes #20674 - DB Manager - load sql query as layer with geom column Well, not sure it really fixes that particular issue because it is not really well described, but for sure this fixes the general case of "SELECT * FROM my_table AS my_alias"
1 parent 527448d commit 6f725b8

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed
 

‎python/plugins/db_manager/dlg_sql_layer_window.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,13 @@ def __init__(self, iface, layer, parent=None):
147147

148148
# Update from layer
149149
# First the SQL from QgsDataSourceUri table
150-
sql = uri.table()
150+
sql = uri.table().replace('\n', ' ').strip()
151151
if uri.keyColumn() == '_uid_':
152-
match = re.search(r'^\(SELECT .+ AS _uid_,\* FROM \((.*)\) AS _subq_.+_\s*\)$', sql, re.S | re.X)
152+
match = re.search(r'^\(SELECT .+ AS _uid_,\* FROM \((.*)\) AS _subq_.+_\s*\)$', sql, re.S | re.X | re.IGNORECASE)
153153
if match:
154154
sql = match.group(1)
155155
else:
156-
match = re.search(r'^\((SELECT .+ FROM .+)\)$', sql, re.S | re.X)
156+
match = re.search(r'^\((SELECT .+ FROM .+)\)$', sql, re.S | re.X | re.IGNORECASE)
157157
if match:
158158
sql = match.group(1)
159159
# Need to check on table() since the parentheses were removed by the regexp

‎python/plugins/db_manager/dlg_sql_window.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ def createView(self):
561561
def _getSqlQuery(self):
562562
sql = self.editSql.selectedText()
563563
if len(sql) == 0:
564-
sql = self.editSql.text()
564+
sql = self.editSql.text().replace('\n', ' ').strip()
565565
return sql
566566

567567
def uniqueChanged(self):

‎src/providers/spatialite/qgsspatialiteprovider.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4603,9 +4603,21 @@ bool QgsSpatiaLiteProvider::checkLayerType()
46034603
// 3. check if ROWID injection works
46044604
if ( ! queryGeomTableName.isEmpty() )
46054605
{
4606+
// Check if the whole sql is aliased (I couldn't find a sqlite API call to get this information)
4607+
QRegularExpression re { R"re(\s+AS\s+(\w+)\n?\)?$)re" };
4608+
re.setPatternOptions( QRegularExpression::PatternOption::MultilineOption |
4609+
QRegularExpression::PatternOption::CaseInsensitiveOption );
4610+
QRegularExpressionMatch match { re.match( mTableName ) };
4611+
regex.setPattern( QStringLiteral( R"re(\s+AS\s+(\w+)\n?\)?$)re" ) );
4612+
QString tableAlias;
4613+
if ( match.hasMatch() )
4614+
{
4615+
tableAlias = match.captured( 1 );
4616+
}
46064617
QString newSql( mQuery.replace( QStringLiteral( "SELECT " ),
46074618
QStringLiteral( "SELECT %1.%2, " )
4608-
.arg( quotedIdentifier( queryGeomTableName ), QStringLiteral( "ROWID" ) ),
4619+
.arg( quotedIdentifier( tableAlias.isEmpty() ? queryGeomTableName : tableAlias ),
4620+
QStringLiteral( "ROWID" ) ),
46094621
Qt::CaseInsensitive ) );
46104622
sql = QStringLiteral( "SELECT ROWID FROM %1 WHERE ROWID IS NOT NULL LIMIT 1" ).arg( newSql );
46114623
ret = sqlite3_get_table( mSqliteHandle, sql.toUtf8().constData(), &results, &rows, &columns, &errMsg );

0 commit comments

Comments
 (0)
Please sign in to comment.