diff --git a/jOOQ/src/main/java/org/jooq/impl/BitCount.java b/jOOQ/src/main/java/org/jooq/impl/BitCount.java index 809de68821..36f9219f87 100644 --- a/jOOQ/src/main/java/org/jooq/impl/BitCount.java +++ b/jOOQ/src/main/java/org/jooq/impl/BitCount.java @@ -37,34 +37,68 @@ */ package org.jooq.impl; -import static org.jooq.impl.DSL.inline; -import static org.jooq.impl.Names.N_BITCOUNT; -import static org.jooq.impl.Names.N_BIT_COUNT; -import static org.jooq.impl.Names.N_COUNTSET; +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 org.jooq.Context; -import org.jooq.Field; /** - * @author Lukas Eder + * The BIT COUNT statement. */ -final class BitCount extends AbstractField { - private final Field field; +@SuppressWarnings({ "rawtypes", "unused" }) +final class BitCount +extends + AbstractField +{ - BitCount(Field field) { - super(N_BIT_COUNT, SQLDataType.INTEGER); + private final Field number; - this.field = field; + BitCount( + Field number + ) { + super( + N_BIT_COUNT, + allNotNull(INTEGER, number) + ); + + this.number = nullSafeNotNull(number, INTEGER); } + // ------------------------------------------------------------------------- + // XXX: QueryPart API + // ------------------------------------------------------------------------- + + + @Override public final void accept(Context ctx) { switch (ctx.family()) { + + + + + + case MARIADB: case MYSQL: - ctx.visit(N_BIT_COUNT).sql('(').visit(field).sql(')'); + ctx.visit(N_BIT_COUNT).sql('(').visit(number).sql(')'); return; @@ -92,9 +126,9 @@ final class BitCount extends AbstractField { case H2: case HSQLDB: { - if (field.getType() == Byte.class) { + if (number.getType() == Byte.class) { @SuppressWarnings("unchecked") - Field f = (Field) field; + Field f = (Field) number; ctx.visit( DSL.bitAnd(f, inline((byte) 0x01)) .add( @@ -107,9 +141,9 @@ final class BitCount extends AbstractField { DSL.bitAnd(f, inline((byte) 0x80)).div(inline((byte) 0x80))).cast(Integer.class)); return; } - else if (field.getType() == Short.class) { + else if (number.getType() == Short.class) { @SuppressWarnings("unchecked") - Field f = (Field) field; + Field f = (Field) number; ctx.visit( DSL.bitAnd(f, inline((short) 0x0001)) .add( @@ -130,9 +164,9 @@ final class BitCount extends AbstractField { DSL.bitAnd(f, inline((short) 0x8000)).div(inline((short) 0x8000))).cast(Integer.class)); return; } - else if (field.getType() == Integer.class) { + else if (number.getType() == Integer.class) { @SuppressWarnings("unchecked") - Field f = (Field) field; + Field f = (Field) number; ctx.visit( DSL.bitAnd(f, inline(0x00000001)) .add( @@ -169,9 +203,9 @@ final class BitCount extends AbstractField { DSL.bitAnd(f, inline(0x80000000)).div(inline(0x80000000)))); return; } - else if (field.getType() == Long.class) { + else if (number.getType() == Long.class) { @SuppressWarnings("unchecked") - Field f = (Field) field; + Field f = (Field) number; ctx.visit( DSL.bitAnd(f, inline(0x0000000000000001L)) .add( @@ -242,15 +276,15 @@ final class BitCount extends AbstractField { } else { // Currently not supported - ctx.visit(N_BIT_COUNT).sql('(').visit(field).sql(')'); + ctx.visit(N_BIT_COUNT).sql('(').visit(number).sql(')'); return; } } default: { - if (field.getType() == Byte.class) { + if (number.getType() == Byte.class) { @SuppressWarnings("unchecked") - Field f = (Field) field; + Field f = (Field) number; byte i = 0; ctx.visit( @@ -264,9 +298,9 @@ final class BitCount extends AbstractField { DSL.shr(DSL.bitAnd(f, inline((byte) 0x80)), inline(++i))).cast(Integer.class)); return; } - else if (field.getType() == Short.class) { + else if (number.getType() == Short.class) { @SuppressWarnings("unchecked") - Field f = (Field) field; + Field f = (Field) number; short i = 0; ctx.visit( @@ -288,9 +322,9 @@ final class BitCount extends AbstractField { DSL.shr(DSL.bitAnd(f, inline((short) 0x8000)), inline(++i))).cast(Integer.class)); return; } - else if (field.getType() == Integer.class) { + else if (number.getType() == Integer.class) { @SuppressWarnings("unchecked") - Field f = (Field) field; + Field f = (Field) number; int i = 0; ctx.visit( @@ -328,9 +362,9 @@ final class BitCount extends AbstractField { DSL.shr(DSL.bitAnd(f, inline(0x80000000)), inline(++i)))); return; } - else if (field.getType() == Long.class) { + else if (number.getType() == Long.class) { @SuppressWarnings("unchecked") - Field f = (Field) field; + Field f = (Field) number; long i = 0; ctx.visit( @@ -402,10 +436,38 @@ final class BitCount extends AbstractField { } else { // Currently not supported - ctx.visit(N_BIT_COUNT).sql('(').visit(field).sql(')'); + ctx.visit(N_BIT_COUNT).sql('(').visit(number).sql(')'); return; } } } } + + + + + + + + + + + + + + + // ------------------------------------------------------------------------- + // The Object API + // ------------------------------------------------------------------------- + + @Override + public boolean equals(Object that) { + if (that instanceof BitCount) { + return + StringUtils.equals(number, ((BitCount) that).number) + ; + } + else + return super.equals(that); + } } diff --git a/jOOQ/src/main/java/org/jooq/impl/DSL.java b/jOOQ/src/main/java/org/jooq/impl/DSL.java index 73fe7a113f..bd175c8283 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DSL.java +++ b/jOOQ/src/main/java/org/jooq/impl/DSL.java @@ -15816,6 +15816,30 @@ public class DSL { return new Atan2(x, y); } + /** + * The BIT_COUNT function. + *

+ * Count the number of bits set in a number + * + * @param number is wrapped as {@link #val(Object)}. + */ + @NotNull + @Support({ CUBRID, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE, YUGABYTE }) + public static Field bitCount(Number number) { + return new BitCount(Tools.field(number)); + } + + /** + * The BIT_COUNT function. + *

+ * Count the number of bits set in a number + */ + @NotNull + @Support({ CUBRID, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE, YUGABYTE }) + public static Field bitCount(Field number) { + return new BitCount(number); + } + /** * The CEIL function. *

@@ -22270,41 +22294,6 @@ public class DSL { // XXX Bitwise operations // ------------------------------------------------------------------------ - /** - * The MySQL BIT_COUNT(field) function, counting the number of - * bits that are set in this number. - * - * @see #bitCount(Field) - */ - @NotNull - @Support({ CUBRID, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE, YUGABYTE }) - public static Field bitCount(Number value) { - return bitCount(Tools.field(value)); - } - - /** - * The MySQL BIT_COUNT(field) function, counting the number of - * bits that are set in this number. - *

- * This function is emulated in most other databases like this (for a - * TINYINT field):

-     * ([field] &   1) +
-     * ([field] &   2) >> 1 +
-     * ([field] &   4) >> 2 +
-     * ([field] &   8) >> 3 +
-     * ([field] &  16) >> 4 +
-     *  ...
-     * ([field] & 128) >> 7
-     * 
- *

- * More efficient algorithms are very welcome - */ - @NotNull - @Support({ CUBRID, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE, YUGABYTE }) - public static Field bitCount(Field field) { - return new BitCount(Tools.nullSafe(field)); - } - /** * The bitwise not operator. *