Skip to content

Commit 2564ecb

Browse files
committed
Merge pull request #41 from vert-x3/fix-decimal-fields
Convert BigDecimal into strings to keep information
2 parents f8e04ae + ccafc80 commit 2564ecb

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

src/main/java/io/vertx/ext/asyncsql/impl/AsyncSQLConnectionImpl.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@
3434
import scala.concurrent.ExecutionContext;
3535
import scala.runtime.AbstractFunction1;
3636

37-
import java.util.ArrayList;
38-
import java.util.Collections;
39-
import java.util.List;
40-
import java.util.UUID;
37+
import java.util.*;
4138

4239
/**
4340
* Implementation of {@link SQLConnection} using the {@link AsyncConnectionPool}.
@@ -281,6 +278,8 @@ private JsonArray rowToJsonArray(RowData data) {
281278
public Void apply(Object value) {
282279
if (value == null) {
283280
array.addNull();
281+
} else if (value instanceof scala.math.BigDecimal) {
282+
array.add(value.toString());
284283
} else if (value instanceof LocalDateTime) {
285284
array.add(value.toString());
286285
} else if (value instanceof LocalDate) {

src/test/java/io/vertx/ext/asyncsql/SQLTestBase.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import io.vertx.ext.unit.TestContext;
2929
import org.junit.Test;
3030

31+
import java.math.BigDecimal;
32+
import java.util.Arrays;
3133
import java.util.List;
3234

3335
public abstract class SQLTestBase extends AbstractTestBase {
@@ -459,6 +461,37 @@ public void testDateValueSelection(TestContext context) {
459461
});
460462
}
461463

464+
@Test
465+
public void testDecimalFields(TestContext context) {
466+
Async async = context.async();
467+
client.getConnection(arConn -> {
468+
ensureSuccess(context, arConn);
469+
conn = arConn.result();
470+
conn.execute("DROP TABLE IF EXISTS test_table", arDrop -> {
471+
ensureSuccess(context, arDrop);
472+
conn.execute("CREATE TABLE test_table (id INT, some_decimal DECIMAL(65,6))", arCreate -> {
473+
ensureSuccess(context, arCreate);
474+
conn.execute("INSERT INTO test_table (id, some_decimal) VALUES " +
475+
"(1, 43210987654321098765432109876543210987654321098765432109871.123451)," +
476+
"(2, 43210987654321098765432109876543210987654321098765432109872.123452)," +
477+
"(3, 43210987654321098765432109876543210987654321098765432109873.123453)", arInsert -> {
478+
ensureSuccess(context, arInsert);
479+
conn.query("SELECT some_decimal FROM test_table ORDER BY id", arQuery -> {
480+
ensureSuccess(context, arQuery);
481+
ResultSet res = arQuery.result();
482+
context.assertEquals(new BigDecimal("43210987654321098765432109876543210987654321098765432109871.123451"), new BigDecimal(res.getRows().get(0).getString("some_decimal")));
483+
context.assertEquals(new BigDecimal("43210987654321098765432109876543210987654321098765432109872.123452"), new BigDecimal(res.getResults().get(1).getString(0)));
484+
context.assertEquals(new BigDecimal("43210987654321098765432109876543210987654321098765432109873.123453"), new BigDecimal(res.getRows().get(2).getString("some_decimal")));
485+
// This will convert both (big) numbers into a double which will loose some information
486+
context.assertEquals(43210987654321098765432109876543210987654321098765432109873.123453, Double.parseDouble(res.getRows().get(2).getString("some_decimal")));
487+
async.complete();
488+
});
489+
});
490+
});
491+
});
492+
});
493+
}
494+
462495
protected void setSqlModeIfPossible(Handler<Void> handler) {
463496
handler.handle(null);
464497
}

0 commit comments

Comments
 (0)