Skip to content

Commit

Permalink
[DB Manager] Fix refresh issue when renaming GPKG table, and disable …
Browse files Browse the repository at this point in the history
…add geometry column button if already one existing
  • Loading branch information
rouault committed Oct 25, 2016
1 parent 9d0bb34 commit d0da880
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 14 deletions.
6 changes: 6 additions & 0 deletions python/plugins/db_manager/db_plugins/connector.py
Expand Up @@ -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

Expand Down
33 changes: 31 additions & 2 deletions python/plugins/db_manager/db_plugins/gpkg/connector.py
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
26 changes: 14 additions & 12 deletions python/plugins/db_manager/dlg_table_properties.py
Expand Up @@ -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()

Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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 """
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 """
Expand All @@ -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
Expand Down Expand Up @@ -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 """
Expand All @@ -294,7 +296,7 @@ def createSpatialIndex(self):

try:
self.table.createSpatialIndex()
self.populateViews()
self.refresh()
except BaseError as e:
DlgDbError.showError(e, self)
return
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d0da880

Please sign in to comment.