MSSQLWhereBuilder extends AbstractWhereBuilder { @Override protected SQLOperation toProtoSQL( PropertyIsLike op ) throws UnmappableException, FilterEvaluationException { if ( !( op.getPattern() instanceof Literal ) ) { String msg = "Mapping of PropertyIsLike with non-literal comparisons to SQL is not implemented yet."; throw new UnsupportedOperationException( msg ); } String literal = ( (Literal) op.getPattern() ).getValue().toString(); String escape = "" + op.getEscapeChar(); String wildCard = "" + op.getWildCard(); String singleChar = "" + op.getSingleChar(); SQLExpression propName = toProtoSQL( op.getExpression() ); IsLikeString specialString = new IsLikeString( literal, wildCard, singleChar, escape ); String sqlEncoded = specialString.toSQL( !op.isMatchCase() ); if ( propName.isMultiValued() ) { sqlEncoded = "%|" + sqlEncoded + "|%"; } SQLOperationBuilder builder = new SQLOperationBuilder(); if ( !op.isMatchCase() ) { builder.add( "LOWER (" + propName + ")" ); } else { builder.add( propName ); } sqlEncoded = replaceAdditionalMsSqlServerSpecialChars( sqlEncoded ); builder.add( " LIKE '" ); builder.add( sqlEncoded ); builder.add( "' ESCAPE '\\'" ); return builder.toOperation(); } MSSQLWhereBuilder( SQLDialect dialect, PropertyNameMapper mapper, OperatorFilter filter,
                              SortProperty[] sortCrit, boolean allowPartialMappings );   }
@Test public void testToProtoSQLPropertyIsLikeRequiredEscapeClausePresent() throws UnmappableException, FilterEvaluationException { Expression testValue = new ValueReference( "shortdesc", CommonNamespaces.getNamespaceContext() ); Expression pattern = new Literal<PrimitiveValue>( "HOWELLCITY" ); String wildCard = "*"; String singleChar = "#"; String escapeChar = "!"; Boolean matchCase = true; MatchAction matchAction = MatchAction.ALL; PropertyIsLike op = new PropertyIsLike( testValue, pattern, wildCard, singleChar, escapeChar, matchCase, matchAction ); SQLOperation protoSQL = whereBuilder.toProtoSQL( op ); StringBuilder sql = protoSQL.getSQL(); Assert.assertEquals( "table.shortdesc LIKE 'HOWELLCITY' ESCAPE '\\'", sql.toString() ); }
@Test public void testToProtoSQLPropertyIsLikeEscapedBrackets() throws UnmappableException, FilterEvaluationException { Expression testValue = new ValueReference( "shortdesc", CommonNamespaces.getNamespaceContext() ); Expression pattern = new Literal<PrimitiveValue>( "HOWELL [CITY]" ); String wildCard = "*"; String singleChar = "#"; String escapeChar = "!"; Boolean matchCase = true; MatchAction matchAction = MatchAction.ALL; PropertyIsLike op = new PropertyIsLike( testValue, pattern, wildCard, singleChar, escapeChar, matchCase, matchAction ); SQLOperation protoSQL = whereBuilder.toProtoSQL( op ); StringBuilder sql = protoSQL.getSQL(); Assert.assertEquals( "table.shortdesc LIKE 'HOWELL \\[CITY\\]' ESCAPE '\\'", sql.toString() ); }