diff --git a/jOOQ/src/main/java/org/jooq/impl/Abs.java b/jOOQ/src/main/java/org/jooq/impl/Abs.java index 7f7a9880d5..e8f3276369 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Abs.java +++ b/jOOQ/src/main/java/org/jooq/impl/Abs.java @@ -67,7 +67,7 @@ extends Abs( Field number ) { - super(N_ABS, allNotNull(INTEGER, number)); + super(N_ABS, allNotNull((DataType) INTEGER, number)); this.number = nullSafeNotNull(number, INTEGER); } diff --git a/jOOQ/src/main/java/org/jooq/impl/Coalesce.java b/jOOQ/src/main/java/org/jooq/impl/Coalesce.java index d7791b7f2e..b373daa4be 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Coalesce.java +++ b/jOOQ/src/main/java/org/jooq/impl/Coalesce.java @@ -58,7 +58,7 @@ final class Coalesce extends AbstractField { @SuppressWarnings({ "unchecked", "rawtypes" }) Coalesce(Field[] fields) { - super(N_COALESCE, anyNotNull((DataType) OTHER, fields[0], fields)); + super(N_COALESCE, anyNotNull((DataType) OTHER, fields)); this.fields = (Field[]) fields; } diff --git a/jOOQ/src/main/java/org/jooq/impl/DSL.java b/jOOQ/src/main/java/org/jooq/impl/DSL.java index 32830930d3..568d5f96fb 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DSL.java +++ b/jOOQ/src/main/java/org/jooq/impl/DSL.java @@ -15007,6 +15007,114 @@ public class DSL { return new Ltrim(string); } + /** + * The POSITION function. + */ + @NotNull + @Support + public static Field position(String in, String search, int startIndex) { + return new Position(Tools.field(in), Tools.field(search), Tools.field(startIndex)); + } + + /** + * The POSITION function. + */ + @NotNull + @Support + public static Field position(String in, String search, Field startIndex) { + return new Position(Tools.field(in), Tools.field(search), startIndex); + } + + /** + * The POSITION function. + */ + @NotNull + @Support + public static Field position(String in, Field search, int startIndex) { + return new Position(Tools.field(in), search, Tools.field(startIndex)); + } + + /** + * The POSITION function. + */ + @NotNull + @Support + public static Field position(String in, Field search, Field startIndex) { + return new Position(Tools.field(in), search, startIndex); + } + + /** + * The POSITION function. + */ + @NotNull + @Support + public static Field position(Field in, String search, int startIndex) { + return new Position(in, Tools.field(search), Tools.field(startIndex)); + } + + /** + * The POSITION function. + */ + @NotNull + @Support + public static Field position(Field in, String search, Field startIndex) { + return new Position(in, Tools.field(search), startIndex); + } + + /** + * The POSITION function. + */ + @NotNull + @Support + public static Field position(Field in, Field search, int startIndex) { + return new Position(in, search, Tools.field(startIndex)); + } + + /** + * The POSITION function. + */ + @NotNull + @Support + public static Field position(Field in, Field search, Field startIndex) { + return new Position(in, search, startIndex); + } + + /** + * The POSITION function. + */ + @NotNull + @Support + public static Field position(String in, String search) { + return new Position(Tools.field(in), Tools.field(search)); + } + + /** + * The POSITION function. + */ + @NotNull + @Support + public static Field position(String in, Field search) { + return new Position(Tools.field(in), search); + } + + /** + * The POSITION function. + */ + @NotNull + @Support + public static Field position(Field in, String search) { + return new Position(in, Tools.field(search)); + } + + /** + * The POSITION function. + */ + @NotNull + @Support + public static Field position(Field in, Field search) { + return new Position(in, search); + } + /** * The REPEAT function. */ @@ -15679,153 +15787,6 @@ public class DSL { return new SplitPart(field, Tools.nullSafe(delimiter), Tools.nullSafe(n)); } - /** - * Get the position(in, search) function. - * - * @see #position(Field, Field) - */ - @NotNull - @Support - public static Field position(String in, String search) { - return position(Tools.field(in), Tools.field(search)); - } - - /** - * Get the position(in, search) function. - * - * @see #position(Field, Field) - */ - @NotNull - @Support - public static Field position(String in, Field search) { - return position(Tools.field(in), Tools.nullSafe(search)); - } - - /** - * Get the position(in, search) function. - * - * @see #position(Field, Field) - */ - @NotNull - @Support - public static Field position(Field in, String search) { - return position(Tools.nullSafe(in), Tools.field(search)); - } - - /** - * Get the position(in, search) function. - *

- * This renders the position or any equivalent function: - *

position([search] in [in]) or
-     * locate([in], [search]) or
-     * locate([search], [in]) or
-     * instr([in], [search]) or
-     * charindex([search], [in])
- */ - @NotNull - @Support - public static Field position(Field in, Field search) { - return new Position(Tools.nullSafe(search), Tools.nullSafe(in)); - } - - /** - * Get the position(in, search, startindex) function. - * - * @see #position(Field, Field, Field) - */ - @NotNull - @Support - public static Field position(String in, String search, int startIndex) { - return position(Tools.field(in), Tools.field(search), Tools.field(startIndex)); - } - - /** - * Get the position(in, search, startindex) function. - * - * @see #position(Field, Field, Field) - */ - @NotNull - @Support - public static Field position(String in, Field search, int startIndex) { - return position(Tools.field(in), Tools.nullSafe(search), Tools.field(startIndex)); - } - - /** - * Get the position(in, search, startindex) function. - * - * @see #position(Field, Field) - */ - @NotNull - @Support - public static Field position(Field in, String search, int startIndex) { - return position(Tools.nullSafe(in), Tools.field(search), Tools.field(startIndex)); - } - - /** - * Get the position(in, search, startindex) function. - *

- * This renders the position or any equivalent function: - *

position([search] in [in]) or
-     * locate([in], [search]) or
-     * locate([search], [in]) or
-     * instr([in], [search]) or
-     * charindex([search], [in])
- */ - @NotNull - @Support - public static Field position(Field in, Field search, int startIndex) { - return position(Tools.nullSafe(search), Tools.nullSafe(in), Tools.field(startIndex)); - } - - /** - * Get the position(in, search, startindex) function. - * - * @see #position(Field, Field, Field) - */ - @NotNull - @Support - public static Field position(String in, String search, Field startIndex) { - return position(Tools.field(in), Tools.field(search), Tools.nullSafe(startIndex)); - } - - /** - * Get the position(in, search, startindex) function. - * - * @see #position(Field, Field, Field) - */ - @NotNull - @Support - public static Field position(String in, Field search, Field startIndex) { - return position(Tools.field(in), Tools.nullSafe(search), Tools.nullSafe(startIndex)); - } - - /** - * Get the position(in, search, startindex) function. - * - * @see #position(Field, Field) - */ - @NotNull - @Support - public static Field position(Field in, String search, Field startIndex) { - return position(Tools.nullSafe(in), Tools.field(search), Tools.nullSafe(startIndex)); - } - - /** - * Get the position(in, search, startindex) function. - *

- * This renders the position or any equivalent function: - *

position([search] in [in]) or
-     * locate([in], [search]) or
-     * locate([search], [in]) or
-     * instr([in], [search]) or
-     * charindex([search], [in])
- */ - @NotNull - @Support - public static Field position(Field in, Field search, Field startIndex) { - return new Position(Tools.nullSafe(search), Tools.nullSafe(in), Tools.nullSafe(startIndex)); - } - /** * Get the overlay(in, placing, startIndex) function. */ diff --git a/jOOQ/src/main/java/org/jooq/impl/Position.java b/jOOQ/src/main/java/org/jooq/impl/Position.java index 7fc90a764b..316997f0d0 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Position.java +++ b/jOOQ/src/main/java/org/jooq/impl/Position.java @@ -35,48 +35,69 @@ * * */ - package org.jooq.impl; -import static org.jooq.impl.DSL.one; -import static org.jooq.impl.Internal.iadd; -import static org.jooq.impl.Internal.isub; -import static org.jooq.impl.Keywords.K_IN; -import static org.jooq.impl.Names.N_CHARINDEX; -import static org.jooq.impl.Names.N_INSTR; -import static org.jooq.impl.Names.N_LOCATE; -import static org.jooq.impl.Names.N_POSITION; -import static org.jooq.impl.SQLDataType.INTEGER; +import static org.jooq.impl.DSL.*; +import static org.jooq.impl.Internal.*; +import static org.jooq.impl.Keywords.*; +import static org.jooq.impl.Names.*; +import static org.jooq.impl.SQLDataType.*; +import static org.jooq.impl.Tools.*; +import static org.jooq.impl.Tools.BooleanDataKey.*; +import static org.jooq.SQLDialect.*; -import org.jooq.Context; -import org.jooq.Field; +import org.jooq.*; +import org.jooq.impl.*; + +import java.util.*; /** - * @author Lukas Eder + * The POSITION statement. */ -final class Position extends AbstractField { +@SuppressWarnings({ "rawtypes", "unchecked", "unused" }) +final class Position +extends + AbstractField +{ - private static final long serialVersionUID = 3544690069533526544L; + private static final long serialVersionUID = 1L; - private final Field search; private final Field in; + private final Field search; private final Field startIndex; - Position(Field search, Field in) { - this(search, in, null); + Position( + Field in, + Field search + ) { + super(N_POSITION, allNotNull(INTEGER, in, search)); + + this.in = nullSafeNotNull(in, VARCHAR); + this.search = nullSafeNotNull(search, VARCHAR); + this.startIndex = null; } - Position(Field search, Field in, Field startIndex) { - super(N_POSITION, INTEGER); + Position( + Field in, + Field search, + Field startIndex + ) { + super(N_POSITION, allNotNull(INTEGER, in, search, startIndex)); - this.search = search; - this.in = in; - this.startIndex = startIndex; + this.in = nullSafeNotNull(in, VARCHAR); + this.search = nullSafeNotNull(search, VARCHAR); + this.startIndex = nullSafeNotNull(startIndex, INTEGER); } + // ------------------------------------------------------------------------- + // XXX: QueryPart API + // ------------------------------------------------------------------------- + + + @Override public final void accept(Context ctx) { - if (startIndex != null) + if (startIndex != null) { switch (ctx.family()) { @@ -94,7 +115,8 @@ final class Position extends AbstractField { ctx.visit(iadd(DSL.position(DSL.substring(in, startIndex), search), isub(startIndex, one()))); break; } - else + } + else { switch (ctx.family()) { @@ -127,5 +149,8 @@ final class Position extends AbstractField { ctx.visit(N_POSITION).sql('(').visit(search).sql(' ').visit(K_IN).sql(' ').visit(in).sql(')'); break; } + } } + + } diff --git a/jOOQ/src/main/java/org/jooq/impl/Tools.java b/jOOQ/src/main/java/org/jooq/impl/Tools.java index ed7d424995..2f36ec80f8 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Tools.java +++ b/jOOQ/src/main/java/org/jooq/impl/Tools.java @@ -1424,7 +1424,7 @@ final class Tools { return result; } - static ReferenceImpl aliasedKey(ForeignKey key, Table child, Table parent) { + static final ReferenceImpl aliasedKey(ForeignKey key, Table child, Table parent) { // [#10603] [#5050] TODO: Solve aliasing constraints more generically return new ReferenceImpl<>( @@ -1559,7 +1559,7 @@ final class Tools { * [#461] [#473] [#2597] [#8234] Some internals need a cast only if necessary. */ @SuppressWarnings("unchecked") - static Field[] castAllIfNeeded(Field[] fields, Class type) { + static final Field[] castAllIfNeeded(Field[] fields, Class type) { Field[] castFields = new Field[fields.length]; for (int i = 0; i < fields.length; i++) @@ -5167,11 +5167,11 @@ final class Tools { ctx.sql(' ').visit(K_COLLATE).sql(' ').visit(type.collation()); } - static boolean storedEnumType(DataType enumType) { + static final boolean storedEnumType(DataType enumType) { return enumConstants(enumType)[0].getSchema() != null; } - private static EnumType[] enumConstants(DataType type) { + private static final EnumType[] enumConstants(DataType type) { EnumType[] enums = type.getType().getEnumConstants(); if (enums == null) @@ -5200,11 +5200,11 @@ final class Tools { - static Supplier blocking(Supplier supplier) { + static final Supplier blocking(Supplier supplier) { return blocking(supplier, false); } - static Supplier blocking(Supplier supplier, boolean threadLocal) { + static final Supplier blocking(Supplier supplier, boolean threadLocal) { // [#5377] In ThreadLocal contexts (e.g. when using ThreadLocalTransactionprovider), // no ManagedBlocker is needed as we're guaranteed by API contract to always @@ -5239,7 +5239,7 @@ final class Tools { } - static String[] enumLiterals(Class type) { + static final String[] enumLiterals(Class type) { EnumType[] values = enums(type); String[] result = new String[values.length]; @@ -5249,7 +5249,7 @@ final class Tools { return result; } - static E[] enums(Class type) { + static final E[] enums(Class type) { // Java implementation if (Enum.class.isAssignableFrom(type)) { @@ -5453,7 +5453,7 @@ final class Tools { return result; } - static Field tableField(Table table, Object field) { + static final Field tableField(Table table, Object field) { if (field instanceof Field) return (Field) field; else if (field instanceof Name) @@ -5899,19 +5899,26 @@ final class Tools { return result; } - static final DataType dataType(Field field) { + private static final DataType dataType(Field field) { return dataType(OTHER, field); } - static final DataType dataType(DataType defaultType, Field field) { - return field == null ? defaultType : field.getDataType(); + @SuppressWarnings("unchecked") + private static final DataType dataType(DataType defaultType, Field field) { + return field == null + ? defaultType + : field.getType() != defaultType.getType() + ? defaultType.nullable(field.getDataType().nullable()) + : (DataType) field.getDataType(); } - static final DataType allNotNull(DataType defaultType, Field f1, Field f2) { - if (f1 == null) - return defaultType.null_(); + static final DataType allNotNull(DataType defaultType, Field f1) { + return dataType(defaultType, f1); + } + + static final DataType allNotNull(DataType defaultType, Field f1, Field f2) { + DataType result = dataType(defaultType, f1); - DataType result = f1.getDataType(); if (result.nullable()) return result; else if (dataType(f2).nullable()) @@ -5920,23 +5927,38 @@ final class Tools { return result; } - static final DataType allNotNull(DataType defaultType, Field f1, Field... fields) { - if (f1 == null) - return defaultType.null_(); + static final DataType allNotNull(DataType defaultType, Field f1, Field f2, Field f3) { + DataType result = dataType(defaultType, f1); - DataType result = f1.getDataType(); if (result.nullable()) return result; + else if (dataType(f2).nullable()) + return result.null_(); + else if (dataType(f3).nullable()) + return result.null_(); + else + return result; + } - for (int i = 0; i < fields.length; i++) - if (dataType(fields[i]).nullable()) - return result.null_(); + static final DataType allNotNull(DataType defaultType, Field... fields) { + DataType result = dataType(defaultType, isEmpty(fields) ? null : fields[0]); + + if (result.nullable()) + return result; + else + for (Field field : fields) + if (dataType(field).nullable()) + return result.null_(); return result; } - static final DataType anyNotNull(DataType defaultType, Field f1, Field f2) { - DataType result = f1 == null ? defaultType : f1.getDataType(); + static final DataType anyNotNull(DataType defaultType, Field f1) { + return dataType(defaultType, f1); + } + + static final DataType anyNotNull(DataType defaultType, Field f1, Field f2) { + DataType result = dataType(defaultType, f1); if (!result.nullable()) return result; @@ -5946,15 +5968,28 @@ final class Tools { return result; } - static final DataType anyNotNull(DataType defaultType, Field f1, Field... fields) { - DataType result = f1 == null ? defaultType : f1.getDataType(); + static final DataType anyNotNull(DataType defaultType, Field f1, Field f2, Field f3) { + DataType result = dataType(defaultType, f1); if (!result.nullable()) return result; + else if (!dataType(f2).nullable()) + return result.notNull(); + else if (!dataType(f3).nullable()) + return result.notNull(); + else + return result; + } - for (int i = 0; i < fields.length; i++) - if (!dataType(fields[i]).nullable()) - return result.notNull(); + static final DataType anyNotNull(DataType defaultType, Field... fields) { + DataType result = dataType(defaultType, isEmpty(fields) ? null : fields[0]); + + if (!result.nullable()) + return result; + else + for (Field field : fields) + if (!dataType(field).nullable()) + return result.notNull(); return result; }