Handle the fact that PostgreSQL uses a backslash as default escape character in the like predicate

This commit is contained in:
Christian Beikov 2021-10-19 18:28:04 +02:00
parent 9c2c7e13dd
commit ae9d99a32c
2 changed files with 29 additions and 2 deletions

View File

@ -16,6 +16,7 @@ import org.hibernate.sql.ast.tree.expression.Expression;
import org.hibernate.sql.ast.tree.expression.Literal;
import org.hibernate.sql.ast.tree.expression.Summarization;
import org.hibernate.sql.ast.tree.predicate.BooleanExpressionPredicate;
import org.hibernate.sql.ast.tree.predicate.LikePredicate;
import org.hibernate.sql.ast.tree.select.QueryGroup;
import org.hibernate.sql.ast.tree.select.QueryPart;
import org.hibernate.sql.ast.tree.select.QuerySpec;
@ -146,4 +147,32 @@ public class PostgreSQLSqlAstTranslator<T extends JdbcOperation> extends Abstrac
}
}
@Override
public void visitLikePredicate(LikePredicate likePredicate) {
// We need a custom implementation here because PostgreSQL
// uses the backslash character as default escape character
// According to the documentation, we can overcome this by specifying an empty escape character
// See https://www.postgresql.org/docs/current/functions-matching.html#FUNCTIONS-LIKE
likePredicate.getMatchExpression().accept( this );
if ( likePredicate.isNegated() ) {
appendSql( " not" );
}
if ( likePredicate.isCaseSensitive() ) {
appendSql( " like " );
}
else {
appendSql( WHITESPACE );
appendSql( getDialect().getCaseInsensitiveLike() );
appendSql( WHITESPACE );
}
likePredicate.getPattern().accept( this );
if ( likePredicate.getEscapeCharacter() != null ) {
appendSql( " escape " );
likePredicate.getEscapeCharacter().accept( this );
}
else {
appendSql( " escape ''" );
}
}
}

View File

@ -4385,12 +4385,10 @@ public abstract class AbstractSqlAstTranslator<T extends JdbcOperation> implemen
appendSql( " escape " );
likePredicate.getEscapeCharacter().accept( this );
}
}
else {
renderCaseInsensitiveLikeEmulation(likePredicate.getMatchExpression(), likePredicate.getPattern(), likePredicate.getEscapeCharacter(), likePredicate.isNegated());
}
}
}