Skip to content

Commit

Permalink
[CONJ-985] ResultSet.getObject() returns ByteSet instead of byte[] fo…
Browse files Browse the repository at this point in the history
…r BIT(x>1) and not Boolean for BIT(1)
  • Loading branch information
rusher committed Jun 23, 2022
1 parent 8033c1f commit 17e60c8
Show file tree
Hide file tree
Showing 14 changed files with 62 additions and 48 deletions.
6 changes: 3 additions & 3 deletions src/main/java/org/mariadb/jdbc/ClientPreparedStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ private void executeInternalPreparedBatch() throws SQLException {
if (possibleLoadLocal) {
String sqlUpper = sql.toUpperCase(Locale.ROOT);
possibleLoadLocal =
sqlUpper.contains(" LOCAL ")
&& sqlUpper.contains("LOAD")
&& sqlUpper.contains(" INFILE");
sqlUpper.contains(" LOCAL ")
&& sqlUpper.contains("LOAD")
&& sqlUpper.contains(" INFILE");
}
if (possibleLoadLocal) {
executeBatchStd();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.util.concurrent.locks.ReentrantLock;
import java.util.regex.Pattern;
import org.mariadb.jdbc.client.Completion;
import org.mariadb.jdbc.client.Context;
import org.mariadb.jdbc.client.result.CompleteResult;
import org.mariadb.jdbc.client.result.Result;
import org.mariadb.jdbc.client.util.Parameters;
Expand Down
16 changes: 6 additions & 10 deletions src/main/java/org/mariadb/jdbc/Statement.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

package org.mariadb.jdbc;

import static org.mariadb.jdbc.util.constants.Capabilities.LOCAL_FILES;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -20,8 +22,6 @@
import org.mariadb.jdbc.util.NativeSql;
import org.mariadb.jdbc.util.constants.ServerStatus;

import static org.mariadb.jdbc.util.constants.Capabilities.LOCAL_FILES;

/** Statement implementation */
public class Statement implements java.sql.Statement {

Expand Down Expand Up @@ -709,9 +709,7 @@ public int[] executeBatch() throws SQLException {
}

List<Completion> res =
possibleLoadLocal
? executeInternalBatchStandard()
: executeInternalBatchPipeline();
possibleLoadLocal ? executeInternalBatchStandard() : executeInternalBatchPipeline();

results = res;

Expand Down Expand Up @@ -857,10 +855,10 @@ public ResultSet getGeneratedKeys() throws SQLException {
OkPacket ok = ((OkPacket) currResult);
if (ok.getLastInsertId() != 0) {
List<String[]> insertIds = new ArrayList<>();
insertIds.add(new String[]{String.valueOf(ok.getLastInsertId())});
insertIds.add(new String[] {String.valueOf(ok.getLastInsertId())});
for (Completion result : results) {
if (result instanceof OkPacket) {
insertIds.add(new String[]{String.valueOf(((OkPacket) result).getLastInsertId())});
insertIds.add(new String[] {String.valueOf(((OkPacket) result).getLastInsertId())});
}
}
String[][] ids = insertIds.toArray(new String[0][]);
Expand Down Expand Up @@ -1460,9 +1458,7 @@ public long[] executeLargeBatch() throws SQLException {
}

List<Completion> res =
possibleLoadLocal
? executeInternalBatchStandard()
: executeInternalBatchPipeline();
possibleLoadLocal ? executeInternalBatchStandard() : executeInternalBatchPipeline();

results = res;
long[] updates = new long[res.size()];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,8 @@ public Codec<?> getDefaultCodec(Configuration conf) {
if (conf.yearIsDateType()) return DateCodec.INSTANCE;
return ShortCodec.INSTANCE;
case BIT:
return BitSetCodec.INSTANCE;
if (this.length == 1) return BooleanCodec.INSTANCE;
return ByteArrayCodec.INSTANCE;
}
throw new IllegalArgumentException(String.format("Unexpected datatype %s", dataType));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class ByteArrayCodec implements Codec<byte[]> {
DataType.STRING);

public String className() {
return byte[].class.getName();
return "byte[]";
}

public boolean canDecode(Column column, Class<?> type) {
Expand All @@ -59,6 +59,7 @@ public byte[] decodeText(ReadableByteBuf buf, int length, Column column, Calenda

private byte[] getBytes(ReadableByteBuf buf, int length, Column column) throws SQLDataException {
switch (column.getType()) {
case BIT:
case BLOB:
case TINYBLOB:
case MEDIUMBLOB:
Expand Down
11 changes: 8 additions & 3 deletions src/test/java/org/mariadb/jdbc/integration/StatementTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,16 @@ public static void beforeAll2() throws SQLException {
public void ensureGetGeneratedKeysReturnsEmptyResult() throws SQLException {
Statement stmt = sharedConn.createStatement();
stmt.execute("CREATE TABLE IF NOT EXISTS key_test (id INT(11) NOT NULL)");
try (PreparedStatement ps = sharedConn.prepareStatement("INSERT INTO key_test(id) VALUES(5)", Statement.RETURN_GENERATED_KEYS)) {
try (PreparedStatement ps =
sharedConn.prepareStatement(
"INSERT INTO key_test(id) VALUES(5)", Statement.RETURN_GENERATED_KEYS)) {
ps.execute();
ResultSet rs = ps.getGeneratedKeys();
assertFalse(rs.next());
}
try (PreparedStatement ps = sharedConn.prepareStatement("UPDATE key_test set id=7 WHERE id=5", Statement.RETURN_GENERATED_KEYS)) {
try (PreparedStatement ps =
sharedConn.prepareStatement(
"UPDATE key_test set id=7 WHERE id=5", Statement.RETURN_GENERATED_KEYS)) {
ps.execute();
ResultSet rs = ps.getGeneratedKeys();
assertFalse(rs.next());
Expand Down Expand Up @@ -913,7 +917,8 @@ public void ensureClassDefined() {
Type it = codec.getClass().getGenericInterfaces()[0];
ParameterizedType parameterizedType = (ParameterizedType) it;
Type typeParameter = parameterizedType.getActualTypeArguments()[0];
assertEquals(((Class<?>) typeParameter).getName(), codec.className());
if (!"byte[]".equals(codec.className()))
assertEquals(((Class<?>) typeParameter).getName(), codec.className());
}
}

Expand Down
42 changes: 28 additions & 14 deletions src/test/java/org/mariadb/jdbc/integration/codec/BitCodecTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ public void getObjectPrepare() throws SQLException {
}

public void getObject(ResultSet rs) throws SQLException {
assertEquals(BitSet.valueOf(new byte[] {(byte) 0}), rs.getObject(1));
assertEquals(false, rs.getObject(1));
assertFalse(rs.wasNull());
assertEquals(BitSet.valueOf(new byte[] {(byte) 1}), rs.getObject(2));
assertEquals(BitSet.valueOf(new byte[] {(byte) 1}), rs.getObject("t2alias"));
assertArrayEquals(new byte[] {(byte) 1}, (byte[]) rs.getObject(2));
assertArrayEquals(new byte[] {(byte) 1}, (byte[]) rs.getObject("t2alias"));
assertFalse(rs.wasNull());
assertEquals(BitSet.valueOf(new byte[] {(byte) 4, (byte) 15}), rs.getObject(3));
assertArrayEquals(new byte[] {(byte) 15, (byte) 4}, (byte[]) rs.getObject(3));
assertFalse(rs.wasNull());
assertNull(rs.getObject(4));
assertTrue(rs.wasNull());
Expand Down Expand Up @@ -112,7 +112,7 @@ public void getObjectType(ResultSet rs) throws Exception {
testErrObject(rs, Double.class);
testErrObject(rs, Float.class);
testObject(rs, Byte.class, (byte) 0);
testErrObject(rs, byte[].class);
testObject(rs, byte[].class, new byte[] {0});
testErrObject(rs, Date.class);
testErrObject(rs, Time.class);
testErrObject(rs, Timestamp.class);
Expand Down Expand Up @@ -510,13 +510,16 @@ public void getBytesPrepare() throws SQLException {
getBytes(getPrepare(sharedConnBinary));
}

public void getBytes(ResultSet rs) {
Common.assertThrowsContains(
SQLDataException.class, () -> rs.getBytes(1), "Data type BIT cannot be decoded as byte[]");
Common.assertThrowsContains(
SQLDataException.class,
() -> rs.getBytes("t1alias"),
"Data type BIT cannot be decoded as byte[]");
public void getBytes(ResultSet rs) throws SQLException {
assertArrayEquals(new byte[] {(byte) 0}, rs.getBytes(1));
assertFalse(rs.wasNull());
assertArrayEquals(new byte[] {(byte) 1}, rs.getBytes(2));
assertArrayEquals(new byte[] {(byte) 1}, rs.getBytes("t2alias"));
assertFalse(rs.wasNull());
assertArrayEquals(new byte[] {(byte) 15, (byte) 4}, rs.getBytes(3));
assertFalse(rs.wasNull());
assertNull(rs.getObject(4));
assertTrue(rs.wasNull());
}

@Test
Expand Down Expand Up @@ -651,15 +654,26 @@ public void getMetaData() throws SQLException {
ResultSetMetaData meta = rs.getMetaData();
assertEquals("BIT", meta.getColumnTypeName(1));
assertEquals(sharedConn.getCatalog(), meta.getCatalogName(1));
assertEquals("java.util.BitSet", meta.getColumnClassName(1));
assertEquals("java.lang.Boolean", meta.getColumnClassName(1));
assertEquals("t1alias", meta.getColumnLabel(1));
assertEquals("t1", meta.getColumnName(1));
assertEquals(Types.BIT, meta.getColumnType(1));
assertEquals(Types.BOOLEAN, meta.getColumnType(1));
assertEquals(4, meta.getColumnCount());
assertEquals(1, meta.getPrecision(1));
assertEquals(0, meta.getScale(1));
assertEquals("", meta.getSchemaName(1));
assertEquals(1, meta.getColumnDisplaySize(1));

assertEquals("BIT", meta.getColumnTypeName(2));
assertEquals(sharedConn.getCatalog(), meta.getCatalogName(2));
assertEquals("byte[]", meta.getColumnClassName(2));
assertEquals("t2alias", meta.getColumnLabel(2));
assertEquals("t2", meta.getColumnName(2));
assertEquals(Types.VARBINARY, meta.getColumnType(2));
assertEquals(4, meta.getPrecision(2));
assertEquals(0, meta.getScale(2));
assertEquals("", meta.getSchemaName(2));
assertEquals(4, meta.getColumnDisplaySize(2));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,7 @@ && minVersion(10, 5, 1)
}
assertEquals(sharedConn.getCatalog(), meta.getCatalogName(1));
assertEquals(
geoDefault ? GeometryCollection.class.getName() : byte[].class.getName(),
meta.getColumnClassName(1));
geoDefault ? GeometryCollection.class.getName() : "byte[]", meta.getColumnClassName(1));
assertEquals("t1alias", meta.getColumnLabel(1));
assertEquals("t1", meta.getColumnName(1));
assertEquals(Types.VARBINARY, meta.getColumnType(1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ && minVersion(10, 5, 1)
&& !"skysql-ha".equals(System.getenv("srv")))
? LineString.class.getName()
: GeometryCollection.class.getName())
: byte[].class.getName(),
: "byte[]",
meta.getColumnClassName(1));
assertEquals("t1alias", meta.getColumnLabel(1));
assertEquals("t1", meta.getColumnName(1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ && minVersion(10, 5, 1)
&& !"skysql-ha".equals(System.getenv("srv")))
? MultiLineString.class.getName()
: GeometryCollection.class.getName())
: byte[].class.getName(),
: "byte[]",
meta.getColumnClassName(1));
assertEquals("t1alias", meta.getColumnLabel(1));
assertEquals("t1", meta.getColumnName(1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ && minVersion(10, 5, 1)
&& !"skysql-ha".equals(System.getenv("srv")))
? MultiPoint.class.getName()
: GeometryCollection.class.getName())
: byte[].class.getName(),
: "byte[]",
meta.getColumnClassName(1));
assertEquals("t1alias", meta.getColumnLabel(1));
assertEquals("t1", meta.getColumnName(1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ && minVersion(10, 5, 1)
&& !"skysql-ha".equals(System.getenv("srv")))
? MultiPolygon.class.getName()
: GeometryCollection.class.getName())
: byte[].class.getName(),
: "byte[]",
meta.getColumnClassName(1));
assertEquals("t1alias", meta.getColumnLabel(1));
assertEquals("t1", meta.getColumnName(1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ && minVersion(10, 5, 1)
&& !"skysql-ha".equals(System.getenv("srv")))
? Point.class.getName()
: GeometryCollection.class.getName())
: byte[].class.getName(),
: "byte[]",
meta.getColumnClassName(1));

assertEquals("t1alias", meta.getColumnLabel(1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,14 +356,13 @@ && minVersion(10, 5, 1)
assertEquals(sharedConn.getCatalog(), meta.getCatalogName(1));
assertEquals(
(geoDefault
? ((isMariaDBServer()
&& minVersion(10, 5, 1)
&& !"maxscale".equals(System.getenv("srv"))
&& !"skysql-ha".equals(System.getenv("srv")))
? Polygon.class
: GeometryCollection.class)
: byte[].class)
.getName(),
? ((isMariaDBServer()
&& minVersion(10, 5, 1)
&& !"maxscale".equals(System.getenv("srv"))
&& !"skysql-ha".equals(System.getenv("srv")))
? Polygon.class.getName()
: GeometryCollection.class.getName())
: "byte[]"),
meta.getColumnClassName(1));
assertEquals("t1alias", meta.getColumnLabel(1));
assertEquals("t1", meta.getColumnName(1));
Expand Down

0 comments on commit 17e60c8

Please sign in to comment.