Skip to content
Permalink
Browse files

db manager: only alias subqueries on postgres (fixes #13731)

  • Loading branch information
jef-n committed Dec 2, 2015
1 parent f92efbc commit c2fb11c8d5fba163068d7ea87aa11062035dd167
Showing with 15 additions and 11 deletions.
  1. +15 −11 python/plugins/db_manager/dlg_sql_window.py
@@ -55,7 +55,8 @@ def __init__(self, iface, db, parent=None):
self.iface = iface
self.db = db
self.filter = ""
self.allowMultiColumnPk = isinstance(db, PGDatabase) # at the moment only PostGIS allows a primary key to span multiple columns, spatialite doesn't
self.allowMultiColumnPk = isinstance(db, PGDatabase) # at the moment only PostgreSQL allows a primary key to span multiple columns, spatialite doesn't
self.aliasSubQuery = isinstance(db, PGDatabase) # only PostgreSQL requires subqueries to be aliases
self.setupUi(self)
self.setWindowTitle(
u"%s - %s [%s]" % (self.windowTitle(), db.connection().connectionName(), db.connection().typeNameString()))
@@ -283,15 +284,6 @@ def fillColumnCombos(self):

QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))

# get a new alias
aliasIndex = 0
while True:
alias = "_%s__%d" % ("subQuery", aliasIndex)
escaped = re.compile('\\b("?)' + re.escape(alias) + '\\1\\b')
if not escaped.search(query):
break
aliasIndex += 1

# remove a trailing ';' from query if present
if query.strip().endswith(';'):
query = query.strip()[:-1]
@@ -300,7 +292,19 @@ def fillColumnCombos(self):
cols = []
quotedCols = []
connector = self.db.connector
sql = u"SELECT * FROM (%s\n) AS %s WHERE 0=1" % (unicode(query), connector.quoteId(alias))
if self.aliasSubQuery:
# get a new alias
aliasIndex = 0
while True:
alias = "_subQuery__%d" % aliasIndex
escaped = re.compile('\\b("?)' + re.escape(alias) + '\\1\\b')
if not escaped.search(query):
break
aliasIndex += 1

sql = u"SELECT * FROM (%s\n) AS %s LIMIT 0" % (unicode(query), connector.quoteId(alias))
else:
sql = u"SELECT * FROM (%s\n) WHERE 1=0" % unicode(query)

c = None
try:

0 comments on commit c2fb11c

Please sign in to comment.
You can’t perform that action at this time.