Skip to content
Permalink
Browse files

[DB Manager] Fix refresh issue when renaming GPKG table, and disable …

…add geometry column button if already one existing
  • Loading branch information
rouault committed Oct 25, 2016
1 parent 9d0bb34 commit d0da880b8c55148ec0bd6d89d98a48b89f7d6932
@@ -49,6 +49,12 @@ def publicUri(self):
def hasSpatialSupport(self):
return False

def canAddGeometryColumn(self, table):
return self.hasSpatialSupport()

def canAddSpatialIndex(self, table):
return self.hasSpatialSupport()

def hasRasterSupport(self):
return False

@@ -47,7 +47,11 @@ def __init__(self, uri):
self.dbname = uri.database()
self.has_raster = False
self.mapSridToName = {}
self._opendb()

def _opendb(self):

self.gdal_ds = None
if hasattr(gdal, 'OpenEx'):
# GDAL >= 2
self.gdal_ds = gdal.OpenEx(self.dbname, gdal.OF_UPDATE)
@@ -216,6 +220,23 @@ def getSpatialInfo(self):
def hasSpatialSupport(self):
return True

# Used by DlgTableProperties
def canAddGeometryColumn(self, table):
_, tablename = self.getSchemaTableName(table)
lyr = self.gdal_ds.GetLayerByName(tablename)
if lyr is None:
return False
return lyr.GetGeomType() == ogr.wkbNone

# Used by DlgTableProperties
def canAddSpatialIndex(self, table):
_, tablename = self.getSchemaTableName(table)
lyr = self.gdal_ds.GetLayerByName(tablename)
if lyr is None or lyr.GetGeometryColumn() == '':
return False
return not self.hasSpatialIndex(table,
lyr.GetGeometryColumn())

def hasRasterSupport(self):
return self.has_raster

@@ -591,7 +612,12 @@ def renameTable(self, table, new_table):

gdal.ErrorReset()
self.gdal_ds.ExecuteSQL('ALTER TABLE %s RENAME TO %s' % (tablename, new_table))
return gdal.GetLastErrorMsg() == ''
if gdal.GetLastErrorMsg() != '':
return False
# we need to reopen after renaming since OGR doesn't update its
# internal state
self._opendb()
return True

def moveTable(self, table, new_table, new_schema=None):
return self.renameTable(table, new_table)
@@ -720,7 +746,10 @@ def addGeometryColumn(self, table, geom_column='geometry', geom_type='POINT', sr
if sr.ImportFromEPSG(srid) == 0:
geom_field_defn.SetSpatialRef(sr)

return lyr.CreateGeomField(geom_field_defn) == 0
if lyr.CreateGeomField(geom_field_defn) != 0:
return False
self._opendb()
return True

def deleteGeometryColumn(self, table, geom_column):
return False # not supported
@@ -69,6 +69,9 @@ def __init__(self, table, parent=None):
self.btnAddSpatialIndex.clicked.connect(self.createSpatialIndex)
self.btnDeleteIndex.clicked.connect(self.deleteIndex)

self.refresh()

def refresh(self):
self.populateViews()
self.checkSupports()

@@ -77,9 +80,8 @@ def checkSupports(self):
self.btnEditColumn.setEnabled(allowEditColumns)
self.btnDeleteColumn.setEnabled(allowEditColumns)

allowSpatial = self.db.connector.hasSpatialSupport()
self.btnAddGeometryColumn.setEnabled(allowSpatial)
self.btnAddSpatialIndex.setEnabled(allowSpatial)
self.btnAddGeometryColumn.setEnabled(self.db.connector.canAddGeometryColumn((self.table.schemaName(), self.table.name)))
self.btnAddSpatialIndex.setEnabled(self.db.connector.canAddSpatialIndex((self.table.schemaName(), self.table.name)))

def populateViews(self):
self.populateFields()
@@ -119,7 +121,7 @@ def addColumn(self):
try:
# add column to table
self.table.addField(fld)
self.populateViews()
self.refresh()
except BaseError as e:
DlgDbError.showError(e, self)
return
@@ -131,7 +133,7 @@ def addGeometryColumn(self):
dlg = DlgAddGeometryColumn(self, self.table)
if not dlg.exec_():
return
self.populateViews()
self.refresh()

def editColumn(self):
""" open dialog to change column info and alter table appropriately """
@@ -153,7 +155,7 @@ def editColumn(self):
self.aboutToChangeTable.emit()
try:
fld.update(new_fld.name, new_fld.type2String(), new_fld.notNull, new_fld.default2String())
self.populateViews()
self.refresh()
except BaseError as e:
DlgDbError.showError(e, self)
return
@@ -178,7 +180,7 @@ def deleteColumn(self):
self.aboutToChangeTable.emit()
try:
fld.delete()
self.populateViews()
self.refresh()
except BaseError as e:
DlgDbError.showError(e, self)
return
@@ -211,7 +213,7 @@ def addConstraint(self):
dlg = DlgCreateConstraint(self, self.table)
if not dlg.exec_():
return
self.populateViews()
self.refresh()

def deleteConstraint(self):
""" delete a constraint """
@@ -233,7 +235,7 @@ def deleteConstraint(self):
self.aboutToChangeTable.emit()
try:
constr.delete()
self.populateViews()
self.refresh()
except BaseError as e:
DlgDbError.showError(e, self)
return
@@ -274,7 +276,7 @@ def createIndex(self):
dlg = DlgCreateIndex(self, self.table)
if not dlg.exec_():
return
self.populateViews()
self.refresh()

def createSpatialIndex(self):
""" create spatial index for the geometry column """
@@ -294,7 +296,7 @@ def createSpatialIndex(self):

try:
self.table.createSpatialIndex()
self.populateViews()
self.refresh()
except BaseError as e:
DlgDbError.showError(e, self)
return
@@ -328,7 +330,7 @@ def deleteIndex(self):
self.aboutToChangeTable.emit()
try:
idx.delete()
self.populateViews()
self.refresh()
except BaseError as e:
DlgDbError.showError(e, self)
return

0 comments on commit d0da880

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