Skip to content

Commit a3e7727

Browse files
author
jef
committed
support unset fields in OGR provider (like NULL in postgres provider; fixes #1023)
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@8309 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 28e9265 commit a3e7727

File tree

1 file changed

+64
-44
lines changed

1 file changed

+64
-44
lines changed

src/providers/ogr/qgsogrprovider.cpp

+64-44
Original file line numberDiff line numberDiff line change
@@ -440,18 +440,22 @@ void QgsOgrProvider::getFeatureAttribute(OGRFeatureH ogrFet, QgsFeature & f, int
440440
return;
441441
}
442442

443-
//QString fld = mEncoding->toUnicode(fldDef->GetNameRef());
444-
QByteArray cstr(OGR_F_GetFieldAsString(ogrFet,attindex));
445-
QString str = mEncoding->toUnicode(cstr);
446443
QVariant value;
447444

448-
switch (mAttributeFields[attindex].type())
445+
if( OGR_F_IsFieldSet(ogrFet, attindex) )
449446
{
450-
case QVariant::String: value = QVariant(str); break;
451-
case QVariant::Int: value = QVariant(str.toInt()); break;
452-
case QVariant::Double: value = QVariant(str.toDouble()); break;
453-
//case QVariant::DateTime: value = QVariant(QDateTime::fromString(str)); break;
454-
default: assert(NULL && "unsupported field type");
447+
switch (mAttributeFields[attindex].type())
448+
{
449+
case QVariant::String: value = QVariant( mEncoding->toUnicode( OGR_F_GetFieldAsString(ogrFet,attindex) ) ); break;
450+
case QVariant::Int: value = QVariant( OGR_F_GetFieldAsInteger( ogrFet, attindex ) ); break;
451+
case QVariant::Double: value = QVariant( OGR_F_GetFieldAsDouble( ogrFet, attindex ) ); break;
452+
//case QVariant::DateTime: value = QVariant(QDateTime::fromString(str)); break;
453+
default: assert(NULL && "unsupported field type");
454+
}
455+
}
456+
else
457+
{
458+
value = QVariant( QString::null );
455459
}
456460

457461
f.addAttribute(attindex, value);
@@ -511,30 +515,38 @@ bool QgsOgrProvider::addFeature(QgsFeature& f)
511515

512516
//if(!s.isEmpty())
513517
// continue;
514-
518+
//
515519
OGRFieldDefnH fldDef = OGR_FD_GetFieldDefn( fdef, targetAttributeId );
520+
OGRFieldType type = OGR_Fld_GetType( fldDef );
516521

517-
switch( OGR_Fld_GetType(fldDef) )
522+
if( it->isNull() || (type!=OFTString && it->toString().isEmpty()) )
518523
{
519-
case OFTInteger:
520-
OGR_F_SetFieldInteger(feature,targetAttributeId,it->toInt());
521-
break;
522-
523-
case OFTReal:
524-
OGR_F_SetFieldDouble(feature,targetAttributeId,it->toDouble());
525-
break;
526-
527-
case OFTString:
528-
QgsDebugMsg( QString("Writing string attribute %1 with %2, encoding %3")
529-
.arg( targetAttributeId )
530-
.arg( it->toString() )
531-
.arg( mEncoding->name().data() ) );
532-
OGR_F_SetFieldString(feature,targetAttributeId,mEncoding->fromUnicode(it->toString()).constData());
533-
break;
534-
535-
default:
536-
QgsLogger::warning("QgsOgrProvider::addFeature, no type found");
537-
break;
524+
OGR_F_UnsetField(feature, targetAttributeId);
525+
}
526+
else
527+
{
528+
switch( type )
529+
{
530+
case OFTInteger:
531+
OGR_F_SetFieldInteger(feature,targetAttributeId,it->toInt());
532+
break;
533+
534+
case OFTReal:
535+
OGR_F_SetFieldDouble(feature,targetAttributeId,it->toDouble());
536+
break;
537+
538+
case OFTString:
539+
QgsDebugMsg( QString("Writing string attribute %1 with %2, encoding %3")
540+
.arg( targetAttributeId )
541+
.arg( it->toString() )
542+
.arg( mEncoding->name().data() ) );
543+
OGR_F_SetFieldString(feature,targetAttributeId,mEncoding->fromUnicode(it->toString()).constData());
544+
break;
545+
546+
default:
547+
QgsLogger::warning("QgsOgrProvider::addFeature, no type found");
548+
break;
549+
}
538550
}
539551
}
540552

@@ -633,22 +645,30 @@ bool QgsOgrProvider::changeAttributeValues(const QgsChangedAttributesMap & attr_
633645
}
634646

635647
OGRFieldType type = OGR_Fld_GetType( fd );
636-
switch ( type )
648+
649+
if( it2->isNull() || (type!=OFTString && it2->toString().isEmpty()) )
637650
{
638-
case OFTInteger:
639-
OGR_F_SetFieldInteger ( of, f, it2->toInt() );
640-
break;
641-
case OFTReal:
642-
OGR_F_SetFieldDouble ( of, f, it2->toDouble() );
643-
break;
644-
case OFTString:
645-
OGR_F_SetFieldString ( of, f, mEncoding->fromUnicode(it2->toString()).constData() );
646-
break;
647-
default:
648-
QgsLogger::warning("QgsOgrProvider::changeAttributeValues, Unknown field type, cannot change attribute");
649-
break;
651+
OGR_F_UnsetField( of, f);
652+
}
653+
else
654+
{
655+
656+
switch ( type )
657+
{
658+
case OFTInteger:
659+
OGR_F_SetFieldInteger ( of, f, it2->toInt() );
660+
break;
661+
case OFTReal:
662+
OGR_F_SetFieldDouble ( of, f, it2->toDouble() );
663+
break;
664+
case OFTString:
665+
OGR_F_SetFieldString ( of, f, mEncoding->fromUnicode(it2->toString()).constData() );
666+
break;
667+
default:
668+
QgsLogger::warning("QgsOgrProvider::changeAttributeValues, Unknown field type, cannot change attribute");
669+
break;
670+
}
650671
}
651-
652672
}
653673

654674
OGR_L_SetFeature( ogrLayer, of );

0 commit comments

Comments
 (0)