Skip to content
Permalink
Browse files

Make QgsVectorDataProvider::fields() return a copy

Implements a QGIS 3.0 TODO
  • Loading branch information
nyalldawson committed Jul 14, 2016
1 parent 1bafa80 commit e683101b1062ddd6dc0308d34d1f4af9dd82be3c
Showing with 70 additions and 104 deletions.
  1. +8 −0 doc/api_break.dox
  2. +2 −4 python/core/qgsvectordataprovider.sip
  3. +3 −3 src/analysis/vector/qgszonalstatistics.cpp
  4. +9 −9 src/core/qgsvectordataprovider.cpp
  5. +2 −5 src/core/qgsvectordataprovider.h
  6. +1 −1 src/core/qgsvirtuallayerdefinitionutils.cpp
  7. +9 −9 src/plugins/evis/eventbrowser/evisgenericeventbrowsergui.cpp
  8. +1 −1 src/providers/arcgisrest/qgsafsprovider.h
  9. +1 −1 src/providers/db2/qgsdb2provider.cpp
  10. +2 −5 src/providers/db2/qgsdb2provider.h
  11. +1 −1 src/providers/delimitedtext/qgsdelimitedtextprovider.cpp
  12. +1 −5 src/providers/delimitedtext/qgsdelimitedtextprovider.h
  13. +1 −1 src/providers/gpx/qgsgpxprovider.cpp
  14. +1 −4 src/providers/gpx/qgsgpxprovider.h
  15. +1 −1 src/providers/grass/qgsgrassprovider.cpp
  16. +1 −4 src/providers/grass/qgsgrassprovider.h
  17. +1 −1 src/providers/memory/qgsmemoryprovider.cpp
  18. +1 −6 src/providers/memory/qgsmemoryprovider.h
  19. +1 −1 src/providers/mssql/qgsmssqlprovider.cpp
  20. +1 −5 src/providers/mssql/qgsmssqlprovider.h
  21. +2 −2 src/providers/ogr/qgsogrprovider.cpp
  22. +1 −4 src/providers/ogr/qgsogrprovider.h
  23. +1 −1 src/providers/postgres/qgspostgresprovider.cpp
  24. +1 −5 src/providers/postgres/qgspostgresprovider.h
  25. +1 −1 src/providers/spatialite/qgsspatialiteprovider.cpp
  26. +1 −5 src/providers/spatialite/qgsspatialiteprovider.h
  27. +1 −1 src/providers/virtual/qgsvirtuallayerprovider.cpp
  28. +1 −5 src/providers/virtual/qgsvirtuallayerprovider.h
  29. +1 −1 src/providers/wfs/qgswfsfeatureiterator.cpp
  30. +1 −1 src/providers/wfs/qgswfsprovider.cpp
  31. +1 −1 src/providers/wfs/qgswfsprovider.h
  32. +5 −5 src/providers/wfs/qgswfsshareddata.cpp
  33. +5 −5 src/server/qgswfsserver.cpp
@@ -33,6 +33,14 @@ attributeIndexes(), pkAttributeIndexes(), isSaveAndLoadStyleToDBSupported()</li>
</li>
</ul>

\subsection qgis_api_break_3_0_DataProviders Data Providers

<ul>
<li>QgsVectorDataProvider::fields() now returns a copy, rather than a const reference. Since QgsFields
objects are implicitly shared, returning a copy helps simplify and make code more robust. This change
only affects third party c++ providers, and does not affect PyQGIS scripts.</li>
</ul>

\subsection qgis_api_break_3_0_QgsVectorFileWriter QgsVectorFileWriter

<ul>
@@ -116,11 +116,9 @@ class QgsVectorDataProvider : QgsDataProvider
virtual long featureCount() const = 0;

/**
* Return a map of indexes with field names for this layer
* @return map of fields
* @see QgsFields
* Returns the fields associated with this data provider.
*/
virtual const QgsFields &fields() const = 0;
virtual QgsFields fields() const = 0;

/**
* Return a short comment for the data that this provider is
@@ -533,13 +533,13 @@ QString QgsZonalStatistics::getUniqueFieldName( const QString& fieldName )
return fieldName;
}

const QgsFields& providerFields = dp->fields();
QgsFields providerFields = dp->fields();
QString shortName = fieldName.mid( 0, 10 );

bool found = false;
for ( int idx = 0; idx < providerFields.count(); ++idx )
{
if ( shortName == providerFields[idx].name() )
if ( shortName == providerFields.at( idx ).name() )
{
found = true;
break;
@@ -559,7 +559,7 @@ QString QgsZonalStatistics::getUniqueFieldName( const QString& fieldName )
found = false;
for ( int idx = 0; idx < providerFields.count(); ++idx )
{
if ( shortName == providerFields[idx].name() )
if ( shortName == providerFields.at( idx ).name() )
{
n += 1;
if ( n < 9 )
@@ -266,10 +266,10 @@ QMap<QString, int> QgsVectorDataProvider::fieldNameMap() const
{
QMap<QString, int> resultMap;

const QgsFields& theFields = fields();
QgsFields theFields = fields();
for ( int i = 0; i < theFields.count(); ++i )
{
resultMap.insert( theFields[i].name(), i );
resultMap.insert( theFields.at( i ).name(), i );
}

return resultMap;
@@ -431,20 +431,20 @@ void QgsVectorDataProvider::fillMinMaxCache() const
if ( !mCacheMinMaxDirty )
return;

const QgsFields& flds = fields();
QgsFields flds = fields();
for ( int i = 0; i < flds.count(); ++i )
{
if ( flds[i].type() == QVariant::Int )
if ( flds.at( i ).type() == QVariant::Int )
{
mCacheMinValues[i] = QVariant( INT_MAX );
mCacheMaxValues[i] = QVariant( INT_MIN );
}
else if ( flds[i].type() == QVariant::LongLong )
else if ( flds.at( i ).type() == QVariant::LongLong )
{
mCacheMinValues[i] = QVariant( std::numeric_limits<qlonglong>::max() );
mCacheMaxValues[i] = QVariant( std::numeric_limits<qlonglong>::min() );
}
else if ( flds[i].type() == QVariant::Double )
else if ( flds.at( i ).type() == QVariant::Double )
{
mCacheMinValues[i] = QVariant( DBL_MAX );
mCacheMaxValues[i] = QVariant( -DBL_MAX );
@@ -470,23 +470,23 @@ void QgsVectorDataProvider::fillMinMaxCache() const
if ( varValue.isNull() )
continue;

if ( flds[*it].type() == QVariant::Int )
if ( flds.at( *it ).type() == QVariant::Int )
{
int value = varValue.toInt();
if ( value < mCacheMinValues[*it].toInt() )
mCacheMinValues[*it] = value;
if ( value > mCacheMaxValues[*it].toInt() )
mCacheMaxValues[*it] = value;
}
else if ( flds[*it].type() == QVariant::LongLong )
else if ( flds.at( *it ).type() == QVariant::LongLong )
{
qlonglong value = varValue.toLongLong();
if ( value < mCacheMinValues[*it].toLongLong() )
mCacheMinValues[*it] = value;
if ( value > mCacheMaxValues[*it].toLongLong() )
mCacheMaxValues[*it] = value;
}
else if ( flds[*it].type() == QVariant::Double )
else if ( flds.at( *it ).type() == QVariant::Double )
{
double value = varValue.toDouble();
if ( value < mCacheMinValues[*it].toDouble() )
@@ -166,12 +166,9 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider
virtual long featureCount() const = 0;

/**
* Return a map of indexes with field names for this layer
* @return map of fields
* @see QgsFields
* Returns the fields associated with this data provider.
*/
// TODO QGIS 3: return by value
virtual const QgsFields &fields() const = 0;
virtual QgsFields fields() const = 0;

/**
* Return a short comment for the data that this provider is
@@ -27,7 +27,7 @@ QgsVirtualLayerDefinition QgsVirtualLayerDefinitionUtils::fromJoinedLayer( QgsVe
QStringList columns;

// look for the uid
const QgsFields& fields = layer->dataProvider()->fields();
QgsFields fields = layer->dataProvider()->fields();
{
QgsAttributeList pk = layer->dataProvider()->pkAttributeIndexes();
if ( pk.size() == 1 )
@@ -282,11 +282,11 @@ bool eVisGenericEventBrowserGui::initBrowser()
return false;
}

const QgsFields& myFields = mDataProvider->fields();
QgsFields myFields = mDataProvider->fields();
mIgnoreEvent = true; //Ignore indexChanged event when adding items to combo boxes
for ( int x = 0; x < myFields.count(); x++ )
{
QString name = myFields[x].name();
QString name = myFields.at( x ).name();
cboxEventImagePathField->addItem( name );
cboxCompassBearingField->addItem( name );
cboxCompassOffsetField->addItem( name );
@@ -594,13 +594,13 @@ void eVisGenericEventBrowserGui::loadRecord()
QString myCompassBearingField = cboxCompassBearingField->currentText();
QString myCompassOffsetField = cboxCompassOffsetField->currentText();
QString myEventImagePathField = cboxEventImagePathField->currentText();
const QgsFields& myFields = mDataProvider->fields();
QgsFields myFields = mDataProvider->fields();
QgsAttributes myAttrs = myFeature->attributes();
//loop through the attributes and display their contents
for ( int i = 0; i < myAttrs.count(); ++i )
{
QStringList myValues;
QString fieldName = myFields[i].name();
QString fieldName = myFields.at( i ).name();
myValues << fieldName << myAttrs.at( i ).toString();
QTreeWidgetItem* myItem = new QTreeWidgetItem( myValues );
if ( fieldName == myEventImagePathField )
@@ -836,7 +836,7 @@ void eVisGenericEventBrowserGui::on_cboxEventImagePathField_currentIndexChanged(
{
mConfiguration.setEventImagePathField( cboxEventImagePathField->currentText() );

const QgsFields& myFields = mDataProvider->fields();
QgsFields myFields = mDataProvider->fields();
QgsFeature* myFeature = featureAtId( mFeatureIds.at( mCurrentFeatureIndex ) );

if ( !myFeature )
@@ -864,7 +864,7 @@ void eVisGenericEventBrowserGui::on_cboxCompassBearingField_currentIndexChanged(
{
mConfiguration.setCompassBearingField( cboxCompassBearingField->currentText() );

const QgsFields& myFields = mDataProvider->fields();
QgsFields myFields = mDataProvider->fields();
QgsFeature* myFeature = featureAtId( mFeatureIds.at( mCurrentFeatureIndex ) );

if ( !myFeature )
@@ -873,7 +873,7 @@ void eVisGenericEventBrowserGui::on_cboxCompassBearingField_currentIndexChanged(
QgsAttributes myAttrs = myFeature->attributes();
for ( int i = 0; i < myAttrs.count(); ++i )
{
if ( myFields[i].name() == cboxCompassBearingField->currentText() )
if ( myFields.at( i ).name() == cboxCompassBearingField->currentText() )
{
mCompassBearing = myAttrs.at( i ).toDouble();
}
@@ -892,7 +892,7 @@ void eVisGenericEventBrowserGui::on_cboxCompassOffsetField_currentIndexChanged(
{
mConfiguration.setCompassOffsetField( cboxCompassOffsetField->currentText() );

const QgsFields& myFields = mDataProvider->fields();
QgsFields myFields = mDataProvider->fields();
QgsFeature* myFeature = featureAtId( mFeatureIds.at( mCurrentFeatureIndex ) );

if ( !myFeature )
@@ -901,7 +901,7 @@ void eVisGenericEventBrowserGui::on_cboxCompassOffsetField_currentIndexChanged(
QgsAttributes myAttrs = myFeature->attributes();
for ( int i = 0; i < myAttrs.count(); ++i )
{
if ( myFields[i].name() == cboxCompassOffsetField->currentText() )
if ( myFields.at( i ).name() == cboxCompassOffsetField->currentText() )
{
mCompassOffset = myAttrs.at( i ).toDouble();
}
@@ -43,7 +43,7 @@ class QgsAfsProvider : public QgsVectorDataProvider
QgsFeatureIterator getFeatures( const QgsFeatureRequest& request = QgsFeatureRequest() ) const override;
QGis::WkbType geometryType() const override { return static_cast<QGis::WkbType>( mGeometryType ); }
long featureCount() const override { return mObjectIds.size(); }
const QgsFields &fields() const override { return mFields; }
QgsFields fields() const override { return mFields; }
/* Read only for the moment
bool addFeatures( QgsFeatureList &flist ) override{ return false; }
bool deleteFeatures( const QgsFeatureIds &id ) override{ return false; }
@@ -494,7 +494,7 @@ long QgsDb2Provider::featureCount() const
}
}

const QgsFields &QgsDb2Provider::fields() const
QgsFields QgsDb2Provider::fields() const
{
return mAttributeFields;
}
@@ -70,11 +70,8 @@ class QgsDb2Provider : public QgsVectorDataProvider
*/
void updateStatistics() const;

/**
* Return a map of indexes with field names for this layer.
* @return map of fields
*/
virtual const QgsFields &fields() const override;

virtual QgsFields fields() const override;

virtual QgsCoordinateReferenceSystem crs() const override;
virtual QgsRectangle extent() const override;
@@ -1135,7 +1135,7 @@ long QgsDelimitedTextProvider::featureCount() const
}


const QgsFields & QgsDelimitedTextProvider::fields() const
QgsFields QgsDelimitedTextProvider::fields() const
{
return attributeFields;
}
@@ -101,11 +101,7 @@ class QgsDelimitedTextProvider : public QgsVectorDataProvider
*/
virtual long featureCount() const override;

/**
* Return a map of indexes with field names for this layer
* @return map of fields
*/
virtual const QgsFields & fields() const override;
virtual QgsFields fields() const override;

/** Returns a bitmask containing the supported capabilities
* Note, some capabilities may change depending on whether
@@ -171,7 +171,7 @@ long QgsGPXProvider::featureCount() const
}


const QgsFields& QgsGPXProvider::fields() const
QgsFields QgsGPXProvider::fields() const
{
return attributeFields;
}
@@ -69,10 +69,7 @@ class QgsGPXProvider : public QgsVectorDataProvider
*/
virtual long featureCount() const override;

/**
* Get the field information for the layer
*/
virtual const QgsFields& fields() const override;
virtual QgsFields fields() const override;

/**
* Adds a list of features
@@ -453,7 +453,7 @@ long QgsGrassProvider::featureCount() const
return mNumberFeatures;
}

const QgsFields & QgsGrassProvider::fields() const
QgsFields QgsGrassProvider::fields() const
{
if ( isTopoType() )
{
@@ -88,10 +88,7 @@ class GRASS_LIB_EXPORT QgsGrassProvider : public QgsVectorDataProvider

virtual QgsRectangle extent() const override;

/**
* Get the field information for the layer
*/
const QgsFields & fields() const override;
QgsFields fields() const override;

// ! Key (category) field index
int keyField();
@@ -309,7 +309,7 @@ long QgsMemoryProvider::featureCount() const
return count;
}

const QgsFields & QgsMemoryProvider::fields() const
QgsFields QgsMemoryProvider::fields() const
{
return mFields;
}
@@ -61,12 +61,7 @@ class QgsMemoryProvider : public QgsVectorDataProvider
*/
virtual long featureCount() const override;

/**
* Return a map of indexes with field names for this layer
* @return map of fields
*/
virtual const QgsFields & fields() const override;

virtual QgsFields fields() const override;

/**
* Adds a list of features
@@ -778,7 +778,7 @@ long QgsMssqlProvider::featureCount() const
}
}

const QgsFields & QgsMssqlProvider::fields() const
QgsFields QgsMssqlProvider::fields() const
{
return mAttributeFields;
}
@@ -93,11 +93,7 @@ class QgsMssqlProvider : public QgsVectorDataProvider
/** Update the extent, feature count, wkb type and srid for this layer */
void UpdateStatistics( bool estimate ) const;

/**
* Return a map of indexes with field names for this layer
* @return map of fields
*/
virtual const QgsFields & fields() const override;
virtual QgsFields fields() const override;

QString subsetString() const override;

@@ -1035,7 +1035,7 @@ long QgsOgrProvider::featureCount() const
}


const QgsFields & QgsOgrProvider::fields() const
QgsFields QgsOgrProvider::fields() const
{
return mAttributeFields;
}
@@ -1666,7 +1666,7 @@ bool QgsOgrProvider::createAttributeIndex( int field )
QByteArray quotedLayerName = quotedIdentifier( OGR_FD_GetName( OGR_L_GetLayerDefn( ogrOrigLayer ) ) );
QByteArray dropSql = "DROP INDEX ON " + quotedLayerName;
OGR_DS_ExecuteSQL( ogrDataSource, dropSql.constData(), OGR_L_GetSpatialFilter( ogrOrigLayer ), nullptr );
QByteArray createSql = "CREATE INDEX ON " + quotedLayerName + " USING " + mEncoding->fromUnicode( fields()[field].name() );
QByteArray createSql = "CREATE INDEX ON " + quotedLayerName + " USING " + mEncoding->fromUnicode( fields().at( field ).name() );
OGR_DS_ExecuteSQL( ogrDataSource, createSql.constData(), OGR_L_GetSpatialFilter( ogrOrigLayer ), nullptr );

QFileInfo fi( mFilePath ); // to get the base name
@@ -117,10 +117,7 @@ class QgsOgrProvider : public QgsVectorDataProvider
*/
virtual long featureCount() const override;

/**
* Get the field information for the layer
*/
virtual const QgsFields & fields() const override;
virtual QgsFields fields() const override;

virtual QgsRectangle extent() const override;

@@ -703,7 +703,7 @@ const QgsField &QgsPostgresProvider::field( int index ) const
return mAttributeFields[index];
}

const QgsFields & QgsPostgresProvider::fields() const
QgsFields QgsPostgresProvider::fields() const
{
return mAttributeFields;
}

0 comments on commit e683101

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