Skip to content

Commit

Permalink
Merge pull request #829 from mhugent/ogr_odbc_subset
Browse files Browse the repository at this point in the history
Fix subset for ODBC ogr layers
  • Loading branch information
mhugent committed Aug 25, 2013
2 parents bb798b4 + 8e43791 commit 7f2d84c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
5 changes: 1 addition & 4 deletions src/providers/ogr/qgsogrfeatureiterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ QgsOgrFeatureIterator::QgsOgrFeatureIterator( QgsOgrProvider* p, const QgsFeatur

if ( !P->subsetString().isEmpty() )
{
QByteArray sql = "SELECT * FROM " + P->quotedIdentifier( OGR_FD_GetName( OGR_L_GetLayerDefn( ogrLayer ) ) );
sql += " WHERE " + P->textEncoding()->fromUnicode( P->subsetString() );
QgsDebugMsg( QString( "SQL: %1" ).arg( P->textEncoding()->toUnicode( sql ) ) );
ogrLayer = OGR_DS_ExecuteSQL( ogrDataSource, sql.constData(), NULL, NULL );
ogrLayer = P->setSubsetString( ogrLayer, ogrDataSource );
mSubsetStringSet = true;
}

Expand Down
26 changes: 21 additions & 5 deletions src/providers/ogr/qgsogrprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,12 +398,8 @@ bool QgsOgrProvider::setSubsetString( QString theSQL, bool updateFeatureCount )

if ( !mSubsetString.isEmpty() )
{
QByteArray sql = "SELECT * FROM " + quotedIdentifier( OGR_FD_GetName( OGR_L_GetLayerDefn( ogrOrigLayer ) ) );
sql += " WHERE " + mEncoding->fromUnicode( mSubsetString );

QgsDebugMsg( QString( "SQL: %1" ).arg( mEncoding->toUnicode( sql ) ) );
ogrLayer = OGR_DS_ExecuteSQL( ogrDataSource, sql.constData(), NULL, NULL );

ogrLayer = setSubsetString( ogrOrigLayer, ogrDataSource );
if ( !ogrLayer )
{
pushError( tr( "OGR[%1] error %2: %3" ).arg( CPLGetLastErrorType() ).arg( CPLGetLastErrorNo() ).arg( CPLGetLastErrorMsg() ) );
Expand Down Expand Up @@ -2415,6 +2411,26 @@ OGRwkbGeometryType QgsOgrProvider::ogrWkbSingleFlatten( OGRwkbGeometryType type
}
}

OGRLayerH QgsOgrProvider::setSubsetString( OGRLayerH layer, OGRDataSourceH ds )
{
QByteArray layerName = OGR_FD_GetName( OGR_L_GetLayerDefn( layer ) );
if ( ogrDriverName == "ODBC" ) //the odbc driver does not like schema names for subset
{
QString layerNameString = mEncoding->toUnicode( layerName );
int dotIndex = layerNameString.indexOf( "." );
if ( dotIndex > 1 )
{
QString modifiedLayerName = layerNameString.right( layerNameString.size() - dotIndex - 1 );
layerName = mEncoding->fromUnicode( modifiedLayerName );
}
}
QByteArray sql = "SELECT * FROM " + quotedIdentifier( layerName );
sql += " WHERE " + mEncoding->fromUnicode( mSubsetString );

QgsDebugMsg( QString( "SQL: %1" ).arg( mEncoding->toUnicode( sql ) ) );
return OGR_DS_ExecuteSQL( ds, sql.constData(), NULL, NULL );
}

// ---------------------------------------------------------------------------

QGISEXTERN QgsVectorLayerImport::ImportError createEmptyLayer(
Expand Down
2 changes: 2 additions & 0 deletions src/providers/ogr/qgsogrprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,8 @@ class QgsOgrProvider : public QgsVectorDataProvider
/**Calls OGR_L_SyncToDisk and recreates the spatial index if present*/
bool syncToDisc();

OGRLayerH setSubsetString( OGRLayerH layer, OGRDataSourceH ds );

friend class QgsOgrFeatureIterator;
QSet< QgsOgrFeatureIterator* > mActiveIterators;
};

0 comments on commit 7f2d84c

Please sign in to comment.