Skip to content

Commit

Permalink
DBManager: use the first suitable field loading a view (fix #5676)
Browse files Browse the repository at this point in the history
  • Loading branch information
brushtyler committed Jun 9, 2012
1 parent 2921588 commit 53b57d1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion python/plugins/db_manager/__init__.py
Expand Up @@ -27,7 +27,7 @@ def description():
return "Manage your databases within QGis"

def version():
return "0.1.19"
return "0.1.20"

def qgisMinimumVersion():
return "1.5.0"
Expand Down
14 changes: 10 additions & 4 deletions python/plugins/db_manager/db_plugins/plugin.py
Expand Up @@ -549,7 +549,8 @@ def uri(self):
uri = self.database().uri()
schema = self.schemaName() if self.schemaName() else ''
geomCol = self.geomColumn if self.type in [Table.VectorType, Table.RasterType] else QString()
uri.setDataSource(schema, self.name, geomCol if geomCol else QString())
uniqueCol = self.getValidQGisUniqueFields(True) if self.isView else None
uri.setDataSource(schema, self.name, geomCol if geomCol else QString(), QString(), uniqueCol.name if uniqueCol else QString() )
return uri

def mimeUri(self):
Expand All @@ -567,23 +568,28 @@ def toMapLayer(self):
def getValidQGisUniqueFields(self, onlyOne=False):
""" list of fields valid to load the table as layer in QGis canvas.
QGis automatically search for a valid unique field, so it's
needed only for queries (e.g. SELECT * FROM table LIMIT 1)"""
needed only for queries and views """

ret = []

# add the pk
pkcols = filter(lambda x: x.primaryKey, self.fields())
if len(pkcols) == 1: ret.append( pkcols[0] )

# add both serial and int4 fields with an unique index
# then add both oid, serial and int fields with an unique index
indexes = self.indexes()
if indexes != None:
for idx in indexes:
if idx.isUnique and len(idx.columns) == 1:
fld = idx.fields()[ idx.columns[0] ]
if fld and fld not in ret and fld.dataType in ["oid", "serial", "int4"]:
if fld.dataType in ["oid", "serial", "int4", "int8"] and fld not in ret:
ret.append( fld )

# and finally append the other suitable fields
for fld in self.fields():
if fld.dataType in ["oid", "serial", "int4", "int8"] and fld not in ret:
ret.append( fld )

if onlyOne:
return ret[0] if len(ret) > 0 else None
return ret
Expand Down

0 comments on commit 53b57d1

Please sign in to comment.