Skip to content

Commit

Permalink
db manager: only alias subqueries on postgres (fixes #13731)
Browse files Browse the repository at this point in the history
  • Loading branch information
jef-n committed Dec 2, 2015
1 parent f92efbc commit c2fb11c
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions python/plugins/db_manager/dlg_sql_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down Expand Up @@ -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]
Expand All @@ -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:
Expand Down

0 comments on commit c2fb11c

Please sign in to comment.