[#620] Add support for the SQL:2008 standard LIKE_REGEX operator

This commit is contained in:
Lukas Eder 2012-07-14 13:34:20 +02:00
parent 6090465d95
commit adf3848278
5 changed files with 336 additions and 0 deletions

View File

@ -285,6 +285,31 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, I, IPK, T658,
assertEquals(asList(2), books.getValues(TBook_ID()));
}
@Test
public void testLikeRegex() throws Exception {
switch (getDialect()) {
case ASE:
case DB2:
case DERBY:
case INGRES:
case SQLITE:
case SQLSERVER:
log.info("SKIPPING", "REGEX tests");
return;
}
Result<B> result =
create().selectFrom(TBook())
.where(TBook_CONTENT_TEXT().likeRegex(".*conscious.*"))
.or(TBook_TITLE().notLikeRegex(".*m.*"))
.orderBy(TBook_ID())
.fetch();
assertEquals(2, result.size());
assertEquals(1, (int) result.get(0).getValue(TBook_ID()));
assertEquals(4, (int) result.get(1).getValue(TBook_ID()));
}
@Test
public void testLargeINCondition() throws Exception {
Field<Integer> count = count();

View File

@ -1399,6 +1399,11 @@ public abstract class jOOQAbstractTest<
new PredicateTests(this).testLike();
}
@Test
public void testLikeRegex() throws Exception {
new PredicateTests(this).testLikeRegex();
}
@Test
public void testDual() throws Exception {
new GeneralTests(this).testDual();

View File

@ -573,6 +573,142 @@ public interface Field<T> extends NamedTypeProviderQueryPart<T>, AliasProvider<F
@Support
Condition likeIgnoreCase(String value, char escape);
/**
* Create a condition to regex-pattern-check this field against a pattern
* <p>
* The SQL:2008 standard specifies a <code>&lt;regex like predicate></code>
* of the following form: <code><pre>
* &lt;regex like predicate> ::=
* &lt;row value predicand> &lt;regex like predicate part 2>
*
* &lt;regex like predicate part 2> ::=
* [ NOT ] LIKE_REGEX &lt;XQuery pattern> [ FLAG &lt;XQuery option flag> ]
* </pre></code>
* <p>
* This particular <code>LIKE_REGEX</code> operator comes in several
* flavours for various databases. jOOQ supports regular expressions as
* follows:
* <table border="1">
* <tr>
* <th>SQL dialect</th>
* <th>SQL syntax</th>
* <th>Pattern syntax</th>
* <th>Documentation</th>
* </tr>
* <tr>
* <td>{@link SQLDialect#ASE}</td>
* <td>-</td>
* <td>-</td>
* <td>-</td>
* </tr>
* <tr>
* <td>{@link SQLDialect#CUBRID}</td>
* <td><code>[search] REGEXP [pattern]</code></td>
* <td>POSIX</td>
* <td><a href=
* "http://www.cubrid.org/manual/841/en/REGEXP%20Conditional%20Expression"
* >http://www.cubrid.org/manual/841/en/REGEXP Conditional Expression</a></td>
* </tr>
* <tr>
* <td>{@link SQLDialect#DB2}</td>
* <td>-</td>
* <td>-</td>
* <td>-</td>
* </tr>
* <tr>
* <td>{@link SQLDialect#DERBY}</td>
* <td>-</td>
* <td>-</td>
* <td>-</td>
* </tr>
* <tr>
* <td>{@link SQLDialect#H2}</td>
* <td><code>[search] REGEXP [pattern]</code></td>
* <td>Java</td>
* <td><a href=
* "http://www.h2database.com/html/grammar.html#condition_right_hand_side"
* >http
* ://www.h2database.com/html/grammar.html#condition_right_hand_side</a></td>
* </tr>
* <tr>
* <td>{@link SQLDialect#HSQLDB}</td>
* <td><code>REGEXP_MATCHES([search], [pattern])</td>
* <td>Java</td>
* <td><a
* href="http://hsqldb.org/doc/guide/builtinfunctions-chapt.html#N13577"
* >http://hsqldb.org/doc/guide/builtinfunctions-chapt.html#N13577</a></td>
* </tr>
* <tr>
* <td>{@link SQLDialect#INGRES}</td>
* <td>-</td>
* <td>-</td>
* <td>-</td>
* </tr>
* <tr>
* <td>{@link SQLDialect#MYSQL}</td>
* <td><code>[search] REGEXP [pattern]</code></td>
* <td>POSIX</td>
* <td><a href=
* "http://dev.mysql.com/doc/refman/5.6/en/regexp.html">http://dev.mysql.com/doc/refman/5.6/en/regexp.html</a
* ></td>
* </tr>
* <tr>
* <td>{@link SQLDialect#ORACLE}</td>
* <td><code>REGEXP_LIKE([search], [pattern])</code></td>
* <td>POSIX</td>
* <td><a href=
* "http://docs.oracle.com/cd/E14072_01/server.112/e10592/conditions007.htm#sthref1994"
* >http://docs.oracle.com/cd/E14072_01/server.112/e10592/conditions007.htm#
* sthref1994</a></td>
* </tr>
* <tr>
* <td>{@link SQLDialect#POSTGRES}</td>
* <td><code>[search] ~ [pattern]</code></td>
* <td>POSIX</td>
* <td><a href=
* "http://www.postgresql.org/docs/9.1/static/functions-matching.html#FUNCTIONS-POSIX-REGEXP"
* >http://www.postgresql.org/docs/9.1/static/functions-matching.html#
* FUNCTIONS-POSIX-REGEXP</a></td>
* </tr>
* <tr>
* <td>{@link SQLDialect#SQLITE}</td>
* <td><code>[search] REGEXP [pattern]</code></td>
* <td>? This module has to be loaded explicitly</td>
* <td><a href="http://www.sqlite.org/lang_expr.html">http://www.sqlite.org/
* lang_expr.html</a></td>
* </tr>
* <tr>
* <td>{@link SQLDialect#SQLSERVER}</td>
* <td>-</td>
* <td>-</td>
* <td>-</td>
* </tr>
* <tr>
* <td>{@link SQLDialect#SYBASE}</td>
* <td><code>[search] REGEXP [pattern]</code></td>
* <td>Perl</td>
* <td><a href=
* "http://infocenter.sybase.com/help/topic/com.sybase.help.sqlanywhere.12.0.1/dbreference/like-regexp-similarto.html"
* >http://infocenter.sybase.com/help/topic/com.sybase.help.sqlanywhere.12.0
* .1/dbreference/like-regexp-similarto.html</a></td>
* </tr>
* </table>
*
* @see #likeRegex(String)
*/
@Support({ CUBRID, H2, HSQLDB, MYSQL, ORACLE, POSTGRES, SQLITE, SYBASE })
Condition likeRegex(String pattern);
/**
* Create a condition to regex-pattern-check this field against a pattern
* <p>
* See {@link #likeRegex(String)} for more details
*
* @see #likeRegex(String)
*/
@Support({ CUBRID, H2, HSQLDB, MYSQL, ORACLE, POSTGRES, SQLITE, SYBASE })
Condition likeRegex(Field<String> pattern);
/**
* Create a condition to pattern-check this field against a value
* <p>
@ -649,6 +785,26 @@ public interface Field<T> extends NamedTypeProviderQueryPart<T>, AliasProvider<F
@Support
Condition notLikeIgnoreCase(String value, char escape);
/**
* Create a condition to regex-pattern-check this field against a pattern
* <p>
* See {@link #likeRegex(String)} for more details
*
* @see #likeRegex(String)
*/
@Support({ CUBRID, H2, HSQLDB, MYSQL, ORACLE, POSTGRES, SQLITE, SYBASE })
Condition notLikeRegex(String pattern);
/**
* Create a condition to regex-pattern-check this field against a pattern
* <p>
* See {@link #likeRegex(String)} for more details
*
* @see #likeRegex(Field)
*/
@Support({ CUBRID, H2, HSQLDB, MYSQL, ORACLE, POSTGRES, SQLITE, SYBASE })
Condition notLikeRegex(Field<String> pattern);
/**
* Convenience method for {@link #like(String, char)} including proper
* adding of wildcards and escaping

View File

@ -361,6 +361,16 @@ abstract class AbstractField<T> extends AbstractNamedTypeProviderQueryPart<T> im
return new CompareCondition(this, nullSafe(value), Comparator.LIKE_IGNORE_CASE, escape);
}
@Override
public final Condition likeRegex(String pattern) {
return likeRegex(val(pattern, String.class));
}
@Override
public final Condition likeRegex(Field<String> pattern) {
return new RegexpLike(this, nullSafe(pattern));
}
@Override
public final Condition notLike(String value) {
return notLike(val(value, String.class));
@ -401,6 +411,16 @@ abstract class AbstractField<T> extends AbstractNamedTypeProviderQueryPart<T> im
return new CompareCondition(this, nullSafe(value), Comparator.NOT_LIKE_IGNORE_CASE, escape);
}
@Override
public final Condition notLikeRegex(String pattern) {
return likeRegex(pattern).not();
}
@Override
public final Condition notLikeRegex(Field<String> pattern) {
return likeRegex(pattern).not();
}
@Override
public final Condition contains(T value) {
return new Contains<T>(this, value);

View File

@ -0,0 +1,130 @@
/**
* Copyright (c) 2009-2012, Lukas Eder, lukas.eder@gmail.com
* All rights reserved.
*
* This software is licensed to you under the Apache License, Version 2.0
* (the "License"); You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* . Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* . Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* . Neither the name "jOOQ" nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package org.jooq.impl;
import java.util.List;
import org.jooq.Attachable;
import org.jooq.BindContext;
import org.jooq.Field;
import org.jooq.RenderContext;
/**
* @author Lukas Eder
*/
class RegexpLike extends AbstractCondition {
/**
* Generated UID
*/
private static final long serialVersionUID = 3162855665213654276L;
private final Field<?> search;
private final Field<String> pattern;
RegexpLike(Field<?> search, Field<String> pattern) {
this.search = search;
this.pattern = pattern;
}
@Override
public final void toSQL(RenderContext context) {
switch (context.getDialect()) {
// [#620] These databases are compatible with the MySQL syntax
case CUBRID:
case H2:
case MYSQL:
case SQLITE:
case SYBASE: {
context.sql(search)
.keyword(" regexp ")
.sql(pattern);
break;
}
// [#620] HSQLDB has its own syntax
case HSQLDB: {
// [#1570] TODO: Replace this by Factory.condition(String, QueryPart...)
context.sql(Factory.field("{regexp_matches}({0}, {1})", Boolean.class, search, pattern));
break;
}
// [#620] Postgres has its own syntax
case POSTGRES: {
// [#1570] TODO: Replace this by Factory.condition(String, QueryPart...)
context.sql(Factory.field("{0} ~ {1}", Boolean.class, search, pattern));
break;
}
// [#620] Oracle has its own syntax
case ORACLE: {
// [#1570] TODO: Replace this by Factory.condition(String, QueryPart...)
context.sql(Factory.field("{regexp_like}({0}, {1})", Boolean.class, search, pattern));
break;
}
// Render the SQL standard for those databases that do not support
// regular expressions
case ASE:
case DB2:
case DERBY:
case INGRES:
case SQLSERVER:
default: {
context.sql(search)
.keyword(" like_regex ")
.sql(pattern);
break;
}
}
}
@Override
public final void bind(BindContext context) {
context.bind(search).bind(pattern);
}
@Override
public final List<Attachable> getAttachables() {
return getAttachables(search, pattern);
}
}