diff --git a/jOOQ/src/main/java/org/jooq/impl/DSL.java b/jOOQ/src/main/java/org/jooq/impl/DSL.java index 56bc8f98b8..e6e119f80e 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DSL.java +++ b/jOOQ/src/main/java/org/jooq/impl/DSL.java @@ -15037,6 +15037,24 @@ public class DSL { return new Degrees(radians); } + /** + * The DIGITS function. + */ + @NotNull + @Support + public static Field digits(Number value) { + return new Digits(Tools.field(value)); + } + + /** + * The DIGITS function. + */ + @NotNull + @Support + public static Field digits(Field value) { + return new Digits(value); + } + /** * The E function. *

diff --git a/jOOQ/src/main/java/org/jooq/impl/DefaultDataType.java b/jOOQ/src/main/java/org/jooq/impl/DefaultDataType.java index 7266b1cf33..e1a1539f90 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DefaultDataType.java +++ b/jOOQ/src/main/java/org/jooq/impl/DefaultDataType.java @@ -135,25 +135,25 @@ public class DefaultDataType extends AbstractDataTypeX { * The minimum decimal precision needed to represent a Java {@link Long} * type. */ - private static final int LONG_PRECISION = String.valueOf(Long.MAX_VALUE).length(); + static final int LONG_PRECISION = String.valueOf(Long.MAX_VALUE).length(); /** * The minimum decimal precision needed to represent a Java {@link Integer} * type. */ - private static final int INTEGER_PRECISION = String.valueOf(Integer.MAX_VALUE).length(); + static final int INTEGER_PRECISION = String.valueOf(Integer.MAX_VALUE).length(); /** * The minimum decimal precision needed to represent a Java {@link Short} * type. */ - private static final int SHORT_PRECISION = String.valueOf(Short.MAX_VALUE).length(); + static final int SHORT_PRECISION = String.valueOf(Short.MAX_VALUE).length(); /** * The minimum decimal precision needed to represent a Java {@link Byte} * type. */ - private static final int BYTE_PRECISION = String.valueOf(Byte.MAX_VALUE).length(); + static final int BYTE_PRECISION = String.valueOf(Byte.MAX_VALUE).length(); // ------------------------------------------------------------------------- // Data type attributes diff --git a/jOOQ/src/main/java/org/jooq/impl/Digits.java b/jOOQ/src/main/java/org/jooq/impl/Digits.java new file mode 100644 index 0000000000..98485641d0 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/Digits.java @@ -0,0 +1,129 @@ +/* + * 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.*; +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 java.math.BigDecimal.TEN; +import static org.jooq.SQLDialect.*; + +import org.jooq.*; +import org.jooq.conf.*; +import org.jooq.impl.*; +import org.jooq.tools.*; + +import java.math.BigDecimal; +import java.util.*; + + +/** + * The DIGITS statement. + */ +@SuppressWarnings({ "rawtypes", "unused" }) +final class Digits +extends + AbstractField +{ + + private static final long serialVersionUID = 1L; + + private final Field value; + + Digits( + Field value + ) { + super( + N_DIGITS, + allNotNull(VARCHAR, value) + ); + + this.value = nullSafeNotNull(value, INTEGER); + } + + // ------------------------------------------------------------------------- + // XXX: QueryPart API + // ------------------------------------------------------------------------- + + + + private static final Set NO_SUPPORT_DIGITS = SQLDialect.supportedBy(CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE); + + @Override + public final void accept(Context ctx) { + if (NO_SUPPORT_DIGITS.contains(ctx.dialect())) { + DataType t = value.getDataType(); + + if (t.getType() == Byte.class) + ctx.visit(DSL.lpad(DSL.abs(value).cast(VARCHAR(DefaultDataType.BYTE_PRECISION)), inline(DefaultDataType.BYTE_PRECISION), inline("0"))); + else if (t.getType() == Short.class) + ctx.visit(DSL.lpad(DSL.abs(value).cast(VARCHAR(DefaultDataType.SHORT_PRECISION)), inline(DefaultDataType.SHORT_PRECISION), inline("0"))); + else if (t.getType() == Integer.class) + ctx.visit(DSL.lpad(DSL.abs(value).cast(VARCHAR(DefaultDataType.INTEGER_PRECISION)), inline(DefaultDataType.INTEGER_PRECISION), inline("0"))); + else if (t.getType() == Long.class) + ctx.visit(DSL.lpad(DSL.abs(value).cast(VARCHAR(DefaultDataType.LONG_PRECISION)), inline(DefaultDataType.LONG_PRECISION), inline("0"))); + else if (t.scaleDefined()) + ctx.visit(DSL.lpad(DSL.abs(value.mul(inline(BigDecimal.TEN.pow(t.scale())))).cast(VARCHAR(t.precision())), inline(t.precision()), inline("0"))); + else + ctx.visit(DSL.lpad(DSL.abs(value).cast(VARCHAR(t.precision())), inline(t.precision()), inline("0"))); + } + else + ctx.visit(N_DIGITS).sql('(').visit(value).sql(')'); + } + + + + // ------------------------------------------------------------------------- + // The Object API + // ------------------------------------------------------------------------- + + @Override + public boolean equals(Object that) { + if (that instanceof Digits) { + return + StringUtils.equals(value, ((Digits) that).value) + ; + } + else + return super.equals(that); + } +} diff --git a/jOOQ/src/main/java/org/jooq/impl/Names.java b/jOOQ/src/main/java/org/jooq/impl/Names.java index 8df7d6884c..5d90ebd334 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Names.java +++ b/jOOQ/src/main/java/org/jooq/impl/Names.java @@ -82,10 +82,10 @@ final class Names { static final Name N_CEIL = unquotedName("ceil"); static final Name N_CEILING = unquotedName("ceiling"); static final Name N_CHAR = unquotedName("char"); - static final Name N_CHARINDEX = unquotedName("charindex"); static final Name N_CHAR_LENGTH = unquotedName("char_length"); - static final Name N_CHR = unquotedName("chr"); + static final Name N_CHARINDEX = unquotedName("charindex"); static final Name N_CHOOSE = unquotedName("choose"); + static final Name N_CHR = unquotedName("chr"); static final Name N_COALESCE = unquotedName("coalesce"); static final Name N_COLLECT = unquotedName("collect"); static final Name N_CONCAT = unquotedName("concat"); @@ -101,7 +101,6 @@ final class Names { static final Name N_COUNTSET = unquotedName("countset"); static final Name N_COVAR_POP = unquotedName("covar_pop"); static final Name N_COVAR_SAMP = unquotedName("covar_samp"); - static final Name N_CURRENTUSER = unquotedName("currentuser"); static final Name N_CURRENT_BIGDATETIME = unquotedName("current_bigdatetime"); static final Name N_CURRENT_CATALOG = unquotedName("current_catalog"); static final Name N_CURRENT_DATABASE = unquotedName("current_database"); @@ -110,14 +109,15 @@ final class Names { static final Name N_CURRENT_TIME = unquotedName("current_time"); static final Name N_CURRENT_TIMESTAMP = unquotedName("current_timestamp"); 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_DATEADD = unquotedName("dateadd"); - static final Name N_DATEDIFF = unquotedName("datediff"); - static final Name N_DATEPART = unquotedName("datepart"); 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"); + static final Name N_DATEADD = unquotedName("dateadd"); + static final Name N_DATEDIFF = unquotedName("datediff"); + static final Name N_DATEPART = unquotedName("datepart"); static final Name N_DAYOFWEEK = unquotedName("dayofweek"); static final Name N_DAYOFYEAR = unquotedName("dayofyear"); static final Name N_DAYS = unquotedName("days"); @@ -125,6 +125,7 @@ final class Names { static final Name N_DB_NAME = unquotedName("db_name"); static final Name N_DECODE = unquotedName("decode"); static final Name N_DEGREES = unquotedName("degrees"); + static final Name N_DIGITS = unquotedName("digits"); static final Name N_DUAL = unquotedName("dual"); static final Name N_E = unquotedName("e"); static final Name N_EULER = unquotedName("e"); @@ -134,8 +135,8 @@ final class Names { static final Name N_FLASHBACK = unquotedName("flashback"); static final Name N_FLOOR = unquotedName("floor"); static final Name N_FUNCTION = unquotedName("function"); - static final Name N_GENERATE_SERIES = unquotedName("generate_series"); static final Name N_GEN_ID = unquotedName("gen_id"); + static final Name N_GENERATE_SERIES = unquotedName("generate_series"); static final Name N_GETDATE = unquotedName("getdate"); static final Name N_GREATEST = unquotedName("greatest"); static final Name N_GROUP_CONCAT = unquotedName("group_concat"); @@ -148,13 +149,6 @@ final class Names { static final Name N_INSTR = unquotedName("instr"); static final Name N_ISJSON = unquotedName("isjson"); static final Name N_JOIN = unquotedName("join"); - static final Name N_JSONB_AGG = unquotedName("jsonb_agg"); - static final Name N_JSONB_BUILD_ARRAY = unquotedName("jsonb_build_array"); - static final Name N_JSONB_OBJECT = unquotedName("jsonb_object"); - static final Name N_JSONB_OBJECTAGG = unquotedName("jsonb_objectagg"); - static final Name N_JSONB_OBJECT_AGG = unquotedName("jsonb_object_agg"); - static final Name N_JSONB_PATH_EXISTS = unquotedName("jsonb_path_exists"); - static final Name N_JSONB_PATH_QUERY_FIRST = unquotedName("jsonb_path_query_first"); static final Name N_JSON_AGG = unquotedName("json_agg"); static final Name N_JSON_ARRAY = unquotedName("json_array"); static final Name N_JSON_ARRAYAGG = unquotedName("json_arrayagg"); @@ -163,12 +157,19 @@ final class Names { static final Name N_JSON_EXTRACT = unquotedName("json_extract"); static final Name N_JSON_MERGE = unquotedName("json_merge"); static final Name N_JSON_OBJECT = unquotedName("json_object"); - static final Name N_JSON_OBJECTAGG = unquotedName("json_objectagg"); static final Name N_JSON_OBJECT_AGG = unquotedName("json_object_agg"); + static final Name N_JSON_OBJECTAGG = unquotedName("json_objectagg"); static final Name N_JSON_QUOTE = unquotedName("json_quote"); static final Name N_JSON_TABLE = unquotedName("json_table"); static final Name N_JSON_VALID = unquotedName("json_valid"); static final Name N_JSON_VALUE = unquotedName("json_value"); + static final Name N_JSONB_AGG = unquotedName("jsonb_agg"); + static final Name N_JSONB_BUILD_ARRAY = unquotedName("jsonb_build_array"); + static final Name N_JSONB_OBJECT = unquotedName("jsonb_object"); + static final Name N_JSONB_OBJECT_AGG = unquotedName("jsonb_object_agg"); + static final Name N_JSONB_OBJECTAGG = unquotedName("jsonb_objectagg"); + static final Name N_JSONB_PATH_EXISTS = unquotedName("jsonb_path_exists"); + static final Name N_JSONB_PATH_QUERY_FIRST = unquotedName("jsonb_path_query_first"); static final Name N_LCASE = unquotedName("lcase"); static final Name N_LEAST = unquotedName("least"); static final Name N_LEFT = unquotedName("left"); @@ -216,8 +217,8 @@ final class Names { static final Name N_RANDOM = unquotedName("random"); static final Name N_RATIO_TO_REPORT = unquotedName("ratio_to_report"); static final Name N_RAWTOHEX = unquotedName("rawtohex"); - static final Name N_REGEXP_REPLACE = unquotedName("regexp_replace"); static final Name N_REGEX_REPLACE = unquotedName("regex_replace"); + static final Name N_REGEXP_REPLACE = unquotedName("regexp_replace"); static final Name N_REGR_AVGX = unquotedName("regr_avgx"); static final Name N_REGR_AVGY = unquotedName("regr_avgy"); static final Name N_REGR_COUNT = unquotedName("regr_count"); @@ -237,9 +238,9 @@ final class Names { static final Name N_ROUND = unquotedName("round"); static final Name N_ROUND_DOWN = unquotedName("round_down"); static final Name N_ROW = unquotedName("row"); + static final Name N_ROW_NUMBER = unquotedName("row_number"); static final Name N_ROWNUM = unquotedName("rownum"); static final Name N_ROWSFROM = unquotedName("rowsfrom"); - static final Name N_ROW_NUMBER = unquotedName("row_number"); static final Name N_RPAD = unquotedName("rpad"); static final Name N_RTRIM = unquotedName("rtrim"); static final Name N_SCHEMA_NAME = unquotedName("schema_name"); @@ -270,16 +271,16 @@ final class Names { static final Name N_STDDEV_POP = unquotedName("stddev_pop"); static final Name N_STDDEV_SAMP = unquotedName("stddev_samp"); static final Name N_STDEV = unquotedName("stdev"); - static final Name N_STDEVP = unquotedName("stdevp"); static final Name N_STDEV_SAMP = unquotedName("stdev_samp"); + static final Name N_STDEVP = unquotedName("stdevp"); static final Name N_STRFTIME = unquotedName("strftime"); static final Name N_STRING_AGG = unquotedName("string_agg"); static final Name N_STRREVERSE = unquotedName("strreverse"); static final Name N_SUBSTR = unquotedName("substr"); static final Name N_SUBSTRING = unquotedName("substring"); + static final Name N_SYS_CONNECT_BY_PATH = unquotedName("sys_connect_by_path"); static final Name N_SYSTEM_RANGE = unquotedName("system_range"); static final Name N_SYSTEM_TIME = unquotedName("system_time"); - static final Name N_SYS_CONNECT_BY_PATH = unquotedName("sys_connect_by_path"); static final Name N_T = unquotedName("t"); static final Name N_TAN = unquotedName("tan"); static final Name N_TANH = unquotedName("tanh"); @@ -302,11 +303,11 @@ final class Names { static final Name N_VALUE = unquotedName("value"); static final Name N_VALUES = unquotedName("values"); static final Name N_VAR = unquotedName("var"); + static final Name N_VAR_POP = unquotedName("var_pop"); + static final Name N_VAR_SAMP = unquotedName("var_samp"); static final Name N_VARIANCE = unquotedName("variance"); static final Name N_VARIANCE_SAMP = unquotedName("variance_samp"); static final Name N_VARP = unquotedName("varp"); - static final Name N_VAR_POP = unquotedName("var_pop"); - static final Name N_VAR_SAMP = unquotedName("var_samp"); static final Name N_WEEKDAY = unquotedName("weekday"); static final Name N_WIDTH_BUCKET = unquotedName("width_bucket"); static final Name N_XMLAGG = unquotedName("xmlagg"); diff --git a/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java b/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java index 42e63999f2..4d63322fe9 100644 --- a/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java @@ -126,6 +126,7 @@ import static org.jooq.impl.DSL.defaultValue; import static org.jooq.impl.DSL.default_; import static org.jooq.impl.DSL.deg; import static org.jooq.impl.DSL.denseRank; +import static org.jooq.impl.DSL.digits; import static org.jooq.impl.DSL.domain; import static org.jooq.impl.DSL.epoch; import static org.jooq.impl.DSL.every; @@ -6767,6 +6768,8 @@ final class ParserContext { return currentCatalog(); else if ((parseFunctionNameIf("DBINFO") && parse('(') && parseStringLiteral("dbname") != null && parse(')'))) return currentCatalog(); + else if (parseFunctionNameIf("DIGITS")) + return digits((Field) parseFieldParenthesised(N)); if (D.is(type)) if ((field = parseFieldDateLiteralIf()) != null)