[#1725] Add support for SQL standard SIMILAR TO predicate

This commit is contained in:
lukaseder 2018-10-09 16:23:24 +02:00
parent 76fc879ab8
commit cdba5086df
4 changed files with 131 additions and 3 deletions

View File

@ -38,6 +38,9 @@
package org.jooq;
import static org.jooq.SQLDialect.FIREBIRD;
import static org.jooq.SQLDialect.POSTGRES;
import org.jooq.impl.DSL;
/**
@ -95,6 +98,12 @@ public enum Comparator {
@Support
NOT_LIKE("not like", false, false),
@Support({ FIREBIRD, POSTGRES })
SIMILAR_TO("similar to", false, false),
@Support({ FIREBIRD, POSTGRES })
NOT_SIMILAR_TO("not similar to", false, false),
@Support
LIKE_IGNORE_CASE("ilike", false, false),

View File

@ -1339,6 +1339,82 @@ extends
@Support({ CUBRID, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE })
Condition notLikeRegex(Field<String> pattern);
// ------------------------------------------------------------------------
// SIMILAR TO predicates
// ------------------------------------------------------------------------
/**
* Create a condition to pattern-check this field against a value.
* <p>
* SQL: <code>this similar to value</code>
*/
@Support({ FIREBIRD, POSTGRES })
LikeEscapeStep similarTo(Field<String> value);
/**
* Create a condition to pattern-check this field against a value.
* <p>
* SQL: <code>this similar to value escape 'e'</code>
*
* @see LikeEscapeStep#escape(char)
*/
@Support({ FIREBIRD, POSTGRES })
Condition similarTo(Field<String> value, char escape);
/**
* Create a condition to pattern-check this field against a value.
* <p>
* SQL: <code>this similar to value</code>
*/
@Support({ FIREBIRD, POSTGRES })
LikeEscapeStep similarTo(String value);
/**
* Create a condition to pattern-check this field against a value.
* <p>
* SQL: <code>this similar to value escape 'e'</code>
*
* @see LikeEscapeStep#escape(char)
*/
@Support({ FIREBIRD, POSTGRES })
Condition similarTo(String value, char escape);
/**
* Create a condition to pattern-check this field against a field.
* <p>
* SQL: <code>this not similar to field</code>
*/
@Support({ FIREBIRD, POSTGRES })
LikeEscapeStep notSimilarTo(Field<String> field);
/**
* Create a condition to pattern-check this field against a field.
* <p>
* SQL: <code>this not similar to field escape 'e'</code>
*
* @see LikeEscapeStep#escape(char)
*/
@Support({ FIREBIRD, POSTGRES })
Condition notSimilarTo(Field<String> field, char escape);
/**
* Create a condition to pattern-check this field against a value.
* <p>
* SQL: <code>this not similar to value</code>
*/
@Support({ FIREBIRD, POSTGRES })
LikeEscapeStep notSimilarTo(String value);
/**
* Create a condition to pattern-check this field against a value.
* <p>
* SQL: <code>this not similar to value escape 'e'</code>
*
* @see LikeEscapeStep#escape(char)
*/
@Support({ FIREBIRD, POSTGRES })
Condition notSimilarTo(String value, char escape);
// ------------------------------------------------------------------------
// LIKE predicates
// ------------------------------------------------------------------------

View File

@ -52,6 +52,8 @@ import static org.jooq.Comparator.NOT_EQUALS;
import static org.jooq.Comparator.NOT_IN;
import static org.jooq.Comparator.NOT_LIKE;
import static org.jooq.Comparator.NOT_LIKE_IGNORE_CASE;
import static org.jooq.Comparator.NOT_SIMILAR_TO;
import static org.jooq.Comparator.SIMILAR_TO;
import static org.jooq.impl.DSL.inline;
import static org.jooq.impl.DSL.nullSafe;
import static org.jooq.impl.DSL.val;
@ -736,6 +738,46 @@ abstract class AbstractField<T> extends AbstractNamed implements Field<T> {
return cast(String.class).in(Tools.inline(FALSE_VALUES.toArray(EMPTY_STRING)));
}
@Override
public final LikeEscapeStep similarTo(String value) {
return similarTo(Tools.field(value));
}
@Override
public final Condition similarTo(String value, char escape) {
return similarTo(Tools.field(value), escape);
}
@Override
public final LikeEscapeStep similarTo(Field<String> field) {
return new CompareCondition(this, nullSafe(field, getDataType()), SIMILAR_TO);
}
@Override
public final Condition similarTo(Field<String> field, char escape) {
return similarTo(field).escape(escape);
}
@Override
public final LikeEscapeStep notSimilarTo(String value) {
return notSimilarTo(Tools.field(value));
}
@Override
public final Condition notSimilarTo(String value, char escape) {
return notSimilarTo(Tools.field(value), escape);
}
@Override
public final LikeEscapeStep notSimilarTo(Field<String> field) {
return new CompareCondition(this, nullSafe(field, getDataType()), NOT_SIMILAR_TO);
}
@Override
public final Condition notSimilarTo(Field<String> field, char escape) {
return notSimilarTo(field).escape(escape);
}
@Override
public final LikeEscapeStep like(String value) {
return like(Tools.field(value));

View File

@ -44,6 +44,8 @@ import static org.jooq.Comparator.LIKE;
import static org.jooq.Comparator.LIKE_IGNORE_CASE;
import static org.jooq.Comparator.NOT_LIKE;
import static org.jooq.Comparator.NOT_LIKE_IGNORE_CASE;
import static org.jooq.Comparator.NOT_SIMILAR_TO;
import static org.jooq.Comparator.SIMILAR_TO;
// ...
// ...
// ...
@ -102,10 +104,9 @@ final class CompareCondition extends AbstractCondition implements LikeEscapeStep
Field<?> rhs = field2;
Comparator op = comparator;
// [#1159] Some dialects cannot auto-convert the LHS operand to a
// [#1159] [#1725] Some dialects cannot auto-convert the LHS operand to a
// VARCHAR when applying a LIKE predicate
// [#293] TODO: This could apply to other operators, too
if ((op == LIKE || op == NOT_LIKE)
if ((op == LIKE || op == NOT_LIKE || op == SIMILAR_TO || op == NOT_SIMILAR_TO)
&& field1.getType() != String.class
&& REQUIRES_CAST_ON_LIKE.contains(family)) {