Skip to content

Commit f7b1163

Browse files
authored
Fix DB2 storage of binary data in char column (#1307)
See #1304 Signed-off-by: Thomas Segismont <tsegismont@gmail.com>
1 parent f499724 commit f7b1163

File tree

4 files changed

+102
-67
lines changed

4 files changed

+102
-67
lines changed

vertx-db2-client/src/main/java/io/vertx/db2client/impl/codec/DB2ParamDesc.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ private static boolean canConvert(Object val, int type) {
6767
return clazz == Numeric.class;
6868
case ClientTypes.VARCHAR:
6969
return Enum.class.isAssignableFrom(clazz) || Buffer.class.isAssignableFrom(clazz);
70+
case ClientTypes.BINARY:
7071
case ClientTypes.VARBINARY:
7172
return Buffer.class.isAssignableFrom(clazz);
7273
}

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

Lines changed: 73 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,24 @@
1515
*/
1616
package io.vertx.db2client;
1717

18-
import static org.junit.Assume.assumeTrue;
18+
import io.vertx.core.buffer.Buffer;
19+
import io.vertx.ext.unit.TestContext;
20+
import io.vertx.ext.unit.junit.VertxUnitRunner;
21+
import io.vertx.sqlclient.Row;
22+
import io.vertx.sqlclient.RowSet;
23+
import io.vertx.sqlclient.Tuple;
24+
import org.junit.Test;
25+
import org.junit.runner.RunWith;
1926

2027
import java.sql.RowId;
2128
import java.time.LocalDateTime;
2229
import java.time.temporal.ChronoField;
2330
import java.util.Arrays;
31+
import java.util.Collections;
32+
import java.util.List;
2433
import java.util.UUID;
2534

26-
import org.junit.Test;
27-
import org.junit.runner.RunWith;
28-
29-
import io.vertx.core.buffer.Buffer;
30-
import io.vertx.ext.unit.TestContext;
31-
import io.vertx.ext.unit.junit.VertxUnitRunner;
32-
import io.vertx.sqlclient.Row;
33-
import io.vertx.sqlclient.RowSet;
34-
import io.vertx.sqlclient.Tuple;
35+
import static org.junit.Assume.assumeTrue;
3536

3637
@RunWith(VertxUnitRunner.class)
3738
public class DB2DataTypeTest extends DB2TestBase {
@@ -93,49 +94,75 @@ public void testByteIntoSmallIntColumn(TestContext ctx) {
9394
}));
9495
}
9596

97+
@Test
98+
public void testByteArrayIntoChar(TestContext ctx) {
99+
byte[] param = "hello world".getBytes();
100+
byte[] expected = new byte[255];
101+
Arrays.fill(expected, (byte) 32);
102+
System.arraycopy(param, 0, expected, 0, param.length);
103+
testByteArrayInto(ctx, "test_bytes", param, expected);
104+
}
105+
96106
@Test
97107
public void testByteArrayIntoVarchar(TestContext ctx) {
98-
byte[] expected = "hello world".getBytes();
108+
byte[] param = "hello world".getBytes();
109+
testByteArrayInto(ctx, "test_vbytes", param, param);
110+
}
111+
112+
private void testByteArrayInto(TestContext ctx, String colName, byte[] param, byte[] expected) {
99113
connect(ctx.asyncAssertSuccess(conn -> {
100114
conn
101-
.preparedQuery("INSERT INTO db2_types (id,test_bytes) VALUES (?, ?)")
102-
.execute(Tuple.of(3, "hello world".getBytes()))
115+
.preparedQuery("INSERT INTO db2_types (id," + colName + ") VALUES (?, ?)")
116+
.execute(Tuple.of(3, param))
103117
.onComplete(ctx.asyncAssertSuccess(insertResult -> {
104-
conn
105-
.preparedQuery("SELECT id,test_bytes FROM db2_types WHERE id = 3")
106-
.execute()
107-
.onComplete(ctx.asyncAssertSuccess(rows -> {
108-
ctx.assertEquals(1, rows.size());
109-
Row row = rows.iterator().next();
110-
ctx.assertEquals(3, row.getInteger(0));
111-
ctx.assertTrue(Arrays.equals(expected, row.getBuffer(1).getBytes()),
112-
"Expecting " + Arrays.toString(expected) + " but got "
113-
+ Arrays.toString(row.getBuffer(1).getBytes()));
114-
}));
115-
}));
118+
conn
119+
.preparedQuery("SELECT id," + colName + " FROM db2_types WHERE id = 3")
120+
.execute()
121+
.onComplete(ctx.asyncAssertSuccess(rows -> {
122+
ctx.assertEquals(1, rows.size());
123+
Row row = rows.iterator().next();
124+
ctx.assertEquals(3, row.getInteger(0));
125+
ctx.assertTrue(Arrays.equals(expected, row.getBuffer(1).getBytes()),
126+
"Expecting " + Arrays.toString(expected) + " but got "
127+
+ Arrays.toString(row.getBuffer(1).getBytes()));
128+
}));
129+
}));
116130
}));
117131
}
118132

119133
@Test
120-
public void testByteBufIntoVarchar(TestContext ctx) {
121-
byte[] expected = "hello world".getBytes();
134+
public void testBufferIntoChar(TestContext ctx) {
135+
byte[] param = "hello world".getBytes();
136+
byte[] expected = new byte[255];
137+
Arrays.fill(expected, (byte) 32);
138+
System.arraycopy(param, 0, expected, 0, param.length);
139+
testBufferInto(ctx, "test_bytes", param, expected);
140+
}
141+
142+
@Test
143+
public void testBufferIntoVarchar(TestContext ctx) {
144+
byte[] param = "hello world".getBytes();
145+
testBufferInto(ctx, "test_vbytes", param, param);
146+
}
147+
148+
private void testBufferInto(TestContext ctx, String colName, byte[] param, byte[] expected) {
122149
connect(ctx.asyncAssertSuccess(conn -> {
123150
conn
124-
.preparedQuery("INSERT INTO db2_types (id,test_bytes) VALUES (?, ?)")
125-
.execute(Tuple.of(4, Buffer.buffer(expected)))
151+
.preparedQuery("INSERT INTO db2_types (id," + colName + ") VALUES (?, ?)")
152+
.execute(Tuple.of(4, Buffer.buffer(param)))
126153
.onComplete(ctx.asyncAssertSuccess(insertResult -> {
127-
conn
128-
.preparedQuery("SELECT id,test_bytes FROM db2_types WHERE id = 4")
129-
.execute()
130-
.onComplete(ctx.asyncAssertSuccess(rows -> {
131-
ctx.assertEquals(1, rows.size());
132-
Row row = rows.iterator().next();
133-
ctx.assertEquals(4, row.getInteger(0));
134-
ctx.assertTrue(Arrays.equals(expected, row.getBuffer(1).getBytes()),
135-
"Expecting " + Arrays.toString(expected) + " but got "
136-
+ Arrays.toString(row.getBuffer(1).getBytes()));
137-
}));
138-
}));
154+
conn
155+
.preparedQuery("SELECT id," + colName + " FROM db2_types WHERE id = 4")
156+
.execute()
157+
.onComplete(ctx.asyncAssertSuccess(rows -> {
158+
ctx.assertEquals(1, rows.size());
159+
Row row = rows.iterator().next();
160+
ctx.assertEquals(4, row.getInteger(0));
161+
ctx.assertTrue(Arrays.equals(expected, row.getBuffer(1).getBytes()),
162+
"Expecting " + Arrays.toString(expected) + " but got "
163+
+ Arrays.toString(row.getBuffer(1).getBytes()));
164+
}));
165+
}));
139166
}));
140167
}
141168

@@ -269,4 +296,9 @@ private RowId verifyRowId(TestContext ctx, RowSet<Row> rows, String msg) {
269296
ctx.assertEquals(22, rowid.getBytes().length);
270297
return rowid;
271298
}
299+
300+
@Override
301+
protected List<String> tablesToClean() {
302+
return Collections.singletonList("db2_types");
303+
}
272304
}

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

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -70,21 +70,21 @@ CREATE TABLE basicdatatype
7070
test_date DATE,
7171
test_time TIME
7272
);
73-
INSERT INTO basicdatatype(id, test_int_2, test_int_4, test_int_8,
74-
test_float_4, test_float_8, test_numeric, test_decimal,
75-
test_boolean, test_char, test_varchar,
73+
INSERT INTO basicdatatype(id, test_int_2, test_int_4, test_int_8,
74+
test_float_4, test_float_8, test_numeric, test_decimal,
75+
test_boolean, test_char, test_varchar,
7676
test_date, test_time)
77-
VALUES (1, 32767, 2147483647, 9223372036854775807,
78-
3.40282E38, 1.7976931348623157E308, 999.99, 12345,
79-
TRUE, 'testchar', 'testvarchar',
77+
VALUES (1, 32767, 2147483647, 9223372036854775807,
78+
3.40282E38, 1.7976931348623157E308, 999.99, 12345,
79+
TRUE, 'testchar', 'testvarchar',
8080
'2019-01-01', '18:45:02');
81-
INSERT INTO basicdatatype(id, test_int_2, test_int_4, test_int_8,
82-
test_float_4, test_float_8, test_numeric, test_decimal,
83-
test_boolean, test_char, test_varchar,
81+
INSERT INTO basicdatatype(id, test_int_2, test_int_4, test_int_8,
82+
test_float_4, test_float_8, test_numeric, test_decimal,
83+
test_boolean, test_char, test_varchar,
8484
test_date, test_time)
85-
VALUES (2, 32767, 2147483647, 9223372036854775807,
86-
3.40282E38, 1.7976931348623157E308, 999.99, 12345,
87-
TRUE, 'testchar', 'testvarchar',
85+
VALUES (2, 32767, 2147483647, 9223372036854775807,
86+
3.40282E38, 1.7976931348623157E308, 999.99, 12345,
87+
TRUE, 'testchar', 'testvarchar',
8888
'2019-01-01', '18:45:02');
8989
INSERT INTO basicdatatype(id, test_int_2, test_int_4, test_int_8, test_float_4, test_float_8, test_numeric, test_decimal, test_boolean, test_char, test_varchar, test_date, test_time)
9090
VALUES (3, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
@@ -109,13 +109,14 @@ INSERT INTO collector_test VALUES (2, 32767, 2147483647, 9223372036854775807, 12
109109
DROP TABLE IF EXISTS db2_types;
110110
CREATE TABLE db2_types
111111
(
112-
id INT,
113-
test_byte SMALLINT,
114-
test_float FLOAT,
115-
test_bytes VARCHAR(255) for bit data,
116-
test_tstamp TIMESTAMP,
117-
test_vchar VARCHAR(255),
118-
test_int INT
112+
id INT,
113+
test_byte SMALLINT,
114+
test_float FLOAT,
115+
test_bytes CHAR(255) for bit data,
116+
test_vbytes VARCHAR(255) for bit data,
117+
test_tstamp TIMESTAMP,
118+
test_vchar VARCHAR(255),
119+
test_int INT
119120
);
120121

121122
-- Sequence used by QueryVariationsTest

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,14 @@ CREATE TABLE ROWTEST(
119119
DROP TABLE db2_types;
120120
CREATE TABLE db2_types
121121
(
122-
id INT,
123-
test_byte SMALLINT,
124-
test_float FLOAT,
125-
test_bytes VARCHAR(255) for bit data,
126-
test_tstamp TIMESTAMP,
127-
test_vchar VARCHAR(255),
128-
test_int INT
122+
id INT,
123+
test_byte SMALLINT,
124+
test_float FLOAT,
125+
test_bytes CHAR(255) for bit data,
126+
test_vbytes VARCHAR(255) for bit data,
127+
test_tstamp TIMESTAMP,
128+
test_vchar VARCHAR(255),
129+
test_int INT
129130
);
130131

131132
-- Sequence used by QueryVariationsTest

0 commit comments

Comments
 (0)