Skip to content

Commit

Permalink
[DbManager] delete layer passed to querybuilder to set sql layer filt…
Browse files Browse the repository at this point in the history
…er (follow f6e3161)
  • Loading branch information
brushtyler committed Nov 29, 2015
1 parent 5408e84 commit c4dcb7b
Showing 1 changed file with 14 additions and 21 deletions.
35 changes: 14 additions & 21 deletions python/plugins/db_manager/dlg_sql_window.py
Expand Up @@ -209,7 +209,7 @@ def executeSql(self):
self.update()
QApplication.restoreOverrideCursor()

def _getSqlLayer(self):
def _getSqlLayer(self, _filter):
hasUniqueField = self.uniqueColumnCheck.checkState() == Qt.Checked
if hasUniqueField:
if self.allowMultiColumnPk:
Expand Down Expand Up @@ -258,24 +258,23 @@ def _getSqlLayer(self):

# create the layer
layer = self.db.toSqlLayer(query, geomFieldName, uniqueFieldName, newLayerName, layerType,
self.avoidSelectById.isChecked(), self.filter)
self.avoidSelectById.isChecked(), _filter)
if layer.isValid():
return layer
else:
return None

def loadSqlLayer(self):
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
layer = self._getSqlLayer()
QApplication.restoreOverrideCursor()

if layer == None:
return
try:
layer = self._getSqlLayer(self.filter)
if layer == None:
return

from qgis.core import QgsMapLayerRegistry
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
QgsMapLayerRegistry.instance().addMapLayers([layer], True)
QApplication.restoreOverrideCursor()
from qgis.core import QgsMapLayerRegistry
QgsMapLayerRegistry.instance().addMapLayers([layer], True)
finally:
QApplication.restoreOverrideCursor()

def fillColumnCombos(self):
query = self._getSqlQuery()
Expand Down Expand Up @@ -460,18 +459,12 @@ def uniqueTextChanged(self, text):

def setFilter(self):
from qgis.gui import QgsQueryBuilder
layer = self._getSqlLayer()

This comment has been minimized.

Copy link
@SebDieBln

SebDieBln Nov 30, 2015

Contributor

The idea was to use the already defined filter first, if it works. I often have the situation where an unfiltered query gives an invalid layer because of duplicate keys, different geometry types or something else.

This comment has been minimized.

Copy link
@brushtyler

brushtyler Nov 30, 2015

Author Contributor

I do not understand how to set the filter the first time if your layer is invalid.

This comment has been minimized.

Copy link
@SebDieBln

SebDieBln Nov 30, 2015

Contributor

The usual trick is to make it valid by adding a LIMIT 0, then defining the filter and removing the LIMIT 0 again. At the moment I have to do this in the project/layer-definition XML file.
(Since PR #2458 any empty query can be made a valid layer, too.)

This comment has been minimized.

Copy link
@brushtyler

brushtyler Dec 3, 2015

Author Contributor

Moving the discussion back to PR #2446


if layer == None:
# probably the defined filter does not work for the query, so try to create the layer without the filter
filter = self.filter
self.filter = ""
layer = self._getSqlLayer()
self.filter = filter
if layer == None:
return
layer = self._getSqlLayer("")
if not layer:
return

dlg = QgsQueryBuilder(layer)
dlg.setSql(self.filter)
if dlg.exec_():
self.filter = dlg.sql()
layer.deleteLater()

This comment has been minimized.

Copy link
@SebDieBln

SebDieBln Nov 30, 2015

Contributor

I thought layer was deleted when it goes out of scope. Why is this not the case here?
How about dlg?
😕

This comment has been minimized.

Copy link
@brushtyler

brushtyler Nov 30, 2015

Author Contributor

In any case the layer is not deleted, although I didn't try what happens in this one. I'll check it this evening, AFAICS there's no ownership sip annotation for QgsQueryBuilder ctor.

If dlg has neither parent nor Qt::DeleteOnClose set, probably it won't be deleted as well.

This comment has been minimized.

Copy link
@brushtyler

brushtyler Nov 30, 2015

Author Contributor

I was wrong... I thought dlg is a class member... it has no parent and it's a local var, so dlg is deleted when it goes out of scope. And the same should happen for layer. BTW, I'll try this evening and let you know.

This comment has been minimized.

Copy link
@brushtyler

brushtyler Nov 30, 2015

Author Contributor

layer is deleted after it goes out of scope, sorry for the noise.

0 comments on commit c4dcb7b

Please sign in to comment.