diff --git a/vertx-mysql-client/src/main/java/io/vertx/mysqlclient/impl/MySQLRowImpl.java b/vertx-mysql-client/src/main/java/io/vertx/mysqlclient/impl/MySQLRowImpl.java index 23418b566..48ee3ab79 100644 --- a/vertx-mysql-client/src/main/java/io/vertx/mysqlclient/impl/MySQLRowImpl.java +++ b/vertx-mysql-client/src/main/java/io/vertx/mysqlclient/impl/MySQLRowImpl.java @@ -21,6 +21,7 @@ import io.vertx.sqlclient.data.Numeric; import io.vertx.sqlclient.impl.ArrayTuple; +import java.math.BigInteger; import java.time.*; import java.time.temporal.Temporal; import java.util.List; @@ -51,6 +52,8 @@ public T get(Class type, int position) { return type.cast(getFloat(position)); } else if (type == Double.class) { return type.cast(getDouble(position)); + } else if (type == BigInteger.class) { + return type.cast(getBigInteger(position)); } else if (type == Numeric.class) { return type.cast(getNumeric(position)); } else if (type == String.class) { diff --git a/vertx-mysql-client/src/main/java/io/vertx/mysqlclient/impl/datatype/DataTypeCodec.java b/vertx-mysql-client/src/main/java/io/vertx/mysqlclient/impl/datatype/DataTypeCodec.java index a2226cc6a..ddb1f114a 100644 --- a/vertx-mysql-client/src/main/java/io/vertx/mysqlclient/impl/datatype/DataTypeCodec.java +++ b/vertx-mysql-client/src/main/java/io/vertx/mysqlclient/impl/datatype/DataTypeCodec.java @@ -78,6 +78,7 @@ public static Object decodeText(DataType dataType, int collationId, ByteBuf buff case INT64: return textDecodeInt64(buffer, index, length); case U_INT64: + return textDecodeBigInteger(collationId, buffer, index, length); case NUMERIC: return textDecodeNUMERIC(collationId, buffer, index, length); case FLOAT: @@ -669,6 +670,11 @@ private static Long textDecodeBit(ByteBuf buffer, int index, int length) { return decodeBit(buffer, index, length); } + private static Number textDecodeBigInteger(int collationId, ByteBuf buff, int index, int length) { + Charset charset = MySQLCollation.getJavaCharsetByCollationId(collationId); + return new BigInteger(buff.toString(index, length, charset)); + } + private static Number textDecodeNUMERIC(int collationId, ByteBuf buff, int index, int length) { Charset charset = MySQLCollation.getJavaCharsetByCollationId(collationId); return Numeric.parse(buff.toString(index, length, charset)); diff --git a/vertx-mysql-client/src/test/java/io/vertx/mysqlclient/data/NumericDataTypeTest.java b/vertx-mysql-client/src/test/java/io/vertx/mysqlclient/data/NumericDataTypeTest.java index bc240f245..18154801d 100644 --- a/vertx-mysql-client/src/test/java/io/vertx/mysqlclient/data/NumericDataTypeTest.java +++ b/vertx-mysql-client/src/test/java/io/vertx/mysqlclient/data/NumericDataTypeTest.java @@ -21,6 +21,7 @@ import org.junit.runner.RunWith; import java.math.BigDecimal; +import java.math.BigInteger; @RunWith(VertxUnitRunner.class) public class NumericDataTypeTest extends MySQLDataTypeTestBase { @@ -218,12 +219,12 @@ public void testTextDecodeUnsignedInt(TestContext ctx) { @Test public void testTextDecodeUnsignedBigInt(TestContext ctx) { testTextDecodeGenericWithTable(ctx, "test_unsigned_bigint", ((row, columnName) -> { - ctx.assertTrue(row.getValue(0) instanceof Numeric); - ctx.assertEquals(Numeric.parse("18446744073709551615"), row.getValue(0)); - ctx.assertEquals(Numeric.parse("18446744073709551615"), row.getValue(columnName)); - ctx.assertEquals(Numeric.parse("18446744073709551615"), row.get(Numeric.class, 0)); - ctx.assertEquals(new BigDecimal("18446744073709551615"), row.getBigDecimal(0)); - ctx.assertEquals(new BigDecimal("18446744073709551615"), row.getBigDecimal(columnName)); + ctx.assertTrue(row.getValue(0) instanceof BigInteger); + ctx.assertEquals(new BigInteger("18446744073709551615"), row.getValue(0)); + ctx.assertEquals(new BigInteger("18446744073709551615"), row.getValue(columnName)); + ctx.assertEquals(new BigInteger("18446744073709551615"), row.get(BigInteger.class, 0)); + ctx.assertEquals(new BigInteger("18446744073709551615"), row.getBigInteger(0)); + ctx.assertEquals(new BigInteger("18446744073709551615"), row.getBigInteger(columnName)); })); } } diff --git a/vertx-sql-client/src/main/java/io/vertx/sqlclient/Row.java b/vertx-sql-client/src/main/java/io/vertx/sqlclient/Row.java index b8b18f77a..92deffe08 100644 --- a/vertx-sql-client/src/main/java/io/vertx/sqlclient/Row.java +++ b/vertx-sql-client/src/main/java/io/vertx/sqlclient/Row.java @@ -25,6 +25,7 @@ import io.vertx.sqlclient.impl.Utils; import java.math.BigDecimal; +import java.math.BigInteger; import java.time.*; import java.time.temporal.Temporal; import java.util.NoSuchElementException; @@ -383,6 +384,22 @@ default BigDecimal getBigDecimal(String column) { return getBigDecimal(pos); } + /** + * Get {@link BigInteger} value for the given {@code column}. + * + * @param column the column name + * @return the {@code column} value + * @throws NoSuchElementException when the {@code column} does not exist + */ + @GenIgnore(GenIgnore.PERMITTED_TYPE) + default BigInteger getBigInteger(String column) { + int pos = getColumnIndex(column); + if (pos == -1) { + throw new NoSuchElementException("Column " + column + " does not exist"); + } + return getBigInteger(pos); + } + /** * Get an array of {@link Boolean} value for the given {@code column}. * diff --git a/vertx-sql-client/src/main/java/io/vertx/sqlclient/Tuple.java b/vertx-sql-client/src/main/java/io/vertx/sqlclient/Tuple.java index 3db9249a0..ea4b3c997 100644 --- a/vertx-sql-client/src/main/java/io/vertx/sqlclient/Tuple.java +++ b/vertx-sql-client/src/main/java/io/vertx/sqlclient/Tuple.java @@ -25,6 +25,7 @@ import java.lang.reflect.Array; import java.math.BigDecimal; +import java.math.BigInteger; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; @@ -633,6 +634,27 @@ default BigDecimal getBigDecimal(int pos) { } } + + /** + * Get {@link BigInteger} value at {@code pos}. + * + * @param pos the position + * @return the value + */ + @GenIgnore(GenIgnore.PERMITTED_TYPE) + default BigInteger getBigInteger(int pos) { + Object val = getValue(pos); + if (val == null) { + return null; + } else if (val instanceof BigInteger) { + return (BigInteger) val; + } else if (val instanceof Number) { + return new BigInteger(val.toString()); + } else { + return (BigInteger) val; // Throw CCE + } + } + /** * Get an array of {@link Boolean} value at {@code pos}. *