Skip to content

Commit

Permalink
[jOOQ#12465] [jOOQ#12432] Extract LIKE, NOT LIKE
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaseder committed Sep 24, 2021
1 parent db8c811 commit e6f3232
Show file tree
Hide file tree
Showing 8 changed files with 552 additions and 171 deletions.
68 changes: 32 additions & 36 deletions jOOQ/src/main/java/org/jooq/Field.java
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,22 @@ <U> Field<U> convert(
@Support
Condition lessThan(Field<T> arg2);

/**
* The <code>LIKE</code> operator.
*
* @param pattern is wrapped as {@link #val(Object)}.
*/
@NotNull
@Support
LikeEscapeStep like(@Stringly.Param String pattern);

/**
* The <code>LIKE</code> operator.
*/
@NotNull
@Support
LikeEscapeStep like(Field<String> pattern);

/**
* The <code>LT</code> operator.
*/
Expand Down Expand Up @@ -1006,6 +1022,22 @@ <U> Field<U> convert(
@Support
Condition notEqual(Field<T> arg2);

/**
* The <code>NOT_LIKE</code> operator.
*
* @param pattern is wrapped as {@link #val(Object)}.
*/
@NotNull
@Support
LikeEscapeStep notLike(@Stringly.Param String pattern);

/**
* The <code>NOT_LIKE</code> operator.
*/
@NotNull
@Support
LikeEscapeStep notLike(Field<String> pattern);

// -------------------------------------------------------------------------
// XML predicates
// -------------------------------------------------------------------------
Expand Down Expand Up @@ -2079,15 +2111,6 @@ <U> Field<U> convert(
// LIKE predicates
// ------------------------------------------------------------------------

/**
* Create a condition to pattern-check this field against a value.
* <p>
* SQL: <code>this like value</code>
*/
@NotNull
@Support
LikeEscapeStep like(Field<String> value);

/**
* Create a condition to pattern-check this field against a value.
* <p>
Expand All @@ -2099,15 +2122,6 @@ <U> Field<U> convert(
@Support
Condition like(Field<String> value, char escape);

/**
* Create a condition to pattern-check this field against a value.
* <p>
* SQL: <code>this like value</code>
*/
@NotNull
@Support
LikeEscapeStep like(String value);

/**
* Create a condition to pattern-check this field against a value.
* <p>
Expand Down Expand Up @@ -2191,15 +2205,6 @@ <U> Field<U> convert(
@Support
Condition likeIgnoreCase(String value, char escape);

/**
* Create a condition to pattern-check this field against a field.
* <p>
* SQL: <code>this not like field</code>
*/
@NotNull
@Support
LikeEscapeStep notLike(Field<String> field);

/**
* Create a condition to pattern-check this field against a field.
* <p>
Expand All @@ -2211,15 +2216,6 @@ <U> Field<U> convert(
@Support
Condition notLike(Field<String> field, char escape);

/**
* Create a condition to pattern-check this field against a value.
* <p>
* SQL: <code>this not like value</code>
*/
@NotNull
@Support
LikeEscapeStep notLike(String value);

/**
* Create a condition to pattern-check this field against a value.
* <p>
Expand Down
45 changes: 10 additions & 35 deletions jOOQ/src/main/java/org/jooq/LikeEscapeStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,37 +37,14 @@
*/
package org.jooq;

// ...
// ...
// ...
// ...
import static org.jooq.SQLDialect.CUBRID;
// ...
import static org.jooq.SQLDialect.DERBY;
import static org.jooq.SQLDialect.FIREBIRD;
import static org.jooq.SQLDialect.H2;
// ...
import static org.jooq.SQLDialect.HSQLDB;
import static org.jooq.SQLDialect.IGNITE;
// ...
// ...
import static org.jooq.SQLDialect.MARIADB;
import static org.jooq.SQLDialect.MYSQL;
// ...
import static org.jooq.SQLDialect.POSTGRES;
// ...
// ...
import static org.jooq.SQLDialect.SQLITE;
// ...
// ...
// ...
// ...
import static org.jooq.SQLDialect.*;

import org.jetbrains.annotations.NotNull;
import java.util.*;

import org.jetbrains.annotations.*;

/**
* A step in the creation of a <code>LIKE</code> predicate to which an
* <code>ESCAPE</code> clause can be appended.
* A step in the construction of the <code>NOT LIKE</code> function.
* <p>
* <h3>Referencing <code>XYZ*Step</code> types directly from client code</h3>
* <p>
Expand All @@ -86,22 +63,20 @@
* <li>They're less readable</li>
* <li>They might have binary incompatible changes between minor releases</li>
* </ul>
*
* @author Lukas Eder
*/
@SuppressWarnings({ "unused" })
public interface LikeEscapeStep extends Condition {


/**
* Add an <code>ESCAPE</code> clause to the <code>LIKE</code> predicate.
* Add the <code>ESCAPE</code> clause to the <code>NOT LIKE</code> function.
* <p>
* For example:
*
*
* <code><pre>
* some_column LIKE 'A!%%' ESCAPE '!'
* </pre></code>
*/
@NotNull
@Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, IGNITE, MARIADB, MYSQL, POSTGRES, SQLITE })
Condition escape(char c);
@NotNull @CheckReturnValue
Condition escape(char escape);
}
51 changes: 30 additions & 21 deletions jOOQ/src/main/java/org/jooq/impl/AbstractField.java
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,18 @@ public final Condition lessThan(Field<T> arg2) {
return lt(arg2);
}

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public final LikeEscapeStep like(String pattern) {
return new Like(this, Tools.field(pattern));
}

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public final LikeEscapeStep like(Field<String> pattern) {
return new Like(this, nullSafe(pattern, getDataType()));
}

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public final Condition lt(T arg2) {
Expand Down Expand Up @@ -583,6 +595,18 @@ public final Condition notEqual(Field<T> arg2) {
return ne(arg2);
}

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public final LikeEscapeStep notLike(String pattern) {
return new NotLike(this, Tools.field(pattern));
}

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public final LikeEscapeStep notLike(Field<String> pattern) {
return new NotLike(this, nullSafe(pattern, getDataType()));
}

// -------------------------------------------------------------------------
// XML predicates
// -------------------------------------------------------------------------
Expand Down Expand Up @@ -1067,19 +1091,9 @@ 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));
}

@Override
public final Condition like(String value, char escape) {
return like(Tools.field(value), escape);
}

@Override
public final LikeEscapeStep like(Field<String> field) {
return new CompareCondition(this, nullSafe(field, getDataType()), LIKE);
return like(value).escape(escape);
}

@Override
Expand Down Expand Up @@ -1122,21 +1136,11 @@ public final Condition likeRegex(Field<String> pattern) {
return new RegexpLike(this, nullSafe(pattern, getDataType()));
}

@Override
public final LikeEscapeStep notLike(String value) {
return notLike(Tools.field(value));
}

@Override
public final Condition notLike(String value, char escape) {
return notLike(Tools.field(value), escape);
}

@Override
public final LikeEscapeStep notLike(Field<String> field) {
return new CompareCondition(this, nullSafe(field, getDataType()), NOT_LIKE);
}

@Override
public final Condition notLike(Field<String> field, char escape) {
return notLike(field).escape(escape);
Expand Down Expand Up @@ -1456,6 +1460,11 @@ public final Condition compare(Comparator comparator, Field<T> field) {
case NOT_EQUALS:
return new Ne<>(this, nullSafe(field, getDataType()));

case LIKE:
return new Like(this, (Field) nullSafe(field, getDataType()));
case NOT_LIKE:
return new NotLike(this, (Field) nullSafe(field, getDataType()));

case IS_DISTINCT_FROM:
return new IsDistinctFrom<>(this, nullSafe(field, getDataType()));
case IS_NOT_DISTINCT_FROM:
Expand Down
15 changes: 15 additions & 0 deletions jOOQ/src/main/java/org/jooq/impl/JSONArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,21 @@ public void accept(Context<?> ctx) {


















// -------------------------------------------------------------------------
// The Object API
// -------------------------------------------------------------------------
Expand Down
27 changes: 17 additions & 10 deletions jOOQ/src/main/java/org/jooq/impl/JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ public final void accept(Context<?> ctx) {









Expand Down Expand Up @@ -213,34 +216,38 @@ private static final boolean isJSONArray(Field<?> field) {
|| field instanceof ScalarSubquery && isJSONArray(((ScalarSubquery<?>) field).query.getSelect().get(0));
}

private final void acceptStandard(Context<?> ctx) {
JSONNull jsonNull;
JSONReturning jsonReturning = new JSONReturning(returning);

// Workaround for https://github.com/h2database/h2database/issues/2496
if (entries.isEmpty() && ctx.family() == H2)
jsonNull = new JSONNull(JSONOnNull.NULL_ON_NULL);





else
jsonNull = new JSONNull(onNull);

ctx.visit(N_JSON_OBJECT).sql('(').visit(QueryPartListView.wrap(QueryPartCollectionView.wrap(entries), jsonNull, jsonReturning).separator("")).sql(')');
}








private final void acceptStandard(Context<?> ctx) {
JSONNull jsonNull;
JSONReturning jsonReturning = new JSONReturning(returning);

// Workaround for https://github.com/h2database/h2database/issues/2496
if (entries.isEmpty() && ctx.family() == H2)
jsonNull = new JSONNull(JSONOnNull.NULL_ON_NULL);





else
jsonNull = new JSONNull(onNull);

ctx.visit(N_JSON_OBJECT).sql('(').visit(QueryPartListView.wrap(QueryPartCollectionView.wrap(entries), jsonNull, jsonReturning).separator("")).sql(')');
}



Expand Down
Loading

0 comments on commit e6f3232

Please sign in to comment.