Skip to content

Commit

Permalink
OGRGenSQLResultsLayer::Compare(): cleanup, and fix potential issue on…
Browse files Browse the repository at this point in the history
… special fields on big-endian hosts

git-svn-id: https://svn.osgeo.org/gdal/trunk@37570 f0d54148-0727-0410-94bb-9a71ac55c965
  • Loading branch information
rouault committed Mar 3, 2017
1 parent 277ab88 commit ef47159
Showing 1 changed file with 31 additions and 30 deletions.
61 changes: 31 additions & 30 deletions gdal/ogr/ogrsf_frmts/generic/ogr_gensql.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2148,6 +2148,19 @@ void OGRGenSQLResultsLayer::SortIndexSection( const OGRField *pasIndexFields,
memcpy( panFIDIndex + nStart, panMerged, sizeof(GIntBig) * nEntries );
}

/************************************************************************/
/* ComparePrimitive() */
/************************************************************************/

template<class T> static inline int ComparePrimitive(const T& a, const T& b)
{
if( a < b )
return -1;
if( a > b )
return 1;
return 0;
}

/************************************************************************/
/* Compare() */
/************************************************************************/
Expand All @@ -2164,12 +2177,7 @@ int OGRGenSQLResultsLayer::Compare( const OGRField *pasFirstTuple,
swq_order_def *psKeyDef = psSelectInfo->order_defs + iKey;
OGRFieldDefn *poFDefn = NULL;

if( psKeyDef->field_index >= iFIDFieldIndex + SPECIAL_FIELD_COUNT )
{
CPLAssert( false );
return 0;
}
else if( psKeyDef->field_index >= iFIDFieldIndex )
if( psKeyDef->field_index >= iFIDFieldIndex )
poFDefn = NULL;
else
poFDefn = poSrcLayer->GetLayerDefn()->GetFieldDefn(
Expand All @@ -2191,22 +2199,21 @@ int OGRGenSQLResultsLayer::Compare( const OGRField *pasFirstTuple,
}
else if ( poFDefn == NULL )
{
CPLAssert( psKeyDef->field_index <
iFIDFieldIndex + SPECIAL_FIELD_COUNT );
switch (SpecialFieldTypes[psKeyDef->field_index - iFIDFieldIndex])
{
case SWQ_INTEGER:
nResult = ComparePrimitive( pasFirstTuple[iKey].Integer,
pasSecondTuple[iKey].Integer );
break;
case SWQ_INTEGER64:
if( pasFirstTuple[iKey].Integer64 <
pasSecondTuple[iKey].Integer64 )
nResult = -1;
else if( pasFirstTuple[iKey].Integer64 >
pasSecondTuple[iKey].Integer64 )
nResult = 1;
nResult = ComparePrimitive( pasFirstTuple[iKey].Integer64,
pasSecondTuple[iKey].Integer64 );
break;
case SWQ_FLOAT:
if( pasFirstTuple[iKey].Real < pasSecondTuple[iKey].Real )
nResult = -1;
else if( pasFirstTuple[iKey].Real > pasSecondTuple[iKey].Real )
nResult = 1;
nResult = ComparePrimitive( pasFirstTuple[iKey].Real,
pasSecondTuple[iKey].Real );
break;
case SWQ_STRING:
nResult = strcmp(pasFirstTuple[iKey].String,
Expand All @@ -2220,29 +2227,23 @@ int OGRGenSQLResultsLayer::Compare( const OGRField *pasFirstTuple,
}
else if( poFDefn->GetType() == OFTInteger )
{
if( pasFirstTuple[iKey].Integer < pasSecondTuple[iKey].Integer )
nResult = -1;
else if( pasFirstTuple[iKey].Integer
> pasSecondTuple[iKey].Integer )
nResult = 1;
nResult = ComparePrimitive( pasFirstTuple[iKey].Integer,
pasSecondTuple[iKey].Integer );
}
else if( poFDefn->GetType() == OFTInteger64 )
{
if( pasFirstTuple[iKey].Integer64 < pasSecondTuple[iKey].Integer64 )
nResult = -1;
else if( pasFirstTuple[iKey].Integer64
> pasSecondTuple[iKey].Integer64 )
nResult = 1;
nResult = ComparePrimitive( pasFirstTuple[iKey].Integer64,
pasSecondTuple[iKey].Integer64 );
}
else if( poFDefn->GetType() == OFTString )
{
nResult = strcmp(pasFirstTuple[iKey].String,
pasSecondTuple[iKey].String);
}
else if( poFDefn->GetType() == OFTReal )
{
if( pasFirstTuple[iKey].Real < pasSecondTuple[iKey].Real )
nResult = -1;
else if( pasFirstTuple[iKey].Real > pasSecondTuple[iKey].Real )
nResult = 1;
nResult = ComparePrimitive( pasFirstTuple[iKey].Real,
pasSecondTuple[iKey].Real );
}
else if( poFDefn->GetType() == OFTDate ||
poFDefn->GetType() == OFTTime ||
Expand Down

0 comments on commit ef47159

Please sign in to comment.