Skip to content

Commit 6e9d39e

Browse files
committed
Fix issue encoding a Java float into a DB2 FLOAT column type
1 parent 20376cc commit 6e9d39e

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

vertx-db2-client/src/main/java/io/vertx/db2client/impl/drda/DRDAQueryRequest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -707,20 +707,20 @@ private void buildFDODTA(int[][] protocolTypesAndLengths, Object[] inputs) {
707707
buffer.writeBoolean(((Boolean) inputs[i]).booleanValue());
708708
break;
709709
case DRDAConstants.DRDA_TYPE_NINTEGER:
710-
buffer.writeInt(((Integer) inputs[i]).intValue());
710+
buffer.writeInt(((Number) inputs[i]).intValue());
711711
break;
712712
case DRDAConstants.DRDA_TYPE_NSMALL:
713713
if (inputs[i] instanceof Boolean) {
714714
buffer.writeShort(((boolean) inputs[i]) ? 1 : 0);
715715
} else {
716-
buffer.writeShort(((Short) inputs[i]).shortValue());
716+
buffer.writeShort(((Number) inputs[i]).shortValue());
717717
}
718718
break;
719719
case DRDAConstants.DRDA_TYPE_NFLOAT4:
720-
buffer.writeFloat(((Float) inputs[i]).floatValue());
720+
buffer.writeFloat(((Number) inputs[i]).floatValue());
721721
break;
722722
case DRDAConstants.DRDA_TYPE_NFLOAT8:
723-
buffer.writeDouble(((Double) inputs[i]).doubleValue());
723+
buffer.writeDouble(((Number) inputs[i]).doubleValue());
724724
break;
725725
case DRDAConstants.DRDA_TYPE_NDECIMAL:
726726
writeBigDecimal((BigDecimal) inputs[i], (protocolTypesAndLengths[i][1] >> 8) & 0xff, // described precision not actual

vertx-db2-client/src/test/java/io/vertx/db2client/DB2DataTypeTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,28 @@
3131
@RunWith(VertxUnitRunner.class)
3232
public class DB2DataTypeTest extends DB2TestBase {
3333

34+
/**
35+
* In DB2 the FLOAT and DOUBLE column types both map to an 8-byte
36+
* double-precision column (i.e. Java double). Ensure that a Java
37+
* float can still be inserted and selected from such a column
38+
*/
39+
@Test
40+
public void testInsertFloatColumn(TestContext ctx) {
41+
connect(ctx.asyncAssertSuccess(conn -> {
42+
// Insert some data
43+
conn.preparedQuery("INSERT INTO db2_types (id,test_float) VALUES (?, ?)")
44+
.execute(Tuple.of(1, 5.0f), ctx.asyncAssertSuccess(insertResult -> {
45+
conn.query("SELECT * FROM db2_types WHERE id = 1")
46+
.execute(ctx.asyncAssertSuccess(rows -> {
47+
ctx.assertEquals(1, rows.size());
48+
Row row = rows.iterator().next();
49+
ctx.assertEquals(1, row.getInteger(0));
50+
ctx.assertEquals(5.0f, row.getFloat(1));
51+
}));
52+
}));
53+
}));
54+
}
55+
3456
@Test
3557
public void testRowId(TestContext ctx) {
3658
assumeTrue("Only DB2/Z supports the ROWID column type", rule.isZOS());

vertx-db2-client/src/test/resources/init.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,11 @@ INSERT INTO collector_test VALUES (2, 32767, 2147483647, 9223372036854775807, 12
108108
-- Sequence used by QueryVariationsTest
109109
DROP SEQUENCE my_seq;
110110
CREATE SEQUENCE my_seq INCREMENT BY 1 START WITH 1;
111+
112+
-- used by DB2DataTypeTest
113+
DROP TABLE IF EXISTS db2_types;
114+
CREATE TABLE db2_types
115+
(
116+
id INT,
117+
test_float FLOAT
118+
);

vertx-db2-client/src/test/resources/init.zos.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,14 @@ CREATE TABLE ROWTEST(
115115
message varchar(1024)
116116
);
117117

118+
-- used by DB2DataTypeTest
119+
DROP TABLE IF EXISTS db2_types;
120+
CREATE TABLE db2_types
121+
(
122+
id INT,
123+
test_float FLOAT
124+
);
125+
118126
-- Sequence used by QueryVariationsTest
119127
DROP SEQUENCE my_seq;
120128
CREATE SEQUENCE my_seq INCREMENT BY 1 START WITH 1;

0 commit comments

Comments
 (0)