Skip to content
Permalink
Browse files

[db-manager] Use QgsDataItem implementation for GPKG layer rename

- Removes code duplication
- Uses a tested and robust implementation (from OGR)
- Takes care of renaming QGIS styles
- Updates the information view in the plugin

Fixes #21227
  • Loading branch information
elpaso committed Feb 13, 2019
1 parent 61d361d commit 8639bcf85369e6533d0d1bc2f6130d9c4520c853
@@ -88,6 +88,8 @@ def itemChanged(self, item):
with OverrideCursor(Qt.WaitCursor):
try:
self.reloadButtons()
# Force-reload information on the layer
self.info.setDirty()
# clear preview, this will delete the layer in preview tab
self.preview.loadPreview(None)
self.refreshTabs()
@@ -30,6 +30,8 @@
from ..plugin import ConnectionError, DbError, Table

from qgis.utils import spatialite_connect
from qgis.core import QgsApplication

import sqlite3

from osgeo import gdal, ogr, osr
@@ -590,28 +592,26 @@ def emptyTable(self, table):
self._execute_and_commit(sql)

def renameTable(self, table, new_table):
""" rename a table """

if self.isRasterTable(table):
return False

_, tablename = self.getSchemaTableName(table)
if new_table == tablename:
return True

if tablename.find('"') >= 0:
tablename = self.quoteId(tablename)
if new_table.find('"') >= 0:
new_table = self.quoteId(new_table)
"""Renames the table
:param table: tuple with schema and table names
:type table: tuple (str, str)
:param new_table: new table name
:type new_table: str
:return: true on success
:rtype: bool
"""

gdal.ErrorReset()
self.gdal_ds.ExecuteSQL('ALTER TABLE %s RENAME TO %s' % (tablename, new_table))
if gdal.GetLastErrorMsg() != '':
return False
table_name = table[1]
provider = [p for p in QgsApplication.dataItemProviderRegistry().providers() if p.name() == 'OGR'][0]
collection_item = provider.createDataItem(self.dbname, None)
data_item = [c for c in collection_item.createChildren() if c.name() == table_name][0]
result = data_item.rename(new_table)
# we need to reopen after renaming since OGR doesn't update its
# internal state
self._opendb()
return True
if result:
self._opendb()
return result

def moveTable(self, table, new_table, new_schema=None):
return self.renameTable(table, new_table)
@@ -594,6 +594,7 @@ def rename(self, new_name):
ret = self.database().connector.renameSchema(self.name, new_name)
if ret is not False:
self.name = new_name
# FIXME: refresh triggers
self.refresh()
return ret

@@ -652,6 +653,9 @@ def rename(self, new_name):
ret = self.database().connector.renameTable((self.schemaName(), self.name), new_name)
if ret is not False:
self.name = new_name
self._triggers = None
self._rules = None
self._constraints = None
self.refresh()
return ret

@@ -40,7 +40,7 @@ def __init__(self, parent=None):

self.item = None
self.dirty = False
self.currentLayer = None
self.currentLayerId = None

# reuse settings from QGIS
settings = QgsSettings()
@@ -118,9 +118,9 @@ def _loadTablePreview(self, table, limit=False):
vl = None

# remove old layer (if any) and set new
if self.currentLayer:
if not QgsProject.instance().layerTreeRoot().findLayer(self.currentLayer.id()):
QgsProject.instance().removeMapLayers([self.currentLayer.id()])
if self.currentLayerId:
if not QgsProject.instance().layerTreeRoot().findLayer(self.currentLayerId):
QgsProject.instance().removeMapLayers([self.currentLayerId])

if vl and vl.isValid():
self.setLayers([vl])
@@ -129,7 +129,7 @@ def _loadTablePreview(self, table, limit=False):
else:
self.setLayers([])

self.currentLayer = vl
self.currentLayerId = vl.id()

self.freeze(False)
super().refresh()

0 comments on commit 8639bcf

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