Skip to content

Commit c2fb11c

Browse files
committed
db manager: only alias subqueries on postgres (fixes #13731)
1 parent f92efbc commit c2fb11c

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

python/plugins/db_manager/dlg_sql_window.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ def __init__(self, iface, db, parent=None):
5555
self.iface = iface
5656
self.db = db
5757
self.filter = ""
58-
self.allowMultiColumnPk = isinstance(db, PGDatabase) # at the moment only PostGIS allows a primary key to span multiple columns, spatialite doesn't
58+
self.allowMultiColumnPk = isinstance(db, PGDatabase) # at the moment only PostgreSQL allows a primary key to span multiple columns, spatialite doesn't
59+
self.aliasSubQuery = isinstance(db, PGDatabase) # only PostgreSQL requires subqueries to be aliases
5960
self.setupUi(self)
6061
self.setWindowTitle(
6162
u"%s - %s [%s]" % (self.windowTitle(), db.connection().connectionName(), db.connection().typeNameString()))
@@ -283,15 +284,6 @@ def fillColumnCombos(self):
283284

284285
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
285286

286-
# get a new alias
287-
aliasIndex = 0
288-
while True:
289-
alias = "_%s__%d" % ("subQuery", aliasIndex)
290-
escaped = re.compile('\\b("?)' + re.escape(alias) + '\\1\\b')
291-
if not escaped.search(query):
292-
break
293-
aliasIndex += 1
294-
295287
# remove a trailing ';' from query if present
296288
if query.strip().endswith(';'):
297289
query = query.strip()[:-1]
@@ -300,7 +292,19 @@ def fillColumnCombos(self):
300292
cols = []
301293
quotedCols = []
302294
connector = self.db.connector
303-
sql = u"SELECT * FROM (%s\n) AS %s WHERE 0=1" % (unicode(query), connector.quoteId(alias))
295+
if self.aliasSubQuery:
296+
# get a new alias
297+
aliasIndex = 0
298+
while True:
299+
alias = "_subQuery__%d" % aliasIndex
300+
escaped = re.compile('\\b("?)' + re.escape(alias) + '\\1\\b')
301+
if not escaped.search(query):
302+
break
303+
aliasIndex += 1
304+
305+
sql = u"SELECT * FROM (%s\n) AS %s LIMIT 0" % (unicode(query), connector.quoteId(alias))
306+
else:
307+
sql = u"SELECT * FROM (%s\n) WHERE 1=0" % unicode(query)
304308

305309
c = None
306310
try:

0 commit comments

Comments
 (0)