[jOOQ/jOOQ#12425] Move BIT_NOT to API generator

This includes:
- [jOOQ/jOOQ#12427] Extract bitwise operations into their own classes
This commit is contained in:
Lukas Eder 2021-09-14 17:21:27 +02:00
parent aedb429518
commit 4837bd9b47
8 changed files with 178 additions and 74 deletions

View File

@ -709,6 +709,13 @@ extends
@Support({ CUBRID, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE, YUGABYTE })
Field<T> bitNor(Field<T> arg2);
/**
* The <code>BIT_NOT</code> operator.
*/
@NotNull
@Support({ CUBRID, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE, YUGABYTE })
Field<T> bitNot();
/**
* The <code>BIT_X_NOR</code> operator.
*
@ -1173,15 +1180,6 @@ extends
// Bitwise operations
// ------------------------------------------------------------------------
/**
* The bitwise not operator.
*
* @see DSL#bitNot(Field)
*/
@NotNull
@Support({ CUBRID, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE, YUGABYTE })
Field<T> bitNot();
/**
* The bitwise and operator.
*

View File

@ -363,6 +363,12 @@ abstract class AbstractField<T> extends AbstractTypedNamed<T> implements Field<T
return DSL.bitNor((Field) this, (Field<Number>) arg2);
}
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public final Field<T> bitNot() {
return DSL.bitNot((Field) this);
}
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public final Field<T> bitXNor(T arg2) {
@ -560,12 +566,6 @@ abstract class AbstractField<T> extends AbstractTypedNamed<T> implements Field<T
// Unsafe casting is needed here, as bitwise operations only work on
// numeric values...
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public final Field<T> bitNot() {
return DSL.bitNot((Field) this);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public final Field<T> bitAnd(T value) {

View File

@ -0,0 +1,144 @@
/*
* 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>BIT NOT</code> statement.
*/
@SuppressWarnings({ "rawtypes", "unchecked", "unused" })
final class BitNot<T extends Number>
extends
AbstractField<T>
{
private final Field<T> value;
BitNot(
Field<T> value
) {
super(
N_BIT_NOT,
allNotNull((DataType) dataType(INTEGER, value, false), value)
);
this.value = nullSafeNotNull(value, INTEGER);
}
// -------------------------------------------------------------------------
// XXX: QueryPart API
// -------------------------------------------------------------------------
@Override
public final void accept(Context<?> ctx) {
switch (ctx.family()) {
case HSQLDB:
ctx.visit(isub(isub(zero(), value), one()));
break;
case H2:
ctx.visit(function(N_BITNOT, getDataType(), value));
break;
case FIREBIRD:
ctx.visit(function(N_BIN_NOT, getDataType(), value));
break;
default:
ctx.sql("~(").visit(value).sql(')');
break;
}
}
// -------------------------------------------------------------------------
// The Object API
// -------------------------------------------------------------------------
@Override
public boolean equals(Object that) {
if (that instanceof BitNot) {
return
StringUtils.equals(value, ((BitNot) that).value)
;
}
else
return super.equals(that);
}
}

View File

@ -15926,6 +15926,26 @@ public class DSL {
return new BitNor(arg1, arg2);
}
/**
* The <code>BIT_NOT</code> function.
*
* @param value is wrapped as {@link #val(Object)}.
*/
@NotNull
@Support({ CUBRID, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE, YUGABYTE })
public static <T extends Number> Field<T> bitNot(T value) {
return new BitNot(Tools.field(value));
}
/**
* The <code>BIT_NOT</code> function.
*/
@NotNull
@Support({ CUBRID, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE, YUGABYTE })
public static <T extends Number> Field<T> bitNot(Field<T> value) {
return new BitNot(value);
}
/**
* The <code>BIT_X_NOR</code> function.
*
@ -22535,29 +22555,6 @@ public class DSL {
// XXX Bitwise operations
// ------------------------------------------------------------------------
/**
* The bitwise not operator.
*
* @see #bitNot(Field)
*/
@NotNull
@Support({ CUBRID, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE, YUGABYTE })
public static <T extends Number> Field<T> bitNot(T value) {
return bitNot(Tools.field(value));
}
/**
* The bitwise not operator.
* <p>
* Most dialects natively support this using <code>~[field]</code>. jOOQ
* emulates this operator in some dialects using <code>-[field] - 1</code>
*/
@NotNull
@Support({ CUBRID, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE, YUGABYTE })
public static <T extends Number> Field<T> bitNot(Field<T> field) {
return new Neg<>(Tools.nullSafe(field), false, ExpressionOperator.BIT_NOT);
}
/**
* The bitwise and operator.
*

View File

@ -252,7 +252,6 @@ final class Expression<T> extends AbstractTransformable<T> {
@Override

View File

@ -76,11 +76,6 @@ enum ExpressionOperator {
*/
MODULO("%"),
/**
* Bitwise not
*/
BIT_NOT("~"),
/**
* Bitwise and
*/

View File

@ -96,6 +96,7 @@ final class Names {
static final Name N_BIT_LENGTH = unquotedName("bit_length");
static final Name N_BIT_NAND = unquotedName("bit_nand");
static final Name N_BIT_NOR = unquotedName("bit_nor");
static final Name N_BIT_NOT = unquotedName("bit_not");
static final Name N_BIT_OR = unquotedName("bit_or");
static final Name N_BIT_OR_AGG = unquotedName("bit_or_agg");
static final Name N_BIT_XOR = unquotedName("bit_xor");

View File

@ -38,29 +38,10 @@
package org.jooq.impl;
// ...
import static org.jooq.SQLDialect.FIREBIRD;
import static org.jooq.SQLDialect.H2;
import static org.jooq.SQLDialect.HSQLDB;
// ...
// ...
// ...
// ...
// ...
// ...
import static org.jooq.impl.DSL.function;
import static org.jooq.impl.DSL.one;
import static org.jooq.impl.DSL.zero;
import static org.jooq.impl.ExpressionOperator.BIT_NOT;
import static org.jooq.impl.Internal.isub;
import static org.jooq.impl.Names.N_BIN_NOT;
import static org.jooq.impl.Names.N_BITNOT;
import java.util.Set;
import org.jooq.Context;
import org.jooq.Field;
import org.jooq.Param;
import org.jooq.SQLDialect;
import org.jooq.conf.TransformUnneededArithmeticExpressions;
/**
@ -68,9 +49,6 @@ import org.jooq.conf.TransformUnneededArithmeticExpressions;
*/
final class Neg<T> extends AbstractTransformable<T> {
private static final Set<SQLDialect> EMULATE_BIT_NOT = SQLDialect.supportedBy(HSQLDB);
private static final Set<SQLDialect> SUPPORT_BIT_NOT = SQLDialect.supportedBy(H2);
private final Field<T> field;
private final boolean internal;
private final ExpressionOperator operator;
@ -110,19 +88,11 @@ final class Neg<T> extends AbstractTransformable<T> {
@Override
public final void accept0(Context<?> ctx) {
SQLDialect family = ctx.family();
if (operator == BIT_NOT && EMULATE_BIT_NOT.contains(ctx.dialect()))
ctx.visit(isub(isub(zero(), field), one()));
else if (operator == BIT_NOT && SUPPORT_BIT_NOT.contains(ctx.dialect()))
ctx.visit(function(N_BITNOT, getDataType(), field));
else if (operator == BIT_NOT && family == FIREBIRD)
ctx.visit(function(N_BIN_NOT, getDataType(), field));
else
ctx.sql(operator.toSQL())
.sql('(')