Skip to content

Commit 9a261cf

Browse files
committed
QgsFields::fieldNameIndex also matches field alias
This method is also doing case insensitive "fuzzy" matching now, this just adds yet another level of tolerance. This changes the expressions to also take the alias into account if no matches have been found.
1 parent 4b86838 commit 9a261cf

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

src/core/qgsfield.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -481,9 +481,9 @@ int QgsFields::fieldOriginIndex( int fieldIdx ) const
481481
return d->fields[fieldIdx].originIndex;
482482
}
483483

484-
int QgsFields::indexFromName( const QString &name ) const
484+
int QgsFields::indexFromName( const QString &fieldName ) const
485485
{
486-
return d->nameToIndex.value( name, -1 );
486+
return d->nameToIndex.value( fieldName, -1 );
487487
}
488488

489489
QList<QgsField> QgsFields::toList() const
@@ -605,6 +605,12 @@ int QgsFields::fieldNameIndex( const QString& fieldName ) const
605605
return idx;
606606
}
607607

608+
for ( int idx = 0; idx < count(); ++idx )
609+
{
610+
if ( QString::compare( d->fields[idx].field.alias(), fieldName, Qt::CaseInsensitive ) == 0 )
611+
return idx;
612+
}
613+
608614
return -1;
609615
}
610616

src/core/qgsfield.h

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -365,12 +365,32 @@ class CORE_EXPORT QgsFields
365365
//! Get field's origin index (its meaning is specific to each type of origin)
366366
int fieldOriginIndex( int fieldIdx ) const;
367367

368-
//! Look up field's index from name. Returns -1 on error
369-
int indexFromName( const QString& name ) const;
368+
/**
369+
* Look up field's index from the field name.
370+
* This method takes is case sensitive and only matches the data source
371+
* name of the field.
372+
*
373+
* @param fieldName The name of the field.
374+
*
375+
* @return The field index if found or -1 in case it cannot be found.
376+
* @see fieldNameIndex For a more tolerant alternative.
377+
*/
378+
int indexFromName( const QString& fieldName ) const;
370379

371-
//! Look up field's index from name
372-
//! also looks up case-insensitive if there is no match otherwise
373-
//! @note added in 2.4
380+
/**
381+
* Look up field's index from the field name.
382+
* This method matches in the following order:
383+
*
384+
* 1. The exact field name taking case sensitivity into account
385+
* 2. Looks for the field name by case insensitive comparison
386+
* 3. The field alias (case insensitive)
387+
*
388+
* @param fieldName The name to look for.
389+
*
390+
* @return The field index if found or -1 in case it cannot be found.
391+
* @see indexFromName For a more performant and precise but less tolerant alternative.
392+
* @note added in 2.4
393+
*/
374394
int fieldNameIndex( const QString& fieldName ) const;
375395

376396
//! Utility function to get list of attribute indexes

tests/src/core/testqgsfields.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ void TestQgsFields::indexFromName()
330330
{
331331
QgsFields fields;
332332
QgsField field( QString( "testfield" ) );
333+
field.setAlias( "testfieldAlias" );
333334
fields.append( field );
334335
QgsField field2( QString( "testfield2" ) );
335336
fields.append( field2 );
@@ -351,6 +352,11 @@ void TestQgsFields::indexFromName()
351352
QgsField sameNameDifferentCase( QString( "teStFielD" ) );
352353
fields.append( sameNameDifferentCase );
353354
QCOMPARE( fields.fieldNameIndex( QString( "teStFielD" ) ), 3 );
355+
356+
//test that the alias is only matched with fieldNameIndex
357+
QCOMPARE( fields.indexFromName( "testfieldAlias" ), -1 );
358+
QCOMPARE( fields.fieldNameIndex( "testfieldAlias" ), 0 );
359+
QCOMPARE( fields.fieldNameIndex( "testfieldalias" ), 0 );
354360
}
355361

356362
void TestQgsFields::toList()

0 commit comments

Comments
 (0)