[#816] Add support for Oracle Text functions

This commit is contained in:
Lukas Eder 2012-08-04 13:34:39 +02:00
parent 8070268def
commit eacb78dc51
6 changed files with 109 additions and 10 deletions

View File

@ -76,6 +76,8 @@ import static org.jooq.test.oracle.generatedclasses.test.udt.UAuthorType.countBo
import static org.jooq.test.oracle.generatedclasses.test.udt.UAuthorType.load;
import static org.jooq.test.oracle2.generatedclasses.Tables.DATE_AS_TIMESTAMP_T_976;
import static org.jooq.test.oracle2.generatedclasses.udt.DateAsTimestampT_976ObjectType.DATE_AS_TIMESTAMP_T_976_OBJECT_TYPE;
import static org.jooq.util.oracle.OracleFactory.contains;
import static org.jooq.util.oracle.OracleFactory.score;
import static org.jooq.util.oracle.OracleFactory.sysContext;
import java.math.BigDecimal;
@ -1294,4 +1296,21 @@ public class jOOQOracleTest extends jOOQAbstractTest<
assertEquals(result1, result2);
}
@Test
public void testOracleText() throws Exception {
// [#816] CONTAINS() tests
Result<Record> result1 =
create().select(TBook_TITLE(), score(2))
.from(TBook())
.where(contains(TBook_TITLE(), "Alq%").greaterThan(BigDecimal.ZERO))
.or(contains(TBook_TITLE(), "O%", 2).greaterThan(BigDecimal.ZERO))
.orderBy(TBook_ID())
.fetch();
assertEquals(1, result1.size());
assertEquals("O Alquimista", result1.getValue(0, TBook_TITLE()));
assertEquals(1, result1.getValue(0, score(2)).compareTo(BigDecimal.ZERO));
}
}

View File

@ -1751,4 +1751,6 @@ create or replace synonym v_author_synonym for v_author/
create or replace synonym p_unused_synonym for p_unused/
create or replace synonym t_author_synonym_synonym for t_author_synonym/
create or replace public synonym t_author_public_synonym for t_author/
create or replace public synonym t_author for t_author/
create or replace public synonym t_author for t_author/
CREATE INDEX i_book_title_context ON t_book(title) INDEXTYPE IS CTXSYS.CONTEXT/

View File

@ -97,3 +97,5 @@ INSERT INTO t_directory VALUES ( 1, null, 1, 'C:')/
INSERT INTO t_directory VALUES (24, 19, 1, 'lib')/
INSERT INTO t_directory VALUES (25, 24, 0, 'javaws.jar')/
INSERT INTO t_directory VALUES (26, 24, 0, 'rt.jar')/
ALTER INDEX i_book_title_context REBUILD/

View File

@ -56,6 +56,7 @@ import java.util.Map;
import org.jooq.impl.Factory;
import org.jooq.types.Interval;
import org.jooq.util.oracle.OracleFactory;
/**
* A field used in tables and conditions
@ -814,14 +815,17 @@ public interface Field<T> extends NamedTypeProviderQueryPart<T>, AliasProvider<F
* Note: This also works with numbers, for instance
* <code>val(1133).contains(13)</code>
* <p>
* If you're using {@link SQLDialect#POSTGRES}, then you can use this method also to express the "ARRAY contains" operator. For example:
* <code><pre>
* If you're using {@link SQLDialect#POSTGRES}, then you can use this method
* also to express the "ARRAY contains" operator. For example: <code><pre>
* // Use this expression
* val(new Integer[] { 1, 2, 3 }).contains(new Integer[] { 1, 2 })
*
* // ... to render this SQL
* ARRAY[1, 2, 3] @> ARRAY[1, 2]
* </pre></code>
* <p>
* Note, this does not correspond to the Oracle Text <code>CONTAINS()</code>
* function. Refer to {@link OracleFactory#contains(Field, String)} instead.
*
* @see Factory#escape(String, char)
* @see #like(String, char)
@ -838,14 +842,17 @@ public interface Field<T> extends NamedTypeProviderQueryPart<T>, AliasProvider<F
* Note: This also works with numbers, for instance
* <code>val(1133).contains(13)</code>
* <p>
* If you're using {@link SQLDialect#POSTGRES}, then you can use this method also to express the "ARRAY contains" operator. For example:
* <code><pre>
* If you're using {@link SQLDialect#POSTGRES}, then you can use this method
* also to express the "ARRAY contains" operator. For example: <code><pre>
* // Use this expression
* val(new Integer[] { 1, 2, 3 }).contains(new Integer[] { 1, 2 })
*
* // ... to render this SQL
* ARRAY[1, 2, 3] @> ARRAY[1, 2]
* </pre></code>
* <p>
* Note, this does not correspond to the Oracle Text <code>CONTAINS()</code>
* function. Refer to {@link OracleFactory#contains(Field, String)} instead.
*
* @see Factory#escape(Field, char)
* @see #like(Field, char)

View File

@ -1943,7 +1943,7 @@ public class Factory implements FactoryOperations {
* {@inheritDoc}
*/
@Override
public <R extends Record> MergeKeyStep<R> mergeInto(Table<R> table, Field<?>... fields) {
public final <R extends Record> MergeKeyStep<R> mergeInto(Table<R> table, Field<?>... fields) {
return mergeInto(table, Arrays.asList(fields));
}
@ -1951,7 +1951,7 @@ public class Factory implements FactoryOperations {
* {@inheritDoc}
*/
@Override
public <R extends Record> MergeKeyStep<R> mergeInto(Table<R> table, Collection<? extends Field<?>> fields) {
public final <R extends Record> MergeKeyStep<R> mergeInto(Table<R> table, Collection<? extends Field<?>> fields) {
return new MergeImpl<R>(this, table, fields);
}
@ -5896,14 +5896,14 @@ public class Factory implements FactoryOperations {
/**
* Null-safety of a field
*/
static <T> Field<T> nullSafe(Field<T> field) {
protected static <T> Field<T> nullSafe(Field<T> field) {
return field == null ? val((T) null) : field;
}
/**
* Null-safety of a field
*/
static Field<?>[] nullSafe(Field<?>... fields) {
protected static Field<?>[] nullSafe(Field<?>... fields) {
Field<?>[] result = new Field<?>[fields.length];
for (int i = 0; i < fields.length; i++) {
@ -5917,7 +5917,7 @@ public class Factory implements FactoryOperations {
* Get a default data type if a field is null
*/
@SuppressWarnings("unchecked")
static <T> DataType<T> nullSafeDataType(Field<T> field) {
protected static <T> DataType<T> nullSafeDataType(Field<T> field) {
return (DataType<T>) (field == null ? SQLDataType.OTHER : field.getDataType());
}

View File

@ -35,6 +35,9 @@
*/
package org.jooq.util.oracle;
import static org.jooq.SQLDialect.ORACLE;
import java.math.BigDecimal;
import java.sql.Connection;
import javax.sql.DataSource;
@ -42,6 +45,7 @@ import javax.sql.DataSource;
import org.jooq.Field;
import org.jooq.SQLDialect;
import org.jooq.SchemaMapping;
import org.jooq.Support;
import org.jooq.conf.Settings;
import org.jooq.impl.Factory;
import org.jooq.impl.SQLDataType;
@ -148,6 +152,7 @@ public class OracleFactory extends Factory {
/**
* Retrieve the Oracle-specific <code>ROWNUM</code> pseudo-field
*/
@Support(ORACLE)
public static Field<Integer> rownum() {
return field("rownum", Integer.class);
}
@ -155,6 +160,7 @@ public class OracleFactory extends Factory {
/**
* Retrieve the Oracle-specific <code>ROWID</code> pseudo-field
*/
@Support(ORACLE)
public static Field<String> rowid() {
return field("rowid", String.class);
}
@ -166,6 +172,7 @@ public class OracleFactory extends Factory {
/**
* The Oracle-specific <code>SYS_CONTEXT</code> function
*/
@Support(ORACLE)
public static Field<String> sysContext(String namespace, String parameter) {
return function("sys_context", SQLDataType.VARCHAR, val(namespace), val(parameter));
}
@ -173,7 +180,69 @@ public class OracleFactory extends Factory {
/**
* The Oracle-specific <code>SYS_CONTEXT</code> function
*/
@Support(ORACLE)
public static Field<String> sysContext(String namespace, String parameter, int length) {
return function("sys_context", SQLDataType.VARCHAR, val(namespace), val(parameter), val(length));
}
// -------------------------------------------------------------------------
// Oracle Text functions
// -------------------------------------------------------------------------
/**
* The Oracle-Text specific <code>CONTAINS</code> function
*/
@Support(ORACLE)
public static Field<BigDecimal> contains(Field<String> field, String query) {
return field("{contains}({0}, {1})", SQLDataType.NUMERIC, nullSafe(field), val(query));
}
/**
* The Oracle-Text specific <code>CONTAINS</code> function
*/
@Support(ORACLE)
public static Field<BigDecimal> contains(Field<String> field, String query, int label) {
return field("{contains}({0}, {1}, {2})", SQLDataType.NUMERIC, nullSafe(field), val(query), inline(label));
}
/**
* The Oracle-Text specific <code>MATCHES</code> function
*/
@Support(ORACLE)
public static Field<BigDecimal> matches(Field<String> field, String query) {
return field("{matches}({0}, {1})", SQLDataType.NUMERIC, nullSafe(field), val(query));
}
/**
* The Oracle-Text specific <code>CONTAINS</code> function
*/
@Support(ORACLE)
public static Field<BigDecimal> matches(Field<String> field, String query, int label) {
return field("{matches}({0}, {1}, {2})", SQLDataType.NUMERIC, nullSafe(field), val(query), inline(label));
}
/**
* The Oracle-Text specific <code>CATSEARCH</code> function
*/
@Support(ORACLE)
public static Field<BigDecimal> catsearch(Field<String> field, String textQuery, String structuredQuery) {
return field("{catsearch}({0}, {1}, {2})", SQLDataType.NUMERIC, nullSafe(field), val(textQuery), val(structuredQuery));
}
/**
* The Oracle-Text specific <code>SCORE</code> function
*/
@Support(ORACLE)
public static Field<BigDecimal> score(int label) {
return field("{score}({0})", SQLDataType.NUMERIC, inline(label));
}
/**
* The Oracle-Text specific <code>MATCH_SCORE</code> function
*/
@Support(ORACLE)
public static Field<BigDecimal> matchScore(int label) {
return field("{match_score}({0})", SQLDataType.NUMERIC, inline(label));
}
}