File tree Expand file tree Collapse file tree 2 files changed +11
-3
lines changed Expand file tree Collapse file tree 2 files changed +11
-3
lines changed Original file line number Diff line number Diff line change @@ -1982,8 +1982,6 @@ bool QgsOgrProvider::createSpatialIndex()
1982
1982
{
1983
1983
QMutex *mutex = nullptr ;
1984
1984
OGRLayerH layer = mOgrOrigLayer ->getHandleAndMutex ( mutex );
1985
- QMutexLocker locker ( mutex );
1986
-
1987
1985
QByteArray sql = QByteArray ( " SELECT CreateSpatialIndex(" + quotedIdentifier ( layerName ) + " ,"
1988
1986
+ quotedIdentifier ( OGR_L_GetGeometryColumn ( layer ) ) + " ) " ); // quote the layer name so spaces are handled
1989
1987
mOgrOrigLayer ->ExecuteSQLNoReturn ( sql );
@@ -2012,6 +2010,12 @@ bool QgsOgrProvider::createAttributeIndex( int field )
2012
2010
if ( mGDALDriverName == QLatin1String ( " GPKG" ) ||
2013
2011
mGDALDriverName == QLatin1String ( " SQLite" ) )
2014
2012
{
2013
+ if ( field == 0 && mFirstFieldIsFid )
2014
+ {
2015
+ // already an index on this field, no need to re-created
2016
+ return false ;
2017
+ }
2018
+
2015
2019
QString indexName = createIndexName ( mOgrOrigLayer ->name (), fields ().at ( field ).name () );
2016
2020
QByteArray createSql = " CREATE INDEX IF NOT EXISTS " + textEncoding ()->fromUnicode ( indexName ) + " ON " + quotedLayerName + " (" + textEncoding ()->fromUnicode ( fields ().at ( field ).name () ) + " )" ;
2017
2021
mOgrOrigLayer ->ExecuteSQLNoReturn ( createSql );
Original file line number Diff line number Diff line change @@ -787,6 +787,10 @@ def testCreateAttributeIndex(self):
787
787
self .assertTrue (vl .dataProvider ().capabilities () & QgsVectorDataProvider .CreateAttributeIndex )
788
788
self .assertFalse (vl .dataProvider ().createAttributeIndex (- 1 ))
789
789
self .assertFalse (vl .dataProvider ().createAttributeIndex (100 ))
790
+
791
+ # should not be allowed - there's already a index on the primary key
792
+ self .assertFalse (vl .dataProvider ().createAttributeIndex (0 ))
793
+
790
794
self .assertTrue (vl .dataProvider ().createAttributeIndex (1 ))
791
795
792
796
con = spatialite_connect (tmpfile , isolation_level = None )
@@ -819,7 +823,7 @@ def testCreateAttributeIndex(self):
819
823
def testCreateSpatialIndex (self ):
820
824
tmpfile = os .path .join (self .basetestpath , 'testGeopackageSpatialIndex.gpkg' )
821
825
ds = ogr .GetDriverByName ('GPKG' ).CreateDataSource (tmpfile )
822
- lyr = ds .CreateLayer ('test' , geom_type = ogr .wkbPolygon )
826
+ lyr = ds .CreateLayer ('test' , geom_type = ogr .wkbPolygon , options = [ 'SPATIAL_INDEX=NO' ] )
823
827
lyr .CreateField (ogr .FieldDefn ('str_field' , ogr .OFTString ))
824
828
lyr .CreateField (ogr .FieldDefn ('str_field2' , ogr .OFTString ))
825
829
f = None
You can’t perform that action at this time.
0 commit comments