2 changes: 1 addition & 1 deletion python/plugins/db_manager/__init__.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
4 changes: 3 additions & 1 deletion python/plugins/db_manager/dlg_add_geometry_column.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

class DlgAddGeometryColumn(QDialog, Ui_DlgAddGeometryColumn):

GEOM_TYPES = ["POINT", "LINESTRING", "POLYGON", "MULTIPOINT", "MULTILINESTRING", "MULTIPOLYGON", "GEOMETRYCOLLECTION"]

def __init__(self, parent=None, table=None, db=None):
QDialog.__init__(self, parent)
self.table = table
Expand All @@ -48,7 +50,7 @@ def createGeomColumn(self):
return

name = self.editName.text()
geom_type = self.cboType.currentText()
geom_type = self.GEOM_TYPES[ self.cboType.currentIndex() ]
dim = self.spinDim.value()
try:
srid = int(self.editSrid.text())
Expand Down
4 changes: 3 additions & 1 deletion python/plugins/db_manager/dlg_create_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ def setModelData(self, editor, model, index):

class DlgCreateTable(QDialog, Ui_DlgCreateTable):

GEOM_TYPES = ["POINT", "LINESTRING", "POLYGON", "MULTIPOINT", "MULTILINESTRING", "MULTIPOLYGON", "GEOMETRYCOLLECTION"]

def __init__(self, item, parent=None):
QDialog.__init__(self, parent)
self.item = item
Expand Down Expand Up @@ -284,7 +286,7 @@ def createTable(self):
QMessageBox.information(self, "sorry", "set geometry column name")
return

geomType = str(self.cboGeomType.currentText())
geomType = self.GEOM_TYPES[ self.cboGeomType.currentIndex() ]
geomDim = self.spinGeomDim.value()
try:
geomSrid = int(self.editGeomSrid.text())
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/db_manager/dlg_import_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def importLayer(self):
QMessageBox.warning(self, "Import to database", u"Error %d\n%s" % (ret, errMsg) )
return

if self.chkGeomColumn.isChecked() and self.chkSpatialIndex.isChecked():
if self.chkSpatialIndex.isChecked():
self.db.connector.createSpatialIndex( (schema, table), geom )

QMessageBox.information(self, "Import to database", "Import was successful.")
Expand Down
2 changes: 2 additions & 0 deletions src/core/qgsproviderregistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,8 @@ QgsDataProvider *QgsProviderRegistry::provider( QString const & providerKey, QSt
// necessarily a reflection on the data provider itself
QgsDebugMsg( "Invalid data provider" );

delete dataProvider;

myLib->unload();
delete myLib;
return 0;
Expand Down
4 changes: 3 additions & 1 deletion src/providers/postgres/qgspostgresprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,13 @@ void QgsPostgresProvider::disconnectDb()
if ( mConnectionRO )
{
mConnectionRO->disconnect();
mConnectionRO = 0;
}

if ( mConnectionRW )
{
mConnectionRW->disconnect();
mConnectionRW = 0;
}
}

Expand Down Expand Up @@ -2870,7 +2872,7 @@ bool QgsPostgresProvider::getGeometryDetails()
mEnabledCapabilities &= ~( QgsVectorDataProvider::ChangeGeometries | QgsVectorDataProvider::AddFeatures );
}

QgsDebugMsg( QString( "Feature type name is %1" ).arg( QGis::qgisFeatureTypes[ geometryType()] ) );
QgsDebugMsg( QString( "Feature type name is %1" ).arg( QGis::qgisFeatureTypes[ geometryType() ] ) );
QgsDebugMsg( QString( "Geometry is geography %1" ).arg( mIsGeography ) );

return mValid;
Expand Down