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 162f4bf commit a1b1ba12568faad435ca5fa69b3cfda8c018ccad
@@ -47,6 +47,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

@@ -46,7 +46,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.encode('UTF-8'), gdal.OF_UPDATE)
@@ -215,6 +219,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.encode('UTF-8'))
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.encode('UTF-8'))
if lyr is None or lyr.GetGeometryColumn() == '':
return False
return not self.hasSpatialIndex(table,
lyr.GetGeometryColumn())

def hasRasterSupport(self):
return self.has_raster

@@ -590,7 +611,12 @@ def renameTable(self, table, new_table):

gdal.ErrorReset()
self.gdal_ds.ExecuteSQL(('ALTER TABLE %s RENAME TO %s' % (tablename, new_table)).encode('UTF-8'))
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)
@@ -719,7 +745,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
@@ -68,6 +68,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()

@@ -76,9 +79,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()
@@ -118,7 +120,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
@@ -130,7 +132,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 """
@@ -152,7 +154,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
@@ -177,7 +179,7 @@ def deleteColumn(self):
self.aboutToChangeTable.emit()
try:
fld.delete()
self.populateViews()
self.refresh()
except BaseError as e:
DlgDbError.showError(e, self)
return
@@ -210,7 +212,7 @@ def addConstraint(self):
dlg = DlgCreateConstraint(self, self.table)
if not dlg.exec_():
return
self.populateViews()
self.refresh()

def deleteConstraint(self):
""" delete a constraint """
@@ -232,7 +234,7 @@ def deleteConstraint(self):
self.aboutToChangeTable.emit()
try:
constr.delete()
self.populateViews()
self.refresh()
except BaseError as e:
DlgDbError.showError(e, self)
return
@@ -273,7 +275,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 """
@@ -293,7 +295,7 @@ def createSpatialIndex(self):

try:
self.table.createSpatialIndex()
self.populateViews()
self.refresh()
except BaseError as e:
DlgDbError.showError(e, self)
return
@@ -327,7 +329,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 a1b1ba1

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