Search in sources :

Example 11 with Numeric

use of io.vertx.sqlclient.data.Numeric in project vertx-sql-client by eclipse-vertx.

the class NumericTest method testEqualsAndHashCode.

@Test
public void testEqualsAndHashCode() {
    int intValue = random.nextInt(1000);
    Numeric[] numerics = { Numeric.create(intValue), Numeric.create((short) intValue), Numeric.create((double) intValue), Numeric.create((long) intValue), Numeric.create((float) intValue), Numeric.create(new BigInteger("" + intValue)), Numeric.create(new BigDecimal("" + intValue)) };
    for (Numeric l : numerics) {
        for (Numeric r : numerics) {
            assertEquals(l, r);
            assertEquals(l.hashCode(), r.hashCode());
        }
    }
    assertEquals(Numeric.create(Double.NaN), Numeric.create(Float.NaN));
    for (Numeric l : numerics) {
        assertNotSame(Numeric.NaN, l);
        assertNotSame(l, Numeric.NaN);
        assertNotSame(Numeric.create(Float.NaN), l);
        assertNotSame(l, Numeric.create(Float.NaN));
        assertNotSame(Numeric.create(Double.NaN), l);
        assertNotSame(l, Numeric.create(Double.NaN));
    }
}
Also used : Numeric(io.vertx.sqlclient.data.Numeric) BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 12 with Numeric

use of io.vertx.sqlclient.data.Numeric in project vertx-sql-client by eclipse-vertx.

the class Tuple method getArrayOfNumerics.

/**
 * Get an array of {@link Numeric} value at {@code pos}.
 *
 * @param pos the column
 * @return the value
 */
@GenIgnore(GenIgnore.PERMITTED_TYPE)
default Numeric[] getArrayOfNumerics(int pos) {
    Object val = getValue(pos);
    if (val == null) {
        return null;
    } else if (val instanceof Numeric[]) {
        return (Numeric[]) val;
    } else if (val instanceof Number[]) {
        Number[] a = (Number[]) val;
        int len = a.length;
        Numeric[] arr = new Numeric[len];
        for (int i = 0; i < len; i++) {
            Number elt = a[i];
            if (elt != null) {
                arr[i] = Numeric.create(elt);
            }
        }
        return arr;
    } else if (val instanceof Enum[]) {
        Enum<?>[] a = (Enum<?>[]) val;
        int len = a.length;
        Numeric[] arr = new Numeric[len];
        for (int i = 0; i < len; i++) {
            Enum<?> elt = a[i];
            if (elt != null) {
                arr[i] = Numeric.create(elt.ordinal());
            }
        }
        return arr;
    } else if (val.getClass() == Object[].class) {
        Object[] array = (Object[]) val;
        Numeric[] doubleArray = new Numeric[array.length];
        for (int i = 0; i < array.length; i++) {
            doubleArray[i] = Numeric.create((Number) array[i]);
        }
        return doubleArray;
    } else {
        throw new ClassCastException();
    }
}
Also used : Numeric(io.vertx.sqlclient.data.Numeric) JsonObject(io.vertx.core.json.JsonObject) GenIgnore(io.vertx.codegen.annotations.GenIgnore)

Example 13 with Numeric

use of io.vertx.sqlclient.data.Numeric in project vertx-sql-client by eclipse-vertx.

the class NumericTypesExtendedCodecTest method testEncodeLongArray.

@Test
public void testEncodeLongArray(TestContext ctx) {
    Async async = ctx.async();
    PgConnection.connect(vertx, options, ctx.asyncAssertSuccess(conn -> {
        conn.prepare("UPDATE \"ArrayDataType\" SET \"Long\" = $1  WHERE \"id\" = $2 RETURNING \"Long\"", ctx.asyncAssertSuccess(p -> {
            p.query().execute(Tuple.tuple().addArrayOfLong(new Long[] { 4L, 5L, 6L, 7L, 8L }).addInteger(2), ctx.asyncAssertSuccess(result -> {
                ColumnChecker.checkColumn(0, "Long").returns(Tuple::getValue, Row::getValue, ColumnChecker.toObjectArray(new long[] { 4, 5, 6, 7, 8 })).returns(Tuple::getArrayOfShorts, Row::getArrayOfShorts, ColumnChecker.toObjectArray(new short[] { 4, 5, 6, 7, 8 })).returns(Tuple::getArrayOfIntegers, Row::getArrayOfIntegers, ColumnChecker.toObjectArray(new int[] { 4, 5, 6, 7, 8 })).returns(Tuple::getArrayOfLongs, Row::getArrayOfLongs, ColumnChecker.toObjectArray(new long[] { 4, 5, 6, 7, 8 })).returns(Tuple::getArrayOfFloats, Row::getArrayOfFloats, ColumnChecker.toObjectArray(new float[] { 4, 5, 6, 7, 8 })).returns(Tuple::getArrayOfDoubles, Row::getArrayOfDoubles, ColumnChecker.toObjectArray(new double[] { 4, 5, 6, 7, 8 })).returns(Tuple::getArrayOfNumerics, Row::getArrayOfNumerics, ColumnChecker.toObjectArray(new Numeric[] { Numeric.create(4), Numeric.create(5), Numeric.create(6), Numeric.create(7), Numeric.create(8) })).forRow(result.iterator().next());
                async.complete();
            }));
        }));
    }));
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) BigDecimal(java.math.BigDecimal) PgConnection(io.vertx.pgclient.PgConnection) ColumnChecker(io.vertx.sqlclient.ColumnChecker) Row(io.vertx.sqlclient.Row) Test(org.junit.Test) Numeric(io.vertx.sqlclient.data.Numeric) Tuple(io.vertx.sqlclient.Tuple) Numeric(io.vertx.sqlclient.data.Numeric) Async(io.vertx.ext.unit.Async) Row(io.vertx.sqlclient.Row) Tuple(io.vertx.sqlclient.Tuple) Test(org.junit.Test)

Example 14 with Numeric

use of io.vertx.sqlclient.data.Numeric in project vertx-sql-client by eclipse-vertx.

the class NumericTypesExtendedCodecTest method testEncodeNumericArray.

@Test
public void testEncodeNumericArray(TestContext ctx) {
    Async async = ctx.async();
    PgConnection.connect(vertx, options, ctx.asyncAssertSuccess(conn -> {
        conn.prepare("UPDATE \"ArrayDataType\" SET \"Numeric\" = $1  WHERE \"id\" = $2 RETURNING \"Numeric\"", ctx.asyncAssertSuccess(p -> {
            Numeric[] expected = { Numeric.create(0), Numeric.create(10000) };
            p.query().execute(Tuple.tuple().addValue(expected).addInteger(2), ctx.asyncAssertSuccess(result -> {
                ColumnChecker.checkColumn(0, "Numeric").returns(Tuple::getValue, Row::getValue, expected).returns(Tuple::getArrayOfShorts, Row::getArrayOfShorts, new Short[] { expected[0].shortValue(), expected[1].shortValue() }).returns(Tuple::getArrayOfIntegers, Row::getArrayOfIntegers, new Integer[] { expected[0].intValue(), expected[1].intValue() }).returns(Tuple::getArrayOfLongs, Row::getArrayOfLongs, new Long[] { expected[0].longValue(), expected[1].longValue() }).returns(Tuple::getArrayOfFloats, Row::getArrayOfFloats, new Float[] { expected[0].floatValue(), expected[1].floatValue() }).returns(Tuple::getArrayOfDoubles, Row::getArrayOfDoubles, new Double[] { expected[0].doubleValue(), expected[1].doubleValue() }).returns(Tuple::getArrayOfNumerics, Row::getArrayOfNumerics, new Numeric[] { Numeric.create(expected[0]), Numeric.create(expected[1]) }).returns(Numeric.class, expected).forRow(result.iterator().next());
                async.complete();
            }));
        }));
    }));
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) BigDecimal(java.math.BigDecimal) PgConnection(io.vertx.pgclient.PgConnection) ColumnChecker(io.vertx.sqlclient.ColumnChecker) Row(io.vertx.sqlclient.Row) Test(org.junit.Test) Numeric(io.vertx.sqlclient.data.Numeric) Tuple(io.vertx.sqlclient.Tuple) Numeric(io.vertx.sqlclient.data.Numeric) Async(io.vertx.ext.unit.Async) Row(io.vertx.sqlclient.Row) Tuple(io.vertx.sqlclient.Tuple) Test(org.junit.Test)

Example 15 with Numeric

use of io.vertx.sqlclient.data.Numeric in project vertx-sql-client by eclipse-vertx.

the class NumericTypesExtendedCodecTest method testDecodeLongArray.

@Test
public void testDecodeLongArray(TestContext ctx) {
    Async async = ctx.async();
    PgConnection.connect(vertx, options, ctx.asyncAssertSuccess(conn -> {
        conn.prepare("SELECT \"Long\" FROM \"ArrayDataType\" WHERE \"id\" = $1", ctx.asyncAssertSuccess(p -> {
            p.query().execute(Tuple.tuple().addInteger(1), ctx.asyncAssertSuccess(result -> {
                ColumnChecker.checkColumn(0, "Long").returns(Tuple::getValue, Row::getValue, new Long[] { 3L }).returns(Tuple::getArrayOfShorts, Row::getArrayOfShorts, new Short[] { (short) 3 }).returns(Tuple::getArrayOfIntegers, Row::getArrayOfIntegers, new Integer[] { 3 }).returns(Tuple::getArrayOfLongs, Row::getArrayOfLongs, new Long[] { 3L }).returns(Tuple::getArrayOfFloats, Row::getArrayOfFloats, new Float[] { 3F }).returns(Tuple::getArrayOfDoubles, Row::getArrayOfDoubles, new Double[] { 3D }).returns(Tuple::getArrayOfNumerics, Row::getArrayOfNumerics, ColumnChecker.toObjectArray(new Numeric[] { Numeric.create(3) })).forRow(result.iterator().next());
                async.complete();
            }));
        }));
    }));
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) BigDecimal(java.math.BigDecimal) PgConnection(io.vertx.pgclient.PgConnection) ColumnChecker(io.vertx.sqlclient.ColumnChecker) Row(io.vertx.sqlclient.Row) Test(org.junit.Test) Numeric(io.vertx.sqlclient.data.Numeric) Tuple(io.vertx.sqlclient.Tuple) Numeric(io.vertx.sqlclient.data.Numeric) Async(io.vertx.ext.unit.Async) Row(io.vertx.sqlclient.Row) Tuple(io.vertx.sqlclient.Tuple) Test(org.junit.Test)

Aggregations

Numeric (io.vertx.sqlclient.data.Numeric)20 BigDecimal (java.math.BigDecimal)18 Test (org.junit.Test)17 Async (io.vertx.ext.unit.Async)16 TestContext (io.vertx.ext.unit.TestContext)16 PgConnection (io.vertx.pgclient.PgConnection)16 ColumnChecker (io.vertx.sqlclient.ColumnChecker)16 Row (io.vertx.sqlclient.Row)16 Tuple (io.vertx.sqlclient.Tuple)16 GenIgnore (io.vertx.codegen.annotations.GenIgnore)1 Buffer (io.vertx.core.buffer.Buffer)1 JsonObject (io.vertx.core.json.JsonObject)1 BigInteger (java.math.BigInteger)1 java.sql (java.sql)1 Duration (java.time.Duration)1 LocalDate (java.time.LocalDate)1 LocalDateTime (java.time.LocalDateTime)1 SneakyThrows (lombok.SneakyThrows)1 ByteString (org.apache.calcite.avatica.util.ByteString)1 NotNull (org.jetbrains.annotations.NotNull)1