Skip to content

Commit 80c5cb6

Browse files
jef-nrldhont
authored andcommitted
db manager: only alias subqueries on postgres (fixes #13731)
Conflicts: python/plugins/db_manager/dlg_sql_window.py
1 parent edd3f8d commit 80c5cb6

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

python/plugins/db_manager/dlg_sql_window.py

+15-11
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ def __init__(self, iface, db, parent=None):
5454
QWidget.__init__(self, parent)
5555
self.iface = iface
5656
self.db = db
57-
self.allowMultiColumnPk = isinstance(db, PGDatabase) # at the moment only PostGIS allows a primary key to span multiple columns, spatialite doesn't
57+
self.allowMultiColumnPk = isinstance(db, PGDatabase) # at the moment only PostgreSQL allows a primary key to span multiple columns, spatialite doesn't
58+
self.aliasSubQuery = isinstance(db, PGDatabase) # only PostgreSQL requires subqueries to be aliases
5859
self.setupUi(self)
5960
self.setWindowTitle(
6061
u"%s - %s [%s]" % (self.windowTitle(), db.connection().connectionName(), db.connection().typeNameString()))
@@ -270,15 +271,6 @@ def fillColumnCombos(self):
270271

271272
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
272273

273-
# get a new alias
274-
aliasIndex = 0
275-
while True:
276-
alias = "_%s__%d" % ("subQuery", aliasIndex)
277-
escaped = re.compile('\\b("?)' + re.escape(alias) + '\\1\\b')
278-
if not escaped.search(query):
279-
break
280-
aliasIndex += 1
281-
282274
# remove a trailing ';' from query if present
283275
if query.strip().endswith(';'):
284276
query = query.strip()[:-1]
@@ -287,7 +279,19 @@ def fillColumnCombos(self):
287279
cols = []
288280
quotedCols = []
289281
connector = self.db.connector
290-
sql = u"SELECT * FROM (%s\n) AS %s WHERE 0=1" % (unicode(query), connector.quoteId(alias))
282+
if self.aliasSubQuery:
283+
# get a new alias
284+
aliasIndex = 0
285+
while True:
286+
alias = "_subQuery__%d" % aliasIndex
287+
escaped = re.compile('\\b("?)' + re.escape(alias) + '\\1\\b')
288+
if not escaped.search(query):
289+
break
290+
aliasIndex += 1
291+
292+
sql = u"SELECT * FROM (%s\n) AS %s LIMIT 0" % (unicode(query), connector.quoteId(alias))
293+
else:
294+
sql = u"SELECT * FROM (%s\n) WHERE 1=0" % unicode(query)
291295

292296
c = None
293297
try:

0 commit comments

Comments
 (0)