Skip to content

Commit

Permalink
Merge pull request #5949 from rouault/postgis_implement_ieq
Browse files Browse the repository at this point in the history
PostGIS backend: translate insensitive equality comparison as PostgreSQL lower(foo::text) = lower('bar').
  • Loading branch information
rouault committed Dec 13, 2019
2 parents b64932c + 7f3cebc commit a531870
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions mappostgis.c
Expand Up @@ -4035,6 +4035,8 @@ int msPostGISLayerTranslateFilter(layerObj *layer, expressionObj *filter, char *
msFree(snippet);
msFree(stresc);
} else if(filter->type == MS_EXPRESSION) {
int ieq_expected = MS_FALSE;

if(msPostGISParseData(layer) != MS_SUCCESS) return MS_FAILURE;

if(layer->debug >= 2) msDebug("msPostGISLayerTranslateFilter. String: %s.\n", filter->string);
Expand All @@ -4049,7 +4051,9 @@ int msPostGISLayerTranslateFilter(layerObj *layer, expressionObj *filter, char *
*/
if(node->token == MS_TOKEN_BINDING_TIME) {
bindingToken = node->token;
} else if(node->token == MS_TOKEN_COMPARISON_EQ || node->token == MS_TOKEN_COMPARISON_NE ||
} else if(node->token == MS_TOKEN_COMPARISON_EQ ||
node->token == MS_TOKEN_COMPARISON_IEQ ||
node->token == MS_TOKEN_COMPARISON_NE ||
node->token == MS_TOKEN_COMPARISON_GT || node->token == MS_TOKEN_COMPARISON_GE ||
node->token == MS_TOKEN_COMPARISON_LT || node->token == MS_TOKEN_COMPARISON_LE ||
node->token == MS_TOKEN_COMPARISON_IN) {
Expand Down Expand Up @@ -4100,7 +4104,10 @@ int msPostGISLayerTranslateFilter(layerObj *layer, expressionObj *filter, char *

msFreeCharArray(strings, nstrings);
} else {
strtmpl = "'%s'";
if(comparisonToken == MS_TOKEN_COMPARISON_IEQ)
strtmpl = "lower('%s')";
else
strtmpl = "'%s'";
stresc = msPostGISEscapeSQLParam(layer, node->tokenval.strval);
snippet = (char *) msSmallMalloc(strlen(strtmpl) + strlen(stresc));
sprintf(snippet, strtmpl, stresc);
Expand Down Expand Up @@ -4148,7 +4155,11 @@ int msPostGISLayerTranslateFilter(layerObj *layer, expressionObj *filter, char *
case MS_TOKEN_BINDING_DOUBLE:
case MS_TOKEN_BINDING_INTEGER:
case MS_TOKEN_BINDING_STRING:
if(node->token == MS_TOKEN_BINDING_STRING || node->next->token == MS_TOKEN_COMPARISON_RE || node->next->token == MS_TOKEN_COMPARISON_IRE)
if (node->token == MS_TOKEN_BINDING_STRING && node->next->token == MS_TOKEN_COMPARISON_IEQ ) {
strtmpl = "lower(%s::text)";
ieq_expected = MS_TRUE;
}
else if(node->token == MS_TOKEN_BINDING_STRING || node->next->token == MS_TOKEN_COMPARISON_RE || node->next->token == MS_TOKEN_COMPARISON_IRE)
strtmpl = "%s::text"; /* explicit cast necessary for certain operators */
else
strtmpl = "%s";
Expand Down Expand Up @@ -4194,8 +4205,19 @@ int msPostGISLayerTranslateFilter(layerObj *layer, expressionObj *filter, char *
native_string = msStringConcatenate(native_string, msExpressionTokenToString(node->token));
break;

/* unsupported tokens */
case MS_TOKEN_COMPARISON_IEQ:
if( ieq_expected )
{
native_string = msStringConcatenate(native_string, "=");
ieq_expected = MS_FALSE;
}
else
{
goto cleanup;
}
break;

/* unsupported tokens */
case MS_TOKEN_COMPARISON_BEYOND:
case MS_TOKEN_FUNCTION_TOSTRING:
case MS_TOKEN_FUNCTION_ROUND:
Expand Down

0 comments on commit a531870

Please sign in to comment.