Skip to content

MySQL BIGINT UNSIGNED / Java BigInteger codec #1384

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -51,6 +52,8 @@ public <T> T get(Class<T> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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));
}));
}
}
17 changes: 17 additions & 0 deletions vertx-sql-client/src/main/java/io/vertx/sqlclient/Row.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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}.
*
Expand Down
22 changes: 22 additions & 0 deletions vertx-sql-client/src/main/java/io/vertx/sqlclient/Tuple.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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}.
*
Expand Down