Skip to content

Commit

Permalink
case-sensitivity of vector layer field (fixes #13032)
Browse files Browse the repository at this point in the history
* fieldNameIndex: resort to case-insensitive lookup only if
  case-sensitive lookup has no match
* file writer: fix handling of fields that only differ by case

(backported from commit 8e2b791)
  • Loading branch information
jef-n committed Nov 26, 2015
1 parent a76c5f6 commit 945187c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
16 changes: 13 additions & 3 deletions python/core/qgsfield.sip
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class QgsField
#include <qgsfield.h>
%End

public:
public:
/** Constructor. Constructs a new QgsField object.
* @param name Field name
* @param type Field variant type, currently supported: String / Int / Double
Expand Down Expand Up @@ -171,6 +171,14 @@ public:
}; // class QgsField


/**
\ingroup core
Container of fields for a vector layer.

In addition to storing a list of QgsField instances, it also:
- allows quick lookups of field names to index in the list
- keeps track of where the field definition comes from (vector data provider, joined layer or newly added from an editing operation)
*/

class QgsFields
{
Expand All @@ -192,6 +200,8 @@ class QgsFields
void clear();
//! Append a field. The field must have unique name, otherwise it is rejected (returns false)
bool append( const QgsField& field, FieldOrigin origin = OriginProvider, int originIndex = -1 );
//! Append an expression field. The field must have unique name, otherwise it is rejected (returns false)
bool appendExpressionField( const QgsField& field, int originIndex );
//! Remove a field with the given index
void remove( int fieldIdx );
//! Extend with fields from another QgsFields container
Expand Down Expand Up @@ -240,8 +250,8 @@ class QgsFields
//! Look up field's index from name. Returns -1 on error
int indexFromName( const QString& name ) const;

//! Look up field's index from name - case insensitive
//! TODO: sort out case sensitive (indexFromName()) vs insensitive (fieldNameIndex()) calls
//! Look up field's index from name
//! also looks up case-insensitive if there is no match otherwise
//! @note added in 2.4
int fieldNameIndex( const QString& fieldName ) const;

Expand Down
9 changes: 7 additions & 2 deletions src/core/qgsfield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,13 +262,18 @@ QList<QgsField> QgsFields::toList() const

int QgsFields::fieldNameIndex( const QString& fieldName ) const
{
for ( int idx = 0; idx < count(); ++idx )
{
if ( mFields[idx].field.name() == fieldName )
return idx;
}

for ( int idx = 0; idx < count(); ++idx )
{
if ( QString::compare( mFields[idx].field.name(), fieldName, Qt::CaseInsensitive ) == 0 )
{
return idx;
}
}

return -1;
}

Expand Down
4 changes: 2 additions & 2 deletions src/core/qgsfield.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ class CORE_EXPORT QgsFields
//! Look up field's index from name. Returns -1 on error
int indexFromName( const QString& name ) const { return mNameToIndex.value( name, -1 ); }

//! Look up field's index from name - case insensitive
//! TODO: sort out case sensitive (indexFromName()) vs insensitive (fieldNameIndex()) calls
//! Look up field's index from name
//! also looks up case-insensitive if there is no match otherwise
//! @note added in 2.4
int fieldNameIndex( const QString& fieldName ) const;

Expand Down
9 changes: 6 additions & 3 deletions src/core/qgsvectorfilewriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ QgsVectorFileWriter::QgsVectorFileWriter(

mFields = fields;
mAttrIdxToOgrIdx.clear();
QSet<int> existingIdxs;

for ( int fldIdx = 0; fldIdx < fields.count(); ++fldIdx )
{
Expand Down Expand Up @@ -445,7 +446,8 @@ QgsVectorFileWriter::QgsVectorFileWriter(
OGR_Fld_Destroy( fld );

int ogrIdx = OGR_FD_GetFieldIndex( defn, mCodec->fromUnicode( name ) );
if ( ogrIdx < 0 )
QgsDebugMsg( QString( "returned field index for %1: %2" ).arg( name ).arg( ogrIdx ) );
if ( ogrIdx < 0 || existingIdxs.contains( ogrIdx ) )
{
#if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM < 1700
// if we didn't find our new column, assume it's name was truncated and
Expand Down Expand Up @@ -478,6 +480,7 @@ QgsVectorFileWriter::QgsVectorFileWriter(
}
}

existingIdxs.insert( ogrIdx );
mAttrIdxToOgrIdx.insert( fldIdx, ogrIdx );
}

Expand Down Expand Up @@ -1386,7 +1389,7 @@ QMap<QString, QgsVectorFileWriter::MetaData> QgsVectorFileWriter::initMetaData()

layerOptions.insert( "LAUNDER", new BoolOption(
QObject::tr( "Controls whether layer and field names will be laundered for easier use "
"in SQLite. Laundered names will be convered to lower case and some special "
"in SQLite. Laundered names will be converted to lower case and some special "
"characters(' - #) will be changed to underscores." ),
true // Default value
) );
Expand Down Expand Up @@ -2594,7 +2597,7 @@ QgsVectorFileWriter::WriterError QgsVectorFileWriter::exportFeaturesSymbolLevels
if ( !styleString.isEmpty() )
{
OGR_F_SetStyleString( ogrFeature, styleString.toLocal8Bit().data() );
if ( ! writeFeature( mLayer, ogrFeature ) )
if ( !writeFeature( mLayer, ogrFeature ) )
{
++nErrors;
}
Expand Down

0 comments on commit 945187c

Please sign in to comment.