diff --git a/jOOQ/src/main/java/org/jooq/impl/BitLength.java b/jOOQ/src/main/java/org/jooq/impl/BitLength.java new file mode 100644 index 0000000000..1e30d34e14 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/BitLength.java @@ -0,0 +1,108 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Other licenses: + * ----------------------------------------------------------------------------- + * Commercial licenses for this work are available. These replace the above + * ASL 2.0 and offer limited warranties, support, maintenance, and commercial + * database integrations. + * + * For more information, please visit: http://www.jooq.org/licenses + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + */ +package org.jooq.impl; + +import static org.jooq.impl.DSL.function; +import static org.jooq.impl.DSL.inline; +import static org.jooq.impl.Names.N_BIT_LENGTH; +import static org.jooq.impl.Names.N_CHAR_LENGTH; +import static org.jooq.impl.Names.N_DATALENGTH; +import static org.jooq.impl.Names.N_LEN; +import static org.jooq.impl.Names.N_LENGTH; +import static org.jooq.impl.Names.N_LENGTHB; +import static org.jooq.impl.SQLDataType.INTEGER; +import static org.jooq.impl.SQLDataType.VARCHAR; +import static org.jooq.impl.Tools.nullSafeNotNull; + +import org.jooq.Context; +import org.jooq.Field; + +/** + * @author Lukas Eder + */ +final class BitLength extends AbstractField { + + /** + * Generated UID + */ + private static final long serialVersionUID = 1484652553287331042L; + + private final Field field; + + BitLength(Field field) { + super(N_BIT_LENGTH, INTEGER.nullable(field == null || field.getDataType().nullable())); + + this.field = nullSafeNotNull(field, VARCHAR); + } + + @Override + public void accept(Context ctx) { + switch (ctx.family()) { + + + + + + + + + + + + + + + + + + + + + + + case DERBY: + case SQLITE: + ctx.visit(inline(8).times(function(N_LENGTH, getDataType(), field))); + break; + + default: + ctx.visit(function(N_BIT_LENGTH, getDataType(), field)); + break; + } + } +} diff --git a/jOOQ/src/main/java/org/jooq/impl/CharLength.java b/jOOQ/src/main/java/org/jooq/impl/CharLength.java new file mode 100644 index 0000000000..835c108e92 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/CharLength.java @@ -0,0 +1,97 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Other licenses: + * ----------------------------------------------------------------------------- + * Commercial licenses for this work are available. These replace the above + * ASL 2.0 and offer limited warranties, support, maintenance, and commercial + * database integrations. + * + * For more information, please visit: http://www.jooq.org/licenses + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + */ +package org.jooq.impl; + +import static org.jooq.impl.DSL.function; +import static org.jooq.impl.Names.N_BIT_LENGTH; +import static org.jooq.impl.Names.N_CHAR_LENGTH; +import static org.jooq.impl.Names.N_LEN; +import static org.jooq.impl.Names.N_LENGTH; +import static org.jooq.impl.SQLDataType.INTEGER; +import static org.jooq.impl.SQLDataType.VARCHAR; +import static org.jooq.impl.Tools.nullSafeNotNull; + +import org.jooq.Context; +import org.jooq.Field; + +/** + * @author Lukas Eder + */ +final class CharLength extends AbstractField { + + /** + * Generated UID + */ + private static final long serialVersionUID = 1484652553287331042L; + + private final Field field; + + CharLength(Field field) { + super(N_CHAR_LENGTH, INTEGER.nullable(field == null || field.getDataType().nullable())); + + this.field = nullSafeNotNull(field, VARCHAR); + } + + @Override + public void accept(Context ctx) { + switch (ctx.family()) { + + + + + + + + + + + + + + + case DERBY: + case SQLITE: + ctx.visit(function(N_LENGTH, getDataType(), field)); + break; + + default: + ctx.visit(function(N_CHAR_LENGTH, getDataType(), field)); + break; + } + } +} diff --git a/jOOQ/src/main/java/org/jooq/impl/DSL.java b/jOOQ/src/main/java/org/jooq/impl/DSL.java index 41a160f498..44bf6c078e 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DSL.java +++ b/jOOQ/src/main/java/org/jooq/impl/DSL.java @@ -15224,7 +15224,7 @@ public class DSL { @NotNull @Support({ CUBRID, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE }) public static Field repeat(String field, Field count) { - return repeat(Tools.field(field), Tools.nullSafe(count)); + return repeat(Tools.field(field), count); } /** @@ -15235,7 +15235,7 @@ public class DSL { @NotNull @Support({ CUBRID, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE }) public static Field repeat(Field field, int count) { - return repeat(Tools.nullSafe(field), Tools.field(count)); + return repeat(field, Tools.field(count)); } /** @@ -15254,7 +15254,7 @@ public class DSL { @NotNull @Support({ CUBRID, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE }) public static Field repeat(Field field, Field count) { - return new Repeat(Tools.nullSafe(field), Tools.nullSafe(count)); + return new Repeat(field, count); } /** @@ -15269,7 +15269,7 @@ public class DSL { @NotNull @Support({ CUBRID, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE }) public static Field space(int value) { - return space(val(value)); + return space(Tools.field(value)); } /** @@ -15284,7 +15284,7 @@ public class DSL { @NotNull @Support({ CUBRID, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE }) public static Field space(Field value) { - return new Space(Tools.nullSafe(value)); + return new Space(value); } /** @@ -15302,7 +15302,7 @@ public class DSL { @NotNull @Support({ CUBRID, HSQLDB, MARIADB, MYSQL, POSTGRES }) public static Field reverse(Field field) { - return new Reverse(Tools.nullSafe(field)); + return new Reverse(field); } /** @@ -15821,7 +15821,7 @@ public class DSL { @NotNull @Support public static Field left(String field, Field length) { - return left(Tools.field(field), Tools.nullSafe(length)); + return left(Tools.field(field), length); } /** @@ -15835,7 +15835,7 @@ public class DSL { @NotNull @Support public static Field left(Field field, int length) { - return left(Tools.nullSafe(field), Tools.field(length)); + return left(field, Tools.field(length)); } /** @@ -15877,7 +15877,7 @@ public class DSL { @NotNull @Support public static Field right(String field, Field length) { - return right(Tools.field(field), Tools.nullSafe(length)); + return right(Tools.field(field), length); } /** @@ -15891,7 +15891,7 @@ public class DSL { @NotNull @Support public static Field right(Field field, int length) { - return right(Tools.nullSafe(field), Tools.field(length)); + return right(field, Tools.field(length)); } /** @@ -15951,7 +15951,7 @@ public class DSL { @NotNull @Support public static Field charLength(Field field) { - return new DefaultAggregateFunction<>(Term.CHAR_LENGTH, INTEGER.nullable(Tools.nullSafeDataType(field).nullable()), Tools.nullSafe(field)); + return new CharLength(field); } /** @@ -15973,7 +15973,7 @@ public class DSL { @NotNull @Support public static Field bitLength(Field field) { - return new DefaultAggregateFunction<>(Term.BIT_LENGTH, INTEGER.nullable(Tools.nullSafeDataType(field).nullable()), Tools.nullSafe(field)); + return new BitLength(field); } /** @@ -15995,7 +15995,7 @@ public class DSL { @NotNull @Support public static Field octetLength(Field field) { - return new DefaultAggregateFunction<>(Term.OCTET_LENGTH, INTEGER.nullable(Tools.nullSafeDataType(field).nullable()), Tools.nullSafe(field)); + return new OctetLength(field); } // ------------------------------------------------------------------------ diff --git a/jOOQ/src/main/java/org/jooq/impl/Left.java b/jOOQ/src/main/java/org/jooq/impl/Left.java index 8e2807b229..65c99bdee3 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Left.java +++ b/jOOQ/src/main/java/org/jooq/impl/Left.java @@ -39,8 +39,10 @@ package org.jooq.impl; import static org.jooq.impl.DSL.inline; import static org.jooq.impl.Names.N_LEFT; +import static org.jooq.impl.SQLDataType.INTEGER; import static org.jooq.impl.SQLDataType.VARCHAR; import static org.jooq.impl.Tools.allNotNull; +import static org.jooq.impl.Tools.nullSafeNotNull; import org.jooq.Context; import org.jooq.Field; @@ -61,8 +63,8 @@ final class Left extends AbstractField { Left(Field field, Field length) { super(N_LEFT, allNotNull(VARCHAR, field, length)); - this.field = field; - this.length = length; + this.field = nullSafeNotNull(field, VARCHAR); + this.length = nullSafeNotNull(length, INTEGER); } @Override diff --git a/jOOQ/src/main/java/org/jooq/impl/Names.java b/jOOQ/src/main/java/org/jooq/impl/Names.java index 5dad012189..b458fbe2e0 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Names.java +++ b/jOOQ/src/main/java/org/jooq/impl/Names.java @@ -68,6 +68,7 @@ final class Names { static final Name N_ATAN = unquotedName("atan"); static final Name N_ATN = unquotedName("atn"); static final Name N_BIT_COUNT = unquotedName("bit_count"); + static final Name N_BIT_LENGTH = unquotedName("bit_length"); static final Name N_BOOL_AND = unquotedName("bool_and"); static final Name N_BOOL_OR = unquotedName("bool_or"); static final Name N_CARDINALITY = unquotedName("cardinality"); @@ -75,6 +76,7 @@ final class Names { static final Name N_CAST = unquotedName("cast"); static final Name N_CEIL = unquotedName("ceil"); static final Name N_CEILING = unquotedName("ceiling"); + static final Name N_CHAR_LENGTH = unquotedName("char_length"); static final Name N_CHARINDEX = unquotedName("charindex"); static final Name N_CHOOSE = unquotedName("choose"); static final Name N_CLNG = unquotedName("clng"); @@ -96,6 +98,7 @@ final class Names { static final Name N_CURRENT_USER = unquotedName("current_user"); static final Name N_CURRENTUSER = unquotedName("currentuser"); static final Name N_CURRVAL = unquotedName("currval"); + static final Name N_DATALENGTH = unquotedName("datalength"); static final Name N_DATE_ADD = unquotedName("date_add"); static final Name N_DATE_DIFF = unquotedName("date_diff"); static final Name N_DATE_TRUNC = unquotedName("date_trunc"); @@ -157,6 +160,7 @@ final class Names { static final Name N_LEFT = unquotedName("left"); static final Name N_LEN = unquotedName("len"); static final Name N_LENGTH = unquotedName("length"); + static final Name N_LENGTHB = unquotedName("lengthb"); static final Name N_LIST = unquotedName("list"); static final Name N_LISTAGG = unquotedName("listagg"); static final Name N_LN = unquotedName("ln"); @@ -178,6 +182,7 @@ final class Names { static final Name N_NUMTODSINTERVAL = unquotedName("numtodsinterval"); static final Name N_NVL = unquotedName("nvl"); static final Name N_NVL2 = unquotedName("nvl2"); + static final Name N_OCTET_LENGTH = unquotedName("octet_length"); static final Name N_OPENJSON = unquotedName("openjson"); static final Name N_OPENXML = unquotedName("openxml"); static final Name N_OVERLAY = unquotedName("overlay"); diff --git a/jOOQ/src/main/java/org/jooq/impl/OctetLength.java b/jOOQ/src/main/java/org/jooq/impl/OctetLength.java new file mode 100644 index 0000000000..e4f96aa020 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/OctetLength.java @@ -0,0 +1,104 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Other licenses: + * ----------------------------------------------------------------------------- + * Commercial licenses for this work are available. These replace the above + * ASL 2.0 and offer limited warranties, support, maintenance, and commercial + * database integrations. + * + * For more information, please visit: http://www.jooq.org/licenses + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + */ +package org.jooq.impl; + +import static org.jooq.impl.DSL.function; +import static org.jooq.impl.DSL.inline; +import static org.jooq.impl.Names.N_BIT_LENGTH; +import static org.jooq.impl.Names.N_CHAR_LENGTH; +import static org.jooq.impl.Names.N_DATALENGTH; +import static org.jooq.impl.Names.N_LEN; +import static org.jooq.impl.Names.N_LENGTH; +import static org.jooq.impl.Names.N_LENGTHB; +import static org.jooq.impl.Names.N_OCTET_LENGTH; +import static org.jooq.impl.SQLDataType.INTEGER; +import static org.jooq.impl.SQLDataType.VARCHAR; +import static org.jooq.impl.Tools.nullSafeNotNull; + +import org.jooq.Context; +import org.jooq.Field; + +/** + * @author Lukas Eder + */ +final class OctetLength extends AbstractField { + + /** + * Generated UID + */ + private static final long serialVersionUID = 1484652553287331042L; + + private final Field field; + + OctetLength(Field field) { + super(N_BIT_LENGTH, INTEGER.nullable(field == null || field.getDataType().nullable())); + + this.field = nullSafeNotNull(field, VARCHAR); + } + + @Override + public void accept(Context ctx) { + switch (ctx.family()) { + + + + + + + + + + + + + + + + + + case DERBY: + case SQLITE: + ctx.visit(function(N_LENGTH, getDataType(), field)); + break; + + default: + ctx.visit(function(N_OCTET_LENGTH, getDataType(), field)); + break; + } + } +} diff --git a/jOOQ/src/main/java/org/jooq/impl/Repeat.java b/jOOQ/src/main/java/org/jooq/impl/Repeat.java index a39e8df702..4e0b094a9d 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Repeat.java +++ b/jOOQ/src/main/java/org/jooq/impl/Repeat.java @@ -43,8 +43,10 @@ import static org.jooq.impl.Names.N_REPEAT; import static org.jooq.impl.Names.N_REPLACE; import static org.jooq.impl.Names.N_REPLICATE; import static org.jooq.impl.Names.N_ZEROBLOB; +import static org.jooq.impl.SQLDataType.INTEGER; import static org.jooq.impl.SQLDataType.VARCHAR; import static org.jooq.impl.Tools.allNotNull; +import static org.jooq.impl.Tools.nullSafeNotNull; import org.jooq.Context; import org.jooq.Field; @@ -65,8 +67,8 @@ final class Repeat extends AbstractField { Repeat(Field string, Field count) { super(N_REPEAT, allNotNull(VARCHAR, string, count)); - this.string = string; - this.count = count; + this.string = nullSafeNotNull(string, VARCHAR); + this.count = nullSafeNotNull(count, INTEGER); } @Override diff --git a/jOOQ/src/main/java/org/jooq/impl/Reverse.java b/jOOQ/src/main/java/org/jooq/impl/Reverse.java index 6d50051755..7144f45c21 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Reverse.java +++ b/jOOQ/src/main/java/org/jooq/impl/Reverse.java @@ -39,6 +39,8 @@ package org.jooq.impl; import static org.jooq.impl.Names.N_REVERSE; import static org.jooq.impl.Names.N_STRREVERSE; +import static org.jooq.impl.SQLDataType.VARCHAR; +import static org.jooq.impl.Tools.nullSafeNotNull; import org.jooq.Context; import org.jooq.Field; @@ -55,9 +57,9 @@ final class Reverse extends AbstractField { private final Field field; Reverse(Field field) { - super(N_REVERSE, field.getDataType()); + super(N_REVERSE, field == null ? VARCHAR : field.getDataType()); - this.field = field; + this.field = nullSafeNotNull(field, VARCHAR); } @Override diff --git a/jOOQ/src/main/java/org/jooq/impl/Right.java b/jOOQ/src/main/java/org/jooq/impl/Right.java index e677c48f0b..046cb09ae1 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Right.java +++ b/jOOQ/src/main/java/org/jooq/impl/Right.java @@ -41,8 +41,10 @@ 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.Names.N_RIGHT; +import static org.jooq.impl.SQLDataType.INTEGER; import static org.jooq.impl.SQLDataType.VARCHAR; import static org.jooq.impl.Tools.allNotNull; +import static org.jooq.impl.Tools.nullSafeNotNull; import org.jooq.Context; import org.jooq.Field; @@ -63,8 +65,8 @@ final class Right extends AbstractField { Right(Field field, Field length) { super(N_RIGHT, allNotNull(VARCHAR, field, length)); - this.field = field; - this.length = length; + this.field = nullSafeNotNull(field, VARCHAR); + this.length = nullSafeNotNull(length, INTEGER); } @Override diff --git a/jOOQ/src/main/java/org/jooq/impl/Space.java b/jOOQ/src/main/java/org/jooq/impl/Space.java index be11f1dc80..5df65d4313 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Space.java +++ b/jOOQ/src/main/java/org/jooq/impl/Space.java @@ -38,7 +38,9 @@ package org.jooq.impl; import static org.jooq.impl.Names.N_SPACE; +import static org.jooq.impl.SQLDataType.INTEGER; import static org.jooq.impl.SQLDataType.VARCHAR; +import static org.jooq.impl.Tools.nullSafeNotNull; import org.jooq.Context; import org.jooq.Field; @@ -53,9 +55,9 @@ final class Space extends AbstractField { private final Field count; Space(Field count) { - super(N_SPACE, VARCHAR.nullable(count.getDataType().nullable())); + super(N_SPACE, VARCHAR.nullable(count == null || count.getDataType().nullable())); - this.count = count; + this.count = nullSafeNotNull(count, INTEGER); } @Override diff --git a/jOOQ/src/main/java/org/jooq/impl/Substring.java b/jOOQ/src/main/java/org/jooq/impl/Substring.java index 04591a4f74..cf6130153b 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Substring.java +++ b/jOOQ/src/main/java/org/jooq/impl/Substring.java @@ -48,6 +48,7 @@ import static org.jooq.impl.SQLDataType.VARCHAR; import static org.jooq.impl.Tools.allNotNull; import static org.jooq.impl.Tools.convertVal; import static org.jooq.impl.Tools.nullSafe; +import static org.jooq.impl.Tools.nullSafeNotNull; import static org.jooq.impl.Tools.nullableIf; import org.jooq.Context; @@ -72,17 +73,17 @@ final class Substring extends AbstractField { Substring(Field field, Field startingPosition) { super(N_SUBSTRING, allNotNull(VARCHAR, field, startingPosition)); - this.field = nullableIf(false, nullSafe(field, VARCHAR)); - this.startingPosition = nullableIf(false, nullSafe(startingPosition, INTEGER)); + this.field = nullSafeNotNull(field, VARCHAR); + this.startingPosition = nullSafeNotNull(startingPosition, INTEGER); this.length = null; } Substring(Field field, Field startingPosition, Field length) { super(N_SUBSTRING, allNotNull(VARCHAR, field, startingPosition, length)); - this.field = nullableIf(false, nullSafe(field, VARCHAR)); - this.startingPosition = nullableIf(false, nullSafe(startingPosition, INTEGER)); - this.length = nullableIf(false, nullSafe(length, INTEGER)); + this.field = nullSafeNotNull(field, VARCHAR); + this.startingPosition = nullSafeNotNull(startingPosition, INTEGER); + this.length = nullSafeNotNull(length, INTEGER); } @Override diff --git a/jOOQ/src/main/java/org/jooq/impl/Term.java b/jOOQ/src/main/java/org/jooq/impl/Term.java index c8be13718f..8f8d9734b0 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Term.java +++ b/jOOQ/src/main/java/org/jooq/impl/Term.java @@ -46,7 +46,9 @@ import org.jooq.SQLDialect; * dialect-specific variants if applicable * * @author Lukas Eder + * @deprecated - This should be removed by jOOQ 3.15.0 */ +@Deprecated enum Term { ATAN2 { @@ -64,89 +66,6 @@ enum Term { return "atan2"; } }, - BIT_LENGTH { - @Override - public String translate(SQLDialect dialect) { - switch (dialect.family()) { - - - - - - - - - - - - - - - - - - - - case DERBY: - case SQLITE: - return "8 * length"; - } - - return "bit_length"; - } - }, - CHAR_LENGTH { - @Override - public String translate(SQLDialect dialect) { - switch (dialect.family()) { - - - - - - - - - - - - - - case DERBY: - case SQLITE: - return "length"; - } - - return "char_length"; - } - }, - OCTET_LENGTH { - @Override - public String translate(SQLDialect dialect) { - switch (dialect.family()) { - - - - - - - - - - - - - - - - case DERBY: - case SQLITE: - return "length"; - } - - return "octet_length"; - } - }, STDDEV_POP { @Override public String translate(SQLDialect dialect) { diff --git a/jOOQ/src/main/java/org/jooq/impl/Tools.java b/jOOQ/src/main/java/org/jooq/impl/Tools.java index 2278e1476c..ed7d424995 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Tools.java +++ b/jOOQ/src/main/java/org/jooq/impl/Tools.java @@ -5900,7 +5900,11 @@ final class Tools { } static final DataType dataType(Field field) { - return field == null ? OTHER : field.getDataType(); + return dataType(OTHER, field); + } + + static final DataType dataType(DataType defaultType, Field field) { + return field == null ? defaultType : field.getDataType(); } static final DataType allNotNull(DataType defaultType, Field f1, Field f2) { @@ -6022,6 +6026,10 @@ final class Tools { return (DataType) (field == null ? SQLDataType.OTHER : field.getDataType()); } + static final Field nullSafeNotNull(Field field, DataType type) { + return nullableIf(false, nullSafe(field, type)); + } + static final Field nullableIf(boolean nullable, Field field) { return isVal(field) ? extractVal(field).convertTo(field.getDataType().nullable(nullable))