Skip to content

Commit ce8a9fc

Browse files
author
jef
committed
OGR provider update (fixes #1746)
- use OGR and not the filename to deduce the layername - fall back to QgsVectorDataProvider methods if OGR SQL doesn't work - check error code of OGR_L_SetFeature git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@10972 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent d953fcd commit ce8a9fc

File tree

1 file changed

+31
-28
lines changed

1 file changed

+31
-28
lines changed

src/providers/ogr/qgsogrprovider.cpp

+31-28
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ static const QString TEXT_PROVIDER_DESCRIPTION =
5353
+ GDALVersionInfo( "RELEASE_NAME" )
5454
+ ")";
5555

56-
57-
5856
QgsOgrProvider::QgsOgrProvider( QString const & uri )
5957
: QgsVectorDataProvider( uri ),
6058
ogrDataSource( 0 ),
@@ -768,7 +766,11 @@ bool QgsOgrProvider::changeAttributeValues( const QgsChangedAttributesMap & attr
768766
}
769767
}
770768

771-
OGR_L_SetFeature( ogrLayer, of );
769+
OGRErr res;
770+
if (( res = OGR_L_SetFeature( ogrLayer, of ) ) != OGRERR_NONE )
771+
{
772+
QgsLogger::warning( "QgsOgrProvider::changeAttributeValues, setting the feature failed: " + QString::number( res ) );
773+
}
772774
}
773775

774776
OGR_L_SyncToDisk( ogrLayer );
@@ -778,6 +780,7 @@ bool QgsOgrProvider::changeAttributeValues( const QgsChangedAttributesMap & attr
778780

779781
bool QgsOgrProvider::changeGeometryValues( QgsGeometryMap & geometry_map )
780782
{
783+
OGRErr res;
781784
OGRFeatureH theOGRFeature = 0;
782785
OGRGeometryH theNewGeometry = 0;
783786

@@ -809,15 +812,23 @@ bool QgsOgrProvider::changeGeometryValues( QgsGeometryMap & geometry_map )
809812
}
810813

811814
//set the new geometry
812-
if ( OGR_F_SetGeometryDirectly( theOGRFeature, theNewGeometry ) != OGRERR_NONE )
815+
if (( res = OGR_F_SetGeometryDirectly( theOGRFeature, theNewGeometry ) ) != OGRERR_NONE )
816+
{
817+
QgsLogger::warning( "QgsOgrProvider::changeGeometryValues, error while replacing geometry: " + QString::number( res ) );
818+
OGR_G_DestroyGeometry( theNewGeometry );
819+
theNewGeometry = 0;
820+
continue;
821+
}
822+
823+
824+
if (( res = OGR_L_SetFeature( ogrLayer, theOGRFeature ) ) != OGRERR_NONE )
813825
{
814-
QgsLogger::warning( "QgsOgrProvider::changeGeometryValues, error while replacing geometry" );
826+
QgsLogger::warning( "QgsOgrProvider::changeGeometryValues, error while setting feature: " + QString::number( res ) );
815827
OGR_G_DestroyGeometry( theNewGeometry );
816828
theNewGeometry = 0;
817829
continue;
818830
}
819831

820-
OGR_L_SetFeature( ogrLayer, theOGRFeature );
821832
OGR_F_Destroy( theOGRFeature );
822833
}
823834
OGR_L_SyncToDisk( ogrLayer );
@@ -914,7 +925,7 @@ int QgsOgrProvider::capabilities() const
914925
// TODO And test appropriately.
915926

916927
ability |= ChangeAttributeValues;
917-
ability |= QgsVectorDataProvider::ChangeGeometries;
928+
ability |= ChangeGeometries;
918929
}
919930

920931
if ( OGR_L_TestCapability( ogrLayer, "FastSpatialFilter" ) )
@@ -1505,48 +1516,42 @@ QgsCoordinateReferenceSystem QgsOgrProvider::crs()
15051516
void QgsOgrProvider::uniqueValues( int index, QList<QVariant> &uniqueValues )
15061517
{
15071518
QgsField fld = mAttributeFields[index];
1508-
QFileInfo fi( dataSourceUri() );
1509-
if ( !fi.exists() )
1510-
return;
1519+
QString theLayerName = OGR_FD_GetName( OGR_L_GetLayerDefn( ogrLayer ) );
15111520

15121521
QString sql = QString( "SELECT DISTINCT %1 FROM %2 ORDER BY %1" )
15131522
.arg( quotedIdentifier( fld.name() ) )
1514-
.arg( quotedIdentifier( fi.completeBaseName() ) );
1523+
.arg( quotedIdentifier( theLayerName ) );
15151524

15161525
uniqueValues.clear();
15171526

15181527
QgsDebugMsg( QString( "SQL: %1" ).arg( sql ) );
1519-
OGRLayerH lyr = OGR_DS_ExecuteSQL( ogrDataSource, mEncoding->fromUnicode( sql ).data(), NULL, "SQL" );
1520-
if ( 0 == lyr )
1521-
return;
1528+
OGRLayerH l = OGR_DS_ExecuteSQL( ogrDataSource, mEncoding->fromUnicode( sql ).data(), NULL, "SQL" );
1529+
if ( l == 0 )
1530+
return QgsVectorDataProvider::uniqueValues( index, uniqueValues );
15221531

15231532
OGRFeatureH f;
1524-
while ( 0 != ( f = OGR_L_GetNextFeature( lyr ) ) )
1533+
while ( 0 != ( f = OGR_L_GetNextFeature( l ) ) )
15251534
{
15261535
uniqueValues << convertValue( fld.type(), mEncoding->toUnicode( OGR_F_GetFieldAsString( f, 0 ) ) );
15271536
OGR_F_Destroy( f );
15281537
}
15291538

1530-
OGR_DS_ReleaseResultSet( ogrDataSource, lyr );
1539+
OGR_DS_ReleaseResultSet( ogrDataSource, l );
15311540
}
15321541

1533-
1534-
15351542
QVariant QgsOgrProvider::minimumValue( int index )
15361543
{
15371544
QgsField fld = mAttributeFields[index];
1538-
QFileInfo fi( dataSourceUri() );
1539-
if ( !fi.exists() )
1540-
return QVariant();
1545+
QString theLayerName = OGR_FD_GetName( OGR_L_GetLayerDefn( ogrLayer ) );
15411546

15421547
QString sql = QString( "SELECT MIN(%1) FROM %2" )
15431548
.arg( quotedIdentifier( fld.name() ) )
1544-
.arg( quotedIdentifier( fi.completeBaseName() ) );
1549+
.arg( quotedIdentifier( theLayerName ) );
15451550

15461551
OGRLayerH l = OGR_DS_ExecuteSQL( ogrDataSource, mEncoding->fromUnicode( sql ).data(), NULL, "SQL" );
15471552

15481553
if ( l == 0 )
1549-
return QVariant();
1554+
return QgsVectorDataProvider::minimumValue( index );
15501555

15511556
OGRFeatureH f = OGR_L_GetNextFeature( l );
15521557
if ( f == 0 )
@@ -1566,17 +1571,15 @@ QVariant QgsOgrProvider::minimumValue( int index )
15661571
QVariant QgsOgrProvider::maximumValue( int index )
15671572
{
15681573
QgsField fld = mAttributeFields[index];
1569-
QFileInfo fi( dataSourceUri() );
1570-
if ( !fi.exists() )
1571-
return QVariant();
1574+
QString theLayerName = OGR_FD_GetName( OGR_L_GetLayerDefn( ogrLayer ) );
15721575

15731576
QString sql = QString( "SELECT MAX(%1) FROM %2" )
15741577
.arg( quotedIdentifier( fld.name() ) )
1575-
.arg( quotedIdentifier( fi.completeBaseName() ) );
1578+
.arg( quotedIdentifier( theLayerName ) );
15761579

15771580
OGRLayerH l = OGR_DS_ExecuteSQL( ogrDataSource, mEncoding->fromUnicode( sql ).data(), NULL, "SQL" );
15781581
if ( l == 0 )
1579-
return QVariant();
1582+
return QgsVectorDataProvider::maximumValue( index );
15801583

15811584
OGRFeatureH f = OGR_L_GetNextFeature( l );
15821585
if ( f == 0 )

0 commit comments

Comments
 (0)