[jOOQ/jOOQ#11656] Added more dialect support for TO_HEX

- Rename function to DSL.toHex()
- Support H2, Oracle, PostgreSQL, SQLite, SQLServer
This commit is contained in:
Lukas Eder 2021-04-28 13:32:00 +02:00
parent 2d8beafd89
commit 7883dd3e89
5 changed files with 322 additions and 24 deletions

View File

@ -16980,28 +16980,6 @@ public class DSL {
return new Digits(value);
}
/**
* The <code>HEX</code> function.
* <p>
* Format a number to its hex value.
*/
@NotNull
@Support({ MYSQL })
public static Field<String> hex(Number value) {
return new Hex(Tools.field(value));
}
/**
* The <code>HEX</code> function.
* <p>
* Format a number to its hex value.
*/
@NotNull
@Support({ MYSQL })
public static Field<String> hex(Field<? extends Number> value) {
return new Hex(value);
}
/**
* The <code>LEFT</code> function.
* <p>
@ -18406,6 +18384,28 @@ public class DSL {
return new ToDate(value, formatMask);
}
/**
* The <code>TO_HEX</code> function.
* <p>
* Format a number to its hex value.
*/
@NotNull
@Support({ H2, MARIADB, MYSQL, POSTGRES, SQLITE })
public static Field<String> toHex(Number value) {
return new ToHex(Tools.field(value));
}
/**
* The <code>TO_HEX</code> function.
* <p>
* Format a number to its hex value.
*/
@NotNull
@Support({ H2, MARIADB, MYSQL, POSTGRES, SQLITE })
public static Field<String> toHex(Field<? extends Number> value) {
return new ToHex(value);
}
/**
* The <code>TO_TIMESTAMP</code> function.
* <p>

View File

@ -142,6 +142,7 @@ final class Names {
static final Name N_EXP = unquotedName("exp");
static final Name N_EXTRACT = unquotedName("extract");
static final Name N_FIELD = unquotedName("field");
static final Name N_FORMAT = unquotedName("format");
static final Name N_FLASHBACK = unquotedName("flashback");
static final Name N_FLOOR = unquotedName("floor");
static final Name N_FUNCTION = unquotedName("function");
@ -237,6 +238,7 @@ final class Names {
static final Name N_PLPGSQL = unquotedName("plpgsql");
static final Name N_POSITION = unquotedName("position");
static final Name N_POWER = unquotedName("power");
static final Name N_PRINTF = unquotedName("printf");
static final Name N_PRIOR = unquotedName("prior");
static final Name N_PRODUCT = unquotedName("product");
static final Name N_RADIANS = unquotedName("radians");

View File

@ -7318,7 +7318,7 @@ final class DefaultParseContext extends AbstractScope implements ParseContext {
if (parseFunctionNameIf("HASH_MD5"))
return md5((Field) parseFieldParenthesised(S));
else if (parseFunctionNameIf("HEX"))
return hex((Field) parseFieldParenthesised(N));
return toHex((Field) parseFieldParenthesised(N));
break;
@ -7606,7 +7606,7 @@ final class DefaultParseContext extends AbstractScope implements ParseContext {
else if ((field = parseFieldToCharIf()) != null)
return field;
else if (parseFunctionNameIf("TO_HEX"))
return hex((Field) parseFieldParenthesised(N));
return toHex((Field) parseFieldParenthesised(N));
if (N.is(type))
if (parseFunctionNameIf("TANH"))

View File

@ -0,0 +1,132 @@
/*
* 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 org.jooq.impl.Tools.DataExtendedKey.*;
import static org.jooq.impl.Tools.DataKey.*;
import static org.jooq.SQLDialect.*;
import org.jooq.*;
import org.jooq.Record;
import org.jooq.conf.*;
import org.jooq.impl.*;
import org.jooq.tools.*;
import java.util.*;
/**
* The <code>TO HEX</code> statement.
*/
@SuppressWarnings({ "rawtypes", "unused" })
final class ToHex
extends
AbstractField<String>
{
private static final long serialVersionUID = 1L;
private final Field<? extends Number> value;
ToHex(
Field<? extends Number> value
) {
super(
N_TO_HEX,
allNotNull(VARCHAR, value)
);
this.value = nullSafeNotNull(value, INTEGER);
}
// -------------------------------------------------------------------------
// XXX: QueryPart API
// -------------------------------------------------------------------------
@Override
public final void accept(Context<?> ctx) {
switch (ctx.family()) {
case MARIADB:
case MYSQL:
ctx.visit(function(N_HEX, getDataType(), value));
break;
case H2:
ctx.visit(DSL.trim(toChar(value, inline("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"))));
break;
case SQLITE:
ctx.visit(function(N_PRINTF, getDataType(), inline("%X"), value));
break;
default:
ctx.visit(function(N_TO_HEX, getDataType(), value));
break;
}
}
// -------------------------------------------------------------------------
// The Object API
// -------------------------------------------------------------------------
@Override
public boolean equals(Object that) {
if (that instanceof ToHex) {
return
StringUtils.equals(value, ((ToHex) that).value)
;
}
else
return super.equals(that);
}
}

View File

@ -0,0 +1,164 @@
/*
* 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 org.jooq.impl.Tools.DataExtendedKey.*;
import static org.jooq.impl.Tools.DataKey.*;
import static org.jooq.SQLDialect.*;
import org.jooq.*;
import org.jooq.Record;
import org.jooq.conf.*;
import org.jooq.impl.*;
import org.jooq.tools.*;
import java.util.*;
import java.util.UUID;
/**
* The <code>UUID</code> statement.
*/
@SuppressWarnings({ "unused" })
final class Uuid
extends
AbstractField<UUID>
{
private static final long serialVersionUID = 1L;
Uuid() {
super(
N_UUID,
allNotNull(UUID)
);
}
// -------------------------------------------------------------------------
// XXX: QueryPart API
// -------------------------------------------------------------------------
@Override
public final void accept(Context<?> ctx) {
switch (ctx.family()) {
case POSTGRES:
ctx.visit(function(N_GEN_RANDOM_UUID, getDataType()));
break;
case FIREBIRD:
ctx.visit(function(N_UUID_TO_CHAR, getDataType(), function(N_GEN_UUID, getDataType())));
break;
case H2:
ctx.visit(function(N_RANDOM_UUID, getDataType()));
break;
case HSQLDB:
case IGNITE:
case MARIADB:
case MYSQL:
ctx.visit(function(N_UUID, getDataType()));
break;
default:
ctx.visit(function(N_UUID, getDataType()));
break;
}
}
// -------------------------------------------------------------------------
// The Object API
// -------------------------------------------------------------------------
@Override
public boolean equals(Object that) {
if (that instanceof Uuid) {
return true;
}
else
return super.equals(that);
}
}