Skip to content

Commit

Permalink
ogr provider update: better support column names containing internati…
Browse files Browse the repository at this point in the history
…onal characters

git-svn-id: http://svn.osgeo.org/qgis/trunk@9415 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Sep 26, 2008
1 parent d9f3afa commit c23a4d9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
27 changes: 21 additions & 6 deletions src/providers/ogr/qgsogrprovider.cpp
Expand Up @@ -1263,11 +1263,14 @@ void QgsOgrProvider::uniqueValues( int index, QList<QVariant> &uniqueValues )
if ( !fi.exists() )
return;

QString sql = QString( "SELECT DISTINCT %1 FROM %2 ORDER BY %1" ).arg( fld.name() ).arg( fi.baseName() );
QString sql = QString( "SELECT DISTINCT %1 FROM %2 ORDER BY %1" )
.arg( quotedIdentifier( fld.name() ) )
.arg( quotedIdentifier( fi.baseName() ) );

uniqueValues.clear();

OGRLayerH lyr = OGR_DS_ExecuteSQL( ogrDataSource, sql.toAscii(), NULL, "SQL" );
QgsDebugMsg( QString( "SQL: %1" ).arg( sql ) );
OGRLayerH lyr = OGR_DS_ExecuteSQL( ogrDataSource, mEncoding->fromUnicode( sql ).data(), NULL, "SQL" );
if ( 0 == lyr )
return;

Expand All @@ -1290,9 +1293,11 @@ QVariant QgsOgrProvider::minimumValue( int index )
if ( !fi.exists() )
return QVariant();

QString sql = QString( "SELECT MIN(%1) FROM %2" ).arg( fld.name() ).arg( fi.baseName() );
QString sql = QString( "SELECT MIN(%1) FROM %2" )
.arg( quotedIdentifier( fld.name() ) )
.arg( quotedIdentifier( fi.baseName() ) );

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

if ( l == 0 )
return QVariant();
Expand All @@ -1319,9 +1324,11 @@ QVariant QgsOgrProvider::maximumValue( int index )
if ( !fi.exists() )
return QVariant();

QString sql = QString( "SELECT MAX(%1) FROM %2" ).arg( fld.name() ).arg( fi.baseName() );
QString sql = QString( "SELECT MAX(%1) FROM %2" )
.arg( quotedIdentifier( fld.name() ) )
.arg( quotedIdentifier( fi.baseName() ) );

OGRLayerH l = OGR_DS_ExecuteSQL( ogrDataSource, sql.toAscii(), NULL, "SQL" );
OGRLayerH l = OGR_DS_ExecuteSQL( ogrDataSource, mEncoding->fromUnicode( sql ).data(), NULL, "SQL" );
if ( l == 0 )
return QVariant();

Expand All @@ -1339,3 +1346,11 @@ QVariant QgsOgrProvider::maximumValue( int index )

return value;
}

QString QgsOgrProvider::quotedIdentifier( QString field )
{
field.replace( '\\', "\\\\" );
field.replace( '"', "\\\"" );
field.replace( "'", "\\'" );
return field.prepend( "\"" ).append( "\"" );
}
2 changes: 2 additions & 0 deletions src/providers/ogr/qgsogrprovider.h
Expand Up @@ -251,4 +251,6 @@ class QgsOgrProvider : public QgsVectorDataProvider
bool addFeature( QgsFeature& f );
/**Deletes one feature*/
bool deleteFeature( int id );

QString quotedIdentifier( QString field );
};

0 comments on commit c23a4d9

Please sign in to comment.