Skip to content
Permalink
Browse files

pkAttributeIndexes override for ogr provider

  • Loading branch information
jgrocha committed Nov 1, 2019
1 parent 4b8d3e7 commit 5fb453676045bba6c5b9e882eff95fe79f148dc1
@@ -1044,6 +1044,7 @@ void QgsOgrProvider::loadFields()
//the attribute fields need to be read again when the encoding changes
mAttributeFields.clear();
mDefaultValues.clear();
mPrimaryKeyAttrs.clear();
if ( !mOgrLayer )
return;

@@ -1084,6 +1085,7 @@ void QgsOgrProvider::loadFields()
);
mDefaultValues.insert( 0, tr( "Autogenerate" ) );
createdFields++;
mPrimaryKeyAttrs << 0;
}

for ( int i = 0; i < fdef.GetFieldCount(); ++i )
@@ -131,6 +131,7 @@ class QgsOgrProvider : public QgsVectorDataProvider
bool createSpatialIndex() override;
bool createAttributeIndex( int field ) override;
QgsVectorDataProvider::Capabilities capabilities() const override;
QgsAttributeList pkAttributeIndexes() const override { return mPrimaryKeyAttrs; }
void setEncoding( const QString &e ) override;
bool enterUpdateMode() override { return _enterUpdateMode(); }
bool leaveUpdateMode() override;
@@ -231,6 +232,8 @@ class QgsOgrProvider : public QgsVectorDataProvider
mutable std::unique_ptr< OGREnvelope > mExtent;
bool mForceRecomputeExtent = false;

QList<int> mPrimaryKeyAttrs;

/**
* This member variable receives the same value as extent_
in the method QgsOgrProvider::extent(). The purpose is to prevent a memory leak*/
@@ -737,6 +737,24 @@ def testReplaceLayerWhileOpen(self):
features = [f for f in vl1.getFeatures(request)]
self.assertEqual(len(features), 1)

def testPkAttributeIndexes(self):
''' Test the primary key index '''
# lyr = ds.CreateLayer('layer', geom_type=ogr.wkbPoint, options=['COLUMN_TYPES=foo=int8,bar=string','GEOMETRY_NAME=the_geom','FID=customfid'])
# lyr = ds.CreateLayer('layer', geom_type=ogr.wkbPoint, options=['GEOMETRY_NAME=the_geom','FID=customfid'])
# lyr = ds.CreateLayer('layer', geom_type=ogr.wkbPoint, options=['GEOMETRY_NAME=the_geom'])
tmpfile = os.path.join(self.basetestpath, 'testPkAttributeIndexes.gpkg')
ds = ogr.GetDriverByName('GPKG').CreateDataSource(tmpfile)
ds.CreateLayer('test', geom_type=ogr.wkbPoint, options=['COLUMN_TYPES=foo=int8,bar=string','GEOMETRY_NAME=the_geom','FID=customfid'])
ds = None
vl = QgsVectorLayer('{}|layerid=0'.format(tmpfile), 'test', 'ogr')
pks = vl.primaryKeyAttributes()
fields = vl.fields()
pkfield = fields.at(pks[0])
self.assertEqual(len(pks), 1)
self.assertEqual(pks[0], 0)
self.assertEqual(pkfield.name(), 'customfid')
self.assertTrue(pkfield.constraints().constraints() & QgsFieldConstraints.ConstraintUnique)

def testSublayerWithComplexLayerName(self):
''' Test reading a gpkg with a sublayer name containing : '''
tmpfile = os.path.join(self.basetestpath, 'testGeopackageComplexLayerName.gpkg')

0 comments on commit 5fb4536

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