From 753ac15fbd9e2920ec1ef44a2e47f8aa559a2ad2 Mon Sep 17 00:00:00 2001 From: rusher Date: Thu, 16 Mar 2023 11:33:16 +0100 Subject: [PATCH 1/8] bump 3.1.4-SNAPSHOT --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index dc2c38774..f47e21fae 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ mariadb-java-client jar mariadb-java-client - 3.1.3 + 3.1.4-SNAPSHOT JDBC driver for MariaDB and MySQL https://mariadb.com/kb/en/mariadb/about-mariadb-connector-j/ From 144831637f693b6a65d2097dba3a4033ceb3d3bd Mon Sep 17 00:00:00 2001 From: rusher Date: Tue, 4 Apr 2023 02:26:48 +0200 Subject: [PATCH 2/8] [CONJ-1067] ResultSetMetaData.getColumnTypeName() does not return UNSIGNED for DECIMAL, DOUBLE and FLOAT columns --- .../jdbc/client/column/BigDecimalColumn.java | 3 ++- .../jdbc/client/column/DoubleColumn.java | 3 ++- .../jdbc/client/column/FloatColumn.java | 3 ++- .../jdbc/integration/StatementTest.java | 25 +++++++++++++++++++ 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/mariadb/jdbc/client/column/BigDecimalColumn.java b/src/main/java/org/mariadb/jdbc/client/column/BigDecimalColumn.java index 527d45b1f..b3f1d31d1 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/BigDecimalColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/BigDecimalColumn.java @@ -53,7 +53,8 @@ public int getColumnType(Configuration conf) { } public String getColumnTypeName(Configuration conf) { - return "DECIMAL"; + if (isSigned()) return "DECIMAL"; + return "DECIMAL UNSIGNED"; } public int getPrecision() { diff --git a/src/main/java/org/mariadb/jdbc/client/column/DoubleColumn.java b/src/main/java/org/mariadb/jdbc/client/column/DoubleColumn.java index c8571dfa7..8b73898e4 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/DoubleColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/DoubleColumn.java @@ -52,7 +52,8 @@ public int getColumnType(Configuration conf) { } public String getColumnTypeName(Configuration conf) { - return "DOUBLE"; + if (isSigned()) return "DOUBLE"; + return "DOUBLE UNSIGNED"; } @Override diff --git a/src/main/java/org/mariadb/jdbc/client/column/FloatColumn.java b/src/main/java/org/mariadb/jdbc/client/column/FloatColumn.java index 365ecaf91..ce63e9a18 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/FloatColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/FloatColumn.java @@ -52,7 +52,8 @@ public int getColumnType(Configuration conf) { } public String getColumnTypeName(Configuration conf) { - return "FLOAT"; + if (isSigned()) return "FLOAT"; + return "FLOAT UNSIGNED"; } @Override diff --git a/src/test/java/org/mariadb/jdbc/integration/StatementTest.java b/src/test/java/org/mariadb/jdbc/integration/StatementTest.java index c7c76da0d..df9fd3873 100644 --- a/src/test/java/org/mariadb/jdbc/integration/StatementTest.java +++ b/src/test/java/org/mariadb/jdbc/integration/StatementTest.java @@ -105,6 +105,31 @@ public void longGeneratedId(BigInteger expected) throws SQLException { assertTrue(new BigDecimal(expected).compareTo(rs.getBigDecimal(1)) == 0); } + @Test + public void unsignedMetadataResult() throws SQLException { + Statement stmt = sharedConn.createStatement(); + stmt.execute("DROP TABLE IF EXISTS unsignedMetadataResult"); + stmt.execute( + "CREATE TABLE unsignedMetadataResult(" + + "c0 TINYINT UNSIGNED, " + + "c1 SMALLINT UNSIGNED, " + + "c2 MEDIUMINT UNSIGNED, " + + "c3 INTEGER UNSIGNED, " + + "c4 BIGINT UNSIGNED, " + + "c5 DOUBLE UNSIGNED, " + + "c6 FLOAT UNSIGNED, " + + "c7 DECIMAL UNSIGNED)"); + stmt.execute("INSERT INTO unsignedMetadataResult VALUES(10,11,12,13,14,15,16,17)"); + assertTrue(stmt.execute("SELECT * FROM unsignedMetadataResult")); + + ResultSet rs = stmt.getResultSet(); + ResultSetMetaData rsMetaData = rs.getMetaData(); + for (int i = 1; i <= rsMetaData.getColumnCount(); i++) { + assertTrue(rsMetaData.getColumnTypeName(i).contains("UNSIGNED")); + } + stmt.execute("DROP TABLE unsignedMetadataResult"); + } + @Test public void getConnection() throws SQLException { Statement stmt = sharedConn.createStatement(); From ba7f739c72aefa667f6d027ff83dc221f6faa981 Mon Sep 17 00:00:00 2001 From: rusher Date: Wed, 12 Apr 2023 10:53:51 +0200 Subject: [PATCH 3/8] [CONJ-1070] getBlob on TEXT columns throw Exception --- .../mariadb/jdbc/plugin/codec/BlobCodec.java | 24 ------------------- .../jdbc/integration/codec/CharCodecTest.java | 14 ++++++++--- .../jdbc/integration/codec/ClobCodecTest.java | 16 +++++++++---- .../jdbc/integration/codec/EnumCodecTest.java | 18 ++++++++++---- .../integration/codec/VarcharCodecTest.java | 16 +++++++++---- 5 files changed, 46 insertions(+), 42 deletions(-) diff --git a/src/main/java/org/mariadb/jdbc/plugin/codec/BlobCodec.java b/src/main/java/org/mariadb/jdbc/plugin/codec/BlobCodec.java index 65b7b8b6b..9b3cc2c76 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/BlobCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/BlobCodec.java @@ -56,24 +56,12 @@ public Blob decodeText(ReadableByteBuf buf, int length, ColumnDecoder column, Ca case STRING: case VARCHAR: case VARSTRING: - if (!column.isBinary()) { - buf.skip(length); - throw new SQLDataException( - String.format( - "Data type %s (not binary) cannot be decoded as Blob", column.getType())); - } case BIT: case TINYBLOB: case MEDIUMBLOB: case LONGBLOB: case BLOB: case GEOMETRY: - if (!column.isBinary()) { - buf.skip(length); - throw new SQLDataException( - String.format( - "Data type %s (not binary) cannot be decoded as Blob", column.getType())); - } return buf.readBlob(length); default: @@ -91,24 +79,12 @@ public Blob decodeBinary(ReadableByteBuf buf, int length, ColumnDecoder column, case STRING: case VARCHAR: case VARSTRING: - if (!column.isBinary()) { - buf.skip(length); - throw new SQLDataException( - String.format( - "Data type %s (not binary) cannot be decoded as Blob", column.getType())); - } case BIT: case TINYBLOB: case MEDIUMBLOB: case LONGBLOB: case BLOB: case GEOMETRY: - if (!column.isBinary()) { - buf.skip(length); - throw new SQLDataException( - String.format( - "Data type %s (not binary) cannot be decoded as Blob", column.getType())); - } buf.skip(length); return new MariaDbBlob(buf.buf(), buf.pos() - length, length); diff --git a/src/test/java/org/mariadb/jdbc/integration/codec/CharCodecTest.java b/src/test/java/org/mariadb/jdbc/integration/codec/CharCodecTest.java index 3bcc93455..78a71086d 100644 --- a/src/test/java/org/mariadb/jdbc/integration/codec/CharCodecTest.java +++ b/src/test/java/org/mariadb/jdbc/integration/codec/CharCodecTest.java @@ -20,6 +20,7 @@ import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import org.mariadb.jdbc.MariaDbBlob; import org.mariadb.jdbc.MariaDbClob; import org.mariadb.jdbc.Statement; import org.mariadb.jdbc.integration.Common; @@ -657,9 +658,16 @@ public void getBlobPrepare() throws Exception { getBlob(getPrepare(sharedConnBinary)); } - public void getBlob(ResultSet rs) { - Common.assertThrowsContains( - SQLDataException.class, () -> rs.getBlob(1), "(not binary) cannot be decoded as Blob"); + public void getBlob(ResultSet rs) throws Exception { + assertStreamEquals(new MariaDbBlob("0".getBytes()), rs.getBlob(1)); + assertFalse(rs.wasNull()); + assertStreamEquals(new MariaDbBlob("1".getBytes()), rs.getBlob(2)); + assertStreamEquals(new MariaDbBlob("1".getBytes()), rs.getBlob("t2alias")); + assertFalse(rs.wasNull()); + assertStreamEquals(new MariaDbBlob("some🌟".getBytes(StandardCharsets.UTF_8)), rs.getBlob(3)); + assertFalse(rs.wasNull()); + assertNull(rs.getBlob(4)); + assertTrue(rs.wasNull()); } @Test diff --git a/src/test/java/org/mariadb/jdbc/integration/codec/ClobCodecTest.java b/src/test/java/org/mariadb/jdbc/integration/codec/ClobCodecTest.java index 86005be29..81db4c956 100644 --- a/src/test/java/org/mariadb/jdbc/integration/codec/ClobCodecTest.java +++ b/src/test/java/org/mariadb/jdbc/integration/codec/ClobCodecTest.java @@ -20,6 +20,7 @@ import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import org.mariadb.jdbc.MariaDbBlob; import org.mariadb.jdbc.MariaDbClob; import org.mariadb.jdbc.Statement; import org.mariadb.jdbc.integration.Common; @@ -671,11 +672,16 @@ public void getBlobPrepare() throws Exception { getBlob(getPrepare(sharedConnBinary)); } - public void getBlob(ResultSet rs) { - Common.assertThrowsContains( - SQLDataException.class, - () -> rs.getBlob(1), - "Data type BLOB (not binary) cannot be decoded as Blob"); + public void getBlob(ResultSet rs) throws Exception { + assertStreamEquals(new MariaDbBlob("0".getBytes()), rs.getBlob(1)); + assertFalse(rs.wasNull()); + assertStreamEquals(new MariaDbBlob("1".getBytes()), rs.getBlob(2)); + assertStreamEquals(new MariaDbBlob("1".getBytes()), rs.getBlob("t2alias")); + assertFalse(rs.wasNull()); + assertStreamEquals(new MariaDbClob("some🌟".getBytes(StandardCharsets.UTF_8)), rs.getClob(3)); + assertFalse(rs.wasNull()); + assertNull(rs.getClob(4)); + assertTrue(rs.wasNull()); } @Test diff --git a/src/test/java/org/mariadb/jdbc/integration/codec/EnumCodecTest.java b/src/test/java/org/mariadb/jdbc/integration/codec/EnumCodecTest.java index 8a1eac20b..cafa8f571 100644 --- a/src/test/java/org/mariadb/jdbc/integration/codec/EnumCodecTest.java +++ b/src/test/java/org/mariadb/jdbc/integration/codec/EnumCodecTest.java @@ -21,6 +21,7 @@ import org.junit.jupiter.api.Assumptions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import org.mariadb.jdbc.MariaDbBlob; import org.mariadb.jdbc.MariaDbClob; import org.mariadb.jdbc.Statement; import org.mariadb.jdbc.integration.Common; @@ -685,19 +686,26 @@ public void getNCharacterStream(ResultSet rs) throws Exception { } @Test - public void getBlob() throws SQLException { + public void getBlob() throws Exception { getBlob(get()); } @Test - public void getBlobPrepare() throws SQLException { + public void getBlobPrepare() throws Exception { getBlob(getPrepare(sharedConn)); getBlob(getPrepare(sharedConnBinary)); } - public void getBlob(ResultSet rs) { - Common.assertThrowsContains( - SQLDataException.class, () -> rs.getBlob(1), " (not binary) cannot be decoded as Blob"); + public void getBlob(ResultSet rs) throws Exception { + assertStreamEquals(new MariaDbBlob("0".getBytes()), rs.getBlob(1)); + assertFalse(rs.wasNull()); + assertStreamEquals(new MariaDbBlob("1".getBytes()), rs.getBlob(2)); + assertStreamEquals(new MariaDbBlob("1".getBytes()), rs.getBlob("t2alias")); + assertFalse(rs.wasNull()); + assertStreamEquals(new MariaDbBlob("some🌟".getBytes(StandardCharsets.UTF_8)), rs.getBlob(3)); + assertFalse(rs.wasNull()); + assertNull(rs.getBlob(4)); + assertTrue(rs.wasNull()); } @Test diff --git a/src/test/java/org/mariadb/jdbc/integration/codec/VarcharCodecTest.java b/src/test/java/org/mariadb/jdbc/integration/codec/VarcharCodecTest.java index 1451266eb..08c1bfe21 100644 --- a/src/test/java/org/mariadb/jdbc/integration/codec/VarcharCodecTest.java +++ b/src/test/java/org/mariadb/jdbc/integration/codec/VarcharCodecTest.java @@ -22,6 +22,7 @@ import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import org.mariadb.jdbc.MariaDbBlob; import org.mariadb.jdbc.MariaDbClob; import org.mariadb.jdbc.Statement; import org.mariadb.jdbc.integration.Common; @@ -781,11 +782,16 @@ public void getBlobPrepare() throws Exception { getBlob(getPrepare(sharedConnBinary)); } - public void getBlob(ResultSet rs) { - Common.assertThrowsContains( - SQLDataException.class, - () -> rs.getBlob(1), - "Data type VARSTRING (not binary) cannot be decoded as Blob"); + public void getBlob(ResultSet rs) throws Exception { + assertStreamEquals(new MariaDbBlob("0".getBytes()), rs.getBlob(1)); + assertFalse(rs.wasNull()); + assertStreamEquals(new MariaDbBlob("1".getBytes()), rs.getBlob(2)); + assertStreamEquals(new MariaDbBlob("1".getBytes()), rs.getBlob("t2alias")); + assertFalse(rs.wasNull()); + assertStreamEquals(new MariaDbBlob("some🌟".getBytes(StandardCharsets.UTF_8)), rs.getBlob(3)); + assertFalse(rs.wasNull()); + assertNull(rs.getBlob(4)); + assertTrue(rs.wasNull()); } @Test From b83148836144081df1888dfb96c07faa3377667a Mon Sep 17 00:00:00 2001 From: rusher Date: Tue, 18 Apr 2023 18:16:55 +0200 Subject: [PATCH 4/8] [misc] using default java ISO6801 when using Instant with default timezone UTC --- .../jdbc/plugin/codec/InstantCodec.java | 22 ++-- .../integration/codec/DateTimeCodecTest.java | 105 ++++++++++++++++++ 2 files changed, 119 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/mariadb/jdbc/plugin/codec/InstantCodec.java b/src/main/java/org/mariadb/jdbc/plugin/codec/InstantCodec.java index ee6f3442a..5ae5a6d74 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/InstantCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/InstantCodec.java @@ -74,15 +74,21 @@ public void encodeText( Instant instant = (Instant) val; encoder.writeByte('\''); - ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, ZoneId.systemDefault()); - if (calParam != null) { - zonedDateTime = zonedDateTime.withZoneSameInstant(calParam.getTimeZone().toZoneId()); + if (calParam == null && "UTC".equals(ZoneId.systemDefault().getId())) { + // reusing ISO6801 format, replacing T by space and removing Z + encoder.writeAscii(instant.toString().replace('T', ' ')); + encoder.pos(encoder.pos() - 1); // remove 'Z' + } else { + ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, ZoneId.systemDefault()); + if (calParam != null) { + zonedDateTime = zonedDateTime.withZoneSameInstant(calParam.getTimeZone().toZoneId()); + } + encoder.writeAscii( + zonedDateTime.format( + instant.getNano() != 0 + ? LocalDateTimeCodec.TIMESTAMP_FORMAT + : LocalDateTimeCodec.TIMESTAMP_FORMAT_NO_FRACTIONAL)); } - encoder.writeAscii( - zonedDateTime.format( - instant.getNano() != 0 - ? LocalDateTimeCodec.TIMESTAMP_FORMAT - : LocalDateTimeCodec.TIMESTAMP_FORMAT_NO_FRACTIONAL)); encoder.writeByte('\''); } diff --git a/src/test/java/org/mariadb/jdbc/integration/codec/DateTimeCodecTest.java b/src/test/java/org/mariadb/jdbc/integration/codec/DateTimeCodecTest.java index ab0c72404..6eacd3861 100644 --- a/src/test/java/org/mariadb/jdbc/integration/codec/DateTimeCodecTest.java +++ b/src/test/java/org/mariadb/jdbc/integration/codec/DateTimeCodecTest.java @@ -408,6 +408,14 @@ public void getDateTimezoneTest() throws SQLException { try (Connection conGmtm8 = createCon("timezone=auto")) { getDateTimezoneTestGmtm8(conGmtm8, getPrepare(conGmtm8), TimeZone.getTimeZone("GMT-8")); } + try (Connection conGmtm8 = createCon("timezone=auto&useServerPrepStmts=true")) { + getDateTimezoneTestGmtm8(conGmtm8, getPrepare(conGmtm8), TimeZone.getTimeZone("GMT-8")); + } + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); + try (Connection conUtc = createCon("timezone=UTC")) { + getDateTimezoneTestUtc(conUtc, getPrepare(conUtc), TimeZone.getTimeZone("UTC")); + } + TimeZone.setDefault(initialTz); try (Connection conAuto = createCon("timezone=auto")) { getDateTimezoneTestNormal(conAuto, getPrepare(conAuto)); @@ -447,6 +455,10 @@ public void getDateTimezoneTestGmt8(Connection conGmt8, ResultSet rs, TimeZone t prep.setInt(1, 4); prep.setObject(2, OffsetDateTime.parse("2010-01-12T17:55:12-04:00")); prep.execute(); + + prep.setInt(1, 5); + prep.setObject(2, Instant.parse("2010-01-12T17:55:13.152Z")); + prep.execute(); } conGmt8.commit(); @@ -492,6 +504,9 @@ public void getDateTimezoneTestGmt8(Connection conGmt8, ResultSet rs, TimeZone t assertEquals(1263333312000L, rs.getTimestamp(2).getTime()); assertEquals("2010-01-13 05:55:12.000000", rs.getString(2)); assertEquals("2010-01-13", rs.getDate(2).toString()); + + rs.next(); + assertEquals("2010-01-12T17:55:13.152Z", rs.getObject(2, Instant.class).toString()); } conGmt8.rollback(); } @@ -523,6 +538,10 @@ public void getDateTimezoneTestGmtm8(Connection conGmt8, ResultSet rs, TimeZone prep.setInt(1, 4); prep.setObject(2, OffsetDateTime.parse("2010-01-12T17:55:12+04:00")); prep.execute(); + + prep.setInt(1, 5); + prep.setObject(2, Instant.parse("2010-01-12T17:55:13.152Z")); + prep.execute(); } conGmt8.commit(); @@ -568,6 +587,92 @@ public void getDateTimezoneTestGmtm8(Connection conGmt8, ResultSet rs, TimeZone assertEquals(1263304512000L, rs.getTimestamp(2).getTime()); assertEquals("2010-01-12 05:55:12.000000", rs.getString(2)); assertEquals("2010-01-12", rs.getDate(2).toString()); + + rs.next(); + assertEquals("2010-01-12T17:55:13.152Z", rs.getObject(2, Instant.class).toString()); + } + conGmt8.rollback(); + } + + public void getDateTimezoneTestUtc(Connection conGmt8, ResultSet rs, TimeZone tz) + throws SQLException { + + assertEquals("2010-01-12T01:55:12Z", rs.getObject(1, OffsetDateTime.class).toString()); + + conGmt8.createStatement().execute("TRUNCATE TABLE DateTimeCodec3"); + try (PreparedStatement prep = + conGmt8.prepareStatement("INSERT INTO DateTimeCodec3 values (?,?)")) { + prep.setInt(1, -2); + prep.setString(2, "2010-01-12 01:55:12"); + prep.execute(); + + prep.setInt(1, 1); + prep.setObject(2, OffsetDateTime.parse("2010-01-12T01:55:12Z")); + prep.execute(); + + prep.setInt(1, 2); + prep.setObject(2, OffsetDateTime.parse("2010-01-12T01:55:12-01:00")); + prep.execute(); + + prep.setInt(1, 3); + prep.setObject(2, OffsetDateTime.parse("2010-01-12T01:55:12Z")); + prep.execute(); + + prep.setInt(1, 4); + prep.setObject(2, OffsetDateTime.parse("2010-01-12T17:55:12+04:00")); + prep.execute(); + + prep.setInt(1, 5); + prep.setObject(2, Instant.parse("2010-01-12T17:55:13.152Z")); + prep.execute(); + } + conGmt8.commit(); + + java.sql.Statement stmt = conGmt8.createStatement(); + stmt.execute("START TRANSACTION"); // if MAXSCALE ensure using WRITER + try (PreparedStatement prepStmt = conGmt8.prepareStatement("select * from DateTimeCodec3")) { + rs = prepStmt.executeQuery(); + rs.next(); + assertEquals("2010-01-12T01:55:12Z", rs.getObject(2, OffsetDateTime.class).toString()); + assertEquals("2010-01-12 01:55:12.000000", rs.getString(2)); + + rs.next(); + assertEquals("2010-01-12T01:55:12Z", rs.getObject(2, OffsetDateTime.class).toString()); + assertEquals("2010-01-12 01:55:12.0", rs.getTimestamp(2).toString()); + assertEquals(1263261312000L, rs.getTimestamp(2).getTime()); + assertEquals( + "2010-01-12 02:55:12.0", + rs.getTimestamp(2, Calendar.getInstance(TimeZone.getTimeZone("GMT-1:00"))).toString()); + assertEquals("2010-01-12 01:55:12.000000", rs.getString(2)); + assertEquals("2010-01-12", rs.getDate(2).toString()); + assertEquals( + "2010-01-12", + rs.getDate(2, Calendar.getInstance(TimeZone.getTimeZone("UTC"))).toString()); + assertEquals("2010-01-12T01:55:12", rs.getObject(2, LocalDateTime.class).toString()); + + rs.next(); + assertEquals("2010-01-12T02:55:12Z", rs.getObject(2, OffsetDateTime.class).toString()); + assertEquals("2010-01-12 02:55:12.0", rs.getTimestamp(2).toString()); + assertEquals(1263264912000L, rs.getTimestamp(2).getTime()); + assertEquals("2010-01-12 02:55:12.000000", rs.getString(2)); + assertEquals("2010-01-12", rs.getDate(2).toString()); + + rs.next(); + assertEquals("2010-01-12T01:55:12Z", rs.getObject(2, OffsetDateTime.class).toString()); + assertEquals("2010-01-12 01:55:12.0", rs.getTimestamp(2).toString()); + assertEquals(1263261312000L, rs.getTimestamp(2).getTime()); + assertEquals("2010-01-12 01:55:12.000000", rs.getString(2)); + assertEquals("2010-01-12", rs.getDate(2).toString()); + + rs.next(); + assertEquals("2010-01-12T13:55:12Z", rs.getObject(2, OffsetDateTime.class).toString()); + assertEquals("2010-01-12 13:55:12.0", rs.getTimestamp(2).toString()); + assertEquals(1263304512000L, rs.getTimestamp(2).getTime()); + assertEquals("2010-01-12 13:55:12.000000", rs.getString(2)); + assertEquals("2010-01-12", rs.getDate(2).toString()); + + rs.next(); + assertEquals("2010-01-12T17:55:13.152Z", rs.getObject(2, Instant.class).toString()); } conGmt8.rollback(); } From 2ebdd43e08b3f9eb9a27c3fe63c03fb61b5f4e9b Mon Sep 17 00:00:00 2001 From: rusher Date: Mon, 24 Apr 2023 16:31:37 +0200 Subject: [PATCH 5/8] [misc] correcting unnecessary lowercase when using SQL escape sequence --- src/main/java/org/mariadb/jdbc/util/NativeSql.java | 6 +----- .../java/org/mariadb/jdbc/integration/ConnectionTest.java | 2 -- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/src/main/java/org/mariadb/jdbc/util/NativeSql.java b/src/main/java/org/mariadb/jdbc/util/NativeSql.java index 424b6936a..e3f894ccf 100644 --- a/src/main/java/org/mariadb/jdbc/util/NativeSql.java +++ b/src/main/java/org/mariadb/jdbc/util/NativeSql.java @@ -141,11 +141,10 @@ public static String parse(String sql, Context context) throws SQLException { private static String resolveEscapes(String escaped, Context context) throws SQLException { int endIndex = escaped.length() - 1; - String escapedLower = escaped.toLowerCase(Locale.ROOT); if (escaped.startsWith("{fn ")) { String resolvedParams = replaceFunctionParameter(escaped.substring(4, endIndex), context); return parse(resolvedParams, context); - } else if (escapedLower.startsWith("{oj ")) { + } else if (escaped.startsWith("{oj ")) { // Outer join // the server supports "oj" in any case, even "oJ" return parse(escaped.substring(4, endIndex), context); @@ -170,10 +169,7 @@ private static String resolveEscapes(String escaped, Context context) throws SQL } else if (escaped.startsWith("{call ") || escaped.startsWith("{CALL ")) { // We support uppercase "{CALL" only because Connector/J supports it. It is not in the JDBC // spec. - return parse(escaped.substring(1, endIndex), context); - } else if (escaped.startsWith("{escape ")) { - return escaped.substring(1, endIndex); } else if (escaped.startsWith("{?")) { // likely ?=call(...) return parse(escaped.substring(1, endIndex), context); diff --git a/src/test/java/org/mariadb/jdbc/integration/ConnectionTest.java b/src/test/java/org/mariadb/jdbc/integration/ConnectionTest.java index 0e2af5fdf..0f4c8b1f3 100644 --- a/src/test/java/org/mariadb/jdbc/integration/ConnectionTest.java +++ b/src/test/java/org/mariadb/jdbc/integration/ConnectionTest.java @@ -134,7 +134,6 @@ public void nativeSQL() throws SQLException { "{call foo(/*{fn now()}*/)}", "{CALL foo({fn now() /* -- * */ -- test \n })}", "{?=call foo({fn now()})}", - "SELECT 'David_' LIKE 'David|_' {escape '|'}", "select {fn dayname ({fn abs({fn now()})})}", "{d '1997-05-24'}", "{d'1997-05-24'}", @@ -159,7 +158,6 @@ public void nativeSQL() throws SQLException { "call foo(/*{fn now()}*/)", "CALL foo(now() /* -- * */ -- test \n )", "?=call foo(now())", - "SELECT 'David_' LIKE 'David|_' escape '|'", "select dayname (abs(now()))", "'1997-05-24'", "'1997-05-24'", From 7d24adec2bfd1ee23804bad33d14e5460cf64bc3 Mon Sep 17 00:00:00 2001 From: rusher Date: Tue, 25 Apr 2023 17:36:52 +0200 Subject: [PATCH 6/8] [CONJ-1065] Resultset.wasNull() wrong return (false) after reading zero-date --- .../mariadb/jdbc/client/ColumnDecoder.java | 57 +++--- .../jdbc/client/column/BigDecimalColumn.java | 90 ++++----- .../mariadb/jdbc/client/column/BitColumn.java | 86 ++++----- .../jdbc/client/column/BlobColumn.java | 100 +++++----- .../jdbc/client/column/DateColumn.java | 117 +++++++----- .../jdbc/client/column/DoubleColumn.java | 80 ++++---- .../jdbc/client/column/FloatColumn.java | 80 ++++---- .../jdbc/client/column/GeometryColumn.java | 17 +- .../client/column/SignedBigIntColumn.java | 80 ++++---- .../jdbc/client/column/SignedIntColumn.java | 80 ++++---- .../client/column/SignedMediumIntColumn.java | 78 ++++---- .../client/column/SignedSmallIntColumn.java | 80 ++++---- .../client/column/SignedTinyIntColumn.java | 80 ++++---- .../jdbc/client/column/StringColumn.java | 96 +++++----- .../jdbc/client/column/TimeColumn.java | 102 +++++----- .../jdbc/client/column/TimestampColumn.java | 140 ++++++++------ .../client/column/UnsignedBigIntColumn.java | 82 ++++---- .../jdbc/client/column/UnsignedIntColumn.java | 80 ++++---- .../column/UnsignedMediumIntColumn.java | 78 ++++---- .../client/column/UnsignedSmallIntColumn.java | 80 ++++---- .../client/column/UnsignedTinyIntColumn.java | 80 ++++---- .../jdbc/client/column/UuidColumn.java | 102 +++++----- .../jdbc/client/column/YearColumn.java | 19 +- .../mariadb/jdbc/client/result/Result.java | 176 +++++++++--------- .../result/rowdecoder/BinaryRowDecoder.java | 33 ++-- .../client/result/rowdecoder/RowDecoder.java | 28 +-- .../result/rowdecoder/TextRowDecoder.java | 30 +-- .../mariadb/jdbc/client/util/MutableInt.java | 10 +- .../java/org/mariadb/jdbc/plugin/Codec.java | 9 +- .../jdbc/plugin/codec/BigDecimalCodec.java | 25 +-- .../jdbc/plugin/codec/BigIntegerCodec.java | 29 +-- .../jdbc/plugin/codec/BitSetCodec.java | 11 +- .../mariadb/jdbc/plugin/codec/BlobCodec.java | 16 +- .../jdbc/plugin/codec/BooleanCodec.java | 5 +- .../jdbc/plugin/codec/ByteArrayCodec.java | 13 +- .../mariadb/jdbc/plugin/codec/ByteCodec.java | 13 +- .../mariadb/jdbc/plugin/codec/ClobCodec.java | 16 +- .../mariadb/jdbc/plugin/codec/DateCodec.java | 6 +- .../jdbc/plugin/codec/DoubleCodec.java | 7 +- .../jdbc/plugin/codec/DurationCodec.java | 43 +++-- .../mariadb/jdbc/plugin/codec/FloatCodec.java | 5 +- .../plugin/codec/GeometryCollectionCodec.java | 11 +- .../jdbc/plugin/codec/InstantCodec.java | 5 +- .../mariadb/jdbc/plugin/codec/IntCodec.java | 5 +- .../jdbc/plugin/codec/LineStringCodec.java | 11 +- .../jdbc/plugin/codec/LocalDateCodec.java | 73 +++++--- .../jdbc/plugin/codec/LocalDateTimeCodec.java | 67 +++++-- .../jdbc/plugin/codec/LocalTimeCodec.java | 64 ++++--- .../mariadb/jdbc/plugin/codec/LongCodec.java | 5 +- .../plugin/codec/MultiLinestringCodec.java | 11 +- .../jdbc/plugin/codec/MultiPointCodec.java | 11 +- .../jdbc/plugin/codec/MultiPolygonCodec.java | 11 +- .../plugin/codec/OffsetDateTimeCodec.java | 13 +- .../mariadb/jdbc/plugin/codec/PointCodec.java | 11 +- .../jdbc/plugin/codec/PolygonCodec.java | 11 +- .../jdbc/plugin/codec/ReaderCodec.java | 13 +- .../mariadb/jdbc/plugin/codec/ShortCodec.java | 5 +- .../jdbc/plugin/codec/StreamCodec.java | 19 +- .../jdbc/plugin/codec/StringCodec.java | 11 +- .../mariadb/jdbc/plugin/codec/TimeCodec.java | 6 +- .../jdbc/plugin/codec/TimestampCodec.java | 7 +- .../mariadb/jdbc/plugin/codec/UuidCodec.java | 11 +- .../jdbc/plugin/codec/ZonedDateTimeCodec.java | 5 +- .../jdbc/integration/codec/DateCodecTest.java | 11 ++ .../integration/codec/DateTimeCodecTest.java | 43 +++++ .../integration/codec/VarcharCodecTest.java | 5 + 66 files changed, 1566 insertions(+), 1238 deletions(-) diff --git a/src/main/java/org/mariadb/jdbc/client/ColumnDecoder.java b/src/main/java/org/mariadb/jdbc/client/ColumnDecoder.java index 33e1465b6..ae2a033ff 100644 --- a/src/main/java/org/mariadb/jdbc/client/ColumnDecoder.java +++ b/src/main/java/org/mariadb/jdbc/client/ColumnDecoder.java @@ -9,6 +9,7 @@ import org.mariadb.jdbc.Configuration; import org.mariadb.jdbc.client.column.UuidColumn; import org.mariadb.jdbc.client.impl.StandardReadableByteBuf; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.util.constants.ColumnFlags; public interface ColumnDecoder extends Column { @@ -55,7 +56,8 @@ default int getPrecision() { * @return default Object * @throws SQLDataException if any decoding error occurs */ - Object getDefaultText(final Configuration conf, final ReadableByteBuf buf, final int length) + Object getDefaultText( + final Configuration conf, final ReadableByteBuf buf, final MutableInt length) throws SQLDataException; /** @@ -67,7 +69,8 @@ Object getDefaultText(final Configuration conf, final ReadableByteBuf buf, final * @return default Object * @throws SQLDataException if any decoding error occurs */ - Object getDefaultBinary(final Configuration conf, final ReadableByteBuf buf, final int length) + Object getDefaultBinary( + final Configuration conf, final ReadableByteBuf buf, final MutableInt length) throws SQLDataException; /** @@ -79,7 +82,7 @@ Object getDefaultBinary(final Configuration conf, final ReadableByteBuf buf, fin * @return String value * @throws SQLDataException if any decoding error occurs */ - String decodeStringText(final ReadableByteBuf buf, final int length, final Calendar cal) + String decodeStringText(final ReadableByteBuf buf, final MutableInt length, final Calendar cal) throws SQLDataException; /** @@ -91,7 +94,7 @@ String decodeStringText(final ReadableByteBuf buf, final int length, final Calen * @return String value * @throws SQLDataException if any decoding error occurs */ - String decodeStringBinary(final ReadableByteBuf buf, final int length, final Calendar cal) + String decodeStringBinary(final ReadableByteBuf buf, final MutableInt length, final Calendar cal) throws SQLDataException; /** @@ -102,7 +105,7 @@ String decodeStringBinary(final ReadableByteBuf buf, final int length, final Cal * @return byte value * @throws SQLDataException if any decoding error occurs */ - byte decodeByteText(final ReadableByteBuf buf, final int length) throws SQLDataException; + byte decodeByteText(final ReadableByteBuf buf, final MutableInt length) throws SQLDataException; /** * Return byte binary encoded value @@ -112,7 +115,7 @@ String decodeStringBinary(final ReadableByteBuf buf, final int length, final Cal * @return byte value * @throws SQLDataException if any decoding error occurs */ - byte decodeByteBinary(final ReadableByteBuf buf, final int length) throws SQLDataException; + byte decodeByteBinary(final ReadableByteBuf buf, final MutableInt length) throws SQLDataException; /** * Return date text encoded value @@ -123,7 +126,7 @@ String decodeStringBinary(final ReadableByteBuf buf, final int length, final Cal * @return date value * @throws SQLDataException if any decoding error occurs */ - Date decodeDateText(final ReadableByteBuf buf, final int length, Calendar cal) + Date decodeDateText(final ReadableByteBuf buf, final MutableInt length, Calendar cal) throws SQLDataException; /** @@ -135,7 +138,7 @@ Date decodeDateText(final ReadableByteBuf buf, final int length, Calendar cal) * @return date value * @throws SQLDataException if any decoding error occurs */ - Date decodeDateBinary(final ReadableByteBuf buf, final int length, Calendar cal) + Date decodeDateBinary(final ReadableByteBuf buf, final MutableInt length, Calendar cal) throws SQLDataException; /** @@ -147,7 +150,7 @@ Date decodeDateBinary(final ReadableByteBuf buf, final int length, Calendar cal) * @return time value * @throws SQLDataException if any decoding error occurs */ - Time decodeTimeText(final ReadableByteBuf buf, final int length, Calendar cal) + Time decodeTimeText(final ReadableByteBuf buf, final MutableInt length, Calendar cal) throws SQLDataException; /** @@ -159,7 +162,7 @@ Time decodeTimeText(final ReadableByteBuf buf, final int length, Calendar cal) * @return time value * @throws SQLDataException if any decoding error occurs */ - Time decodeTimeBinary(final ReadableByteBuf buf, final int length, Calendar cal) + Time decodeTimeBinary(final ReadableByteBuf buf, final MutableInt length, Calendar cal) throws SQLDataException; /** @@ -171,7 +174,7 @@ Time decodeTimeBinary(final ReadableByteBuf buf, final int length, Calendar cal) * @return timestamp value * @throws SQLDataException if any decoding error occurs */ - Timestamp decodeTimestampText(final ReadableByteBuf buf, final int length, Calendar cal) + Timestamp decodeTimestampText(final ReadableByteBuf buf, final MutableInt length, Calendar cal) throws SQLDataException; /** @@ -183,7 +186,7 @@ Timestamp decodeTimestampText(final ReadableByteBuf buf, final int length, Calen * @return timestamp value * @throws SQLDataException if any decoding error occurs */ - Timestamp decodeTimestampBinary(final ReadableByteBuf buf, final int length, Calendar cal) + Timestamp decodeTimestampBinary(final ReadableByteBuf buf, final MutableInt length, Calendar cal) throws SQLDataException; /** * Return boolean text encoded value @@ -193,7 +196,8 @@ Timestamp decodeTimestampBinary(final ReadableByteBuf buf, final int length, Cal * @return boolean value * @throws SQLDataException if any decoding error occurs */ - boolean decodeBooleanText(final ReadableByteBuf buf, final int length) throws SQLDataException; + boolean decodeBooleanText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException; /** * Parse boolean binary encoded value @@ -203,7 +207,8 @@ Timestamp decodeTimestampBinary(final ReadableByteBuf buf, final int length, Cal * @return boolean value * @throws SQLDataException if any decoding error occurs */ - boolean decodeBooleanBinary(final ReadableByteBuf buf, final int length) throws SQLDataException; + boolean decodeBooleanBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException; /** * Parse short text encoded value * @@ -212,7 +217,7 @@ Timestamp decodeTimestampBinary(final ReadableByteBuf buf, final int length, Cal * @return short value * @throws SQLDataException if any decoding error occurs */ - short decodeShortText(final ReadableByteBuf buf, final int length) throws SQLDataException; + short decodeShortText(final ReadableByteBuf buf, final MutableInt length) throws SQLDataException; /** * Parse short binary encoded value * @@ -221,7 +226,8 @@ Timestamp decodeTimestampBinary(final ReadableByteBuf buf, final int length, Cal * @return short value * @throws SQLDataException if any decoding error occurs */ - short decodeShortBinary(final ReadableByteBuf buf, final int length) throws SQLDataException; + short decodeShortBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException; /** * Parse int text encoded value * @@ -230,7 +236,7 @@ Timestamp decodeTimestampBinary(final ReadableByteBuf buf, final int length, Cal * @return int value * @throws SQLDataException if any decoding error occurs */ - int decodeIntText(final ReadableByteBuf buf, final int length) throws SQLDataException; + int decodeIntText(final ReadableByteBuf buf, final MutableInt length) throws SQLDataException; /** * Parse int binary encoded value @@ -240,7 +246,7 @@ Timestamp decodeTimestampBinary(final ReadableByteBuf buf, final int length, Cal * @return int value * @throws SQLDataException if any decoding error occurs */ - int decodeIntBinary(final ReadableByteBuf buf, final int length) throws SQLDataException; + int decodeIntBinary(final ReadableByteBuf buf, final MutableInt length) throws SQLDataException; /** * Parse long text encoded value @@ -250,7 +256,7 @@ Timestamp decodeTimestampBinary(final ReadableByteBuf buf, final int length, Cal * @return long value * @throws SQLDataException if any decoding error occurs */ - long decodeLongText(final ReadableByteBuf buf, final int length) throws SQLDataException; + long decodeLongText(final ReadableByteBuf buf, final MutableInt length) throws SQLDataException; /** * Parse long binary encoded value * @@ -259,7 +265,7 @@ Timestamp decodeTimestampBinary(final ReadableByteBuf buf, final int length, Cal * @return long value * @throws SQLDataException if any decoding error occurs */ - long decodeLongBinary(final ReadableByteBuf buf, final int length) throws SQLDataException; + long decodeLongBinary(final ReadableByteBuf buf, final MutableInt length) throws SQLDataException; /** * Parse float text encoded value @@ -269,7 +275,7 @@ Timestamp decodeTimestampBinary(final ReadableByteBuf buf, final int length, Cal * @return float value * @throws SQLDataException if any decoding error occurs */ - float decodeFloatText(final ReadableByteBuf buf, final int length) throws SQLDataException; + float decodeFloatText(final ReadableByteBuf buf, final MutableInt length) throws SQLDataException; /** * Parse float binary encoded value @@ -279,7 +285,8 @@ Timestamp decodeTimestampBinary(final ReadableByteBuf buf, final int length, Cal * @return float value * @throws SQLDataException if any decoding error occurs */ - float decodeFloatBinary(final ReadableByteBuf buf, final int length) throws SQLDataException; + float decodeFloatBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException; /** * Parse double text encoded value @@ -289,7 +296,8 @@ Timestamp decodeTimestampBinary(final ReadableByteBuf buf, final int length, Cal * @return double value * @throws SQLDataException if any decoding error occurs */ - double decodeDoubleText(final ReadableByteBuf buf, final int length) throws SQLDataException; + double decodeDoubleText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException; /** * Parse double binary encoded value @@ -299,7 +307,8 @@ Timestamp decodeTimestampBinary(final ReadableByteBuf buf, final int length, Cal * @return double value * @throws SQLDataException if any decoding error occurs */ - double decodeDoubleBinary(final ReadableByteBuf buf, final int length) throws SQLDataException; + double decodeDoubleBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException; /** * Decode Column from mysql packet diff --git a/src/main/java/org/mariadb/jdbc/client/column/BigDecimalColumn.java b/src/main/java/org/mariadb/jdbc/client/column/BigDecimalColumn.java index b3f1d31d1..7b114a6e1 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/BigDecimalColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/BigDecimalColumn.java @@ -12,6 +12,7 @@ import org.mariadb.jdbc.client.ColumnDecoder; import org.mariadb.jdbc.client.DataType; import org.mariadb.jdbc.client.ReadableByteBuf; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.message.server.ColumnDefinitionPacket; /** Column metadata definition */ @@ -70,30 +71,31 @@ public int getPrecision() { } @Override - public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, int length) + public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, MutableInt length) throws SQLDataException { - return new BigDecimal(buf.readAscii(length)); + return new BigDecimal(buf.readAscii(length.get())); } @Override - public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, int length) + public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, MutableInt length) throws SQLDataException { - return new BigDecimal(buf.readAscii(length)); + return new BigDecimal(buf.readAscii(length.get())); } @Override - public boolean decodeBooleanText(ReadableByteBuf buf, int length) throws SQLDataException { - return new BigDecimal(buf.readAscii(length)).intValue() != 0; + public boolean decodeBooleanText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + return new BigDecimal(buf.readAscii(length.get())).intValue() != 0; } @Override - public boolean decodeBooleanBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public boolean decodeBooleanBinary(ReadableByteBuf buf, MutableInt length) + throws SQLDataException { return decodeBooleanText(buf, length); } @Override - public byte decodeByteText(ReadableByteBuf buf, int length) throws SQLDataException { - String str = buf.readString(length); + public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + String str = buf.readString(length.get()); try { return new BigDecimal(str).setScale(0, RoundingMode.DOWN).byteValueExact(); } catch (NumberFormatException | ArithmeticException nfe) { @@ -103,26 +105,26 @@ public byte decodeByteText(ReadableByteBuf buf, int length) throws SQLDataExcept } @Override - public byte decodeByteBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public byte decodeByteBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { return decodeByteText(buf, length); } @Override - public String decodeStringText(ReadableByteBuf buf, int length, Calendar cal) + public String decodeStringText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - return buf.readString(length); + return buf.readString(length.get()); } @Override - public String decodeStringBinary(ReadableByteBuf buf, int length, Calendar cal) + public String decodeStringBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - return buf.readString(length); + return buf.readString(length.get()); } @Override - public short decodeShortText(ReadableByteBuf buf, int length) throws SQLDataException { + public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { long result; - String str = buf.readString(length); + String str = buf.readString(length.get()); try { result = new BigDecimal(str).setScale(0, RoundingMode.DOWN).longValueExact(); } catch (NumberFormatException | ArithmeticException nfe) { @@ -135,14 +137,14 @@ public short decodeShortText(ReadableByteBuf buf, int length) throws SQLDataExce } @Override - public short decodeShortBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public short decodeShortBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { return decodeShortText(buf, length); } @Override - public int decodeIntText(ReadableByteBuf buf, int length) throws SQLDataException { + public int decodeIntText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { long result; - String str = buf.readString(length); + String str = buf.readString(length.get()); try { result = new BigDecimal(str).setScale(0, RoundingMode.DOWN).longValueExact(); } catch (NumberFormatException | ArithmeticException nfe) { @@ -156,13 +158,13 @@ public int decodeIntText(ReadableByteBuf buf, int length) throws SQLDataExceptio } @Override - public int decodeIntBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public int decodeIntBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { return decodeIntText(buf, length); } @Override - public long decodeLongText(ReadableByteBuf buf, int length) throws SQLDataException { - String str2 = buf.readAscii(length); + public long decodeLongText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + String str2 = buf.readAscii(length.get()); try { return new BigDecimal(str2).setScale(0, RoundingMode.DOWN).longValueExact(); } catch (NumberFormatException | ArithmeticException nfe) { @@ -171,8 +173,8 @@ public long decodeLongText(ReadableByteBuf buf, int length) throws SQLDataExcept } @Override - public long decodeLongBinary(ReadableByteBuf buf, int length) throws SQLDataException { - String str = buf.readString(length); + public long decodeLongBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + String str = buf.readString(length.get()); try { return new BigDecimal(str).setScale(0, RoundingMode.DOWN).longValueExact(); } catch (NumberFormatException | ArithmeticException nfe) { @@ -181,65 +183,65 @@ public long decodeLongBinary(ReadableByteBuf buf, int length) throws SQLDataExce } @Override - public float decodeFloatText(ReadableByteBuf buf, int length) throws SQLDataException { - return Float.parseFloat(buf.readAscii(length)); + public float decodeFloatText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + return Float.parseFloat(buf.readAscii(length.get())); } @Override - public float decodeFloatBinary(ReadableByteBuf buf, int length) throws SQLDataException { - return new BigDecimal(buf.readAscii(length)).floatValue(); + public float decodeFloatBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + return new BigDecimal(buf.readAscii(length.get())).floatValue(); } @Override - public double decodeDoubleText(ReadableByteBuf buf, int length) throws SQLDataException { - return Double.parseDouble(buf.readAscii(length)); + public double decodeDoubleText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + return Double.parseDouble(buf.readAscii(length.get())); } @Override - public double decodeDoubleBinary(ReadableByteBuf buf, int length) throws SQLDataException { - return new BigDecimal(buf.readAscii(length)).doubleValue(); + public double decodeDoubleBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + return new BigDecimal(buf.readAscii(length.get())).doubleValue(); } @Override - public Date decodeDateText(ReadableByteBuf buf, int length, Calendar cal) + public Date decodeDateText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Date decodeDateBinary(ReadableByteBuf buf, int length, Calendar cal) + public Date decodeDateBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Time decodeTimeText(ReadableByteBuf buf, int length, Calendar cal) + public Time decodeTimeText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Time decodeTimeBinary(ReadableByteBuf buf, int length, Calendar cal) + public Time decodeTimeBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Timestamp decodeTimestampText(ReadableByteBuf buf, int length, Calendar cal) + public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Timestamp", dataType)); } @Override - public Timestamp decodeTimestampBinary(ReadableByteBuf buf, int length, Calendar cal) + public Timestamp decodeTimestampBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Timestamp", dataType)); } diff --git a/src/main/java/org/mariadb/jdbc/client/column/BitColumn.java b/src/main/java/org/mariadb/jdbc/client/column/BitColumn.java index cc62f16fc..4a58cb072 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/BitColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/BitColumn.java @@ -10,6 +10,7 @@ import org.mariadb.jdbc.client.ColumnDecoder; import org.mariadb.jdbc.client.DataType; import org.mariadb.jdbc.client.ReadableByteBuf; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.message.server.ColumnDefinitionPacket; import org.mariadb.jdbc.plugin.codec.ByteCodec; @@ -59,48 +60,49 @@ public int getPrecision() { } @Override - public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, int length) + public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, MutableInt length) throws SQLDataException { if (columnLength == 1 && conf.transformedBitIsBoolean()) { return ByteCodec.parseBit(buf, length) != 0; } - byte[] arr = new byte[length]; + byte[] arr = new byte[length.get()]; buf.readBytes(arr); return arr; } @Override - public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, int length) + public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, MutableInt length) throws SQLDataException { return getDefaultText(conf, buf, length); } @Override - public boolean decodeBooleanText(ReadableByteBuf buf, int length) throws SQLDataException { + public boolean decodeBooleanText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { return ByteCodec.parseBit(buf, length) != 0; } @Override - public boolean decodeBooleanBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public boolean decodeBooleanBinary(ReadableByteBuf buf, MutableInt length) + throws SQLDataException { return ByteCodec.parseBit(buf, length) != 0; } @Override - public byte decodeByteText(ReadableByteBuf buf, int length) throws SQLDataException { + public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { byte val = buf.readByte(); - if (length > 1) buf.skip(length - 1); + if (length.get() > 1) buf.skip(length.get() - 1); return val; } @Override - public byte decodeByteBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public byte decodeByteBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { return decodeByteText(buf, length); } @Override - public String decodeStringText(ReadableByteBuf buf, int length, Calendar cal) + public String decodeStringText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - byte[] bytes = new byte[length]; + byte[] bytes = new byte[length.get()]; buf.readBytes(bytes); StringBuilder sb = new StringBuilder(bytes.length * Byte.SIZE + 3); sb.append("b'"); @@ -119,9 +121,9 @@ public String decodeStringText(ReadableByteBuf buf, int length, Calendar cal) } @Override - public String decodeStringBinary(ReadableByteBuf buf, int length, Calendar cal) + public String decodeStringBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - byte[] bytes = new byte[length]; + byte[] bytes = new byte[length.get()]; buf.readBytes(bytes); StringBuilder sb = new StringBuilder(bytes.length * Byte.SIZE + 3); sb.append("b'"); @@ -140,9 +142,9 @@ public String decodeStringBinary(ReadableByteBuf buf, int length, Calendar cal) } @Override - public short decodeShortText(ReadableByteBuf buf, int length) throws SQLDataException { + public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { long result = 0; - for (int i = 0; i < length; i++) { + for (int i = 0; i < length.get(); i++) { byte b = buf.readByte(); result = (result << 8) + (b & 0xff); } @@ -153,14 +155,14 @@ public short decodeShortText(ReadableByteBuf buf, int length) throws SQLDataExce } @Override - public short decodeShortBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public short decodeShortBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { return decodeShortText(buf, length); } @Override - public int decodeIntText(ReadableByteBuf buf, int length) throws SQLDataException { + public int decodeIntText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { long result = 0; - for (int i = 0; i < length; i++) { + for (int i = 0; i < length.get(); i++) { byte b = buf.readByte(); result = (result << 8) + (b & 0xff); } @@ -172,9 +174,9 @@ public int decodeIntText(ReadableByteBuf buf, int length) throws SQLDataExceptio } @Override - public int decodeIntBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public int decodeIntBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { long result = 0; - for (int i = 0; i < length; i++) { + for (int i = 0; i < length.get(); i++) { byte b = buf.readByte(); result = (result << 8) + (b & 0xff); } @@ -188,9 +190,9 @@ public int decodeIntBinary(ReadableByteBuf buf, int length) throws SQLDataExcept } @Override - public long decodeLongText(ReadableByteBuf buf, int length) throws SQLDataException { + public long decodeLongText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { long result = 0; - for (int i = 0; i < length; i++) { + for (int i = 0; i < length.get(); i++) { byte b = buf.readByte(); result = (result << 8) + (b & 0xff); } @@ -198,74 +200,74 @@ public long decodeLongText(ReadableByteBuf buf, int length) throws SQLDataExcept } @Override - public long decodeLongBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public long decodeLongBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { return decodeLongText(buf, length); } @Override - public float decodeFloatText(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public float decodeFloatText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Float", dataType)); } @Override - public float decodeFloatBinary(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public float decodeFloatBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Float", dataType)); } @Override - public double decodeDoubleText(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public double decodeDoubleText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Double", dataType)); } @Override - public double decodeDoubleBinary(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public double decodeDoubleBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Double", dataType)); } @Override - public Date decodeDateText(ReadableByteBuf buf, int length, Calendar cal) + public Date decodeDateText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Date decodeDateBinary(ReadableByteBuf buf, int length, Calendar cal) + public Date decodeDateBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Time decodeTimeText(ReadableByteBuf buf, int length, Calendar cal) + public Time decodeTimeText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Time decodeTimeBinary(ReadableByteBuf buf, int length, Calendar cal) + public Time decodeTimeBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Timestamp decodeTimestampText(ReadableByteBuf buf, int length, Calendar cal) + public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Timestamp", dataType)); } @Override - public Timestamp decodeTimestampBinary(ReadableByteBuf buf, int length, Calendar cal) + public Timestamp decodeTimestampBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Timestamp", dataType)); } diff --git a/src/main/java/org/mariadb/jdbc/client/column/BlobColumn.java b/src/main/java/org/mariadb/jdbc/client/column/BlobColumn.java index a23c8739a..5c591b46a 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/BlobColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/BlobColumn.java @@ -13,6 +13,7 @@ import org.mariadb.jdbc.client.ColumnDecoder; import org.mariadb.jdbc.client.DataType; import org.mariadb.jdbc.client.ReadableByteBuf; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.util.CharsetEncodingLength; /** Column metadata definition */ @@ -103,42 +104,43 @@ public int getPrecision() { } @Override - public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, int length) + public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, MutableInt length) throws SQLDataException { if (isBinary()) { - return buf.readBlob(length); + return buf.readBlob(length.get()); } - return buf.readString(length); + return buf.readString(length.get()); } @Override - public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, int length) + public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, MutableInt length) throws SQLDataException { return getDefaultText(conf, buf, length); } @Override - public boolean decodeBooleanText(ReadableByteBuf buf, int length) throws SQLDataException { + public boolean decodeBooleanText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { if (isBinary()) { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Boolean", dataType)); } - String s = buf.readAscii(length); + String s = buf.readAscii(length.get()); return !"0".equals(s); } @Override - public boolean decodeBooleanBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public boolean decodeBooleanBinary(ReadableByteBuf buf, MutableInt length) + throws SQLDataException { return decodeBooleanText(buf, length); } @Override - public byte decodeByteText(ReadableByteBuf buf, int length) throws SQLDataException { + public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { long result; if (!isBinary()) { // TEXT column - String str2 = buf.readString(length); + String str2 = buf.readString(length.get()); try { result = new BigDecimal(str2).setScale(0, RoundingMode.DOWN).longValue(); } catch (NumberFormatException nfe) { @@ -151,35 +153,35 @@ public byte decodeByteText(ReadableByteBuf buf, int length) throws SQLDataExcept return (byte) result; } - if (length > 0) { + if (length.get() > 0) { byte b = buf.readByte(); - buf.skip(length - 1); + buf.skip(length.get() - 1); return b; } throw new SQLDataException("empty String value cannot be decoded as Byte"); } @Override - public byte decodeByteBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public byte decodeByteBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { return decodeByteText(buf, length); } @Override - public String decodeStringText(ReadableByteBuf buf, int length, Calendar cal) + public String decodeStringText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - return buf.readString(length); + return buf.readString(length.get()); } @Override - public String decodeStringBinary(ReadableByteBuf buf, int length, Calendar cal) + public String decodeStringBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - return buf.readString(length); + return buf.readString(length.get()); } @Override - public short decodeShortText(ReadableByteBuf buf, int length) throws SQLDataException { + public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { if (isBinary()) { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Short", dataType)); } @@ -187,9 +189,9 @@ public short decodeShortText(ReadableByteBuf buf, int length) throws SQLDataExce } @Override - public short decodeShortBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public short decodeShortBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { if (isBinary()) { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Short", dataType)); } @@ -197,9 +199,9 @@ public short decodeShortBinary(ReadableByteBuf buf, int length) throws SQLDataEx } @Override - public int decodeIntText(ReadableByteBuf buf, int length) throws SQLDataException { + public int decodeIntText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { if (isBinary()) { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Integer", dataType)); } @@ -207,9 +209,9 @@ public int decodeIntText(ReadableByteBuf buf, int length) throws SQLDataExceptio } @Override - public int decodeIntBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public int decodeIntBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { if (isBinary()) { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Integer", dataType)); } @@ -217,27 +219,27 @@ public int decodeIntBinary(ReadableByteBuf buf, int length) throws SQLDataExcept } @Override - public long decodeLongText(ReadableByteBuf buf, int length) throws SQLDataException { + public long decodeLongText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { if (isBinary()) { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Long", dataType)); } return super.decodeLongText(buf, length); } @Override - public long decodeLongBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public long decodeLongBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { if (isBinary()) { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Long", dataType)); } return super.decodeLongBinary(buf, length); } @Override - public float decodeFloatText(ReadableByteBuf buf, int length) throws SQLDataException { + public float decodeFloatText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { if (isBinary()) { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Float", dataType)); } @@ -245,9 +247,9 @@ public float decodeFloatText(ReadableByteBuf buf, int length) throws SQLDataExce } @Override - public float decodeFloatBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public float decodeFloatBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { if (isBinary()) { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Float", dataType)); } @@ -255,9 +257,9 @@ public float decodeFloatBinary(ReadableByteBuf buf, int length) throws SQLDataEx } @Override - public double decodeDoubleText(ReadableByteBuf buf, int length) throws SQLDataException { + public double decodeDoubleText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { if (isBinary()) { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Double", dataType)); } @@ -265,9 +267,9 @@ public double decodeDoubleText(ReadableByteBuf buf, int length) throws SQLDataEx } @Override - public double decodeDoubleBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public double decodeDoubleBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { if (isBinary()) { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Double", dataType)); } @@ -275,50 +277,50 @@ public double decodeDoubleBinary(ReadableByteBuf buf, int length) throws SQLData } @Override - public Date decodeDateText(ReadableByteBuf buf, int length, Calendar cal) + public Date decodeDateText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { if (isBinary()) { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } return super.decodeDateText(buf, length, cal); } @Override - public Date decodeDateBinary(ReadableByteBuf buf, int length, Calendar cal) + public Date decodeDateBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { if (isBinary()) { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } return super.decodeDateBinary(buf, length, cal); } @Override - public Time decodeTimeText(ReadableByteBuf buf, int length, Calendar cal) + public Time decodeTimeText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { if (isBinary()) { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } return super.decodeTimeText(buf, length, cal); } @Override - public Time decodeTimeBinary(ReadableByteBuf buf, int length, Calendar cal) + public Time decodeTimeBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { if (isBinary()) { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } return super.decodeTimeBinary(buf, length, cal); } @Override - public Timestamp decodeTimestampText(ReadableByteBuf buf, int length, Calendar cal) + public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { if (isBinary()) { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Timestamp", dataType)); } @@ -326,10 +328,10 @@ public Timestamp decodeTimestampText(ReadableByteBuf buf, int length, Calendar c } @Override - public Timestamp decodeTimestampBinary(ReadableByteBuf buf, int length, Calendar cal) + public Timestamp decodeTimestampBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { if (isBinary()) { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Timestamp", dataType)); } diff --git a/src/main/java/org/mariadb/jdbc/client/column/DateColumn.java b/src/main/java/org/mariadb/jdbc/client/column/DateColumn.java index 28a00098c..b4f32704a 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/DateColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/DateColumn.java @@ -4,6 +4,8 @@ package org.mariadb.jdbc.client.column; +import static org.mariadb.jdbc.client.result.Result.NULL_LENGTH; + import java.sql.*; import java.time.LocalDate; import java.util.Calendar; @@ -12,6 +14,7 @@ import org.mariadb.jdbc.client.ColumnDecoder; import org.mariadb.jdbc.client.DataType; import org.mariadb.jdbc.client.ReadableByteBuf; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.message.server.ColumnDefinitionPacket; /** Column metadata definition */ @@ -56,53 +59,54 @@ public String getColumnTypeName(Configuration conf) { } @Override - public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, int length) + public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, MutableInt length) throws SQLDataException { return decodeDateText(buf, length, null); } @Override - public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, int length) + public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, MutableInt length) throws SQLDataException { return decodeDateBinary(buf, length, null); } @Override - public boolean decodeBooleanText(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public boolean decodeBooleanText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Boolean", dataType)); } @Override - public boolean decodeBooleanBinary(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public boolean decodeBooleanBinary(ReadableByteBuf buf, MutableInt length) + throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Boolean", dataType)); } @Override - public byte decodeByteText(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Byte", dataType)); } @Override - public byte decodeByteBinary(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public byte decodeByteBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Byte", dataType)); } @Override - public String decodeStringText(ReadableByteBuf buf, int length, Calendar cal) + public String decodeStringText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - return buf.readString(length); + return buf.readString(length.get()); } @Override - public String decodeStringBinary(ReadableByteBuf buf, int length, Calendar cal) + public String decodeStringBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - if (length == 0) return "0000-00-00"; + if (length.get() == 0) return "0000-00-00"; int dateYear = buf.readUnsignedShort(); int dateMonth = buf.readByte(); int dateDay = buf.readByte(); @@ -110,76 +114,79 @@ public String decodeStringBinary(ReadableByteBuf buf, int length, Calendar cal) } @Override - public short decodeShortText(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Short", dataType)); } @Override - public short decodeShortBinary(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public short decodeShortBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Short", dataType)); } @Override - public int decodeIntText(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public int decodeIntText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Integer", dataType)); } @Override - public int decodeIntBinary(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public int decodeIntBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Integer", dataType)); } @Override - public long decodeLongText(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public long decodeLongText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Long", dataType)); } @Override - public long decodeLongBinary(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public long decodeLongBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Long", dataType)); } @Override - public float decodeFloatText(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public float decodeFloatText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Float", dataType)); } @Override - public float decodeFloatBinary(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public float decodeFloatBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Float", dataType)); } @Override - public double decodeDoubleText(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public double decodeDoubleText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Double", dataType)); } @Override - public double decodeDoubleBinary(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public double decodeDoubleBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Double", dataType)); } @Override - public Date decodeDateText(ReadableByteBuf buf, int length, Calendar cal) + public Date decodeDateText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { int year = (int) buf.atoull(4); buf.skip(1); int month = (int) buf.atoull(2); buf.skip(1); int dayOfMonth = (int) buf.atoull(2); - if (year == 0 && month == 0 && dayOfMonth == 0) return null; + if (year == 0 && month == 0 && dayOfMonth == 0) { + length.set(NULL_LENGTH); + return null; + } Calendar c = cal == null ? Calendar.getInstance() : cal; synchronized (c) { @@ -192,9 +199,12 @@ public Date decodeDateText(ReadableByteBuf buf, int length, Calendar cal) } @Override - public Date decodeDateBinary(ReadableByteBuf buf, int length, Calendar cal) + public Date decodeDateBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - if (length == 0) return null; + if (length.get() == 0) { + length.set(NULL_LENGTH); + return null; + } Calendar c = cal == null ? Calendar.getInstance() : cal; synchronized (c) { @@ -207,29 +217,32 @@ public Date decodeDateBinary(ReadableByteBuf buf, int length, Calendar cal) } @Override - public Time decodeTimeText(ReadableByteBuf buf, int length, Calendar cal) + public Time decodeTimeText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Time decodeTimeBinary(ReadableByteBuf buf, int length, Calendar cal) + public Time decodeTimeBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Timestamp decodeTimestampText(ReadableByteBuf buf, int length, Calendar calParam) + public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Calendar calParam) throws SQLDataException { if (calParam == null || calParam.getTimeZone().equals(TimeZone.getDefault())) { - String s = buf.readAscii(length); - if ("0000-00-00".equals(s)) return null; + String s = buf.readAscii(length.get()); + if ("0000-00-00".equals(s)) { + length.set(NULL_LENGTH); + return null; + } return new Timestamp(Date.valueOf(s).getTime()); } - String[] datePart = buf.readAscii(length).split("-"); + String[] datePart = buf.readAscii(length.get()).split("-"); synchronized (calParam) { calParam.clear(); calParam.set( @@ -241,9 +254,12 @@ public Timestamp decodeTimestampText(ReadableByteBuf buf, int length, Calendar c } @Override - public Timestamp decodeTimestampBinary(ReadableByteBuf buf, int length, Calendar calParam) + public Timestamp decodeTimestampBinary(ReadableByteBuf buf, MutableInt length, Calendar calParam) throws SQLDataException { - if (length == 0) return null; + if (length.get() == 0) { + length.set(NULL_LENGTH); + return null; + } Calendar cal = calParam == null ? Calendar.getInstance() : calParam; int year; @@ -254,7 +270,10 @@ public Timestamp decodeTimestampBinary(ReadableByteBuf buf, int length, Calendar month = buf.readByte(); dayOfMonth = buf.readByte(); - if (year == 0 && month == 0 && dayOfMonth == 0) return null; + if (year == 0 && month == 0 && dayOfMonth == 0) { + length.set(NULL_LENGTH); + return null; + } Timestamp timestamp; synchronized (cal) { diff --git a/src/main/java/org/mariadb/jdbc/client/column/DoubleColumn.java b/src/main/java/org/mariadb/jdbc/client/column/DoubleColumn.java index 8b73898e4..0484b769b 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/DoubleColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/DoubleColumn.java @@ -12,6 +12,7 @@ import org.mariadb.jdbc.client.ColumnDecoder; import org.mariadb.jdbc.client.DataType; import org.mariadb.jdbc.client.ReadableByteBuf; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.message.server.ColumnDefinitionPacket; /** Column metadata definition */ @@ -57,32 +58,33 @@ public String getColumnTypeName(Configuration conf) { } @Override - public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, int length) + public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, MutableInt length) throws SQLDataException { - return Double.parseDouble(buf.readAscii(length)); + return Double.parseDouble(buf.readAscii(length.get())); } @Override - public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, int length) + public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, MutableInt length) throws SQLDataException { return buf.readDouble(); } @Override - public boolean decodeBooleanText(ReadableByteBuf buf, int length) throws SQLDataException { - String s = buf.readAscii(length); + public boolean decodeBooleanText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + String s = buf.readAscii(length.get()); return !"0".equals(s); } @Override - public boolean decodeBooleanBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public boolean decodeBooleanBinary(ReadableByteBuf buf, MutableInt length) + throws SQLDataException { return ((int) buf.readDouble()) != 0; } @Override - public byte decodeByteText(ReadableByteBuf buf, int length) throws SQLDataException { + public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { long result; - String str = buf.readString(length); + String str = buf.readString(length.get()); try { result = new BigDecimal(str).setScale(0, RoundingMode.DOWN).byteValueExact(); } catch (NumberFormatException | ArithmeticException nfe) { @@ -96,7 +98,7 @@ public byte decodeByteText(ReadableByteBuf buf, int length) throws SQLDataExcept } @Override - public byte decodeByteBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public byte decodeByteBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { long result = (long) buf.readDouble(); if ((byte) result != result) { throw new SQLDataException("byte overflow"); @@ -106,21 +108,21 @@ public byte decodeByteBinary(ReadableByteBuf buf, int length) throws SQLDataExce } @Override - public String decodeStringText(ReadableByteBuf buf, int length, Calendar cal) + public String decodeStringText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - return buf.readString(length); + return buf.readString(length.get()); } @Override - public String decodeStringBinary(ReadableByteBuf buf, int length, Calendar cal) + public String decodeStringBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { return String.valueOf(buf.readDouble()); } @Override - public short decodeShortText(ReadableByteBuf buf, int length) throws SQLDataException { + public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { long result; - String str = buf.readString(length); + String str = buf.readString(length.get()); try { result = new BigDecimal(str).setScale(0, RoundingMode.DOWN).longValueExact(); } catch (NumberFormatException | ArithmeticException nfe) { @@ -134,7 +136,7 @@ public short decodeShortText(ReadableByteBuf buf, int length) throws SQLDataExce } @Override - public short decodeShortBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public short decodeShortBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { long result = (long) buf.readDouble(); if ((short) result != result || (result < 0 && !isSigned())) { throw new SQLDataException("Short overflow"); @@ -143,9 +145,9 @@ public short decodeShortBinary(ReadableByteBuf buf, int length) throws SQLDataEx } @Override - public int decodeIntText(ReadableByteBuf buf, int length) throws SQLDataException { + public int decodeIntText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { long result; - String str = buf.readString(length); + String str = buf.readString(length.get()); try { result = new BigDecimal(str).setScale(0, RoundingMode.DOWN).longValueExact(); } catch (NumberFormatException | ArithmeticException nfe) { @@ -160,7 +162,7 @@ public int decodeIntText(ReadableByteBuf buf, int length) throws SQLDataExceptio } @Override - public int decodeIntBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public int decodeIntBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { long result = (long) buf.readDouble(); int res = (int) result; if (res != result) { @@ -170,8 +172,8 @@ public int decodeIntBinary(ReadableByteBuf buf, int length) throws SQLDataExcept } @Override - public long decodeLongText(ReadableByteBuf buf, int length) throws SQLDataException { - String str2 = buf.readAscii(length); + public long decodeLongText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + String str2 = buf.readAscii(length.get()); try { return new BigDecimal(str2).setScale(0, RoundingMode.DOWN).longValueExact(); } catch (NumberFormatException | ArithmeticException nfe) { @@ -180,70 +182,70 @@ public long decodeLongText(ReadableByteBuf buf, int length) throws SQLDataExcept } @Override - public long decodeLongBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public long decodeLongBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { return (long) buf.readDouble(); } @Override - public float decodeFloatText(ReadableByteBuf buf, int length) throws SQLDataException { - return Float.parseFloat(buf.readAscii(length)); + public float decodeFloatText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + return Float.parseFloat(buf.readAscii(length.get())); } @Override - public float decodeFloatBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public float decodeFloatBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { return (float) buf.readDouble(); } @Override - public double decodeDoubleText(ReadableByteBuf buf, int length) throws SQLDataException { - return Double.parseDouble(buf.readAscii(length)); + public double decodeDoubleText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + return Double.parseDouble(buf.readAscii(length.get())); } @Override - public double decodeDoubleBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public double decodeDoubleBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { return buf.readDouble(); } @Override - public Date decodeDateText(ReadableByteBuf buf, int length, Calendar cal) + public Date decodeDateText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Date decodeDateBinary(ReadableByteBuf buf, int length, Calendar cal) + public Date decodeDateBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Time decodeTimeText(ReadableByteBuf buf, int length, Calendar cal) + public Time decodeTimeText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Time decodeTimeBinary(ReadableByteBuf buf, int length, Calendar cal) + public Time decodeTimeBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Timestamp decodeTimestampText(ReadableByteBuf buf, int length, Calendar cal) + public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Timestamp", dataType)); } @Override - public Timestamp decodeTimestampBinary(ReadableByteBuf buf, int length, Calendar cal) + public Timestamp decodeTimestampBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Timestamp", dataType)); } diff --git a/src/main/java/org/mariadb/jdbc/client/column/FloatColumn.java b/src/main/java/org/mariadb/jdbc/client/column/FloatColumn.java index ce63e9a18..a2f08594c 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/FloatColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/FloatColumn.java @@ -12,6 +12,7 @@ import org.mariadb.jdbc.client.ColumnDecoder; import org.mariadb.jdbc.client.DataType; import org.mariadb.jdbc.client.ReadableByteBuf; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.message.server.ColumnDefinitionPacket; /** Column metadata definition */ @@ -57,32 +58,33 @@ public String getColumnTypeName(Configuration conf) { } @Override - public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, int length) + public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, MutableInt length) throws SQLDataException { - return Float.parseFloat(buf.readAscii(length)); + return Float.parseFloat(buf.readAscii(length.get())); } @Override - public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, int length) + public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, MutableInt length) throws SQLDataException { return buf.readFloat(); } @Override - public boolean decodeBooleanText(ReadableByteBuf buf, int length) throws SQLDataException { - String s = buf.readAscii(length); + public boolean decodeBooleanText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + String s = buf.readAscii(length.get()); return !"0".equals(s); } @Override - public boolean decodeBooleanBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public boolean decodeBooleanBinary(ReadableByteBuf buf, MutableInt length) + throws SQLDataException { return ((int) buf.readFloat()) != 0; } @Override - public byte decodeByteText(ReadableByteBuf buf, int length) throws SQLDataException { + public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { long result; - String str = buf.readString(length); + String str = buf.readString(length.get()); try { result = new BigDecimal(str).setScale(0, RoundingMode.DOWN).byteValueExact(); } catch (NumberFormatException | ArithmeticException nfe) { @@ -96,7 +98,7 @@ public byte decodeByteText(ReadableByteBuf buf, int length) throws SQLDataExcept } @Override - public byte decodeByteBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public byte decodeByteBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { long result = (long) buf.readFloat(); if ((byte) result != result) { throw new SQLDataException("byte overflow"); @@ -106,21 +108,21 @@ public byte decodeByteBinary(ReadableByteBuf buf, int length) throws SQLDataExce } @Override - public String decodeStringText(ReadableByteBuf buf, int length, Calendar cal) + public String decodeStringText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - return buf.readString(length); + return buf.readString(length.get()); } @Override - public String decodeStringBinary(ReadableByteBuf buf, int length, Calendar cal) + public String decodeStringBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { return String.valueOf(buf.readFloat()); } @Override - public short decodeShortText(ReadableByteBuf buf, int length) throws SQLDataException { + public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { long result; - String str = buf.readString(length); + String str = buf.readString(length.get()); try { result = new BigDecimal(str).setScale(0, RoundingMode.DOWN).longValueExact(); } catch (NumberFormatException | ArithmeticException nfe) { @@ -134,7 +136,7 @@ public short decodeShortText(ReadableByteBuf buf, int length) throws SQLDataExce } @Override - public short decodeShortBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public short decodeShortBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { long result = (long) buf.readFloat(); if ((short) result != result || (result < 0 && !isSigned())) { throw new SQLDataException("Short overflow"); @@ -143,9 +145,9 @@ public short decodeShortBinary(ReadableByteBuf buf, int length) throws SQLDataEx } @Override - public int decodeIntText(ReadableByteBuf buf, int length) throws SQLDataException { + public int decodeIntText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { long result; - String str = buf.readString(length); + String str = buf.readString(length.get()); try { result = new BigDecimal(str).setScale(0, RoundingMode.DOWN).longValueExact(); } catch (NumberFormatException | ArithmeticException nfe) { @@ -160,7 +162,7 @@ public int decodeIntText(ReadableByteBuf buf, int length) throws SQLDataExceptio } @Override - public int decodeIntBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public int decodeIntBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { long result = (long) buf.readFloat(); int res = (int) result; if (res != result) { @@ -170,8 +172,8 @@ public int decodeIntBinary(ReadableByteBuf buf, int length) throws SQLDataExcept } @Override - public long decodeLongText(ReadableByteBuf buf, int length) throws SQLDataException { - String str2 = buf.readAscii(length); + public long decodeLongText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + String str2 = buf.readAscii(length.get()); try { return new BigDecimal(str2).setScale(0, RoundingMode.DOWN).longValueExact(); } catch (NumberFormatException | ArithmeticException nfe) { @@ -180,70 +182,70 @@ public long decodeLongText(ReadableByteBuf buf, int length) throws SQLDataExcept } @Override - public long decodeLongBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public long decodeLongBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { return (long) buf.readFloat(); } @Override - public float decodeFloatText(ReadableByteBuf buf, int length) throws SQLDataException { - return Float.parseFloat(buf.readAscii(length)); + public float decodeFloatText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + return Float.parseFloat(buf.readAscii(length.get())); } @Override - public float decodeFloatBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public float decodeFloatBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { return buf.readFloat(); } @Override - public double decodeDoubleText(ReadableByteBuf buf, int length) throws SQLDataException { - return Double.parseDouble(buf.readAscii(length)); + public double decodeDoubleText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + return Double.parseDouble(buf.readAscii(length.get())); } @Override - public double decodeDoubleBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public double decodeDoubleBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { return buf.readFloat(); } @Override - public Date decodeDateText(ReadableByteBuf buf, int length, Calendar cal) + public Date decodeDateText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Date decodeDateBinary(ReadableByteBuf buf, int length, Calendar cal) + public Date decodeDateBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Time decodeTimeText(ReadableByteBuf buf, int length, Calendar cal) + public Time decodeTimeText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Time decodeTimeBinary(ReadableByteBuf buf, int length, Calendar cal) + public Time decodeTimeBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Timestamp decodeTimestampText(ReadableByteBuf buf, int length, Calendar cal) + public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Timestamp", dataType)); } @Override - public Timestamp decodeTimestampBinary(ReadableByteBuf buf, int length, Calendar cal) + public Timestamp decodeTimestampBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Timestamp", dataType)); } diff --git a/src/main/java/org/mariadb/jdbc/client/column/GeometryColumn.java b/src/main/java/org/mariadb/jdbc/client/column/GeometryColumn.java index 37bd06fa3..468c5a21c 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/GeometryColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/GeometryColumn.java @@ -10,6 +10,7 @@ import org.mariadb.jdbc.Configuration; import org.mariadb.jdbc.client.DataType; import org.mariadb.jdbc.client.ReadableByteBuf; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.plugin.codec.*; import org.mariadb.jdbc.type.*; @@ -79,35 +80,35 @@ public String getColumnTypeName(Configuration conf) { } @Override - public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, int length) + public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, MutableInt length) throws SQLDataException { if (conf.geometryDefaultType() != null && "default".equals(conf.geometryDefaultType())) { buf.skip(4); // SRID - return Geometry.getGeometry(buf, length - 4, this); + return Geometry.getGeometry(buf, length.get() - 4, this); } - byte[] arr = new byte[length]; + byte[] arr = new byte[length.get()]; buf.readBytes(arr); return arr; } @Override - public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, int length) + public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, MutableInt length) throws SQLDataException { return getDefaultText(conf, buf, length); } @Override - public Timestamp decodeTimestampText(ReadableByteBuf buf, int length, Calendar cal) + public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Timestamp", dataType)); } @Override - public Timestamp decodeTimestampBinary(ReadableByteBuf buf, int length, Calendar cal) + public Timestamp decodeTimestampBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Timestamp", dataType)); } diff --git a/src/main/java/org/mariadb/jdbc/client/column/SignedBigIntColumn.java b/src/main/java/org/mariadb/jdbc/client/column/SignedBigIntColumn.java index 5afd95b75..9fe947697 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/SignedBigIntColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/SignedBigIntColumn.java @@ -11,6 +11,7 @@ import org.mariadb.jdbc.client.ColumnDecoder; import org.mariadb.jdbc.client.DataType; import org.mariadb.jdbc.client.ReadableByteBuf; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.message.server.ColumnDefinitionPacket; /** Column metadata definition */ @@ -55,31 +56,32 @@ public String getColumnTypeName(Configuration conf) { } @Override - public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, int length) + public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, MutableInt length) throws SQLDataException { - return buf.atoll(length); + return buf.atoll(length.get()); } @Override - public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, int length) + public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, MutableInt length) throws SQLDataException { return buf.readLong(); } @Override - public boolean decodeBooleanText(ReadableByteBuf buf, int length) throws SQLDataException { - String s = buf.readAscii(length); + public boolean decodeBooleanText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + String s = buf.readAscii(length.get()); return !"0".equals(s); } @Override - public boolean decodeBooleanBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public boolean decodeBooleanBinary(ReadableByteBuf buf, MutableInt length) + throws SQLDataException { return buf.readLong() != 0; } @Override - public byte decodeByteText(ReadableByteBuf buf, int length) throws SQLDataException { - long result = buf.atoll(length); + public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + long result = buf.atoll(length.get()); if ((byte) result != result) { throw new SQLDataException("byte overflow"); } @@ -87,7 +89,7 @@ public byte decodeByteText(ReadableByteBuf buf, int length) throws SQLDataExcept } @Override - public byte decodeByteBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public byte decodeByteBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { long result = buf.readLong(); if ((byte) result != result) { throw new SQLDataException("byte overflow"); @@ -97,20 +99,20 @@ public byte decodeByteBinary(ReadableByteBuf buf, int length) throws SQLDataExce } @Override - public String decodeStringText(ReadableByteBuf buf, int length, Calendar cal) + public String decodeStringText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - return buf.readString(length); + return buf.readString(length.get()); } @Override - public String decodeStringBinary(ReadableByteBuf buf, int length, Calendar cal) + public String decodeStringBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { return BigInteger.valueOf(buf.readLong()).toString(); } @Override - public short decodeShortText(ReadableByteBuf buf, int length) throws SQLDataException { - long result = buf.atoll(length); + public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + long result = buf.atoll(length.get()); if ((short) result != result) { throw new SQLDataException("Short overflow"); } @@ -118,7 +120,7 @@ public short decodeShortText(ReadableByteBuf buf, int length) throws SQLDataExce } @Override - public short decodeShortBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public short decodeShortBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { long result = buf.readLong(); if ((short) result != result) { throw new SQLDataException("Short overflow"); @@ -127,8 +129,8 @@ public short decodeShortBinary(ReadableByteBuf buf, int length) throws SQLDataEx } @Override - public int decodeIntText(ReadableByteBuf buf, int length) throws SQLDataException { - long result = buf.atoll(length); + public int decodeIntText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + long result = buf.atoll(length.get()); int res = (int) result; if (res != result) { throw new SQLDataException("integer overflow"); @@ -137,7 +139,7 @@ public int decodeIntText(ReadableByteBuf buf, int length) throws SQLDataExceptio } @Override - public int decodeIntBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public int decodeIntBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { long result = buf.readLong(); int res = (int) result; if (res != result) { @@ -147,75 +149,75 @@ public int decodeIntBinary(ReadableByteBuf buf, int length) throws SQLDataExcept } @Override - public long decodeLongText(ReadableByteBuf buf, int length) throws SQLDataException { - return buf.atoll(length); + public long decodeLongText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + return buf.atoll(length.get()); } @Override - public long decodeLongBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public long decodeLongBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { return buf.readLong(); } @Override - public float decodeFloatText(ReadableByteBuf buf, int length) throws SQLDataException { - return Float.parseFloat(buf.readAscii(length)); + public float decodeFloatText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + return Float.parseFloat(buf.readAscii(length.get())); } @Override - public float decodeFloatBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public float decodeFloatBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { return (float) buf.readLong(); } @Override - public double decodeDoubleText(ReadableByteBuf buf, int length) throws SQLDataException { - return Double.parseDouble(buf.readAscii(length)); + public double decodeDoubleText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + return Double.parseDouble(buf.readAscii(length.get())); } @Override - public double decodeDoubleBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public double decodeDoubleBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { return buf.readLong(); } @Override - public Date decodeDateText(ReadableByteBuf buf, int length, Calendar cal) + public Date decodeDateText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Date decodeDateBinary(ReadableByteBuf buf, int length, Calendar cal) + public Date decodeDateBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Time decodeTimeText(ReadableByteBuf buf, int length, Calendar cal) + public Time decodeTimeText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Time decodeTimeBinary(ReadableByteBuf buf, int length, Calendar cal) + public Time decodeTimeBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Timestamp decodeTimestampText(ReadableByteBuf buf, int length, Calendar cal) + public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Timestamp", dataType)); } @Override - public Timestamp decodeTimestampBinary(ReadableByteBuf buf, int length, Calendar cal) + public Timestamp decodeTimestampBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Timestamp", dataType)); } diff --git a/src/main/java/org/mariadb/jdbc/client/column/SignedIntColumn.java b/src/main/java/org/mariadb/jdbc/client/column/SignedIntColumn.java index 5cebf4b36..e48b08346 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/SignedIntColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/SignedIntColumn.java @@ -10,6 +10,7 @@ import org.mariadb.jdbc.client.ColumnDecoder; import org.mariadb.jdbc.client.DataType; import org.mariadb.jdbc.client.ReadableByteBuf; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.message.server.ColumnDefinitionPacket; /** Column metadata definition */ @@ -54,31 +55,32 @@ public String getColumnTypeName(Configuration conf) { } @Override - public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, int length) + public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, MutableInt length) throws SQLDataException { - return (int) buf.atoll(length); + return (int) buf.atoll(length.get()); } @Override - public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, int length) + public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, MutableInt length) throws SQLDataException { return buf.readInt(); } @Override - public boolean decodeBooleanText(ReadableByteBuf buf, int length) throws SQLDataException { - String s = buf.readAscii(length); + public boolean decodeBooleanText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + String s = buf.readAscii(length.get()); return !"0".equals(s); } @Override - public boolean decodeBooleanBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public boolean decodeBooleanBinary(ReadableByteBuf buf, MutableInt length) + throws SQLDataException { return buf.readInt() != 0; } @Override - public byte decodeByteText(ReadableByteBuf buf, int length) throws SQLDataException { - long result = buf.atoll(length); + public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + long result = buf.atoll(length.get()); if ((byte) result != result || (result < 0 && !isSigned())) { throw new SQLDataException("byte overflow"); } @@ -86,7 +88,7 @@ public byte decodeByteText(ReadableByteBuf buf, int length) throws SQLDataExcept } @Override - public byte decodeByteBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public byte decodeByteBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { long result = buf.readInt(); if ((byte) result != result) { throw new SQLDataException("byte overflow"); @@ -96,20 +98,20 @@ public byte decodeByteBinary(ReadableByteBuf buf, int length) throws SQLDataExce } @Override - public String decodeStringText(ReadableByteBuf buf, int length, Calendar cal) + public String decodeStringText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - return buf.readString(length); + return buf.readString(length.get()); } @Override - public String decodeStringBinary(ReadableByteBuf buf, int length, Calendar cal) + public String decodeStringBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { return String.valueOf(buf.readInt()); } @Override - public short decodeShortText(ReadableByteBuf buf, int length) throws SQLDataException { - long result = buf.atoll(length); + public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + long result = buf.atoll(length.get()); if ((short) result != result) { throw new SQLDataException("Short overflow"); } @@ -117,7 +119,7 @@ public short decodeShortText(ReadableByteBuf buf, int length) throws SQLDataExce } @Override - public short decodeShortBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public short decodeShortBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { long result = isSigned() ? buf.readInt() : buf.readUnsignedInt(); if ((short) result != result) { throw new SQLDataException("Short overflow"); @@ -126,85 +128,85 @@ public short decodeShortBinary(ReadableByteBuf buf, int length) throws SQLDataEx } @Override - public int decodeIntText(ReadableByteBuf buf, int length) throws SQLDataException { - return (int) buf.atoll(length); + public int decodeIntText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + return (int) buf.atoll(length.get()); } @Override - public int decodeIntBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public int decodeIntBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { return buf.readInt(); } @Override - public long decodeLongText(ReadableByteBuf buf, int length) throws SQLDataException { - return buf.atoll(length); + public long decodeLongText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + return buf.atoll(length.get()); } @Override - public long decodeLongBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public long decodeLongBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { return buf.readInt(); } @Override - public float decodeFloatText(ReadableByteBuf buf, int length) throws SQLDataException { - return Float.parseFloat(buf.readAscii(length)); + public float decodeFloatText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + return Float.parseFloat(buf.readAscii(length.get())); } @Override - public float decodeFloatBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public float decodeFloatBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { return (float) buf.readInt(); } @Override - public double decodeDoubleText(ReadableByteBuf buf, int length) throws SQLDataException { - return Double.parseDouble(buf.readAscii(length)); + public double decodeDoubleText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + return Double.parseDouble(buf.readAscii(length.get())); } @Override - public double decodeDoubleBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public double decodeDoubleBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { return buf.readInt(); } @Override - public Date decodeDateText(ReadableByteBuf buf, int length, Calendar cal) + public Date decodeDateText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Date decodeDateBinary(ReadableByteBuf buf, int length, Calendar cal) + public Date decodeDateBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Time decodeTimeText(ReadableByteBuf buf, int length, Calendar cal) + public Time decodeTimeText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Time decodeTimeBinary(ReadableByteBuf buf, int length, Calendar cal) + public Time decodeTimeBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Timestamp decodeTimestampText(ReadableByteBuf buf, int length, Calendar cal) + public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Timestamp", dataType)); } @Override - public Timestamp decodeTimestampBinary(ReadableByteBuf buf, int length, Calendar cal) + public Timestamp decodeTimestampBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Timestamp", dataType)); } diff --git a/src/main/java/org/mariadb/jdbc/client/column/SignedMediumIntColumn.java b/src/main/java/org/mariadb/jdbc/client/column/SignedMediumIntColumn.java index 2f03e1ff2..f58bcb46f 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/SignedMediumIntColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/SignedMediumIntColumn.java @@ -10,6 +10,7 @@ import org.mariadb.jdbc.client.ColumnDecoder; import org.mariadb.jdbc.client.DataType; import org.mariadb.jdbc.client.ReadableByteBuf; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.message.server.ColumnDefinitionPacket; /** Column metadata definition */ @@ -54,31 +55,32 @@ public String getColumnTypeName(Configuration conf) { } @Override - public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, int length) + public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, MutableInt length) throws SQLDataException { return decodeIntText(buf, length); } @Override - public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, int length) + public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, MutableInt length) throws SQLDataException { return decodeIntBinary(buf, length); } @Override - public boolean decodeBooleanText(ReadableByteBuf buf, int length) throws SQLDataException { - String s = buf.readAscii(length); + public boolean decodeBooleanText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + String s = buf.readAscii(length.get()); return !"0".equals(s); } @Override - public boolean decodeBooleanBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public boolean decodeBooleanBinary(ReadableByteBuf buf, MutableInt length) + throws SQLDataException { return buf.readInt() != 0; } @Override - public byte decodeByteText(ReadableByteBuf buf, int length) throws SQLDataException { - long result = buf.atoll(length); + public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + long result = buf.atoll(length.get()); if ((byte) result != result) { throw new SQLDataException("byte overflow"); } @@ -86,7 +88,7 @@ public byte decodeByteText(ReadableByteBuf buf, int length) throws SQLDataExcept } @Override - public byte decodeByteBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public byte decodeByteBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { long result = buf.readMedium(); buf.skip(); // MEDIUMINT is encoded on 4 bytes in exchanges ! if ((byte) result != result) { @@ -97,13 +99,13 @@ public byte decodeByteBinary(ReadableByteBuf buf, int length) throws SQLDataExce } @Override - public String decodeStringText(ReadableByteBuf buf, int length, Calendar cal) + public String decodeStringText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - return buf.readString(length); + return buf.readString(length.get()); } @Override - public String decodeStringBinary(ReadableByteBuf buf, int length, Calendar cal) + public String decodeStringBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { String mediumStr = String.valueOf(isSigned() ? buf.readMedium() : buf.readUnsignedMedium()); buf.skip(); // MEDIUMINT is encoded on 4 bytes in exchanges ! @@ -111,8 +113,8 @@ public String decodeStringBinary(ReadableByteBuf buf, int length, Calendar cal) } @Override - public short decodeShortText(ReadableByteBuf buf, int length) throws SQLDataException { - long result = buf.atoll(length); + public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + long result = buf.atoll(length.get()); if ((short) result != result) { throw new SQLDataException("Short overflow"); } @@ -120,7 +122,7 @@ public short decodeShortText(ReadableByteBuf buf, int length) throws SQLDataExce } @Override - public short decodeShortBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public short decodeShortBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { long result = buf.readMedium(); buf.skip(); // MEDIUMINT is encoded on 4 bytes in exchanges ! if ((short) result != result) { @@ -130,93 +132,93 @@ public short decodeShortBinary(ReadableByteBuf buf, int length) throws SQLDataEx } @Override - public int decodeIntText(ReadableByteBuf buf, int length) throws SQLDataException { - return (int) buf.atoll(length); + public int decodeIntText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + return (int) buf.atoll(length.get()); } @Override - public int decodeIntBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public int decodeIntBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { int res = buf.readMedium(); buf.skip(); // MEDIUMINT is encoded on 4 bytes in exchanges ! return res; } @Override - public long decodeLongText(ReadableByteBuf buf, int length) throws SQLDataException { - return buf.atoll(length); + public long decodeLongText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + return buf.atoll(length.get()); } @Override - public long decodeLongBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public long decodeLongBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { long l = buf.readMedium(); buf.skip(); // MEDIUMINT is encoded on 4 bytes in exchanges ! return l; } @Override - public float decodeFloatText(ReadableByteBuf buf, int length) throws SQLDataException { - return Float.parseFloat(buf.readAscii(length)); + public float decodeFloatText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + return Float.parseFloat(buf.readAscii(length.get())); } @Override - public float decodeFloatBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public float decodeFloatBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { float f = buf.readMedium(); buf.skip(); // MEDIUMINT is encoded on 4 bytes in exchanges ! return f; } @Override - public double decodeDoubleText(ReadableByteBuf buf, int length) throws SQLDataException { - return Double.parseDouble(buf.readAscii(length)); + public double decodeDoubleText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + return Double.parseDouble(buf.readAscii(length.get())); } @Override - public double decodeDoubleBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public double decodeDoubleBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { double f = buf.readMedium(); buf.skip(); // MEDIUMINT is encoded on 4 bytes in exchanges ! return f; } @Override - public Date decodeDateText(ReadableByteBuf buf, int length, Calendar cal) + public Date decodeDateText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Date decodeDateBinary(ReadableByteBuf buf, int length, Calendar cal) + public Date decodeDateBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Time decodeTimeText(ReadableByteBuf buf, int length, Calendar cal) + public Time decodeTimeText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Time decodeTimeBinary(ReadableByteBuf buf, int length, Calendar cal) + public Time decodeTimeBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Timestamp decodeTimestampText(ReadableByteBuf buf, int length, Calendar cal) + public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Timestamp", dataType)); } @Override - public Timestamp decodeTimestampBinary(ReadableByteBuf buf, int length, Calendar cal) + public Timestamp decodeTimestampBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Timestamp", dataType)); } diff --git a/src/main/java/org/mariadb/jdbc/client/column/SignedSmallIntColumn.java b/src/main/java/org/mariadb/jdbc/client/column/SignedSmallIntColumn.java index 232591dcb..8495106d0 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/SignedSmallIntColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/SignedSmallIntColumn.java @@ -10,6 +10,7 @@ import org.mariadb.jdbc.client.ColumnDecoder; import org.mariadb.jdbc.client.DataType; import org.mariadb.jdbc.client.ReadableByteBuf; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.message.server.ColumnDefinitionPacket; /** Column metadata definition */ @@ -54,31 +55,32 @@ public String getColumnTypeName(Configuration conf) { } @Override - public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, int length) + public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, MutableInt length) throws SQLDataException { - return (short) buf.atoll(length); + return (short) buf.atoll(length.get()); } @Override - public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, int length) + public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, MutableInt length) throws SQLDataException { return buf.readShort(); } @Override - public boolean decodeBooleanText(ReadableByteBuf buf, int length) throws SQLDataException { - String s = buf.readAscii(length); + public boolean decodeBooleanText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + String s = buf.readAscii(length.get()); return !"0".equals(s); } @Override - public boolean decodeBooleanBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public boolean decodeBooleanBinary(ReadableByteBuf buf, MutableInt length) + throws SQLDataException { return buf.readShort() != 0; } @Override - public byte decodeByteText(ReadableByteBuf buf, int length) throws SQLDataException { - long result = buf.atoll(length); + public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + long result = buf.atoll(length.get()); if ((byte) result != result) { throw new SQLDataException("byte overflow"); } @@ -86,7 +88,7 @@ public byte decodeByteText(ReadableByteBuf buf, int length) throws SQLDataExcept } @Override - public byte decodeByteBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public byte decodeByteBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { long result = buf.readShort(); if ((byte) result != result) { throw new SQLDataException("byte overflow"); @@ -95,107 +97,107 @@ public byte decodeByteBinary(ReadableByteBuf buf, int length) throws SQLDataExce } @Override - public String decodeStringText(ReadableByteBuf buf, int length, Calendar cal) + public String decodeStringText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - return buf.readString(length); + return buf.readString(length.get()); } @Override - public String decodeStringBinary(ReadableByteBuf buf, int length, Calendar cal) + public String decodeStringBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { return String.valueOf(buf.readShort()); } @Override - public short decodeShortText(ReadableByteBuf buf, int length) throws SQLDataException { - return (short) buf.atoll(length); + public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + return (short) buf.atoll(length.get()); } @Override - public short decodeShortBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public short decodeShortBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { return buf.readShort(); } @Override - public int decodeIntText(ReadableByteBuf buf, int length) throws SQLDataException { - return (int) buf.atoll(length); + public int decodeIntText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + return (int) buf.atoll(length.get()); } @Override - public int decodeIntBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public int decodeIntBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { return buf.readShort(); } @Override - public long decodeLongText(ReadableByteBuf buf, int length) throws SQLDataException { - return buf.atoll(length); + public long decodeLongText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + return buf.atoll(length.get()); } @Override - public long decodeLongBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public long decodeLongBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { return buf.readShort(); } @Override - public float decodeFloatText(ReadableByteBuf buf, int length) throws SQLDataException { - return Float.parseFloat(buf.readAscii(length)); + public float decodeFloatText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + return Float.parseFloat(buf.readAscii(length.get())); } @Override - public float decodeFloatBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public float decodeFloatBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { return buf.readShort(); } @Override - public double decodeDoubleText(ReadableByteBuf buf, int length) throws SQLDataException { - return Double.parseDouble(buf.readAscii(length)); + public double decodeDoubleText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + return Double.parseDouble(buf.readAscii(length.get())); } @Override - public double decodeDoubleBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public double decodeDoubleBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { return buf.readShort(); } @Override - public Date decodeDateText(ReadableByteBuf buf, int length, Calendar cal) + public Date decodeDateText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Date decodeDateBinary(ReadableByteBuf buf, int length, Calendar cal) + public Date decodeDateBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Time decodeTimeText(ReadableByteBuf buf, int length, Calendar cal) + public Time decodeTimeText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Time decodeTimeBinary(ReadableByteBuf buf, int length, Calendar cal) + public Time decodeTimeBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Timestamp decodeTimestampText(ReadableByteBuf buf, int length, Calendar cal) + public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Timestamp", dataType)); } @Override - public Timestamp decodeTimestampBinary(ReadableByteBuf buf, int length, Calendar cal) + public Timestamp decodeTimestampBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Timestamp", dataType)); } diff --git a/src/main/java/org/mariadb/jdbc/client/column/SignedTinyIntColumn.java b/src/main/java/org/mariadb/jdbc/client/column/SignedTinyIntColumn.java index 2f79f8cea..dc8d49cc5 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/SignedTinyIntColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/SignedTinyIntColumn.java @@ -10,6 +10,7 @@ import org.mariadb.jdbc.client.ColumnDecoder; import org.mariadb.jdbc.client.DataType; import org.mariadb.jdbc.client.ReadableByteBuf; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.message.server.ColumnDefinitionPacket; /** Column metadata definition */ @@ -61,16 +62,16 @@ public String getColumnTypeName(Configuration conf) { } @Override - public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, int length) + public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, MutableInt length) throws SQLDataException { if (conf.tinyInt1isBit() && columnLength == 1) { return decodeBooleanText(buf, length); } - return (int) buf.atoll(length); + return (int) buf.atoll(length.get()); } @Override - public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, int length) + public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, MutableInt length) throws SQLDataException { if (conf.tinyInt1isBit() && columnLength == 1) { return decodeBooleanBinary(buf, length); @@ -82,19 +83,20 @@ public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, in } @Override - public boolean decodeBooleanText(ReadableByteBuf buf, int length) throws SQLDataException { - String s = buf.readAscii(length); + public boolean decodeBooleanText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + String s = buf.readAscii(length.get()); return !"0".equals(s); } @Override - public boolean decodeBooleanBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public boolean decodeBooleanBinary(ReadableByteBuf buf, MutableInt length) + throws SQLDataException { return buf.readByte() != 0; } @Override - public byte decodeByteText(ReadableByteBuf buf, int length) throws SQLDataException { - long result = buf.atoll(length); + public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + long result = buf.atoll(length.get()); if ((byte) result != result) { throw new SQLDataException("byte overflow"); } @@ -102,7 +104,7 @@ public byte decodeByteText(ReadableByteBuf buf, int length) throws SQLDataExcept } @Override - public byte decodeByteBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public byte decodeByteBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { if (isSigned()) return buf.readByte(); long result = buf.readUnsignedByte(); @@ -113,13 +115,13 @@ public byte decodeByteBinary(ReadableByteBuf buf, int length) throws SQLDataExce } @Override - public String decodeStringText(ReadableByteBuf buf, int length, Calendar cal) + public String decodeStringText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - return buf.readString(length); + return buf.readString(length.get()); } @Override - public String decodeStringBinary(ReadableByteBuf buf, int length, Calendar cal) + public String decodeStringBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { if (!isSigned()) { return String.valueOf(buf.readUnsignedByte()); @@ -128,32 +130,32 @@ public String decodeStringBinary(ReadableByteBuf buf, int length, Calendar cal) } @Override - public short decodeShortText(ReadableByteBuf buf, int length) throws SQLDataException { - return (short) buf.atoll(length); + public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + return (short) buf.atoll(length.get()); } @Override - public short decodeShortBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public short decodeShortBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { return (isSigned() ? buf.readByte() : buf.readUnsignedByte()); } @Override - public int decodeIntText(ReadableByteBuf buf, int length) throws SQLDataException { - return (int) buf.atoll(length); + public int decodeIntText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + return (int) buf.atoll(length.get()); } @Override - public int decodeIntBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public int decodeIntBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { return (isSigned() ? buf.readByte() : buf.readUnsignedByte()); } @Override - public long decodeLongText(ReadableByteBuf buf, int length) throws SQLDataException { - return buf.atoll(length); + public long decodeLongText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + return buf.atoll(length.get()); } @Override - public long decodeLongBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public long decodeLongBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { if (!isSigned()) { return buf.readUnsignedByte(); } @@ -161,12 +163,12 @@ public long decodeLongBinary(ReadableByteBuf buf, int length) throws SQLDataExce } @Override - public float decodeFloatText(ReadableByteBuf buf, int length) throws SQLDataException { - return Float.parseFloat(buf.readAscii(length)); + public float decodeFloatText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + return Float.parseFloat(buf.readAscii(length.get())); } @Override - public float decodeFloatBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public float decodeFloatBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { if (!isSigned()) { return buf.readUnsignedByte(); } @@ -174,12 +176,12 @@ public float decodeFloatBinary(ReadableByteBuf buf, int length) throws SQLDataEx } @Override - public double decodeDoubleText(ReadableByteBuf buf, int length) throws SQLDataException { - return Double.parseDouble(buf.readAscii(length)); + public double decodeDoubleText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + return Double.parseDouble(buf.readAscii(length.get())); } @Override - public double decodeDoubleBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public double decodeDoubleBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { if (!isSigned()) { return buf.readUnsignedByte(); } @@ -187,45 +189,45 @@ public double decodeDoubleBinary(ReadableByteBuf buf, int length) throws SQLData } @Override - public Date decodeDateText(ReadableByteBuf buf, int length, Calendar cal) + public Date decodeDateText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Date decodeDateBinary(ReadableByteBuf buf, int length, Calendar cal) + public Date decodeDateBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Time decodeTimeText(ReadableByteBuf buf, int length, Calendar cal) + public Time decodeTimeText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Time decodeTimeBinary(ReadableByteBuf buf, int length, Calendar cal) + public Time decodeTimeBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Timestamp decodeTimestampText(ReadableByteBuf buf, int length, Calendar cal) + public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Timestamp", dataType)); } @Override - public Timestamp decodeTimestampBinary(ReadableByteBuf buf, int length, Calendar cal) + public Timestamp decodeTimestampBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Timestamp", dataType)); } diff --git a/src/main/java/org/mariadb/jdbc/client/column/StringColumn.java b/src/main/java/org/mariadb/jdbc/client/column/StringColumn.java index 7338478cd..bafbecab6 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/StringColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/StringColumn.java @@ -4,6 +4,8 @@ package org.mariadb.jdbc.client.column; +import static org.mariadb.jdbc.client.result.Result.NULL_LENGTH; + import java.math.BigDecimal; import java.math.BigInteger; import java.math.RoundingMode; @@ -14,6 +16,7 @@ import org.mariadb.jdbc.client.ColumnDecoder; import org.mariadb.jdbc.client.DataType; import org.mariadb.jdbc.client.ReadableByteBuf; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.message.server.ColumnDefinitionPacket; import org.mariadb.jdbc.plugin.codec.LocalDateTimeCodec; import org.mariadb.jdbc.plugin.codec.LocalTimeCodec; @@ -102,40 +105,41 @@ public int getPrecision() { } @Override - public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, int length) + public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, MutableInt length) throws SQLDataException { if (isBinary()) { - byte[] arr = new byte[length]; + byte[] arr = new byte[length.get()]; buf.readBytes(arr); return arr; } - return buf.readString(length); + return buf.readString(length.get()); } @Override - public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, int length) + public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, MutableInt length) throws SQLDataException { if (isBinary()) { - byte[] arr = new byte[length]; + byte[] arr = new byte[length.get()]; buf.readBytes(arr); return arr; } - return buf.readString(length); + return buf.readString(length.get()); } @Override - public boolean decodeBooleanText(ReadableByteBuf buf, int length) throws SQLDataException { - return !"0".equals(buf.readAscii(length)); + public boolean decodeBooleanText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + return !"0".equals(buf.readAscii(length.get())); } @Override - public boolean decodeBooleanBinary(ReadableByteBuf buf, int length) throws SQLDataException { - return !"0".equals(buf.readAscii(length)); + public boolean decodeBooleanBinary(ReadableByteBuf buf, MutableInt length) + throws SQLDataException { + return !"0".equals(buf.readAscii(length.get())); } @Override - public byte decodeByteText(ReadableByteBuf buf, int length) throws SQLDataException { - String str = buf.readString(length); + public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + String str = buf.readString(length.get()); long result; try { result = new BigDecimal(str).setScale(0, RoundingMode.DOWN).longValue(); @@ -150,25 +154,25 @@ public byte decodeByteText(ReadableByteBuf buf, int length) throws SQLDataExcept } @Override - public byte decodeByteBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public byte decodeByteBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { return decodeByteText(buf, length); } @Override - public String decodeStringText(ReadableByteBuf buf, int length, Calendar cal) + public String decodeStringText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - return buf.readString(length); + return buf.readString(length.get()); } @Override - public String decodeStringBinary(ReadableByteBuf buf, int length, Calendar cal) + public String decodeStringBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - return buf.readString(length); + return buf.readString(length.get()); } @Override - public short decodeShortText(ReadableByteBuf buf, int length) throws SQLDataException { - String str = buf.readString(length); + public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + String str = buf.readString(length.get()); try { return new BigDecimal(str).setScale(0, RoundingMode.DOWN).shortValueExact(); } catch (NumberFormatException | ArithmeticException nfe) { @@ -177,13 +181,13 @@ public short decodeShortText(ReadableByteBuf buf, int length) throws SQLDataExce } @Override - public short decodeShortBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public short decodeShortBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { return decodeShortText(buf, length); } @Override - public int decodeIntText(ReadableByteBuf buf, int length) throws SQLDataException { - String str = buf.readString(length); + public int decodeIntText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + String str = buf.readString(length.get()); try { return new BigDecimal(str).setScale(0, RoundingMode.DOWN).intValueExact(); } catch (NumberFormatException | ArithmeticException nfe) { @@ -192,13 +196,13 @@ public int decodeIntText(ReadableByteBuf buf, int length) throws SQLDataExceptio } @Override - public int decodeIntBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public int decodeIntBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { return decodeIntText(buf, length); } @Override - public long decodeLongText(ReadableByteBuf buf, int length) throws SQLDataException { - String str = buf.readString(length); + public long decodeLongText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + String str = buf.readString(length.get()); try { return new BigInteger(str).longValueExact(); } catch (NumberFormatException nfe) { @@ -207,13 +211,13 @@ public long decodeLongText(ReadableByteBuf buf, int length) throws SQLDataExcept } @Override - public long decodeLongBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public long decodeLongBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { return decodeLongText(buf, length); } @Override - public float decodeFloatText(ReadableByteBuf buf, int length) throws SQLDataException { - String val = buf.readString(length); + public float decodeFloatText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + String val = buf.readString(length.get()); try { return Float.parseFloat(val); } catch (NumberFormatException nfe) { @@ -222,13 +226,13 @@ public float decodeFloatText(ReadableByteBuf buf, int length) throws SQLDataExce } @Override - public float decodeFloatBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public float decodeFloatBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { return decodeFloatText(buf, length); } @Override - public double decodeDoubleText(ReadableByteBuf buf, int length) throws SQLDataException { - String str2 = buf.readString(length); + public double decodeDoubleText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + String str2 = buf.readString(length.get()); try { return Double.parseDouble(str2); } catch (NumberFormatException nfe) { @@ -237,14 +241,14 @@ public double decodeDoubleText(ReadableByteBuf buf, int length) throws SQLDataEx } @Override - public double decodeDoubleBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public double decodeDoubleBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { return decodeDoubleText(buf, length); } @Override - public Date decodeDateText(ReadableByteBuf buf, int length, Calendar cal) + public Date decodeDateText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - String val = buf.readString(length); + String val = buf.readString(length.get()); if ("0000-00-00".equals(val)) return null; String[] stDatePart = val.split("[- ]"); if (stDatePart.length < 3) { @@ -272,13 +276,13 @@ public Date decodeDateText(ReadableByteBuf buf, int length, Calendar cal) } @Override - public Date decodeDateBinary(ReadableByteBuf buf, int length, Calendar cal) + public Date decodeDateBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { return decodeDateText(buf, length, cal); } @Override - public Time decodeTimeText(ReadableByteBuf buf, int length, Calendar cal) + public Time decodeTimeText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { Calendar c = cal == null ? Calendar.getInstance() : cal; int offset = c.getTimeZone().getOffset(0); @@ -291,7 +295,7 @@ public Time decodeTimeText(ReadableByteBuf buf, int length, Calendar cal) } @Override - public Time decodeTimeBinary(ReadableByteBuf buf, int length, Calendar calParam) + public Time decodeTimeBinary(ReadableByteBuf buf, MutableInt length, Calendar calParam) throws SQLDataException { Calendar cal = calParam == null ? Calendar.getInstance() : calParam; int[] parts = LocalTimeCodec.parseTime(buf, length, this); @@ -319,13 +323,13 @@ public Time decodeTimeBinary(ReadableByteBuf buf, int length, Calendar calParam) } @Override - public Timestamp decodeTimestampText(ReadableByteBuf buf, int length, Calendar calParam) + public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Calendar calParam) throws SQLDataException { int pos = buf.pos(); int nanoBegin = -1; int[] timestampsPart = new int[] {0, 0, 0, 0, 0, 0, 0}; int partIdx = 0; - for (int begin = 0; begin < length; begin++) { + for (int begin = 0; begin < length.get(); begin++) { byte b = buf.readByte(); if (b == '-' || b == ' ' || b == ':') { partIdx++; @@ -341,7 +345,7 @@ public Timestamp decodeTimestampText(ReadableByteBuf buf, int length, Calendar c throw new SQLDataException( String.format( "value '%s' (%s) cannot be decoded as Timestamp", - buf.readString(length), dataType)); + buf.readString(length.get()), dataType)); } timestampsPart[partIdx] = timestampsPart[partIdx] * 10 + b - 48; @@ -353,12 +357,13 @@ public Timestamp decodeTimestampText(ReadableByteBuf buf, int length, Calendar c && timestampsPart[4] == 0 && timestampsPart[5] == 0 && timestampsPart[6] == 0) { + length.set(NULL_LENGTH); return null; } // fix non-leading tray for nanoseconds if (nanoBegin > 0) { - for (int begin = 0; begin < 6 - (length - nanoBegin - 1); begin++) { + for (int begin = 0; begin < 6 - (length.get() - nanoBegin - 1); begin++) { timestampsPart[6] = timestampsPart[6] * 10; } } @@ -393,14 +398,17 @@ public Timestamp decodeTimestampText(ReadableByteBuf buf, int length, Calendar c } @Override - public Timestamp decodeTimestampBinary(ReadableByteBuf buf, int length, Calendar calParam) + public Timestamp decodeTimestampBinary(ReadableByteBuf buf, MutableInt length, Calendar calParam) throws SQLDataException { Calendar cal = calParam == null ? Calendar.getInstance() : calParam; - String val = buf.readString(length); + String val = buf.readString(length.get()); try { int[] parts = LocalDateTimeCodec.parseTimestamp(val); - if (parts == null) return null; + if (parts == null) { + length.set(NULL_LENGTH); + return null; + } int year = parts[0]; int month = parts[1]; int dayOfMonth = parts[2]; diff --git a/src/main/java/org/mariadb/jdbc/client/column/TimeColumn.java b/src/main/java/org/mariadb/jdbc/client/column/TimeColumn.java index 5ac66be4c..aefdd6dd7 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/TimeColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/TimeColumn.java @@ -10,6 +10,7 @@ import org.mariadb.jdbc.client.ColumnDecoder; import org.mariadb.jdbc.client.DataType; import org.mariadb.jdbc.client.ReadableByteBuf; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.message.server.ColumnDefinitionPacket; import org.mariadb.jdbc.plugin.codec.LocalTimeCodec; @@ -55,7 +56,7 @@ public String getColumnTypeName(Configuration conf) { } @Override - public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, int length) + public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, MutableInt length) throws SQLDataException { Calendar c = Calendar.getInstance(); int offset = c.getTimeZone().getOffset(0); @@ -68,7 +69,7 @@ public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, int } @Override - public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, int length) + public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, MutableInt length) throws SQLDataException { boolean negate = false; Calendar cal = Calendar.getInstance(); @@ -77,14 +78,14 @@ public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, in int minutes = 0; int seconds = 0; long microseconds = 0; - if (length > 0) { + if (length.get() > 0) { // specific case for TIME, to handle value not in 00:00:00-23:59:59 negate = buf.readByte() == 1; dayOfMonth = buf.readUnsignedInt(); hour = buf.readByte(); minutes = buf.readByte(); seconds = buf.readByte(); - if (length > 8) { + if (length.get() > 8) { microseconds = buf.readUnsignedInt(); } } @@ -100,46 +101,47 @@ public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, in } @Override - public byte decodeByteText(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Byte", dataType)); } @Override - public byte decodeByteBinary(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public byte decodeByteBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Byte", dataType)); } @Override - public boolean decodeBooleanText(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public boolean decodeBooleanText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Boolean", dataType)); } @Override - public boolean decodeBooleanBinary(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public boolean decodeBooleanBinary(ReadableByteBuf buf, MutableInt length) + throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Boolean", dataType)); } @Override - public String decodeStringText(ReadableByteBuf buf, int length, Calendar cal) + public String decodeStringText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - return buf.readString(length); + return buf.readString(length.get()); } @Override - public String decodeStringBinary(ReadableByteBuf buf, int length, Calendar cal) + public String decodeStringBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { long tDays = 0; int tHours = 0; int tMinutes = 0; int tSeconds = 0; long tMicroseconds = 0; - if (length == 0) { + if (length.get() == 0) { StringBuilder zeroValue = new StringBuilder("00:00:00"); if (getDecimals() > 0) { zeroValue.append("."); @@ -148,13 +150,13 @@ public String decodeStringBinary(ReadableByteBuf buf, int length, Calendar cal) return zeroValue.toString(); } boolean negate = buf.readByte() == 0x01; - if (length > 4) { + if (length.get() > 4) { tDays = buf.readUnsignedInt(); - if (length > 7) { + if (length.get() > 7) { tHours = buf.readByte(); tMinutes = buf.readByte(); tSeconds = buf.readByte(); - if (length > 8) { + if (length.get() > 8) { tMicroseconds = buf.readInt(); } } @@ -188,83 +190,83 @@ public String decodeStringBinary(ReadableByteBuf buf, int length, Calendar cal) } @Override - public short decodeShortText(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Short", dataType)); } @Override - public short decodeShortBinary(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public short decodeShortBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Short", dataType)); } @Override - public int decodeIntText(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public int decodeIntText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Integer", dataType)); } @Override - public int decodeIntBinary(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public int decodeIntBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Integer", dataType)); } @Override - public long decodeLongText(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public long decodeLongText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Long", dataType)); } @Override - public long decodeLongBinary(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public long decodeLongBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Long", dataType)); } @Override - public float decodeFloatText(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public float decodeFloatText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Float", dataType)); } @Override - public float decodeFloatBinary(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public float decodeFloatBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Float", dataType)); } @Override - public double decodeDoubleText(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public double decodeDoubleText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Double", dataType)); } @Override - public double decodeDoubleBinary(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public double decodeDoubleBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Double", dataType)); } @Override - public Date decodeDateText(ReadableByteBuf buf, int length, Calendar cal) + public Date decodeDateText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Date decodeDateBinary(ReadableByteBuf buf, int length, Calendar cal) + public Date decodeDateBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Time decodeTimeText(ReadableByteBuf buf, int length, Calendar cal) + public Time decodeTimeText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { Calendar c = cal == null ? Calendar.getInstance() : cal; int offset = c.getTimeZone().getOffset(0); @@ -277,7 +279,7 @@ public Time decodeTimeText(ReadableByteBuf buf, int length, Calendar cal) } @Override - public Time decodeTimeBinary(ReadableByteBuf buf, int length, Calendar calParam) + public Time decodeTimeBinary(ReadableByteBuf buf, MutableInt length, Calendar calParam) throws SQLDataException { Calendar cal = calParam == null ? Calendar.getInstance() : calParam; long dayOfMonth = 0; @@ -286,14 +288,14 @@ public Time decodeTimeBinary(ReadableByteBuf buf, int length, Calendar calParam) int seconds = 0; long microseconds = 0; boolean negate = false; - if (length > 0) { + if (length.get() > 0) { // specific case for TIME, to handle value not in 00:00:00-23:59:59 negate = buf.readByte() == 1; dayOfMonth = buf.readUnsignedInt(); hour = buf.readByte(); minutes = buf.readByte(); seconds = buf.readByte(); - if (length > 8) { + if (length.get() > 8) { microseconds = buf.readUnsignedInt(); } } @@ -309,7 +311,7 @@ public Time decodeTimeBinary(ReadableByteBuf buf, int length, Calendar calParam) } @Override - public Timestamp decodeTimestampText(ReadableByteBuf buf, int length, Calendar calParam) + public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Calendar calParam) throws SQLDataException { int[] parts = LocalTimeCodec.parseTime(buf, length, this); Timestamp t; @@ -339,7 +341,7 @@ public Timestamp decodeTimestampText(ReadableByteBuf buf, int length, Calendar c } @Override - public Timestamp decodeTimestampBinary(ReadableByteBuf buf, int length, Calendar calParam) + public Timestamp decodeTimestampBinary(ReadableByteBuf buf, MutableInt length, Calendar calParam) throws SQLDataException { Calendar cal = calParam == null ? Calendar.getInstance() : calParam; long microseconds = 0; @@ -350,7 +352,7 @@ public Timestamp decodeTimestampBinary(ReadableByteBuf buf, int length, Calendar int hour = buf.readByte(); int minutes = buf.readByte(); int seconds = buf.readByte(); - if (length > 8) { + if (length.get() > 8) { microseconds = buf.readUnsignedInt(); } int offset = cal.getTimeZone().getOffset(0); diff --git a/src/main/java/org/mariadb/jdbc/client/column/TimestampColumn.java b/src/main/java/org/mariadb/jdbc/client/column/TimestampColumn.java index fd9d90e78..05238afd5 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/TimestampColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/TimestampColumn.java @@ -4,6 +4,8 @@ package org.mariadb.jdbc.client.column; +import static org.mariadb.jdbc.client.result.Result.NULL_LENGTH; + import java.sql.*; import java.time.LocalDateTime; import java.time.ZonedDateTime; @@ -13,6 +15,7 @@ import org.mariadb.jdbc.client.ColumnDecoder; import org.mariadb.jdbc.client.DataType; import org.mariadb.jdbc.client.ReadableByteBuf; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.message.server.ColumnDefinitionPacket; import org.mariadb.jdbc.plugin.codec.LocalDateTimeCodec; import org.mariadb.jdbc.plugin.codec.TimeCodec; @@ -59,53 +62,54 @@ public String getColumnTypeName(Configuration conf) { } @Override - public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, int length) + public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, MutableInt length) throws SQLDataException { return decodeTimestampText(buf, length, null); } @Override - public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, int length) + public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, MutableInt length) throws SQLDataException { return decodeTimestampBinary(buf, length, null); } @Override - public boolean decodeBooleanText(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public boolean decodeBooleanText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Boolean", dataType)); } @Override - public boolean decodeBooleanBinary(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public boolean decodeBooleanBinary(ReadableByteBuf buf, MutableInt length) + throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Boolean", dataType)); } @Override - public byte decodeByteText(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Byte", dataType)); } @Override - public byte decodeByteBinary(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public byte decodeByteBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Byte", dataType)); } @Override - public String decodeStringText(ReadableByteBuf buf, int length, Calendar cal) + public String decodeStringText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - return buf.readString(length); + return buf.readString(length.get()); } @Override - public String decodeStringBinary(ReadableByteBuf buf, int length, Calendar cal) + public String decodeStringBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - if (length == 0) { + if (length.get() == 0) { StringBuilder zeroValue = new StringBuilder("0000-00-00 00:00:00"); if (getDecimals() > 0) { zeroValue.append("."); @@ -121,12 +125,12 @@ public String decodeStringBinary(ReadableByteBuf buf, int length, Calendar cal) int seconds = 0; long microseconds = 0; - if (length > 4) { + if (length.get() > 4) { hour = buf.readByte(); minutes = buf.readByte(); seconds = buf.readByte(); - if (length > 7) { + if (length.get() > 7) { microseconds = buf.readUnsignedInt(); } } @@ -151,76 +155,76 @@ public String decodeStringBinary(ReadableByteBuf buf, int length, Calendar cal) } @Override - public short decodeShortText(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Short", dataType)); } @Override - public short decodeShortBinary(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public short decodeShortBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Short", dataType)); } @Override - public int decodeIntText(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public int decodeIntText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Integer", dataType)); } @Override - public int decodeIntBinary(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public int decodeIntBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Integer", dataType)); } @Override - public long decodeLongText(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public long decodeLongText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Long", dataType)); } @Override - public long decodeLongBinary(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public long decodeLongBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Long", dataType)); } @Override - public float decodeFloatText(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public float decodeFloatText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Float", dataType)); } @Override - public float decodeFloatBinary(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public float decodeFloatBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Float", dataType)); } @Override - public double decodeDoubleText(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public double decodeDoubleText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Double", dataType)); } @Override - public double decodeDoubleBinary(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public double decodeDoubleBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Double", dataType)); } @Override - public Date decodeDateText(ReadableByteBuf buf, int length, Calendar cal) + public Date decodeDateText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { int pos = buf.pos(); int nanoBegin = -1; int[] timestampsPart = new int[] {0, 0, 0, 0, 0, 0, 0}; int partIdx = 0; - for (int begin = 0; begin < length; begin++) { + for (int begin = 0; begin < length.get(); begin++) { byte b = buf.readByte(); if (b == '-' || b == ' ' || b == ':') { partIdx++; @@ -236,7 +240,7 @@ public Date decodeDateText(ReadableByteBuf buf, int length, Calendar cal) throw new SQLDataException( String.format( "value '%s' (%s) cannot be decoded as Timestamp", - buf.readString(length), dataType)); + buf.readString(length.get()), dataType)); } timestampsPart[partIdx] = timestampsPart[partIdx] * 10 + b - 48; @@ -248,12 +252,13 @@ public Date decodeDateText(ReadableByteBuf buf, int length, Calendar cal) && timestampsPart[4] == 0 && timestampsPart[5] == 0 && timestampsPart[6] == 0) { + length.set(NULL_LENGTH); return null; } // fix non-leading tray for nanoseconds if (nanoBegin > 0) { - for (int begin = 0; begin < 6 - (length - nanoBegin - 1); begin++) { + for (int begin = 0; begin < 6 - (length.get() - nanoBegin - 1); begin++) { timestampsPart[6] = timestampsPart[6] * 10; } } @@ -290,11 +295,14 @@ public Date decodeDateText(ReadableByteBuf buf, int length, Calendar cal) } @Override - public Date decodeDateBinary(ReadableByteBuf buf, int length, Calendar calParam) + public Date decodeDateBinary(ReadableByteBuf buf, MutableInt length, Calendar calParam) throws SQLDataException { Calendar cal = calParam == null ? Calendar.getInstance() : calParam; - if (length == 0) return null; + if (length.get() == 0) { + length.set(NULL_LENGTH); + return null; + } int year = buf.readUnsignedShort(); int month = buf.readByte(); int dayOfMonth = buf.readByte(); @@ -303,12 +311,12 @@ public Date decodeDateBinary(ReadableByteBuf buf, int length, Calendar calParam) int seconds = 0; long microseconds = 0; - if (length > 4) { + if (length.get() > 4) { hour = buf.readByte(); minutes = buf.readByte(); seconds = buf.readByte(); - if (length > 7) { + if (length.get() > 7) { microseconds = buf.readUnsignedInt(); } } @@ -320,7 +328,10 @@ public Date decodeDateBinary(ReadableByteBuf buf, int length, Calendar calParam) && hour == 0 && minutes == 0 && seconds == 0 - && microseconds == 0) return null; + && microseconds == 0) { + length.set(NULL_LENGTH); + return null; + } Timestamp timestamp; synchronized (cal) { @@ -334,7 +345,7 @@ public Date decodeDateBinary(ReadableByteBuf buf, int length, Calendar calParam) } @Override - public Time decodeTimeText(ReadableByteBuf buf, int length, Calendar cal) + public Time decodeTimeText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { LocalDateTime lt = LocalDateTimeCodec.INSTANCE.decodeText(buf, length, this, cal); if (lt == null) return null; @@ -345,9 +356,12 @@ public Time decodeTimeText(ReadableByteBuf buf, int length, Calendar cal) } @Override - public Time decodeTimeBinary(ReadableByteBuf buf, int length, Calendar calParam) + public Time decodeTimeBinary(ReadableByteBuf buf, MutableInt length, Calendar calParam) throws SQLDataException { - if (length == 0) return null; + if (length.get() == 0) { + length.set(NULL_LENGTH); + return null; + } Calendar cal = calParam == null ? Calendar.getInstance() : calParam; int year = buf.readUnsignedShort(); @@ -359,17 +373,18 @@ public Time decodeTimeBinary(ReadableByteBuf buf, int length, Calendar calParam) int seconds = 0; long microseconds = 0; - if (length > 4) { + if (length.get() > 4) { hour = buf.readByte(); minutes = buf.readByte(); seconds = buf.readByte(); - if (length > 7) { + if (length.get() > 7) { microseconds = buf.readUnsignedInt(); } } if (year == 0 && month == 0 && dayOfMonth == 0 && hour == 0 && minutes == 0 && seconds == 0) { + length.set(NULL_LENGTH); return null; } @@ -381,13 +396,13 @@ public Time decodeTimeBinary(ReadableByteBuf buf, int length, Calendar calParam) } @Override - public Timestamp decodeTimestampText(ReadableByteBuf buf, int length, Calendar calParam) + public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Calendar calParam) throws SQLDataException { int pos = buf.pos(); int nanoBegin = -1; int[] timestampsPart = new int[] {0, 0, 0, 0, 0, 0, 0}; int partIdx = 0; - for (int begin = 0; begin < length; begin++) { + for (int begin = 0; begin < length.get(); begin++) { byte b = buf.readByte(); if (b == '-' || b == ' ' || b == ':') { partIdx++; @@ -403,7 +418,7 @@ public Timestamp decodeTimestampText(ReadableByteBuf buf, int length, Calendar c throw new SQLDataException( String.format( "value '%s' (%s) cannot be decoded as Timestamp", - buf.readString(length), dataType)); + buf.readString(length.get()), dataType)); } timestampsPart[partIdx] = timestampsPart[partIdx] * 10 + b - 48; @@ -415,12 +430,13 @@ public Timestamp decodeTimestampText(ReadableByteBuf buf, int length, Calendar c && timestampsPart[4] == 0 && timestampsPart[5] == 0 && timestampsPart[6] == 0) { + length.set(NULL_LENGTH); return null; } // fix non-leading tray for nanoseconds if (nanoBegin > 0) { - for (int begin = 0; begin < 6 - (length - nanoBegin - 1); begin++) { + for (int begin = 0; begin < 6 - (length.get() - nanoBegin - 1); begin++) { timestampsPart[6] = timestampsPart[6] * 10; } } @@ -455,9 +471,12 @@ public Timestamp decodeTimestampText(ReadableByteBuf buf, int length, Calendar c } @Override - public Timestamp decodeTimestampBinary(ReadableByteBuf buf, int length, Calendar calParam) + public Timestamp decodeTimestampBinary(ReadableByteBuf buf, MutableInt length, Calendar calParam) throws SQLDataException { - if (length == 0) return null; + if (length.get() == 0) { + length.set(NULL_LENGTH); + return null; + } Calendar cal = calParam == null ? Calendar.getInstance() : calParam; int year = buf.readUnsignedShort(); @@ -468,12 +487,12 @@ public Timestamp decodeTimestampBinary(ReadableByteBuf buf, int length, Calendar int seconds = 0; long microseconds = 0; - if (length > 4) { + if (length.get() > 4) { hour = buf.readByte(); minutes = buf.readByte(); seconds = buf.readByte(); - if (length > 7) { + if (length.get() > 7) { microseconds = buf.readUnsignedInt(); } } @@ -485,7 +504,10 @@ public Timestamp decodeTimestampBinary(ReadableByteBuf buf, int length, Calendar && hour == 0 && minutes == 0 && seconds == 0 - && microseconds == 0) return null; + && microseconds == 0) { + length.set(NULL_LENGTH); + return null; + } Timestamp timestamp; synchronized (cal) { cal.clear(); diff --git a/src/main/java/org/mariadb/jdbc/client/column/UnsignedBigIntColumn.java b/src/main/java/org/mariadb/jdbc/client/column/UnsignedBigIntColumn.java index 0eb8a72c4..be98ef2f8 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/UnsignedBigIntColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/UnsignedBigIntColumn.java @@ -11,6 +11,7 @@ import org.mariadb.jdbc.client.ColumnDecoder; import org.mariadb.jdbc.client.DataType; import org.mariadb.jdbc.client.ReadableByteBuf; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.message.server.ColumnDefinitionPacket; /** Column metadata definition */ @@ -55,13 +56,13 @@ public String getColumnTypeName(Configuration conf) { } @Override - public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, int length) + public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, MutableInt length) throws SQLDataException { - return new BigInteger(buf.readAscii(length)); + return new BigInteger(buf.readAscii(length.get())); } @Override - public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, int length) + public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, MutableInt length) throws SQLDataException { // need BIG ENDIAN, so reverse order byte[] bb = new byte[8]; @@ -72,19 +73,20 @@ public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, in } @Override - public boolean decodeBooleanText(ReadableByteBuf buf, int length) throws SQLDataException { - String s = buf.readAscii(length); + public boolean decodeBooleanText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + String s = buf.readAscii(length.get()); return !"0".equals(s); } @Override - public boolean decodeBooleanBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public boolean decodeBooleanBinary(ReadableByteBuf buf, MutableInt length) + throws SQLDataException { return buf.readLong() != 0; } @Override - public byte decodeByteText(ReadableByteBuf buf, int length) throws SQLDataException { - long result = buf.atoull(length); + public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + long result = buf.atoull(length.get()); if ((byte) result != result || result < 0) { throw new SQLDataException("byte overflow"); } @@ -92,7 +94,7 @@ public byte decodeByteText(ReadableByteBuf buf, int length) throws SQLDataExcept } @Override - public byte decodeByteBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public byte decodeByteBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { // need BIG ENDIAN, so reverse order byte[] bb = new byte[8]; for (int i = 7; i >= 0; i--) { @@ -108,13 +110,13 @@ public byte decodeByteBinary(ReadableByteBuf buf, int length) throws SQLDataExce } @Override - public String decodeStringText(ReadableByteBuf buf, int length, Calendar cal) + public String decodeStringText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - return buf.readString(length); + return buf.readString(length.get()); } @Override - public String decodeStringBinary(ReadableByteBuf buf, int length, Calendar cal) + public String decodeStringBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { // need BIG ENDIAN, so reverse order byte[] bb = new byte[8]; @@ -125,8 +127,8 @@ public String decodeStringBinary(ReadableByteBuf buf, int length, Calendar cal) } @Override - public short decodeShortText(ReadableByteBuf buf, int length) throws SQLDataException { - long result = buf.atoull(length); + public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + long result = buf.atoull(length.get()); if ((short) result != result || result < 0) { throw new SQLDataException("Short overflow"); } @@ -134,7 +136,7 @@ public short decodeShortText(ReadableByteBuf buf, int length) throws SQLDataExce } @Override - public short decodeShortBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public short decodeShortBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { long result = buf.readLong(); if ((short) result != result || result < 0) { throw new SQLDataException("Short overflow"); @@ -143,8 +145,8 @@ public short decodeShortBinary(ReadableByteBuf buf, int length) throws SQLDataEx } @Override - public int decodeIntText(ReadableByteBuf buf, int length) throws SQLDataException { - long result = buf.atoull(length); + public int decodeIntText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + long result = buf.atoull(length.get()); int res = (int) result; if (res != result || result < 0) { throw new SQLDataException("integer overflow"); @@ -153,7 +155,7 @@ public int decodeIntText(ReadableByteBuf buf, int length) throws SQLDataExceptio } @Override - public int decodeIntBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public int decodeIntBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { // need BIG ENDIAN, so reverse order byte[] bb = new byte[8]; @@ -169,9 +171,9 @@ public int decodeIntBinary(ReadableByteBuf buf, int length) throws SQLDataExcept } @Override - public long decodeLongText(ReadableByteBuf buf, int length) throws SQLDataException { - if (length < 10) return buf.atoull(length); - BigInteger val = new BigInteger(buf.readAscii(length)); + public long decodeLongText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + if (length.get() < 10) return buf.atoull(length.get()); + BigInteger val = new BigInteger(buf.readAscii(length.get())); try { return val.longValueExact(); } catch (ArithmeticException ae) { @@ -180,7 +182,7 @@ public long decodeLongText(ReadableByteBuf buf, int length) throws SQLDataExcept } @Override - public long decodeLongBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public long decodeLongBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { if ((buf.getByte(buf.pos() + 7) & 0x80) == 0) { return buf.readLong(); } else { @@ -199,12 +201,12 @@ public long decodeLongBinary(ReadableByteBuf buf, int length) throws SQLDataExce } @Override - public float decodeFloatText(ReadableByteBuf buf, int length) throws SQLDataException { - return Float.parseFloat(buf.readAscii(length)); + public float decodeFloatText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + return Float.parseFloat(buf.readAscii(length.get())); } @Override - public float decodeFloatBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public float decodeFloatBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { // need BIG ENDIAN, so reverse order byte[] bb = new byte[8]; for (int i = 7; i >= 0; i--) { @@ -214,12 +216,12 @@ public float decodeFloatBinary(ReadableByteBuf buf, int length) throws SQLDataEx } @Override - public double decodeDoubleText(ReadableByteBuf buf, int length) throws SQLDataException { - return Double.parseDouble(buf.readAscii(length)); + public double decodeDoubleText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + return Double.parseDouble(buf.readAscii(length.get())); } @Override - public double decodeDoubleBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public double decodeDoubleBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { // need BIG ENDIAN, so reverse order byte[] bb = new byte[8]; for (int i = 7; i >= 0; i--) { @@ -229,45 +231,45 @@ public double decodeDoubleBinary(ReadableByteBuf buf, int length) throws SQLData } @Override - public Date decodeDateText(ReadableByteBuf buf, int length, Calendar cal) + public Date decodeDateText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Date decodeDateBinary(ReadableByteBuf buf, int length, Calendar cal) + public Date decodeDateBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Time decodeTimeText(ReadableByteBuf buf, int length, Calendar cal) + public Time decodeTimeText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Time decodeTimeBinary(ReadableByteBuf buf, int length, Calendar cal) + public Time decodeTimeBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Timestamp decodeTimestampText(ReadableByteBuf buf, int length, Calendar cal) + public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Timestamp", dataType)); } @Override - public Timestamp decodeTimestampBinary(ReadableByteBuf buf, int length, Calendar cal) + public Timestamp decodeTimestampBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Timestamp", dataType)); } diff --git a/src/main/java/org/mariadb/jdbc/client/column/UnsignedIntColumn.java b/src/main/java/org/mariadb/jdbc/client/column/UnsignedIntColumn.java index 00a806d91..6fe7d2825 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/UnsignedIntColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/UnsignedIntColumn.java @@ -10,6 +10,7 @@ import org.mariadb.jdbc.client.ColumnDecoder; import org.mariadb.jdbc.client.DataType; import org.mariadb.jdbc.client.ReadableByteBuf; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.message.server.ColumnDefinitionPacket; /** Column metadata definition */ @@ -54,31 +55,32 @@ public String getColumnTypeName(Configuration conf) { } @Override - public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, int length) + public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, MutableInt length) throws SQLDataException { - return buf.atoull(length); + return buf.atoull(length.get()); } @Override - public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, int length) + public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, MutableInt length) throws SQLDataException { return buf.readUnsignedInt(); } @Override - public boolean decodeBooleanText(ReadableByteBuf buf, int length) throws SQLDataException { - String s = buf.readAscii(length); + public boolean decodeBooleanText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + String s = buf.readAscii(length.get()); return !"0".equals(s); } @Override - public boolean decodeBooleanBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public boolean decodeBooleanBinary(ReadableByteBuf buf, MutableInt length) + throws SQLDataException { return buf.readInt() != 0; } @Override - public byte decodeByteText(ReadableByteBuf buf, int length) throws SQLDataException { - long result = buf.atoull(length); + public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + long result = buf.atoull(length.get()); if ((byte) result != result || (result < 0 && !isSigned())) { throw new SQLDataException("byte overflow"); } @@ -86,7 +88,7 @@ public byte decodeByteText(ReadableByteBuf buf, int length) throws SQLDataExcept } @Override - public byte decodeByteBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public byte decodeByteBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { long result = buf.readUnsignedInt(); if ((byte) result != result) { throw new SQLDataException("byte overflow"); @@ -96,20 +98,20 @@ public byte decodeByteBinary(ReadableByteBuf buf, int length) throws SQLDataExce } @Override - public String decodeStringText(ReadableByteBuf buf, int length, Calendar cal) + public String decodeStringText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - return buf.readString(length); + return buf.readString(length.get()); } @Override - public String decodeStringBinary(ReadableByteBuf buf, int length, Calendar cal) + public String decodeStringBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { return String.valueOf(buf.readUnsignedInt()); } @Override - public short decodeShortText(ReadableByteBuf buf, int length) throws SQLDataException { - long result = buf.atoll(length); + public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + long result = buf.atoll(length.get()); if ((short) result != result || result < 0) { throw new SQLDataException("Short overflow"); } @@ -117,7 +119,7 @@ public short decodeShortText(ReadableByteBuf buf, int length) throws SQLDataExce } @Override - public short decodeShortBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public short decodeShortBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { long result = buf.readUnsignedInt(); if ((short) result != result || result < 0) { throw new SQLDataException("Short overflow"); @@ -126,8 +128,8 @@ public short decodeShortBinary(ReadableByteBuf buf, int length) throws SQLDataEx } @Override - public int decodeIntText(ReadableByteBuf buf, int length) throws SQLDataException { - long result = buf.atoull(length); + public int decodeIntText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + long result = buf.atoull(length.get()); int res = (int) result; if (res != result || result < 0) { throw new SQLDataException("integer overflow"); @@ -136,7 +138,7 @@ public int decodeIntText(ReadableByteBuf buf, int length) throws SQLDataExceptio } @Override - public int decodeIntBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public int decodeIntBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { long result = buf.readUnsignedInt(); int res = (int) result; if (res != result) { @@ -147,75 +149,75 @@ public int decodeIntBinary(ReadableByteBuf buf, int length) throws SQLDataExcept } @Override - public long decodeLongText(ReadableByteBuf buf, int length) throws SQLDataException { - return buf.atoull(length); + public long decodeLongText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + return buf.atoull(length.get()); } @Override - public long decodeLongBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public long decodeLongBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { return buf.readUnsignedInt(); } @Override - public float decodeFloatText(ReadableByteBuf buf, int length) throws SQLDataException { - return Float.parseFloat(buf.readAscii(length)); + public float decodeFloatText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + return Float.parseFloat(buf.readAscii(length.get())); } @Override - public float decodeFloatBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public float decodeFloatBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { return (float) buf.readUnsignedInt(); } @Override - public double decodeDoubleText(ReadableByteBuf buf, int length) throws SQLDataException { - return Double.parseDouble(buf.readAscii(length)); + public double decodeDoubleText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + return Double.parseDouble(buf.readAscii(length.get())); } @Override - public double decodeDoubleBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public double decodeDoubleBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { return buf.readUnsignedInt(); } @Override - public Date decodeDateText(ReadableByteBuf buf, int length, Calendar cal) + public Date decodeDateText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Date decodeDateBinary(ReadableByteBuf buf, int length, Calendar cal) + public Date decodeDateBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Time decodeTimeText(ReadableByteBuf buf, int length, Calendar cal) + public Time decodeTimeText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Time decodeTimeBinary(ReadableByteBuf buf, int length, Calendar cal) + public Time decodeTimeBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Timestamp decodeTimestampText(ReadableByteBuf buf, int length, Calendar cal) + public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Timestamp", dataType)); } @Override - public Timestamp decodeTimestampBinary(ReadableByteBuf buf, int length, Calendar cal) + public Timestamp decodeTimestampBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Timestamp", dataType)); } diff --git a/src/main/java/org/mariadb/jdbc/client/column/UnsignedMediumIntColumn.java b/src/main/java/org/mariadb/jdbc/client/column/UnsignedMediumIntColumn.java index d24294106..d05e49979 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/UnsignedMediumIntColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/UnsignedMediumIntColumn.java @@ -10,6 +10,7 @@ import org.mariadb.jdbc.client.ColumnDecoder; import org.mariadb.jdbc.client.DataType; import org.mariadb.jdbc.client.ReadableByteBuf; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.message.server.ColumnDefinitionPacket; /** Column metadata definition */ @@ -54,31 +55,32 @@ public String getColumnTypeName(Configuration conf) { } @Override - public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, int length) + public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, MutableInt length) throws SQLDataException { return decodeIntText(buf, length); } @Override - public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, int length) + public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, MutableInt length) throws SQLDataException { return decodeIntBinary(buf, length); } @Override - public boolean decodeBooleanText(ReadableByteBuf buf, int length) throws SQLDataException { - String s = buf.readAscii(length); + public boolean decodeBooleanText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + String s = buf.readAscii(length.get()); return !"0".equals(s); } @Override - public boolean decodeBooleanBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public boolean decodeBooleanBinary(ReadableByteBuf buf, MutableInt length) + throws SQLDataException { return buf.readInt() != 0; } @Override - public byte decodeByteText(ReadableByteBuf buf, int length) throws SQLDataException { - long result = buf.atoull(length); + public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + long result = buf.atoull(length.get()); if ((byte) result != result || result < 0) { throw new SQLDataException("byte overflow"); } @@ -86,7 +88,7 @@ public byte decodeByteText(ReadableByteBuf buf, int length) throws SQLDataExcept } @Override - public byte decodeByteBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public byte decodeByteBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { long result = buf.readUnsignedMedium(); buf.skip(); // MEDIUMINT is encoded on 4 bytes in exchanges ! if ((byte) result != result) { @@ -97,13 +99,13 @@ public byte decodeByteBinary(ReadableByteBuf buf, int length) throws SQLDataExce } @Override - public String decodeStringText(ReadableByteBuf buf, int length, Calendar cal) + public String decodeStringText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - return buf.readString(length); + return buf.readString(length.get()); } @Override - public String decodeStringBinary(ReadableByteBuf buf, int length, Calendar cal) + public String decodeStringBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { String mediumStr = String.valueOf(buf.readUnsignedMedium()); buf.skip(); // MEDIUMINT is encoded on 4 bytes in exchanges ! @@ -111,8 +113,8 @@ public String decodeStringBinary(ReadableByteBuf buf, int length, Calendar cal) } @Override - public short decodeShortText(ReadableByteBuf buf, int length) throws SQLDataException { - long result = buf.atoull(length); + public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + long result = buf.atoull(length.get()); if ((short) result != result || result < 0) { throw new SQLDataException("Short overflow"); } @@ -120,7 +122,7 @@ public short decodeShortText(ReadableByteBuf buf, int length) throws SQLDataExce } @Override - public short decodeShortBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public short decodeShortBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { long result = buf.readUnsignedMedium(); buf.skip(); // MEDIUMINT is encoded on 4 bytes in exchanges ! if ((short) result != result || result < 0) { @@ -130,93 +132,93 @@ public short decodeShortBinary(ReadableByteBuf buf, int length) throws SQLDataEx } @Override - public int decodeIntText(ReadableByteBuf buf, int length) throws SQLDataException { - return (int) buf.atoll(length); + public int decodeIntText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + return (int) buf.atoll(length.get()); } @Override - public int decodeIntBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public int decodeIntBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { int res = buf.readUnsignedMedium(); buf.skip(); // MEDIUMINT is encoded on 4 bytes in exchanges ! return res; } @Override - public long decodeLongText(ReadableByteBuf buf, int length) throws SQLDataException { - return buf.atoull(length); + public long decodeLongText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + return buf.atoull(length.get()); } @Override - public long decodeLongBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public long decodeLongBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { long l = buf.readUnsignedMedium(); buf.skip(); // MEDIUMINT is encoded on 4 bytes in exchanges ! return l; } @Override - public float decodeFloatText(ReadableByteBuf buf, int length) throws SQLDataException { - return Float.parseFloat(buf.readAscii(length)); + public float decodeFloatText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + return Float.parseFloat(buf.readAscii(length.get())); } @Override - public float decodeFloatBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public float decodeFloatBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { float f = buf.readUnsignedMedium(); buf.skip(); // MEDIUMINT is encoded on 4 bytes in exchanges ! return f; } @Override - public double decodeDoubleText(ReadableByteBuf buf, int length) throws SQLDataException { - return Double.parseDouble(buf.readAscii(length)); + public double decodeDoubleText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + return Double.parseDouble(buf.readAscii(length.get())); } @Override - public double decodeDoubleBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public double decodeDoubleBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { double f = buf.readUnsignedMedium(); buf.skip(); // MEDIUMINT is encoded on 4 bytes in exchanges ! return f; } @Override - public Date decodeDateText(ReadableByteBuf buf, int length, Calendar cal) + public Date decodeDateText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Date decodeDateBinary(ReadableByteBuf buf, int length, Calendar cal) + public Date decodeDateBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Time decodeTimeText(ReadableByteBuf buf, int length, Calendar cal) + public Time decodeTimeText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Time decodeTimeBinary(ReadableByteBuf buf, int length, Calendar cal) + public Time decodeTimeBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Timestamp decodeTimestampText(ReadableByteBuf buf, int length, Calendar cal) + public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Timestamp", dataType)); } @Override - public Timestamp decodeTimestampBinary(ReadableByteBuf buf, int length, Calendar cal) + public Timestamp decodeTimestampBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Timestamp", dataType)); } diff --git a/src/main/java/org/mariadb/jdbc/client/column/UnsignedSmallIntColumn.java b/src/main/java/org/mariadb/jdbc/client/column/UnsignedSmallIntColumn.java index b8976714c..cda4428cb 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/UnsignedSmallIntColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/UnsignedSmallIntColumn.java @@ -10,6 +10,7 @@ import org.mariadb.jdbc.client.ColumnDecoder; import org.mariadb.jdbc.client.DataType; import org.mariadb.jdbc.client.ReadableByteBuf; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.message.server.ColumnDefinitionPacket; /** Column metadata definition */ @@ -54,31 +55,32 @@ public String getColumnTypeName(Configuration conf) { } @Override - public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, int length) + public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, MutableInt length) throws SQLDataException { - return (int) buf.atoull(length); + return (int) buf.atoull(length.get()); } @Override - public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, int length) + public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, MutableInt length) throws SQLDataException { return buf.readUnsignedShort(); } @Override - public boolean decodeBooleanText(ReadableByteBuf buf, int length) throws SQLDataException { - String s = buf.readAscii(length); + public boolean decodeBooleanText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + String s = buf.readAscii(length.get()); return !"0".equals(s); } @Override - public boolean decodeBooleanBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public boolean decodeBooleanBinary(ReadableByteBuf buf, MutableInt length) + throws SQLDataException { return buf.readShort() != 0; } @Override - public byte decodeByteText(ReadableByteBuf buf, int length) throws SQLDataException { - long result = buf.atoull(length); + public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + long result = buf.atoull(length.get()); if ((byte) result != result || result < 0) { throw new SQLDataException("byte overflow"); } @@ -86,7 +88,7 @@ public byte decodeByteText(ReadableByteBuf buf, int length) throws SQLDataExcept } @Override - public byte decodeByteBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public byte decodeByteBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { long result = buf.readUnsignedShort(); if ((byte) result != result) { throw new SQLDataException("byte overflow"); @@ -95,20 +97,20 @@ public byte decodeByteBinary(ReadableByteBuf buf, int length) throws SQLDataExce } @Override - public String decodeStringText(ReadableByteBuf buf, int length, Calendar cal) + public String decodeStringText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - return buf.readString(length); + return buf.readString(length.get()); } @Override - public String decodeStringBinary(ReadableByteBuf buf, int length, Calendar cal) + public String decodeStringBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { return String.valueOf(buf.readUnsignedShort()); } @Override - public short decodeShortText(ReadableByteBuf buf, int length) throws SQLDataException { - long result = buf.atoull(length); + public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + long result = buf.atoull(length.get()); if ((short) result != result) { throw new SQLDataException("Short overflow"); } @@ -116,7 +118,7 @@ public short decodeShortText(ReadableByteBuf buf, int length) throws SQLDataExce } @Override - public short decodeShortBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public short decodeShortBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { int result = buf.readUnsignedShort(); if ((short) result != result) { throw new SQLDataException("Short overflow"); @@ -125,85 +127,85 @@ public short decodeShortBinary(ReadableByteBuf buf, int length) throws SQLDataEx } @Override - public int decodeIntText(ReadableByteBuf buf, int length) throws SQLDataException { - return (int) buf.atoull(length); + public int decodeIntText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + return (int) buf.atoull(length.get()); } @Override - public int decodeIntBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public int decodeIntBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { return buf.readUnsignedShort(); } @Override - public long decodeLongText(ReadableByteBuf buf, int length) throws SQLDataException { - return buf.atoull(length); + public long decodeLongText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + return buf.atoull(length.get()); } @Override - public long decodeLongBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public long decodeLongBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { return buf.readUnsignedShort(); } @Override - public float decodeFloatText(ReadableByteBuf buf, int length) throws SQLDataException { - return Float.parseFloat(buf.readAscii(length)); + public float decodeFloatText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + return Float.parseFloat(buf.readAscii(length.get())); } @Override - public float decodeFloatBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public float decodeFloatBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { return (float) buf.readUnsignedShort(); } @Override - public double decodeDoubleText(ReadableByteBuf buf, int length) throws SQLDataException { - return Double.parseDouble(buf.readAscii(length)); + public double decodeDoubleText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + return Double.parseDouble(buf.readAscii(length.get())); } @Override - public double decodeDoubleBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public double decodeDoubleBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { return buf.readUnsignedShort(); } @Override - public Date decodeDateText(ReadableByteBuf buf, int length, Calendar cal) + public Date decodeDateText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Date decodeDateBinary(ReadableByteBuf buf, int length, Calendar cal) + public Date decodeDateBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Time decodeTimeText(ReadableByteBuf buf, int length, Calendar cal) + public Time decodeTimeText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Time decodeTimeBinary(ReadableByteBuf buf, int length, Calendar cal) + public Time decodeTimeBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Timestamp decodeTimestampText(ReadableByteBuf buf, int length, Calendar cal) + public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Timestamp", dataType)); } @Override - public Timestamp decodeTimestampBinary(ReadableByteBuf buf, int length, Calendar cal) + public Timestamp decodeTimestampBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Timestamp", dataType)); } diff --git a/src/main/java/org/mariadb/jdbc/client/column/UnsignedTinyIntColumn.java b/src/main/java/org/mariadb/jdbc/client/column/UnsignedTinyIntColumn.java index 29f3cc16a..8c43a2d44 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/UnsignedTinyIntColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/UnsignedTinyIntColumn.java @@ -10,6 +10,7 @@ import org.mariadb.jdbc.client.ColumnDecoder; import org.mariadb.jdbc.client.DataType; import org.mariadb.jdbc.client.ReadableByteBuf; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.message.server.ColumnDefinitionPacket; /** Column metadata definition */ @@ -61,16 +62,16 @@ public String getColumnTypeName(Configuration conf) { } @Override - public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, int length) + public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, MutableInt length) throws SQLDataException { if (conf.tinyInt1isBit() && columnLength == 1) { return decodeBooleanText(buf, length); } - return (int) buf.atoull(length); + return (int) buf.atoull(length.get()); } @Override - public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, int length) + public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, MutableInt length) throws SQLDataException { if (conf.tinyInt1isBit() && columnLength == 1) { return decodeBooleanBinary(buf, length); @@ -79,19 +80,20 @@ public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, in } @Override - public boolean decodeBooleanText(ReadableByteBuf buf, int length) throws SQLDataException { - String s = buf.readAscii(length); + public boolean decodeBooleanText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + String s = buf.readAscii(length.get()); return !"0".equals(s); } @Override - public boolean decodeBooleanBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public boolean decodeBooleanBinary(ReadableByteBuf buf, MutableInt length) + throws SQLDataException { return buf.readByte() != 0; } @Override - public byte decodeByteText(ReadableByteBuf buf, int length) throws SQLDataException { - long result = buf.atoull(length); + public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + long result = buf.atoull(length.get()); if ((byte) result != result) { throw new SQLDataException("byte overflow"); } @@ -99,7 +101,7 @@ public byte decodeByteText(ReadableByteBuf buf, int length) throws SQLDataExcept } @Override - public byte decodeByteBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public byte decodeByteBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { long result = buf.readUnsignedByte(); if ((byte) result != result) { throw new SQLDataException("byte overflow"); @@ -108,107 +110,107 @@ public byte decodeByteBinary(ReadableByteBuf buf, int length) throws SQLDataExce } @Override - public String decodeStringText(ReadableByteBuf buf, int length, Calendar cal) + public String decodeStringText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - return buf.readString(length); + return buf.readString(length.get()); } @Override - public String decodeStringBinary(ReadableByteBuf buf, int length, Calendar cal) + public String decodeStringBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { return String.valueOf(buf.readUnsignedByte()); } @Override - public short decodeShortText(ReadableByteBuf buf, int length) throws SQLDataException { - return (short) buf.atoull(length); + public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + return (short) buf.atoull(length.get()); } @Override - public short decodeShortBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public short decodeShortBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { return buf.readUnsignedByte(); } @Override - public int decodeIntText(ReadableByteBuf buf, int length) throws SQLDataException { - return (int) buf.atoull(length); + public int decodeIntText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + return (int) buf.atoull(length.get()); } @Override - public int decodeIntBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public int decodeIntBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { return buf.readUnsignedByte(); } @Override - public long decodeLongText(ReadableByteBuf buf, int length) throws SQLDataException { - return buf.atoull(length); + public long decodeLongText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + return buf.atoull(length.get()); } @Override - public long decodeLongBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public long decodeLongBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { return buf.readUnsignedByte(); } @Override - public float decodeFloatText(ReadableByteBuf buf, int length) throws SQLDataException { - return Float.parseFloat(buf.readAscii(length)); + public float decodeFloatText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + return Float.parseFloat(buf.readAscii(length.get())); } @Override - public float decodeFloatBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public float decodeFloatBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { return buf.readUnsignedByte(); } @Override - public double decodeDoubleText(ReadableByteBuf buf, int length) throws SQLDataException { - return Double.parseDouble(buf.readAscii(length)); + public double decodeDoubleText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + return Double.parseDouble(buf.readAscii(length.get())); } @Override - public double decodeDoubleBinary(ReadableByteBuf buf, int length) throws SQLDataException { + public double decodeDoubleBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { return buf.readUnsignedByte(); } @Override - public Date decodeDateText(ReadableByteBuf buf, int length, Calendar cal) + public Date decodeDateText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Date decodeDateBinary(ReadableByteBuf buf, int length, Calendar cal) + public Date decodeDateBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Time decodeTimeText(ReadableByteBuf buf, int length, Calendar cal) + public Time decodeTimeText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Time decodeTimeBinary(ReadableByteBuf buf, int length, Calendar cal) + public Time decodeTimeBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Timestamp decodeTimestampText(ReadableByteBuf buf, int length, Calendar cal) + public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Timestamp", dataType)); } @Override - public Timestamp decodeTimestampBinary(ReadableByteBuf buf, int length, Calendar cal) + public Timestamp decodeTimestampBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Timestamp", dataType)); } diff --git a/src/main/java/org/mariadb/jdbc/client/column/UuidColumn.java b/src/main/java/org/mariadb/jdbc/client/column/UuidColumn.java index 2294130fc..349e068c7 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/UuidColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/UuidColumn.java @@ -11,6 +11,7 @@ import org.mariadb.jdbc.client.ColumnDecoder; import org.mariadb.jdbc.client.DataType; import org.mariadb.jdbc.client.ReadableByteBuf; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.message.server.ColumnDefinitionPacket; import org.mariadb.jdbc.util.CharsetEncodingLength; @@ -64,152 +65,157 @@ public int getPrecision() { } @Override - public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, int length) + public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, MutableInt length) throws SQLDataException { - return conf.uuidAsString() ? buf.readString(length) : UUID.fromString(buf.readAscii(length)); + return conf.uuidAsString() + ? buf.readString(length.get()) + : UUID.fromString(buf.readAscii(length.get())); } @Override - public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, int length) + public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, MutableInt length) throws SQLDataException { - return conf.uuidAsString() ? buf.readString(length) : UUID.fromString(buf.readAscii(length)); + return conf.uuidAsString() + ? buf.readString(length.get()) + : UUID.fromString(buf.readAscii(length.get())); } @Override - public boolean decodeBooleanText(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public boolean decodeBooleanText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException("Data type UUID cannot be decoded as Boolean"); } @Override - public boolean decodeBooleanBinary(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public boolean decodeBooleanBinary(ReadableByteBuf buf, MutableInt length) + throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException("Data type UUID cannot be decoded as Boolean"); } @Override - public byte decodeByteText(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException("Data type UUID cannot be decoded as byte"); } @Override - public byte decodeByteBinary(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public byte decodeByteBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException("Data type UUID cannot be decoded as byte"); } @Override - public String decodeStringText(ReadableByteBuf buf, int length, Calendar cal) + public String decodeStringText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - return buf.readString(length); + return buf.readString(length.get()); } @Override - public String decodeStringBinary(ReadableByteBuf buf, int length, Calendar cal) + public String decodeStringBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - return buf.readString(length); + return buf.readString(length.get()); } @Override - public short decodeShortText(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException("Data type UUID cannot be decoded as Short"); } @Override - public short decodeShortBinary(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public short decodeShortBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException("Data type UUID cannot be decoded as Short"); } @Override - public int decodeIntText(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public int decodeIntText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException("Data type UUID cannot be decoded as Integer"); } @Override - public int decodeIntBinary(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public int decodeIntBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException("Data type UUID cannot be decoded as Integer"); } @Override - public long decodeLongText(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public long decodeLongText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException("Data type UUID cannot be decoded as Long"); } @Override - public long decodeLongBinary(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public long decodeLongBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException("Data type UUID cannot be decoded as Long"); } @Override - public float decodeFloatText(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public float decodeFloatText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException("Data type UUID cannot be decoded as Float"); } @Override - public float decodeFloatBinary(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public float decodeFloatBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException("Data type UUID cannot be decoded as Float"); } @Override - public double decodeDoubleText(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public double decodeDoubleText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException("Data type UUID cannot be decoded as Double"); } @Override - public double decodeDoubleBinary(ReadableByteBuf buf, int length) throws SQLDataException { - buf.skip(length); + public double decodeDoubleBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + buf.skip(length.get()); throw new SQLDataException("Data type UUID cannot be decoded as Double"); } @Override - public Date decodeDateText(ReadableByteBuf buf, int length, Calendar cal) + public Date decodeDateText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException("Data type UUID cannot be decoded as Date"); } @Override - public Date decodeDateBinary(ReadableByteBuf buf, int length, Calendar cal) + public Date decodeDateBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException("Data type UUID cannot be decoded as Date"); } @Override - public Time decodeTimeText(ReadableByteBuf buf, int length, Calendar cal) + public Time decodeTimeText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException("Data type UUID cannot be decoded as Time"); } @Override - public Time decodeTimeBinary(ReadableByteBuf buf, int length, Calendar calParam) + public Time decodeTimeBinary(ReadableByteBuf buf, MutableInt length, Calendar calParam) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException("Data type UUID cannot be decoded as Time"); } @Override - public Timestamp decodeTimestampText(ReadableByteBuf buf, int length, Calendar calParam) + public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Calendar calParam) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException("Data type UUID cannot be decoded as Timestamp"); } @Override - public Timestamp decodeTimestampBinary(ReadableByteBuf buf, int length, Calendar calParam) + public Timestamp decodeTimestampBinary(ReadableByteBuf buf, MutableInt length, Calendar calParam) throws SQLDataException { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException("Data type UUID cannot be decoded as Timestamp"); } } diff --git a/src/main/java/org/mariadb/jdbc/client/column/YearColumn.java b/src/main/java/org/mariadb/jdbc/client/column/YearColumn.java index 1985d857e..5c1445eaa 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/YearColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/YearColumn.java @@ -9,6 +9,7 @@ import org.mariadb.jdbc.Configuration; import org.mariadb.jdbc.client.DataType; import org.mariadb.jdbc.client.ReadableByteBuf; +import org.mariadb.jdbc.client.util.MutableInt; /** Column metadata definition */ public class YearColumn extends UnsignedSmallIntColumn { @@ -52,10 +53,10 @@ public String getColumnTypeName(Configuration conf) { } @Override - public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, int length) + public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, MutableInt length) throws SQLDataException { if (conf.yearIsDateType()) { - short y = (short) buf.atoull(length); + short y = (short) buf.atoull(length.get()); if (columnLength == 2) { // YEAR(2) - deprecated if (y <= 69) { @@ -70,7 +71,7 @@ public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, int } @Override - public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, int length) + public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, MutableInt length) throws SQLDataException { if (conf.yearIsDateType()) { int v = buf.readShort(); @@ -88,9 +89,9 @@ public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, in } @Override - public Date decodeDateText(ReadableByteBuf buf, int length, Calendar cal) + public Date decodeDateText(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { - short y = (short) buf.atoll(length); + short y = (short) buf.atoll(length.get()); if (columnLength == 2) { // YEAR(2) - deprecated if (y <= 69) { @@ -103,7 +104,7 @@ public Date decodeDateText(ReadableByteBuf buf, int length, Calendar cal) } @Override - public Date decodeDateBinary(ReadableByteBuf buf, int length, Calendar cal) + public Date decodeDateBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) throws SQLDataException { int v = buf.readShort(); @@ -119,11 +120,11 @@ public Date decodeDateBinary(ReadableByteBuf buf, int length, Calendar cal) } @Override - public Timestamp decodeTimestampText(ReadableByteBuf buf, int length, Calendar calParam) + public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Calendar calParam) throws SQLDataException { Calendar cal1 = calParam == null ? Calendar.getInstance() : calParam; - int year = Integer.parseInt(buf.readAscii(length)); + int year = Integer.parseInt(buf.readAscii(length.get())); if (columnLength <= 2) year += year >= 70 ? 1900 : 2000; synchronized (cal1) { cal1.clear(); @@ -133,7 +134,7 @@ public Timestamp decodeTimestampText(ReadableByteBuf buf, int length, Calendar c } @Override - public Timestamp decodeTimestampBinary(ReadableByteBuf buf, int length, Calendar calParam) + public Timestamp decodeTimestampBinary(ReadableByteBuf buf, MutableInt length, Calendar calParam) throws SQLDataException { Calendar cal = calParam == null ? Calendar.getInstance() : calParam; diff --git a/src/main/java/org/mariadb/jdbc/client/result/Result.java b/src/main/java/org/mariadb/jdbc/client/result/Result.java index 19c4a8f8c..fe7ab994f 100644 --- a/src/main/java/org/mariadb/jdbc/client/result/Result.java +++ b/src/main/java/org/mariadb/jdbc/client/result/Result.java @@ -70,7 +70,7 @@ public abstract class Result implements ResultSet, Completion { /** reusable row buffer decoder */ protected final StandardReadableByteBuf rowBuf = new StandardReadableByteBuf(null, 0); - private int fieldLength; + private MutableInt fieldLength = new MutableInt(0); /** mutable field index */ protected MutableInt fieldIndex = new MutableInt(); @@ -408,10 +408,10 @@ public boolean wasNull() { @Override public String getString(int columnIndex) throws SQLException { checkIndex(columnIndex); - fieldLength = + fieldLength.set( rowDecoder.setPosition( - columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList); - if (fieldLength == NULL_LENGTH) { + columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList)); + if (fieldLength.get() == NULL_LENGTH) { return null; } return rowDecoder.decodeString(metadataList, fieldIndex, rowBuf, fieldLength); @@ -420,10 +420,10 @@ public String getString(int columnIndex) throws SQLException { @Override public boolean getBoolean(int columnIndex) throws SQLException { checkIndex(columnIndex); - fieldLength = + fieldLength.set( rowDecoder.setPosition( - columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList); - if (fieldLength == NULL_LENGTH) { + columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList)); + if (fieldLength.get() == NULL_LENGTH) { return false; } return rowDecoder.decodeBoolean(metadataList, fieldIndex, rowBuf, fieldLength); @@ -432,10 +432,10 @@ public boolean getBoolean(int columnIndex) throws SQLException { @Override public byte getByte(int columnIndex) throws SQLException { checkIndex(columnIndex); - fieldLength = + fieldLength.set( rowDecoder.setPosition( - columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList); - if (fieldLength == NULL_LENGTH) { + columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList)); + if (fieldLength.get() == NULL_LENGTH) { return 0; } return rowDecoder.decodeByte(metadataList, fieldIndex, rowBuf, fieldLength); @@ -444,10 +444,10 @@ public byte getByte(int columnIndex) throws SQLException { @Override public short getShort(int columnIndex) throws SQLException { checkIndex(columnIndex); - fieldLength = + fieldLength.set( rowDecoder.setPosition( - columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList); - if (fieldLength == NULL_LENGTH) { + columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList)); + if (fieldLength.get() == NULL_LENGTH) { return 0; } return rowDecoder.decodeShort(metadataList, fieldIndex, rowBuf, fieldLength); @@ -456,10 +456,10 @@ public short getShort(int columnIndex) throws SQLException { @Override public int getInt(int columnIndex) throws SQLException { checkIndex(columnIndex); - fieldLength = + fieldLength.set( rowDecoder.setPosition( - columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList); - if (fieldLength == NULL_LENGTH) { + columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList)); + if (fieldLength.get() == NULL_LENGTH) { return 0; } return rowDecoder.decodeInt(metadataList, fieldIndex, rowBuf, fieldLength); @@ -468,10 +468,10 @@ public int getInt(int columnIndex) throws SQLException { @Override public long getLong(int columnIndex) throws SQLException { checkIndex(columnIndex); - fieldLength = + fieldLength.set( rowDecoder.setPosition( - columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList); - if (fieldLength == NULL_LENGTH) { + columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList)); + if (fieldLength.get() == NULL_LENGTH) { return 0L; } return rowDecoder.decodeLong(metadataList, fieldIndex, rowBuf, fieldLength); @@ -487,10 +487,10 @@ public long getLong(int columnIndex) throws SQLException { */ public BigInteger getBigInteger(int columnIndex) throws SQLException { checkIndex(columnIndex); - fieldLength = + fieldLength.set( rowDecoder.setPosition( - columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList); - if (fieldLength == NULL_LENGTH) { + columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList)); + if (fieldLength.get() == NULL_LENGTH) { return null; } return rowDecoder.decode( @@ -512,10 +512,10 @@ public BigInteger getBigInteger(String columnLabel) throws SQLException { @Override public float getFloat(int columnIndex) throws SQLException { checkIndex(columnIndex); - fieldLength = + fieldLength.set( rowDecoder.setPosition( - columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList); - if (fieldLength == NULL_LENGTH) { + columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList)); + if (fieldLength.get() == NULL_LENGTH) { return 0F; } return rowDecoder.decodeFloat(metadataList, fieldIndex, rowBuf, fieldLength); @@ -524,10 +524,10 @@ public float getFloat(int columnIndex) throws SQLException { @Override public double getDouble(int columnIndex) throws SQLException { checkIndex(columnIndex); - fieldLength = + fieldLength.set( rowDecoder.setPosition( - columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList); - if (fieldLength == NULL_LENGTH) { + columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList)); + if (fieldLength.get() == NULL_LENGTH) { return 0D; } return rowDecoder.decodeDouble(metadataList, fieldIndex, rowBuf, fieldLength); @@ -537,10 +537,10 @@ public double getDouble(int columnIndex) throws SQLException { @Deprecated public BigDecimal getBigDecimal(int columnIndex, int scale) throws SQLException { checkIndex(columnIndex); - fieldLength = + fieldLength.set( rowDecoder.setPosition( - columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList); - if (fieldLength == NULL_LENGTH) { + columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList)); + if (fieldLength.get() == NULL_LENGTH) { return null; } BigDecimal d = @@ -553,10 +553,10 @@ public BigDecimal getBigDecimal(int columnIndex, int scale) throws SQLException @Override public byte[] getBytes(int columnIndex) throws SQLException { checkIndex(columnIndex); - fieldLength = + fieldLength.set( rowDecoder.setPosition( - columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList); - if (fieldLength == NULL_LENGTH) { + columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList)); + if (fieldLength.get() == NULL_LENGTH) { return null; } return rowDecoder.decode( @@ -566,10 +566,10 @@ public byte[] getBytes(int columnIndex) throws SQLException { @Override public Date getDate(int columnIndex) throws SQLException { checkIndex(columnIndex); - fieldLength = + fieldLength.set( rowDecoder.setPosition( - columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList); - if (fieldLength == NULL_LENGTH) { + columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList)); + if (fieldLength.get() == NULL_LENGTH) { return null; } return rowDecoder.decodeDate(metadataList, fieldIndex, rowBuf, fieldLength, null); @@ -578,10 +578,10 @@ public Date getDate(int columnIndex) throws SQLException { @Override public Time getTime(int columnIndex) throws SQLException { checkIndex(columnIndex); - fieldLength = + fieldLength.set( rowDecoder.setPosition( - columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList); - if (fieldLength == NULL_LENGTH) { + columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList)); + if (fieldLength.get() == NULL_LENGTH) { return null; } return rowDecoder.decodeTime(metadataList, fieldIndex, rowBuf, fieldLength, null); @@ -590,10 +590,10 @@ public Time getTime(int columnIndex) throws SQLException { @Override public Timestamp getTimestamp(int columnIndex) throws SQLException { checkIndex(columnIndex); - fieldLength = + fieldLength.set( rowDecoder.setPosition( - columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList); - if (fieldLength == NULL_LENGTH) { + columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList)); + if (fieldLength.get() == NULL_LENGTH) { return null; } return rowDecoder.decodeTimestamp(metadataList, fieldIndex, rowBuf, fieldLength, null); @@ -602,10 +602,10 @@ public Timestamp getTimestamp(int columnIndex) throws SQLException { @Override public InputStream getAsciiStream(int columnIndex) throws SQLException { checkIndex(columnIndex); - fieldLength = + fieldLength.set( rowDecoder.setPosition( - columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList); - if (fieldLength == NULL_LENGTH) { + columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList)); + if (fieldLength.get() == NULL_LENGTH) { return null; } return rowDecoder.decode( @@ -616,10 +616,10 @@ public InputStream getAsciiStream(int columnIndex) throws SQLException { @Deprecated public InputStream getUnicodeStream(int columnIndex) throws SQLException { checkIndex(columnIndex); - fieldLength = + fieldLength.set( rowDecoder.setPosition( - columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList); - if (fieldLength == NULL_LENGTH) { + columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList)); + if (fieldLength.get() == NULL_LENGTH) { return null; } return rowDecoder.decode( @@ -629,10 +629,10 @@ public InputStream getUnicodeStream(int columnIndex) throws SQLException { @Override public InputStream getBinaryStream(int columnIndex) throws SQLException { checkIndex(columnIndex); - fieldLength = + fieldLength.set( rowDecoder.setPosition( - columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList); - if (fieldLength == NULL_LENGTH) { + columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList)); + if (fieldLength.get() == NULL_LENGTH) { return null; } return rowDecoder.decode( @@ -749,10 +749,10 @@ public ResultSetMetaData getMetaData() { @Override public Object getObject(int columnIndex) throws SQLException { checkIndex(columnIndex); - fieldLength = + fieldLength.set( rowDecoder.setPosition( - columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList); - if (fieldLength == NULL_LENGTH) { + columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList)); + if (fieldLength.get() == NULL_LENGTH) { return null; } return rowDecoder.defaultDecode( @@ -767,10 +767,10 @@ public Object getObject(String columnLabel) throws SQLException { @Override public Reader getCharacterStream(int columnIndex) throws SQLException { checkIndex(columnIndex); - fieldLength = + fieldLength.set( rowDecoder.setPosition( - columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList); - if (fieldLength == NULL_LENGTH) { + columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList)); + if (fieldLength.get() == NULL_LENGTH) { return null; } return rowDecoder.decode( @@ -785,10 +785,10 @@ public Reader getCharacterStream(String columnLabel) throws SQLException { @Override public BigDecimal getBigDecimal(int columnIndex) throws SQLException { checkIndex(columnIndex); - fieldLength = + fieldLength.set( rowDecoder.setPosition( - columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList); - if (fieldLength == NULL_LENGTH) { + columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList)); + if (fieldLength.get() == NULL_LENGTH) { return null; } return rowDecoder.decode( @@ -1165,10 +1165,10 @@ public Ref getRef(int columnIndex) throws SQLException { @Override public Blob getBlob(int columnIndex) throws SQLException { checkIndex(columnIndex); - fieldLength = + fieldLength.set( rowDecoder.setPosition( - columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList); - if (fieldLength == NULL_LENGTH) { + columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList)); + if (fieldLength.get() == NULL_LENGTH) { return null; } return rowDecoder.decode( @@ -1178,10 +1178,10 @@ public Blob getBlob(int columnIndex) throws SQLException { @Override public Clob getClob(int columnIndex) throws SQLException { checkIndex(columnIndex); - fieldLength = + fieldLength.set( rowDecoder.setPosition( - columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList); - if (fieldLength == NULL_LENGTH) { + columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList)); + if (fieldLength.get() == NULL_LENGTH) { return null; } return rowDecoder.decode( @@ -1225,10 +1225,10 @@ public Array getArray(String columnLabel) throws SQLException { @Override public Date getDate(int columnIndex, Calendar cal) throws SQLException { checkIndex(columnIndex); - fieldLength = + fieldLength.set( rowDecoder.setPosition( - columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList); - if (fieldLength == NULL_LENGTH) { + columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList)); + if (fieldLength.get() == NULL_LENGTH) { return null; } return rowDecoder.decodeDate(metadataList, fieldIndex, rowBuf, fieldLength, cal); @@ -1242,10 +1242,10 @@ public Date getDate(String columnLabel, Calendar cal) throws SQLException { @Override public Time getTime(int columnIndex, Calendar cal) throws SQLException { checkIndex(columnIndex); - fieldLength = + fieldLength.set( rowDecoder.setPosition( - columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList); - if (fieldLength == NULL_LENGTH) { + columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList)); + if (fieldLength.get() == NULL_LENGTH) { return null; } return rowDecoder.decodeTime(metadataList, fieldIndex, rowBuf, fieldLength, cal); @@ -1259,10 +1259,10 @@ public Time getTime(String columnLabel, Calendar cal) throws SQLException { @Override public Timestamp getTimestamp(int columnIndex, Calendar cal) throws SQLException { checkIndex(columnIndex); - fieldLength = + fieldLength.set( rowDecoder.setPosition( - columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList); - if (fieldLength == NULL_LENGTH) { + columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList)); + if (fieldLength.get() == NULL_LENGTH) { return null; } return rowDecoder.decodeTimestamp(metadataList, fieldIndex, rowBuf, fieldLength, cal); @@ -1276,10 +1276,10 @@ public Timestamp getTimestamp(String columnLabel, Calendar cal) throws SQLExcept @Override public URL getURL(int columnIndex) throws SQLException { checkIndex(columnIndex); - fieldLength = + fieldLength.set( rowDecoder.setPosition( - columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList); - if (fieldLength == NULL_LENGTH) { + columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList)); + if (fieldLength.get() == NULL_LENGTH) { return null; } @@ -1392,10 +1392,10 @@ public void updateNClob(String columnLabel, NClob nClob) throws SQLException { @Override public NClob getNClob(int columnIndex) throws SQLException { checkIndex(columnIndex); - fieldLength = + fieldLength.set( rowDecoder.setPosition( - columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList); - if (fieldLength == NULL_LENGTH) { + columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList)); + if (fieldLength.get() == NULL_LENGTH) { return null; } return (NClob) @@ -1440,10 +1440,10 @@ public String getNString(String columnLabel) throws SQLException { @Override public Reader getNCharacterStream(int columnIndex) throws SQLException { checkIndex(columnIndex); - fieldLength = + fieldLength.set( rowDecoder.setPosition( - columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList); - if (fieldLength == NULL_LENGTH) { + columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList)); + if (fieldLength.get() == NULL_LENGTH) { return null; } return rowDecoder.decode( @@ -1605,9 +1605,9 @@ public void updateNClob(String columnLabel, Reader reader) throws SQLException { @SuppressWarnings("unchecked") public T getObject(int columnIndex, Class type) throws SQLException { checkIndex(columnIndex); - fieldLength = + fieldLength.set( rowDecoder.setPosition( - columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList); + columnIndex - 1, fieldIndex, maxIndex, rowBuf, nullBitmap, metadataList)); Calendar calendar = null; if (wasNull()) { if (type.isPrimitive()) { @@ -1629,7 +1629,7 @@ public T getObject(int columnIndex, Class type) throws SQLException { (Codec) codec, calendar, rowBuf, fieldLength, metadataList, fieldIndex); } } - rowBuf.skip(fieldLength); + rowBuf.skip(fieldLength.get()); throw new SQLException( String.format("Type %s not supported type for %s type", type, column.getType().name())); } diff --git a/src/main/java/org/mariadb/jdbc/client/result/rowdecoder/BinaryRowDecoder.java b/src/main/java/org/mariadb/jdbc/client/result/rowdecoder/BinaryRowDecoder.java index dffb6ff81..d247aeeea 100644 --- a/src/main/java/org/mariadb/jdbc/client/result/rowdecoder/BinaryRowDecoder.java +++ b/src/main/java/org/mariadb/jdbc/client/result/rowdecoder/BinaryRowDecoder.java @@ -33,7 +33,7 @@ public T decode( Codec codec, Calendar cal, StandardReadableByteBuf rowBuf, - int fieldLength, + MutableInt fieldLength, ColumnDecoder[] metadataList, final MutableInt fieldIndex) throws SQLException { @@ -46,7 +46,7 @@ public Object defaultDecode( ColumnDecoder[] metadataList, final MutableInt fieldIndex, StandardReadableByteBuf rowBuf, - int fieldLength) + MutableInt fieldLength) throws SQLException { return metadataList[fieldIndex.get()].getDefaultBinary(conf, rowBuf, fieldLength); } @@ -55,7 +55,7 @@ public String decodeString( ColumnDecoder[] metadataList, final MutableInt fieldIndex, StandardReadableByteBuf rowBuf, - int fieldLength) + MutableInt fieldLength) throws SQLException { return metadataList[fieldIndex.get()].decodeStringBinary(rowBuf, fieldLength, null); } @@ -64,7 +64,7 @@ public byte decodeByte( ColumnDecoder[] metadataList, final MutableInt fieldIndex, StandardReadableByteBuf rowBuf, - int fieldLength) + MutableInt fieldLength) throws SQLException { return metadataList[fieldIndex.get()].decodeByteBinary(rowBuf, fieldLength); } @@ -73,7 +73,7 @@ public boolean decodeBoolean( ColumnDecoder[] metadataList, final MutableInt fieldIndex, StandardReadableByteBuf rowBuf, - int fieldLength) + MutableInt fieldLength) throws SQLException { return metadataList[fieldIndex.get()].decodeBooleanBinary(rowBuf, fieldLength); } @@ -82,7 +82,7 @@ public Date decodeDate( ColumnDecoder[] metadataList, MutableInt fieldIndex, StandardReadableByteBuf rowBuf, - int fieldLength, + MutableInt fieldLength, Calendar cal) throws SQLException { return metadataList[fieldIndex.get()].decodeDateBinary(rowBuf, fieldLength, cal); @@ -92,7 +92,7 @@ public Time decodeTime( ColumnDecoder[] metadataList, MutableInt fieldIndex, StandardReadableByteBuf rowBuf, - int fieldLength, + MutableInt fieldLength, Calendar cal) throws SQLException { return metadataList[fieldIndex.get()].decodeTimeBinary(rowBuf, fieldLength, cal); @@ -102,7 +102,7 @@ public Timestamp decodeTimestamp( ColumnDecoder[] metadataList, MutableInt fieldIndex, StandardReadableByteBuf rowBuf, - int fieldLength, + MutableInt fieldLength, Calendar cal) throws SQLException { return metadataList[fieldIndex.get()].decodeTimestampBinary(rowBuf, fieldLength, cal); @@ -112,7 +112,7 @@ public short decodeShort( ColumnDecoder[] metadataList, final MutableInt fieldIndex, StandardReadableByteBuf rowBuf, - int fieldLength) + MutableInt fieldLength) throws SQLException { return metadataList[fieldIndex.get()].decodeShortBinary(rowBuf, fieldLength); } @@ -121,7 +121,7 @@ public int decodeInt( ColumnDecoder[] metadataList, final MutableInt fieldIndex, StandardReadableByteBuf rowBuf, - int fieldLength) + MutableInt fieldLength) throws SQLException { return metadataList[fieldIndex.get()].decodeIntBinary(rowBuf, fieldLength); } @@ -130,7 +130,7 @@ public long decodeLong( ColumnDecoder[] metadataList, final MutableInt fieldIndex, StandardReadableByteBuf rowBuf, - int fieldLength) + MutableInt fieldLength) throws SQLException { return metadataList[fieldIndex.get()].decodeLongBinary(rowBuf, fieldLength); } @@ -139,7 +139,7 @@ public float decodeFloat( ColumnDecoder[] metadataList, final MutableInt fieldIndex, StandardReadableByteBuf rowBuf, - int fieldLength) + MutableInt fieldLength) throws SQLException { return metadataList[fieldIndex.get()].decodeFloatBinary(rowBuf, fieldLength); } @@ -148,13 +148,14 @@ public double decodeDouble( ColumnDecoder[] metadataList, final MutableInt fieldIndex, StandardReadableByteBuf rowBuf, - int fieldLength) + MutableInt fieldLength) throws SQLException { return metadataList[fieldIndex.get()].decodeDoubleBinary(rowBuf, fieldLength); } - public boolean wasNull(byte[] nullBitmap, final MutableInt fieldIndex, int fieldLength) { - return (nullBitmap[(fieldIndex.get() + 2) / 8] & (1 << ((fieldIndex.get() + 2) % 8))) > 0; + public boolean wasNull(byte[] nullBitmap, final MutableInt fieldIndex, MutableInt fieldLength) { + return (nullBitmap[(fieldIndex.get() + 2) / 8] & (1 << ((fieldIndex.get() + 2) % 8))) > 0 + || fieldLength.get() == NULL_LENGTH; } /** @@ -216,7 +217,7 @@ public int setPosition( fieldIndex.incrementAndGet(); } - if (wasNull(nullBitmap, fieldIndex, 0)) { + if ((nullBitmap[(fieldIndex.get() + 2) / 8] & (1 << ((fieldIndex.get() + 2) % 8))) > 0) { return NULL_LENGTH; } diff --git a/src/main/java/org/mariadb/jdbc/client/result/rowdecoder/RowDecoder.java b/src/main/java/org/mariadb/jdbc/client/result/rowdecoder/RowDecoder.java index ec173d896..2334ae47e 100644 --- a/src/main/java/org/mariadb/jdbc/client/result/rowdecoder/RowDecoder.java +++ b/src/main/java/org/mariadb/jdbc/client/result/rowdecoder/RowDecoder.java @@ -21,7 +21,7 @@ public interface RowDecoder { * @param fieldLength field length * @return true if last value was null */ - boolean wasNull(byte[] nullBitmap, MutableInt fieldIndex, int fieldLength); + boolean wasNull(byte[] nullBitmap, MutableInt fieldIndex, MutableInt fieldLength); /** * Position the read index on buffer to data at indicated index. @@ -59,7 +59,7 @@ T decode( Codec codec, Calendar calendar, StandardReadableByteBuf rowBuf, - int fieldLength, + MutableInt fieldLength, ColumnDecoder[] metadataList, MutableInt fieldIndex) throws SQLException; @@ -80,7 +80,7 @@ Object defaultDecode( ColumnDecoder[] metadataList, MutableInt fieldIndex, StandardReadableByteBuf rowBuf, - int fieldLength) + MutableInt fieldLength) throws SQLException; /** @@ -97,7 +97,7 @@ byte decodeByte( ColumnDecoder[] metadataList, MutableInt fieldIndex, StandardReadableByteBuf rowBuf, - int fieldLength) + MutableInt fieldLength) throws SQLException; /** @@ -114,7 +114,7 @@ boolean decodeBoolean( ColumnDecoder[] metadataList, MutableInt fieldIndex, StandardReadableByteBuf rowBuf, - int fieldLength) + MutableInt fieldLength) throws SQLException; /** * Decode data according to Date. @@ -131,7 +131,7 @@ Date decodeDate( ColumnDecoder[] metadataList, MutableInt fieldIndex, StandardReadableByteBuf rowBuf, - int fieldLength, + MutableInt fieldLength, Calendar cal) throws SQLException; /** @@ -149,7 +149,7 @@ Time decodeTime( ColumnDecoder[] metadataList, MutableInt fieldIndex, StandardReadableByteBuf rowBuf, - int fieldLength, + MutableInt fieldLength, Calendar cal) throws SQLException; /** @@ -167,7 +167,7 @@ Timestamp decodeTimestamp( ColumnDecoder[] metadataList, MutableInt fieldIndex, StandardReadableByteBuf rowBuf, - int fieldLength, + MutableInt fieldLength, Calendar cal) throws SQLException; /** @@ -184,7 +184,7 @@ short decodeShort( ColumnDecoder[] metadataList, MutableInt fieldIndex, StandardReadableByteBuf rowBuf, - int fieldLength) + MutableInt fieldLength) throws SQLException; /** * Decode data according to int. @@ -200,7 +200,7 @@ int decodeInt( ColumnDecoder[] metadataList, MutableInt fieldIndex, StandardReadableByteBuf rowBuf, - int fieldLength) + MutableInt fieldLength) throws SQLException; /** * Decode data according to String. @@ -216,7 +216,7 @@ String decodeString( ColumnDecoder[] metadataList, MutableInt fieldIndex, StandardReadableByteBuf rowBuf, - int fieldLength) + MutableInt fieldLength) throws SQLException; /** * Decode data according to long. @@ -232,7 +232,7 @@ long decodeLong( ColumnDecoder[] metadataList, MutableInt fieldIndex, StandardReadableByteBuf rowBuf, - int fieldLength) + MutableInt fieldLength) throws SQLException; /** * Decode data according to float. @@ -248,7 +248,7 @@ float decodeFloat( ColumnDecoder[] metadataList, MutableInt fieldIndex, StandardReadableByteBuf rowBuf, - int fieldLength) + MutableInt fieldLength) throws SQLException; /** * Decode data according to double. @@ -264,6 +264,6 @@ float decodeFloat( ColumnDecoder[] metadataList, MutableInt fieldIndex, StandardReadableByteBuf rowBuf, - int fieldLength) + MutableInt fieldLength) throws SQLException; } diff --git a/src/main/java/org/mariadb/jdbc/client/result/rowdecoder/TextRowDecoder.java b/src/main/java/org/mariadb/jdbc/client/result/rowdecoder/TextRowDecoder.java index 5f08db36c..2a91c772d 100644 --- a/src/main/java/org/mariadb/jdbc/client/result/rowdecoder/TextRowDecoder.java +++ b/src/main/java/org/mariadb/jdbc/client/result/rowdecoder/TextRowDecoder.java @@ -21,7 +21,7 @@ public T decode( Codec codec, Calendar cal, StandardReadableByteBuf rowBuf, - int fieldLength, + MutableInt fieldLength, ColumnDecoder[] metadataList, MutableInt fieldIndex) throws SQLException { @@ -34,7 +34,7 @@ public Object defaultDecode( ColumnDecoder[] metadataList, MutableInt fieldIndex, StandardReadableByteBuf rowBuf, - int fieldLength) + MutableInt fieldLength) throws SQLException { return metadataList[fieldIndex.get()].getDefaultText(conf, rowBuf, fieldLength); } @@ -44,7 +44,7 @@ public String decodeString( ColumnDecoder[] metadataList, MutableInt fieldIndex, StandardReadableByteBuf rowBuf, - int fieldLength) + MutableInt fieldLength) throws SQLException { return metadataList[fieldIndex.get()].decodeStringText(rowBuf, fieldLength, null); } @@ -53,7 +53,7 @@ public byte decodeByte( ColumnDecoder[] metadataList, MutableInt fieldIndex, StandardReadableByteBuf rowBuf, - int fieldLength) + MutableInt fieldLength) throws SQLException { return metadataList[fieldIndex.get()].decodeByteText(rowBuf, fieldLength); } @@ -62,7 +62,7 @@ public boolean decodeBoolean( ColumnDecoder[] metadataList, MutableInt fieldIndex, StandardReadableByteBuf rowBuf, - int fieldLength) + MutableInt fieldLength) throws SQLException { return metadataList[fieldIndex.get()].decodeBooleanText(rowBuf, fieldLength); } @@ -71,7 +71,7 @@ public Date decodeDate( ColumnDecoder[] metadataList, MutableInt fieldIndex, StandardReadableByteBuf rowBuf, - int fieldLength, + MutableInt fieldLength, Calendar cal) throws SQLException { return metadataList[fieldIndex.get()].decodeDateText(rowBuf, fieldLength, cal); @@ -81,7 +81,7 @@ public Time decodeTime( ColumnDecoder[] metadataList, MutableInt fieldIndex, StandardReadableByteBuf rowBuf, - int fieldLength, + MutableInt fieldLength, Calendar cal) throws SQLException { return metadataList[fieldIndex.get()].decodeTimeText(rowBuf, fieldLength, cal); @@ -91,7 +91,7 @@ public Timestamp decodeTimestamp( ColumnDecoder[] metadataList, MutableInt fieldIndex, StandardReadableByteBuf rowBuf, - int fieldLength, + MutableInt fieldLength, Calendar cal) throws SQLException { return metadataList[fieldIndex.get()].decodeTimestampText(rowBuf, fieldLength, cal); @@ -101,7 +101,7 @@ public short decodeShort( ColumnDecoder[] metadataList, MutableInt fieldIndex, StandardReadableByteBuf rowBuf, - int fieldLength) + MutableInt fieldLength) throws SQLException { return metadataList[fieldIndex.get()].decodeShortText(rowBuf, fieldLength); } @@ -110,7 +110,7 @@ public int decodeInt( ColumnDecoder[] metadataList, MutableInt fieldIndex, StandardReadableByteBuf rowBuf, - int fieldLength) + MutableInt fieldLength) throws SQLException { return metadataList[fieldIndex.get()].decodeIntText(rowBuf, fieldLength); } @@ -119,7 +119,7 @@ public long decodeLong( ColumnDecoder[] metadataList, MutableInt fieldIndex, StandardReadableByteBuf rowBuf, - int fieldLength) + MutableInt fieldLength) throws SQLException { return metadataList[fieldIndex.get()].decodeLongText(rowBuf, fieldLength); } @@ -128,7 +128,7 @@ public float decodeFloat( ColumnDecoder[] metadataList, MutableInt fieldIndex, StandardReadableByteBuf rowBuf, - int fieldLength) + MutableInt fieldLength) throws SQLException { return metadataList[fieldIndex.get()].decodeFloatText(rowBuf, fieldLength); } @@ -137,13 +137,13 @@ public double decodeDouble( ColumnDecoder[] metadataList, MutableInt fieldIndex, StandardReadableByteBuf rowBuf, - int fieldLength) + MutableInt fieldLength) throws SQLException { return metadataList[fieldIndex.get()].decodeDoubleText(rowBuf, fieldLength); } - public boolean wasNull(byte[] nullBitmap, MutableInt fieldIndex, int fieldLength) { - return fieldLength == NULL_LENGTH; + public boolean wasNull(byte[] nullBitmap, MutableInt fieldIndex, MutableInt fieldLength) { + return fieldLength.get() == NULL_LENGTH; } /** diff --git a/src/main/java/org/mariadb/jdbc/client/util/MutableInt.java b/src/main/java/org/mariadb/jdbc/client/util/MutableInt.java index a9657f64b..05ec48ec1 100644 --- a/src/main/java/org/mariadb/jdbc/client/util/MutableInt.java +++ b/src/main/java/org/mariadb/jdbc/client/util/MutableInt.java @@ -6,7 +6,15 @@ /** Mutable int */ public class MutableInt { - private int value = -1; + public MutableInt() { + this.value = -1; + } + + public MutableInt(int value) { + this.value = value; + } + + private int value; /** * Set new sequence value diff --git a/src/main/java/org/mariadb/jdbc/plugin/Codec.java b/src/main/java/org/mariadb/jdbc/plugin/Codec.java index 8e991d80b..2b5c0e214 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/Codec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/Codec.java @@ -12,6 +12,7 @@ import org.mariadb.jdbc.client.Context; import org.mariadb.jdbc.client.ReadableByteBuf; import org.mariadb.jdbc.client.socket.Writer; +import org.mariadb.jdbc.client.util.MutableInt; /** * Codec interface, to describe how a certain type of data must be encoded / decoded @@ -48,7 +49,7 @@ public interface Codec { * Decode from a mysql packet text encoded a value to codec java type * * @param buffer mysql packet buffer - * @param length encoded value length + * @param fieldLength encoded value length * @param column server column metadata * @param cal calendar * @return decoded value @@ -56,7 +57,7 @@ public interface Codec { */ T decodeText( final ReadableByteBuf buffer, - final int length, + final MutableInt fieldLength, final ColumnDecoder column, final Calendar cal) throws SQLDataException; @@ -65,7 +66,7 @@ T decodeText( * Decode from a mysql packet binary encoded a value to codec java type * * @param buffer mysql packet buffer - * @param length encoded value length + * @param fieldLength encoded value length * @param column server column metadata * @param cal calendar * @return decoded value @@ -73,7 +74,7 @@ T decodeText( */ T decodeBinary( final ReadableByteBuf buffer, - final int length, + final MutableInt fieldLength, final ColumnDecoder column, final Calendar cal) throws SQLDataException; diff --git a/src/main/java/org/mariadb/jdbc/plugin/codec/BigDecimalCodec.java b/src/main/java/org/mariadb/jdbc/plugin/codec/BigDecimalCodec.java index 41664f2ac..5a8fdca3e 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/BigDecimalCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/BigDecimalCodec.java @@ -13,6 +13,7 @@ import java.util.EnumSet; import org.mariadb.jdbc.client.*; import org.mariadb.jdbc.client.socket.Writer; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.plugin.Codec; /** Big decimal codec */ @@ -56,7 +57,8 @@ public boolean canEncode(Object value) { @Override @SuppressWarnings("fallthrough") - public BigDecimal decodeText(ReadableByteBuf buf, int length, ColumnDecoder column, Calendar cal) + public BigDecimal decodeText( + ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) throws SQLDataException { switch (column.getType()) { case TINYINT: @@ -69,14 +71,14 @@ public BigDecimal decodeText(ReadableByteBuf buf, int length, ColumnDecoder colu case DECIMAL: case OLDDECIMAL: case YEAR: - return new BigDecimal(buf.readAscii(length)); + return new BigDecimal(buf.readAscii(length.get())); case BLOB: case TINYBLOB: case MEDIUMBLOB: case LONGBLOB: if (column.isBinary()) { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as BigDecimal", column.getType())); } @@ -86,7 +88,7 @@ public BigDecimal decodeText(ReadableByteBuf buf, int length, ColumnDecoder colu case VARCHAR: case VARSTRING: case STRING: - String str = buf.readString(length); + String str = buf.readString(length.get()); try { return new BigDecimal(str); } catch (NumberFormatException nfe) { @@ -96,14 +98,14 @@ public BigDecimal decodeText(ReadableByteBuf buf, int length, ColumnDecoder colu case BIT: long result = 0; - for (int i = 0; i < length; i++) { + for (int i = 0; i < length.get(); i++) { byte b = buf.readByte(); result = (result << 8) + (b & 0xff); } return BigDecimal.valueOf(result); default: - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as BigDecimal", column.getType())); } @@ -112,7 +114,8 @@ public BigDecimal decodeText(ReadableByteBuf buf, int length, ColumnDecoder colu @Override @SuppressWarnings("fallthrough") public BigDecimal decodeBinary( - ReadableByteBuf buf, int length, ColumnDecoder column, Calendar cal) throws SQLDataException { + ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + throws SQLDataException { switch (column.getType()) { case TINYINT: @@ -155,7 +158,7 @@ public BigDecimal decodeBinary( case BIT: long result = 0; - for (int i = 0; i < length; i++) { + for (int i = 0; i < length.get(); i++) { byte b = buf.readByte(); result = (result << 8) + (b & 0xff); } @@ -166,7 +169,7 @@ public BigDecimal decodeBinary( case MEDIUMBLOB: case LONGBLOB: if (column.isBinary()) { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as BigDecimal", column.getType())); } @@ -178,7 +181,7 @@ public BigDecimal decodeBinary( case STRING: case DECIMAL: case OLDDECIMAL: - String str = buf.readString(length); + String str = buf.readString(length.get()); try { return new BigDecimal(str); } catch (NumberFormatException nfe) { @@ -187,7 +190,7 @@ public BigDecimal decodeBinary( } default: - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as BigDecimal", column.getType())); } diff --git a/src/main/java/org/mariadb/jdbc/plugin/codec/BigIntegerCodec.java b/src/main/java/org/mariadb/jdbc/plugin/codec/BigIntegerCodec.java index 1f59e26e9..cdaa01f6d 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/BigIntegerCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/BigIntegerCodec.java @@ -12,6 +12,7 @@ import java.util.EnumSet; import org.mariadb.jdbc.client.*; import org.mariadb.jdbc.client.socket.Writer; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.plugin.Codec; /** BigInteger codec */ @@ -55,7 +56,8 @@ public boolean canEncode(Object value) { @Override @SuppressWarnings("fallthrough") - public BigInteger decodeText(ReadableByteBuf buf, int length, ColumnDecoder column, Calendar cal) + public BigInteger decodeText( + ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) throws SQLDataException { switch (column.getType()) { @@ -63,14 +65,14 @@ public BigInteger decodeText(ReadableByteBuf buf, int length, ColumnDecoder colu case DOUBLE: case DECIMAL: case OLDDECIMAL: - return new BigDecimal(buf.readAscii(length)).toBigInteger(); + return new BigDecimal(buf.readAscii(length.get())).toBigInteger(); case BLOB: case TINYBLOB: case MEDIUMBLOB: case LONGBLOB: if (column.isBinary()) { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as BigInteger", column.getType())); } @@ -80,7 +82,7 @@ public BigInteger decodeText(ReadableByteBuf buf, int length, ColumnDecoder colu case VARCHAR: case VARSTRING: case STRING: - String str2 = buf.readString(length); + String str2 = buf.readString(length.get()); try { return new BigDecimal(str2).toBigInteger(); } catch (NumberFormatException nfe) { @@ -90,7 +92,7 @@ public BigInteger decodeText(ReadableByteBuf buf, int length, ColumnDecoder colu case BIT: long result = 0; - for (int i = 0; i < length; i++) { + for (int i = 0; i < length.get(); i++) { byte b = buf.readByte(); result = (result << 8) + (b & 0xff); } @@ -102,10 +104,10 @@ public BigInteger decodeText(ReadableByteBuf buf, int length, ColumnDecoder colu case INTEGER: case BIGINT: case YEAR: - return new BigInteger(buf.readAscii(length)); + return new BigInteger(buf.readAscii(length.get())); default: - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as BigInteger", column.getType())); } @@ -114,12 +116,13 @@ public BigInteger decodeText(ReadableByteBuf buf, int length, ColumnDecoder colu @Override @SuppressWarnings("fallthrough") public BigInteger decodeBinary( - ReadableByteBuf buf, int length, ColumnDecoder column, Calendar cal) throws SQLDataException { + ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + throws SQLDataException { switch (column.getType()) { case BIT: long result = 0; - for (int i = 0; i < length; i++) { + for (int i = 0; i < length.get(); i++) { byte b = buf.readByte(); result = (result << 8) + (b & 0xff); } @@ -158,7 +161,7 @@ public BigInteger decodeBinary( return BigDecimal.valueOf(buf.readDouble()).toBigInteger(); case DECIMAL: - return new BigDecimal(buf.readAscii(length)).toBigInteger(); + return new BigDecimal(buf.readAscii(length.get())).toBigInteger(); case BIGINT: if (column.isSigned()) return BigInteger.valueOf(buf.readLong()); @@ -175,7 +178,7 @@ public BigInteger decodeBinary( case MEDIUMBLOB: case LONGBLOB: if (column.isBinary()) { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as BigInteger", column.getType())); } @@ -185,7 +188,7 @@ public BigInteger decodeBinary( case VARCHAR: case VARSTRING: case STRING: - String str = buf.readString(length); + String str = buf.readString(length.get()); try { return new BigInteger(str); } catch (NumberFormatException nfe) { @@ -194,7 +197,7 @@ public BigInteger decodeBinary( } default: - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as BigInteger", column.getType())); } diff --git a/src/main/java/org/mariadb/jdbc/plugin/codec/BitSetCodec.java b/src/main/java/org/mariadb/jdbc/plugin/codec/BitSetCodec.java index 6999f5a54..780204460 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/BitSetCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/BitSetCodec.java @@ -9,6 +9,7 @@ import java.util.Calendar; import org.mariadb.jdbc.client.*; import org.mariadb.jdbc.client.socket.Writer; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.plugin.Codec; /** BitSet Codec */ @@ -24,8 +25,8 @@ public class BitSetCodec implements Codec { * @param length encoded length * @return BitSet value */ - public static BitSet parseBit(ReadableByteBuf buf, int length) { - byte[] arr = new byte[length]; + public static BitSet parseBit(ReadableByteBuf buf, MutableInt length) { + byte[] arr = new byte[length.get()]; buf.readBytes(arr); revertOrder(arr); return BitSet.valueOf(arr); @@ -58,12 +59,14 @@ public boolean canDecode(ColumnDecoder column, Class type) { } @Override - public BitSet decodeText(ReadableByteBuf buf, int length, ColumnDecoder column, Calendar cal) { + public BitSet decodeText( + ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) { return parseBit(buf, length); } @Override - public BitSet decodeBinary(ReadableByteBuf buf, int length, ColumnDecoder column, Calendar cal) { + public BitSet decodeBinary( + ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) { return parseBit(buf, length); } diff --git a/src/main/java/org/mariadb/jdbc/plugin/codec/BlobCodec.java b/src/main/java/org/mariadb/jdbc/plugin/codec/BlobCodec.java index 9b3cc2c76..869e7def1 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/BlobCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/BlobCodec.java @@ -16,6 +16,7 @@ import org.mariadb.jdbc.MariaDbBlob; import org.mariadb.jdbc.client.*; import org.mariadb.jdbc.client.socket.Writer; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.plugin.Codec; import org.mariadb.jdbc.util.constants.ServerStatus; @@ -50,7 +51,7 @@ public boolean canEncode(Object value) { @Override @SuppressWarnings("fallthrough") - public Blob decodeText(ReadableByteBuf buf, int length, ColumnDecoder column, Calendar cal) + public Blob decodeText(ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) throws SQLDataException { switch (column.getType()) { case STRING: @@ -62,10 +63,10 @@ public Blob decodeText(ReadableByteBuf buf, int length, ColumnDecoder column, Ca case LONGBLOB: case BLOB: case GEOMETRY: - return buf.readBlob(length); + return buf.readBlob(length.get()); default: - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Blob", column.getType())); } @@ -73,7 +74,8 @@ public Blob decodeText(ReadableByteBuf buf, int length, ColumnDecoder column, Ca @Override @SuppressWarnings("fallthrough") - public Blob decodeBinary(ReadableByteBuf buf, int length, ColumnDecoder column, Calendar cal) + public Blob decodeBinary( + ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) throws SQLDataException { switch (column.getType()) { case STRING: @@ -85,11 +87,11 @@ public Blob decodeBinary(ReadableByteBuf buf, int length, ColumnDecoder column, case LONGBLOB: case BLOB: case GEOMETRY: - buf.skip(length); - return new MariaDbBlob(buf.buf(), buf.pos() - length, length); + buf.skip(length.get()); + return new MariaDbBlob(buf.buf(), buf.pos() - length.get(), length.get()); default: - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Blob", column.getType())); } diff --git a/src/main/java/org/mariadb/jdbc/plugin/codec/BooleanCodec.java b/src/main/java/org/mariadb/jdbc/plugin/codec/BooleanCodec.java index 5eaede1e2..2681a20eb 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/BooleanCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/BooleanCodec.java @@ -10,6 +10,7 @@ import java.util.EnumSet; import org.mariadb.jdbc.client.*; import org.mariadb.jdbc.client.socket.Writer; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.plugin.Codec; /** Boolean codec */ @@ -54,7 +55,7 @@ public boolean canEncode(Object value) { public Boolean decodeText( final ReadableByteBuf buffer, - final int length, + final MutableInt length, final ColumnDecoder column, final Calendar cal) throws SQLDataException { @@ -63,7 +64,7 @@ public Boolean decodeText( public Boolean decodeBinary( final ReadableByteBuf buffer, - final int length, + final MutableInt length, final ColumnDecoder column, final Calendar cal) throws SQLDataException { diff --git a/src/main/java/org/mariadb/jdbc/plugin/codec/ByteArrayCodec.java b/src/main/java/org/mariadb/jdbc/plugin/codec/ByteArrayCodec.java index afde4b74b..a5b9d7699 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/ByteArrayCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/ByteArrayCodec.java @@ -10,6 +10,7 @@ import java.util.EnumSet; import org.mariadb.jdbc.client.*; import org.mariadb.jdbc.client.socket.Writer; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.plugin.Codec; import org.mariadb.jdbc.util.constants.ServerStatus; @@ -49,12 +50,13 @@ public boolean canEncode(Object value) { } @Override - public byte[] decodeText(ReadableByteBuf buf, int length, ColumnDecoder column, Calendar cal) + public byte[] decodeText( + ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) throws SQLDataException { return getBytes(buf, length, column); } - private byte[] getBytes(ReadableByteBuf buf, int length, ColumnDecoder column) + private byte[] getBytes(ReadableByteBuf buf, MutableInt length, ColumnDecoder column) throws SQLDataException { switch (column.getType()) { case BIT: @@ -66,19 +68,20 @@ private byte[] getBytes(ReadableByteBuf buf, int length, ColumnDecoder column) case VARSTRING: case VARCHAR: case GEOMETRY: - byte[] arr = new byte[length]; + byte[] arr = new byte[length.get()]; buf.readBytes(arr); return arr; default: - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as byte[]", column.getType())); } } @Override - public byte[] decodeBinary(ReadableByteBuf buf, int length, ColumnDecoder column, Calendar cal) + public byte[] decodeBinary( + ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) throws SQLDataException { return getBytes(buf, length, column); } diff --git a/src/main/java/org/mariadb/jdbc/plugin/codec/ByteCodec.java b/src/main/java/org/mariadb/jdbc/plugin/codec/ByteCodec.java index ff8eefa6a..0555bc0be 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/ByteCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/ByteCodec.java @@ -10,6 +10,7 @@ import java.util.EnumSet; import org.mariadb.jdbc.client.*; import org.mariadb.jdbc.client.socket.Writer; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.plugin.Codec; /** Byte codec */ @@ -47,16 +48,16 @@ public class ByteCodec implements Codec { * @param length encoded length * @return long value */ - public static long parseBit(ReadableByteBuf buf, int length) { - if (length == 1) { + public static long parseBit(ReadableByteBuf buf, MutableInt length) { + if (length.get() == 1) { return buf.readUnsignedByte(); } long val = 0; int idx = 0; do { - val += ((long) buf.readUnsignedByte()) << (8 * length); + val += ((long) buf.readUnsignedByte()) << (8 * length.get()); idx++; - } while (idx < length); + } while (idx < length.get()); return val; } @@ -76,7 +77,7 @@ public boolean canEncode(Object value) { @Override public Byte decodeText( final ReadableByteBuf buffer, - final int length, + final MutableInt length, final ColumnDecoder column, final Calendar cal) throws SQLDataException { @@ -86,7 +87,7 @@ public Byte decodeText( @Override public Byte decodeBinary( final ReadableByteBuf buffer, - final int length, + final MutableInt length, final ColumnDecoder column, final Calendar cal) throws SQLDataException { diff --git a/src/main/java/org/mariadb/jdbc/plugin/codec/ClobCodec.java b/src/main/java/org/mariadb/jdbc/plugin/codec/ClobCodec.java index a7e33a81f..2f2848e4e 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/ClobCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/ClobCodec.java @@ -14,6 +14,7 @@ import org.mariadb.jdbc.MariaDbClob; import org.mariadb.jdbc.client.*; import org.mariadb.jdbc.client.socket.Writer; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.plugin.Codec; import org.mariadb.jdbc.util.constants.ServerStatus; @@ -47,13 +48,13 @@ public boolean canEncode(Object value) { } @Override - public Clob decodeText(ReadableByteBuf buf, int length, ColumnDecoder column, Calendar cal) + public Clob decodeText(ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) throws SQLDataException { return getClob(buf, length, column); } @SuppressWarnings("fallthrough") - private Clob getClob(ReadableByteBuf buf, int length, ColumnDecoder column) + private Clob getClob(ReadableByteBuf buf, MutableInt length, ColumnDecoder column) throws SQLDataException { switch (column.getType()) { case BLOB: @@ -61,7 +62,7 @@ private Clob getClob(ReadableByteBuf buf, int length, ColumnDecoder column) case MEDIUMBLOB: case LONGBLOB: if (column.isBinary()) { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Clob", column.getType())); } @@ -71,19 +72,20 @@ private Clob getClob(ReadableByteBuf buf, int length, ColumnDecoder column) case STRING: case VARCHAR: case VARSTRING: - Clob clob = new MariaDbClob(buf.buf(), buf.pos(), length); - buf.skip(length); + Clob clob = new MariaDbClob(buf.buf(), buf.pos(), length.get()); + buf.skip(length.get()); return clob; default: - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Clob", column.getType())); } } @Override - public Clob decodeBinary(ReadableByteBuf buf, int length, ColumnDecoder column, Calendar cal) + public Clob decodeBinary( + ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) throws SQLDataException { return getClob(buf, length, column); } diff --git a/src/main/java/org/mariadb/jdbc/plugin/codec/DateCodec.java b/src/main/java/org/mariadb/jdbc/plugin/codec/DateCodec.java index 4e83e631a..b6c60d93c 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/DateCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/DateCodec.java @@ -12,6 +12,7 @@ import java.util.EnumSet; import org.mariadb.jdbc.client.*; import org.mariadb.jdbc.client.socket.Writer; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.plugin.Codec; /** Date codec */ @@ -49,14 +50,15 @@ public boolean canEncode(Object value) { @Override @SuppressWarnings("fallthrough") - public Date decodeText(ReadableByteBuf buf, int length, ColumnDecoder column, Calendar cal) + public Date decodeText(ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) throws SQLDataException { return column.decodeDateText(buf, length, cal); } @Override @SuppressWarnings("fallthrough") - public Date decodeBinary(ReadableByteBuf buf, int length, ColumnDecoder column, Calendar cal) + public Date decodeBinary( + ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) throws SQLDataException { return column.decodeDateBinary(buf, length, cal); } diff --git a/src/main/java/org/mariadb/jdbc/plugin/codec/DoubleCodec.java b/src/main/java/org/mariadb/jdbc/plugin/codec/DoubleCodec.java index 47da185aa..5b3299fd7 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/DoubleCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/DoubleCodec.java @@ -10,6 +10,7 @@ import java.util.EnumSet; import org.mariadb.jdbc.client.*; import org.mariadb.jdbc.client.socket.Writer; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.plugin.Codec; /** Double codec */ @@ -53,14 +54,16 @@ public boolean canEncode(Object value) { @Override @SuppressWarnings("fallthrough") - public Double decodeText(ReadableByteBuf buf, int length, ColumnDecoder column, Calendar cal) + public Double decodeText( + ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) throws SQLDataException { return column.decodeDoubleText(buf, length); } @Override @SuppressWarnings("fallthrough") - public Double decodeBinary(ReadableByteBuf buf, int length, ColumnDecoder column, Calendar cal) + public Double decodeBinary( + ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) throws SQLDataException { return column.decodeDoubleBinary(buf, length); } diff --git a/src/main/java/org/mariadb/jdbc/plugin/codec/DurationCodec.java b/src/main/java/org/mariadb/jdbc/plugin/codec/DurationCodec.java index d0f235f87..a374d6a3a 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/DurationCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/DurationCodec.java @@ -4,6 +4,8 @@ package org.mariadb.jdbc.plugin.codec; +import static org.mariadb.jdbc.client.result.Result.NULL_LENGTH; + import java.io.IOException; import java.sql.SQLDataException; import java.time.Duration; @@ -11,6 +13,7 @@ import java.util.EnumSet; import org.mariadb.jdbc.client.*; import org.mariadb.jdbc.client.socket.Writer; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.plugin.Codec; /** Duration codec */ @@ -46,15 +49,19 @@ public boolean canEncode(Object value) { @Override @SuppressWarnings("fallthrough") - public Duration decodeText(ReadableByteBuf buf, int length, ColumnDecoder column, Calendar cal) + public Duration decodeText( + ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) throws SQLDataException { int[] parts; switch (column.getType()) { case TIMESTAMP: case DATETIME: - parts = LocalDateTimeCodec.parseTimestamp(buf.readAscii(length)); - if (parts == null) return null; + parts = LocalDateTimeCodec.parseTimestamp(buf.readAscii(length.get())); + if (parts == null) { + length.set(NULL_LENGTH); + return null; + } return Duration.ZERO .plusDays(parts[2] - 1) .plusHours(parts[3]) @@ -67,7 +74,7 @@ public Duration decodeText(ReadableByteBuf buf, int length, ColumnDecoder column case MEDIUMBLOB: case LONGBLOB: if (column.isBinary()) { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Duration", column.getType())); } @@ -89,7 +96,7 @@ public Duration decodeText(ReadableByteBuf buf, int length, ColumnDecoder column return d; default: - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Duration", column.getType())); } @@ -97,7 +104,8 @@ public Duration decodeText(ReadableByteBuf buf, int length, ColumnDecoder column @Override @SuppressWarnings("fallthrough") - public Duration decodeBinary(ReadableByteBuf buf, int length, ColumnDecoder column, Calendar cal) + public Duration decodeBinary( + ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) throws SQLDataException { long days = 0; @@ -108,15 +116,15 @@ public Duration decodeBinary(ReadableByteBuf buf, int length, ColumnDecoder colu switch (column.getType()) { case TIME: boolean negate = false; - if (length > 0) { + if (length.get() > 0) { negate = buf.readUnsignedByte() == 0x01; - if (length > 4) { + if (length.get() > 4) { days = buf.readUnsignedInt(); - if (length > 7) { + if (length.get() > 7) { hours = buf.readByte(); minutes = buf.readByte(); seconds = buf.readByte(); - if (length > 8) { + if (length.get() > 8) { microseconds = buf.readInt(); } } @@ -135,23 +143,28 @@ public Duration decodeBinary(ReadableByteBuf buf, int length, ColumnDecoder colu case TIMESTAMP: case DATETIME: - if (length == 0) return null; + if (length.get() == 0) { + length.set(NULL_LENGTH); + return null; + } int year = buf.readUnsignedShort(); int month = buf.readByte(); days = buf.readByte(); - if (length > 4) { + if (length.get() > 4) { hours = buf.readByte(); minutes = buf.readByte(); seconds = buf.readByte(); - if (length > 7) { + if (length.get() > 7) { microseconds = buf.readUnsignedInt(); } } // xpand workaround https://jira.mariadb.org/browse/XPT-274 - if (year == 0 && month == 0 && days == 0 && hours == 0 && minutes == 0 && seconds == 0) + if (year == 0 && month == 0 && days == 0 && hours == 0 && minutes == 0 && seconds == 0) { + length.set(NULL_LENGTH); return null; + } return Duration.ZERO .plusDays(days - 1) @@ -174,7 +187,7 @@ public Duration decodeBinary(ReadableByteBuf buf, int length, ColumnDecoder colu return d; default: - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Duration", column.getType())); } diff --git a/src/main/java/org/mariadb/jdbc/plugin/codec/FloatCodec.java b/src/main/java/org/mariadb/jdbc/plugin/codec/FloatCodec.java index 87256c452..6b5a6556a 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/FloatCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/FloatCodec.java @@ -10,6 +10,7 @@ import java.util.EnumSet; import org.mariadb.jdbc.client.*; import org.mariadb.jdbc.client.socket.Writer; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.plugin.Codec; /** Float codec */ @@ -54,7 +55,7 @@ public boolean canEncode(Object value) { @Override public Float decodeText( final ReadableByteBuf buffer, - final int length, + final MutableInt length, final ColumnDecoder column, final Calendar cal) throws SQLDataException { @@ -64,7 +65,7 @@ public Float decodeText( @Override public Float decodeBinary( final ReadableByteBuf buffer, - final int length, + final MutableInt length, final ColumnDecoder column, final Calendar cal) throws SQLDataException { diff --git a/src/main/java/org/mariadb/jdbc/plugin/codec/GeometryCollectionCodec.java b/src/main/java/org/mariadb/jdbc/plugin/codec/GeometryCollectionCodec.java index a72bdcab2..ff0806816 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/GeometryCollectionCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/GeometryCollectionCodec.java @@ -9,6 +9,7 @@ import java.util.Calendar; import org.mariadb.jdbc.client.*; import org.mariadb.jdbc.client.socket.Writer; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.plugin.Codec; import org.mariadb.jdbc.type.*; @@ -32,23 +33,25 @@ public boolean canEncode(Object value) { @Override public GeometryCollection decodeText( - ReadableByteBuf buf, int length, ColumnDecoder column, Calendar cal) throws SQLDataException { + ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + throws SQLDataException { return decodeBinary(buf, length, column, cal); } @Override public GeometryCollection decodeBinary( - ReadableByteBuf buf, int length, ColumnDecoder column, Calendar cal) throws SQLDataException { + ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + throws SQLDataException { if (column.getType() == DataType.GEOMETRY) { buf.skip(4); // SRID - Geometry geo = Geometry.getGeometry(buf, length - 4, column); + Geometry geo = Geometry.getGeometry(buf, length.get() - 4, column); if (geo instanceof GeometryCollection) return (GeometryCollection) geo; throw new SQLDataException( String.format( "Geometric type %s cannot be decoded as GeometryCollection", geo.getClass().getName())); } - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as GeometryCollection", column.getType())); } diff --git a/src/main/java/org/mariadb/jdbc/plugin/codec/InstantCodec.java b/src/main/java/org/mariadb/jdbc/plugin/codec/InstantCodec.java index 5ae5a6d74..579d45e2d 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/InstantCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/InstantCodec.java @@ -12,6 +12,7 @@ import java.util.EnumSet; import org.mariadb.jdbc.client.*; import org.mariadb.jdbc.client.socket.Writer; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.plugin.Codec; /** Instant codec */ @@ -49,7 +50,7 @@ public boolean canEncode(Object value) { @Override public Instant decodeText( - ReadableByteBuf buf, int length, ColumnDecoder column, Calendar calParam) + ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar calParam) throws SQLDataException { LocalDateTime localDateTime = LocalDateTimeCodec.INSTANCE.decodeText(buf, length, column, calParam); @@ -59,7 +60,7 @@ public Instant decodeText( @Override public Instant decodeBinary( - ReadableByteBuf buf, int length, ColumnDecoder column, Calendar calParam) + ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar calParam) throws SQLDataException { LocalDateTime localDateTime = LocalDateTimeCodec.INSTANCE.decodeBinary(buf, length, column, calParam); diff --git a/src/main/java/org/mariadb/jdbc/plugin/codec/IntCodec.java b/src/main/java/org/mariadb/jdbc/plugin/codec/IntCodec.java index f1d9e1620..d1b02831e 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/IntCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/IntCodec.java @@ -10,6 +10,7 @@ import java.util.EnumSet; import org.mariadb.jdbc.client.*; import org.mariadb.jdbc.client.socket.Writer; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.plugin.Codec; /** Integer codec */ @@ -56,7 +57,7 @@ public boolean canEncode(Object value) { @Override public Integer decodeText( final ReadableByteBuf buffer, - final int length, + final MutableInt length, final ColumnDecoder column, final Calendar cal) throws SQLDataException { @@ -66,7 +67,7 @@ public Integer decodeText( @Override public Integer decodeBinary( final ReadableByteBuf buffer, - final int length, + final MutableInt length, final ColumnDecoder column, final Calendar cal) throws SQLDataException { diff --git a/src/main/java/org/mariadb/jdbc/plugin/codec/LineStringCodec.java b/src/main/java/org/mariadb/jdbc/plugin/codec/LineStringCodec.java index 8ca68f4b2..3a69aec3b 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/LineStringCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/LineStringCodec.java @@ -9,6 +9,7 @@ import java.util.Calendar; import org.mariadb.jdbc.client.*; import org.mariadb.jdbc.client.socket.Writer; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.plugin.Codec; import org.mariadb.jdbc.type.*; @@ -31,23 +32,25 @@ public boolean canEncode(Object value) { } @Override - public LineString decodeText(ReadableByteBuf buf, int length, ColumnDecoder column, Calendar cal) + public LineString decodeText( + ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) throws SQLDataException { return decodeBinary(buf, length, column, cal); } @Override public LineString decodeBinary( - ReadableByteBuf buf, int length, ColumnDecoder column, Calendar cal) throws SQLDataException { + ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + throws SQLDataException { if (column.getType() == DataType.GEOMETRY) { buf.skip(4); // SRID - Geometry geo = Geometry.getGeometry(buf, length - 4, column); + Geometry geo = Geometry.getGeometry(buf, length.get() - 4, column); if (geo instanceof LineString) return (LineString) geo; throw new SQLDataException( String.format( "Geometric type %s cannot be decoded as LineString", geo.getClass().getName())); } - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as LineString", column.getType())); } diff --git a/src/main/java/org/mariadb/jdbc/plugin/codec/LocalDateCodec.java b/src/main/java/org/mariadb/jdbc/plugin/codec/LocalDateCodec.java index 0bb65bf6e..fc2b84459 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/LocalDateCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/LocalDateCodec.java @@ -4,6 +4,8 @@ package org.mariadb.jdbc.plugin.codec; +import static org.mariadb.jdbc.client.result.Result.NULL_LENGTH; + import java.io.IOException; import java.sql.SQLDataException; import java.time.LocalDate; @@ -13,6 +15,7 @@ import java.util.EnumSet; import org.mariadb.jdbc.client.*; import org.mariadb.jdbc.client.socket.Writer; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.plugin.Codec; /** LocalDate codec */ @@ -43,12 +46,12 @@ public class LocalDateCodec implements Codec { * @param length data length * @return date/month/year array */ - public static int[] parseDate(ReadableByteBuf buf, int length) { + public static int[] parseDate(ReadableByteBuf buf, MutableInt length) { int[] datePart = new int[] {0, 0, 0}; int partIdx = 0; int idx = 0; - while (idx++ < length) { + while (idx++ < length.get()) { byte b = buf.readByte(); if (b == '-') { partIdx++; @@ -58,6 +61,7 @@ public static int[] parseDate(ReadableByteBuf buf, int length) { } if (datePart[0] == 0 && datePart[1] == 0 && datePart[2] == 0) { + length.set(NULL_LENGTH); return null; } return datePart; @@ -77,15 +81,16 @@ public boolean canEncode(Object value) { @Override @SuppressWarnings("fallthrough") - public LocalDate decodeText(ReadableByteBuf buf, int length, ColumnDecoder column, Calendar cal) + public LocalDate decodeText( + ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) throws SQLDataException { int[] parts; switch (column.getType()) { case YEAR: - short y = (short) buf.atoull(length); + short y = (short) buf.atoull(length.get()); - if (length == 2 && column.getColumnLength() == 2) { + if (length.get() == 2 && column.getColumnLength() == 2) { // YEAR(2) - deprecated if (y <= 69) { y += 2000; @@ -102,7 +107,7 @@ public LocalDate decodeText(ReadableByteBuf buf, int length, ColumnDecoder colum case TIMESTAMP: case DATETIME: - parts = LocalDateTimeCodec.parseTimestamp(buf.readAscii(length)); + parts = LocalDateTimeCodec.parseTimestamp(buf.readAscii(length.get())); break; case BLOB: @@ -110,7 +115,7 @@ public LocalDate decodeText(ReadableByteBuf buf, int length, ColumnDecoder colum case MEDIUMBLOB: case LONGBLOB: if (column.isBinary()) { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Date", column.getType())); } @@ -120,7 +125,7 @@ public LocalDate decodeText(ReadableByteBuf buf, int length, ColumnDecoder colum case VARSTRING: case VARCHAR: case STRING: - String val = buf.readString(length); + String val = buf.readString(length.get()); String[] stDatePart = val.split("[- ]"); if (stDatePart.length < 3) { throw new SQLDataException( @@ -131,7 +136,10 @@ public LocalDate decodeText(ReadableByteBuf buf, int length, ColumnDecoder colum int year = Integer.parseInt(stDatePart[0]); int month = Integer.parseInt(stDatePart[1]); int dayOfMonth = Integer.parseInt(stDatePart[2]); - if (year == 0 && month == 0 && dayOfMonth == 0) return null; + if (year == 0 && month == 0 && dayOfMonth == 0) { + length.set(NULL_LENGTH); + return null; + } return LocalDate.of(year, month, dayOfMonth); } catch (NumberFormatException nfe) { throw new SQLDataException( @@ -139,17 +147,21 @@ public LocalDate decodeText(ReadableByteBuf buf, int length, ColumnDecoder colum } default: - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Date", column.getType())); } - if (parts == null) return null; + if (parts == null) { + length.set(NULL_LENGTH); + return null; + } return LocalDate.of(parts[0], parts[1], parts[2]); } @Override @SuppressWarnings("fallthrough") - public LocalDate decodeBinary(ReadableByteBuf buf, int length, ColumnDecoder column, Calendar cal) + public LocalDate decodeBinary( + ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) throws SQLDataException { int year; @@ -159,17 +171,23 @@ public LocalDate decodeBinary(ReadableByteBuf buf, int length, ColumnDecoder col switch (column.getType()) { case TIMESTAMP: case DATETIME: - if (length == 0) return null; + if (length.get() == 0) { + length.set(NULL_LENGTH); + return null; + } year = buf.readUnsignedShort(); month = buf.readByte(); dayOfMonth = buf.readByte(); - if (length > 4) { - buf.skip(length - 4); + if (length.get() > 4) { + buf.skip(length.get() - 4); } // xpand workaround https://jira.mariadb.org/browse/XPT-274 - if (year == 0 && month == 0 && dayOfMonth == 0) return null; + if (year == 0 && month == 0 && dayOfMonth == 0) { + length.set(NULL_LENGTH); + return null; + } return LocalDate.of(year, month, dayOfMonth); case BLOB: @@ -177,7 +195,7 @@ public LocalDate decodeBinary(ReadableByteBuf buf, int length, ColumnDecoder col case MEDIUMBLOB: case LONGBLOB: if (column.isBinary()) { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Date", column.getType())); } @@ -187,7 +205,7 @@ public LocalDate decodeBinary(ReadableByteBuf buf, int length, ColumnDecoder col case STRING: case VARCHAR: case VARSTRING: - String val = buf.readString(length); + String val = buf.readString(length.get()); String[] stDatePart = val.split("[- ]"); if (stDatePart.length < 3) { throw new SQLDataException( @@ -198,7 +216,10 @@ public LocalDate decodeBinary(ReadableByteBuf buf, int length, ColumnDecoder col year = Integer.parseInt(stDatePart[0]); month = Integer.parseInt(stDatePart[1]); dayOfMonth = Integer.parseInt(stDatePart[2]); - if (year == 0 && month == 0 && dayOfMonth == 0) return null; + if (year == 0 && month == 0 && dayOfMonth == 0) { + length.set(NULL_LENGTH); + return null; + } return LocalDate.of(year, month, dayOfMonth); } catch (NumberFormatException nfe) { throw new SQLDataException( @@ -207,7 +228,10 @@ public LocalDate decodeBinary(ReadableByteBuf buf, int length, ColumnDecoder col case DATE: case YEAR: - if (length == 0) return null; + if (length.get() == 0) { + length.set(NULL_LENGTH); + return null; + } year = buf.readUnsignedShort(); if (column.getColumnLength() == 2) { @@ -219,18 +243,21 @@ public LocalDate decodeBinary(ReadableByteBuf buf, int length, ColumnDecoder col } } - if (length >= 4) { + if (length.get() >= 4) { month = buf.readByte(); dayOfMonth = buf.readByte(); } // xpand workaround https://jira.mariadb.org/browse/XPT-274 - if (year == 0 && month == 0 && dayOfMonth == 0) return null; + if (year == 0 && month == 0 && dayOfMonth == 0) { + length.set(NULL_LENGTH); + return null; + } return LocalDate.of(year, month, dayOfMonth); default: - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Date", column.getType())); } diff --git a/src/main/java/org/mariadb/jdbc/plugin/codec/LocalDateTimeCodec.java b/src/main/java/org/mariadb/jdbc/plugin/codec/LocalDateTimeCodec.java index e949c8d57..3ab4269b8 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/LocalDateTimeCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/LocalDateTimeCodec.java @@ -4,6 +4,8 @@ package org.mariadb.jdbc.plugin.codec; +import static org.mariadb.jdbc.client.result.Result.NULL_LENGTH; + import java.io.IOException; import java.sql.SQLDataException; import java.time.DateTimeException; @@ -15,6 +17,7 @@ import java.util.EnumSet; import org.mariadb.jdbc.client.*; import org.mariadb.jdbc.client.socket.Writer; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.plugin.Codec; /** LocalDateTime codec */ @@ -120,7 +123,8 @@ public boolean canEncode(Object value) { @Override @SuppressWarnings("fallthrough") public LocalDateTime decodeText( - ReadableByteBuf buf, int length, ColumnDecoder column, Calendar cal) throws SQLDataException { + ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + throws SQLDataException { int[] parts; switch (column.getType()) { case BLOB: @@ -128,7 +132,7 @@ public LocalDateTime decodeText( case MEDIUMBLOB: case LONGBLOB: if (column.isBinary()) { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as LocalDateTime", column.getType())); } @@ -138,10 +142,13 @@ public LocalDateTime decodeText( case STRING: case VARCHAR: case VARSTRING: - String val = buf.readString(length); + String val = buf.readString(length.get()); try { parts = parseTimestamp(val); - if (parts == null) return null; + if (parts == null) { + length.set(NULL_LENGTH); + return null; + } return LocalDateTime.of(parts[0], parts[1], parts[2], parts[3], parts[4], parts[5]) .plusNanos(parts[6]); } catch (DateTimeException dte) { @@ -152,13 +159,19 @@ public LocalDateTime decodeText( case DATE: parts = LocalDateCodec.parseDate(buf, length); - if (parts == null) return null; + if (parts == null) { + length.set(NULL_LENGTH); + return null; + } return LocalDateTime.of(parts[0], parts[1], parts[2], 0, 0, 0); case DATETIME: case TIMESTAMP: - parts = parseTimestamp(buf.readAscii(length)); - if (parts == null) return null; + parts = parseTimestamp(buf.readAscii(length.get())); + if (parts == null) { + length.set(NULL_LENGTH); + return null; + } return LocalDateTime.of(parts[0], parts[1], parts[2], parts[3], parts[4], parts[5]) .plusNanos(parts[6]); @@ -174,12 +187,12 @@ public LocalDateTime decodeText( return LocalDateTime.of(1970, 1, 1, parts[1] % 24, parts[2], parts[3]).plusNanos(parts[4]); case YEAR: - int year = Integer.parseInt(buf.readAscii(length)); + int year = Integer.parseInt(buf.readAscii(length.get())); if (column.getColumnLength() <= 2) year += year >= 70 ? 1900 : 2000; return LocalDateTime.of(year, 1, 1, 0, 0); default: - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as LocalDateTime", column.getType())); } @@ -188,7 +201,8 @@ public LocalDateTime decodeText( @Override @SuppressWarnings("fallthrough") public LocalDateTime decodeBinary( - ReadableByteBuf buf, int length, ColumnDecoder column, Calendar cal) throws SQLDataException { + ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + throws SQLDataException { int year = 1970; int month = 1; long dayOfMonth = 1; @@ -199,14 +213,14 @@ public LocalDateTime decodeBinary( switch (column.getType()) { case TIME: - if (length > 0) { + if (length.get() > 0) { // specific case for TIME, to handle value not in 00:00:00-23:59:59 boolean negate = buf.readByte() == 1; int day = buf.readInt(); hour = buf.readByte(); minutes = buf.readByte(); seconds = buf.readByte(); - if (length > 8) { + if (length.get() > 8) { microseconds = buf.readUnsignedInt(); } @@ -226,7 +240,7 @@ public LocalDateTime decodeBinary( case MEDIUMBLOB: case LONGBLOB: if (column.isBinary()) { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as LocalDateTime", column.getType())); } @@ -236,10 +250,13 @@ public LocalDateTime decodeBinary( case STRING: case VARCHAR: case VARSTRING: - String val = buf.readString(length); + String val = buf.readString(length.get()); try { int[] parts = parseTimestamp(val); - if (parts == null) return null; + if (parts == null) { + length.set(NULL_LENGTH); + return null; + } return LocalDateTime.of(parts[0], parts[1], parts[2], parts[3], parts[4], parts[5]) .plusNanos(parts[6]); } catch (DateTimeException dte) { @@ -251,24 +268,34 @@ public LocalDateTime decodeBinary( case DATE: case TIMESTAMP: case DATETIME: - if (length == 0) return null; + if (length.get() == 0) { + length.set(NULL_LENGTH); + return null; + } year = buf.readUnsignedShort(); month = buf.readByte(); dayOfMonth = buf.readByte(); - if (length > 4) { + if (length.get() > 4) { hour = buf.readByte(); minutes = buf.readByte(); seconds = buf.readByte(); - if (length > 7) { + if (length.get() > 7) { microseconds = buf.readUnsignedInt(); } } // xpand workaround https://jira.mariadb.org/browse/XPT-274 - if (year == 0 && month == 0 && dayOfMonth == 0 && hour == 0 && minutes == 0 && seconds == 0) + if (year == 0 + && month == 0 + && dayOfMonth == 0 + && hour == 0 + && minutes == 0 + && seconds == 0) { + length.set(NULL_LENGTH); return null; + } break; @@ -277,7 +304,7 @@ public LocalDateTime decodeBinary( if (column.getColumnLength() <= 2) year += year >= 70 ? 1900 : 2000; break; default: - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as LocalDateTime", column.getType())); } diff --git a/src/main/java/org/mariadb/jdbc/plugin/codec/LocalTimeCodec.java b/src/main/java/org/mariadb/jdbc/plugin/codec/LocalTimeCodec.java index 7843b8409..adb2d95f6 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/LocalTimeCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/LocalTimeCodec.java @@ -4,6 +4,8 @@ package org.mariadb.jdbc.plugin.codec; +import static org.mariadb.jdbc.client.result.Result.NULL_LENGTH; + import java.io.IOException; import java.sql.SQLDataException; import java.time.LocalDateTime; @@ -16,6 +18,7 @@ import java.util.TimeZone; import org.mariadb.jdbc.client.*; import org.mariadb.jdbc.client.socket.Writer; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.plugin.Codec; /** LocalTime codec */ @@ -46,7 +49,7 @@ public class LocalTimeCodec implements Codec { * @return hour/minutes/seconds/microseconds array * @throws SQLDataException if parsing error occurs */ - public static int[] parseTime(ReadableByteBuf buf, int length, ColumnDecoder column) + public static int[] parseTime(ReadableByteBuf buf, MutableInt length, ColumnDecoder column) throws SQLDataException { int initialPos = buf.pos(); int[] parts = new int[5]; @@ -55,13 +58,13 @@ public static int[] parseTime(ReadableByteBuf buf, int length, ColumnDecoder col int partLength = 0; byte b; int i = 0; - if (length > 0 && buf.getByte() == '-') { + if (length.get() > 0 && buf.getByte() == '-') { buf.skip(); i++; parts[0] = -1; } - for (; i < length; i++) { + for (; i < length.get(); i++) { b = buf.readByte(); if (b == ':' || b == '.') { idx++; @@ -70,7 +73,7 @@ public static int[] parseTime(ReadableByteBuf buf, int length, ColumnDecoder col } if (b < '0' || b > '9') { buf.pos(initialPos); - String val = buf.readString(length); + String val = buf.readString(length.get()); throw new SQLDataException( String.format("%s value '%s' cannot be decoded as Time", column.getType(), val)); } @@ -80,7 +83,7 @@ public static int[] parseTime(ReadableByteBuf buf, int length, ColumnDecoder col if (idx < 2) { buf.pos(initialPos); - String val = buf.readString(length); + String val = buf.readString(length.get()); throw new SQLDataException( String.format("%s value '%s' cannot be decoded as Time", column.getType(), val)); } @@ -108,15 +111,19 @@ public boolean canEncode(Object value) { @Override @SuppressWarnings("fallthrough") - public LocalTime decodeText(ReadableByteBuf buf, int length, ColumnDecoder column, Calendar cal) + public LocalTime decodeText( + ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) throws SQLDataException { int[] parts; switch (column.getType()) { case TIMESTAMP: case DATETIME: - parts = LocalDateTimeCodec.parseTimestamp(buf.readString(length)); - if (parts == null) return null; + parts = LocalDateTimeCodec.parseTimestamp(buf.readString(length.get())); + if (parts == null) { + length.set(NULL_LENGTH); + return null; + } return LocalTime.of(parts[3], parts[4], parts[5], parts[6]); case TIME: @@ -134,7 +141,7 @@ public LocalTime decodeText(ReadableByteBuf buf, int length, ColumnDecoder colum case MEDIUMBLOB: case LONGBLOB: if (column.isBinary()) { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as LocalTime", column.getType())); } @@ -144,7 +151,7 @@ public LocalTime decodeText(ReadableByteBuf buf, int length, ColumnDecoder colum case VARSTRING: case VARCHAR: case STRING: - String val = buf.readString(length); + String val = buf.readString(length.get()); try { if (val.contains(" ")) { ZoneId tz = @@ -161,7 +168,7 @@ public LocalTime decodeText(ReadableByteBuf buf, int length, ColumnDecoder colum } default: - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as LocalTime", column.getType())); } @@ -169,7 +176,8 @@ public LocalTime decodeText(ReadableByteBuf buf, int length, ColumnDecoder colum @Override @SuppressWarnings("fallthrough") - public LocalTime decodeBinary(ReadableByteBuf buf, int length, ColumnDecoder column, Calendar cal) + public LocalTime decodeBinary( + ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) throws SQLDataException { int hour = 0; @@ -179,38 +187,48 @@ public LocalTime decodeBinary(ReadableByteBuf buf, int length, ColumnDecoder col switch (column.getType()) { case TIMESTAMP: case DATETIME: - if (length == 0) return null; + if (length.get() == 0) { + length.set(NULL_LENGTH); + return null; + } int year = buf.readUnsignedShort(); int month = buf.readByte(); int dayOfMonth = buf.readByte(); - if (length > 4) { + if (length.get() > 4) { hour = buf.readByte(); minutes = buf.readByte(); seconds = buf.readByte(); - if (length > 7) { + if (length.get() > 7) { microseconds = buf.readInt(); } } // xpand workaround https://jira.mariadb.org/browse/XPT-274 - if (year == 0 && month == 0 && dayOfMonth == 0 && hour == 0 && minutes == 0 && seconds == 0) + if (year == 0 + && month == 0 + && dayOfMonth == 0 + && hour == 0 + && minutes == 0 + && seconds == 0) { + length.set(NULL_LENGTH); return null; + } return LocalTime.of(hour, minutes, seconds).plusNanos(microseconds * 1000); case TIME: - if (length > 0) { + if (length.get() > 0) { boolean negate = buf.readByte() == 1; - if (length > 4) { + if (length.get() > 4) { buf.skip(4); // skip days - if (length > 7) { + if (length.get() > 7) { hour = buf.readByte(); minutes = buf.readByte(); seconds = buf.readByte(); - if (length > 8) { + if (length.get() > 8) { microseconds = buf.readInt(); } } @@ -228,7 +246,7 @@ public LocalTime decodeBinary(ReadableByteBuf buf, int length, ColumnDecoder col case MEDIUMBLOB: case LONGBLOB: if (column.isBinary()) { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as LocalTime", column.getType())); } @@ -238,7 +256,7 @@ public LocalTime decodeBinary(ReadableByteBuf buf, int length, ColumnDecoder col case VARSTRING: case VARCHAR: case STRING: - String val = buf.readString(length); + String val = buf.readString(length.get()); try { if (val.contains(" ")) { ZoneId tz = @@ -255,7 +273,7 @@ public LocalTime decodeBinary(ReadableByteBuf buf, int length, ColumnDecoder col } default: - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as LocalTime", column.getType())); } diff --git a/src/main/java/org/mariadb/jdbc/plugin/codec/LongCodec.java b/src/main/java/org/mariadb/jdbc/plugin/codec/LongCodec.java index 22f05d4f2..e4d844e42 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/LongCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/LongCodec.java @@ -10,6 +10,7 @@ import java.util.EnumSet; import org.mariadb.jdbc.client.*; import org.mariadb.jdbc.client.socket.Writer; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.plugin.Codec; /** LongCodec codec */ @@ -56,7 +57,7 @@ public boolean canEncode(Object value) { @Override public Long decodeText( final ReadableByteBuf buffer, - final int length, + final MutableInt length, final ColumnDecoder column, final Calendar cal) throws SQLDataException { @@ -66,7 +67,7 @@ public Long decodeText( @Override public Long decodeBinary( final ReadableByteBuf buffer, - final int length, + final MutableInt length, final ColumnDecoder column, final Calendar cal) throws SQLDataException { diff --git a/src/main/java/org/mariadb/jdbc/plugin/codec/MultiLinestringCodec.java b/src/main/java/org/mariadb/jdbc/plugin/codec/MultiLinestringCodec.java index 8c1867fdc..b28fca9b3 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/MultiLinestringCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/MultiLinestringCodec.java @@ -9,6 +9,7 @@ import java.util.Calendar; import org.mariadb.jdbc.client.*; import org.mariadb.jdbc.client.socket.Writer; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.plugin.Codec; import org.mariadb.jdbc.type.Geometry; import org.mariadb.jdbc.type.LineString; @@ -35,22 +36,24 @@ public boolean canEncode(Object value) { @Override public MultiLineString decodeText( - ReadableByteBuf buf, int length, ColumnDecoder column, Calendar cal) throws SQLDataException { + ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + throws SQLDataException { return decodeBinary(buf, length, column, cal); } @Override public MultiLineString decodeBinary( - ReadableByteBuf buf, int length, ColumnDecoder column, Calendar cal) throws SQLDataException { + ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + throws SQLDataException { if (column.getType() == DataType.GEOMETRY) { buf.skip(4); // SRID - Geometry geo = Geometry.getGeometry(buf, length - 4, column); + Geometry geo = Geometry.getGeometry(buf, length.get() - 4, column); if (geo instanceof MultiLineString) return (MultiLineString) geo; throw new SQLDataException( String.format( "Geometric type %s cannot be decoded as MultiLineString", geo.getClass().getName())); } - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as MultiLineString", column.getType())); } diff --git a/src/main/java/org/mariadb/jdbc/plugin/codec/MultiPointCodec.java b/src/main/java/org/mariadb/jdbc/plugin/codec/MultiPointCodec.java index f18de3b90..a89a3fceb 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/MultiPointCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/MultiPointCodec.java @@ -9,6 +9,7 @@ import java.util.Calendar; import org.mariadb.jdbc.client.*; import org.mariadb.jdbc.client.socket.Writer; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.plugin.Codec; import org.mariadb.jdbc.type.Geometry; import org.mariadb.jdbc.type.MultiPoint; @@ -33,23 +34,25 @@ public boolean canEncode(Object value) { } @Override - public MultiPoint decodeText(ReadableByteBuf buf, int length, ColumnDecoder column, Calendar cal) + public MultiPoint decodeText( + ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) throws SQLDataException { return decodeBinary(buf, length, column, cal); } @Override public MultiPoint decodeBinary( - ReadableByteBuf buf, int length, ColumnDecoder column, Calendar cal) throws SQLDataException { + ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + throws SQLDataException { if (column.getType() == DataType.GEOMETRY) { buf.skip(4); // SRID - Geometry geo = Geometry.getGeometry(buf, length - 4, column); + Geometry geo = Geometry.getGeometry(buf, length.get() - 4, column); if (geo instanceof MultiPoint) return (MultiPoint) geo; throw new SQLDataException( String.format( "Geometric type %s cannot be decoded as MultiPoint", geo.getClass().getName())); } - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as MultiPoint", column.getType())); } diff --git a/src/main/java/org/mariadb/jdbc/plugin/codec/MultiPolygonCodec.java b/src/main/java/org/mariadb/jdbc/plugin/codec/MultiPolygonCodec.java index 2f74c523b..ad0d6cd53 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/MultiPolygonCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/MultiPolygonCodec.java @@ -9,6 +9,7 @@ import java.util.Calendar; import org.mariadb.jdbc.client.*; import org.mariadb.jdbc.client.socket.Writer; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.plugin.Codec; import org.mariadb.jdbc.type.*; @@ -32,22 +33,24 @@ public boolean canEncode(Object value) { @Override public MultiPolygon decodeText( - ReadableByteBuf buf, int length, ColumnDecoder column, Calendar cal) throws SQLDataException { + ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + throws SQLDataException { return decodeBinary(buf, length, column, cal); } @Override public MultiPolygon decodeBinary( - ReadableByteBuf buf, int length, ColumnDecoder column, Calendar cal) throws SQLDataException { + ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + throws SQLDataException { if (column.getType() == DataType.GEOMETRY) { buf.skip(4); // SRID - Geometry geo = Geometry.getGeometry(buf, length - 4, column); + Geometry geo = Geometry.getGeometry(buf, length.get() - 4, column); if (geo instanceof MultiPolygon) return (MultiPolygon) geo; throw new SQLDataException( String.format( "Geometric type %s cannot be decoded as MultiPolygon", geo.getClass().getName())); } - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as MultiPolygon", column.getType())); } diff --git a/src/main/java/org/mariadb/jdbc/plugin/codec/OffsetDateTimeCodec.java b/src/main/java/org/mariadb/jdbc/plugin/codec/OffsetDateTimeCodec.java index 3e702a486..0d448c6ec 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/OffsetDateTimeCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/OffsetDateTimeCodec.java @@ -14,6 +14,7 @@ import java.util.EnumSet; import org.mariadb.jdbc.client.*; import org.mariadb.jdbc.client.socket.Writer; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.plugin.Codec; /** OffsetDateTime codec */ @@ -52,7 +53,7 @@ public boolean canEncode(Object value) { @Override public OffsetDateTime decodeText( - ReadableByteBuf buf, int length, ColumnDecoder column, Calendar calParam) + ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar calParam) throws SQLDataException { switch (column.getType()) { @@ -66,7 +67,7 @@ public OffsetDateTime decodeText( case STRING: case VARCHAR: case VARSTRING: - String val = buf.readString(length); + String val = buf.readString(length.get()); try { return OffsetDateTime.parse(val); } catch (Throwable e) { @@ -76,7 +77,7 @@ public OffsetDateTime decodeText( String.format( "value '%s' (%s) cannot be decoded as OffsetDateTime", val, column.getType())); default: - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format( "value of type %s cannot be decoded as OffsetDateTime", column.getType())); @@ -85,7 +86,7 @@ public OffsetDateTime decodeText( @Override public OffsetDateTime decodeBinary( - ReadableByteBuf buf, int length, ColumnDecoder column, Calendar calParam) + ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar calParam) throws SQLDataException { switch (column.getType()) { @@ -99,7 +100,7 @@ public OffsetDateTime decodeBinary( case STRING: case VARCHAR: case VARSTRING: - String val = buf.readString(length); + String val = buf.readString(length.get()); try { return OffsetDateTime.parse(val); } catch (Throwable e) { @@ -110,7 +111,7 @@ public OffsetDateTime decodeBinary( "value '%s' (%s) cannot be decoded as OffsetDateTime", val, column.getType())); default: - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format( "value of type %s cannot be decoded as OffsetDateTime", column.getType())); diff --git a/src/main/java/org/mariadb/jdbc/plugin/codec/PointCodec.java b/src/main/java/org/mariadb/jdbc/plugin/codec/PointCodec.java index c5c015b93..11315234b 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/PointCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/PointCodec.java @@ -9,6 +9,7 @@ import java.util.Calendar; import org.mariadb.jdbc.client.*; import org.mariadb.jdbc.client.socket.Writer; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.plugin.Codec; import org.mariadb.jdbc.type.*; @@ -31,22 +32,24 @@ public boolean canEncode(Object value) { } @Override - public Point decodeText(ReadableByteBuf buf, int length, ColumnDecoder column, Calendar cal) + public Point decodeText( + ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) throws SQLDataException { return decodeBinary(buf, length, column, cal); } @Override - public Point decodeBinary(ReadableByteBuf buf, int length, ColumnDecoder column, Calendar cal) + public Point decodeBinary( + ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) throws SQLDataException { if (column.getType() == DataType.GEOMETRY) { buf.skip(4); // SRID - Geometry geo = Geometry.getGeometry(buf, length - 4, column); + Geometry geo = Geometry.getGeometry(buf, length.get() - 4, column); if (geo instanceof Point) return (Point) geo; throw new SQLDataException( String.format("Geometric type %s cannot be decoded as Point", geo.getClass().getName())); } - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Point", column.getType())); } diff --git a/src/main/java/org/mariadb/jdbc/plugin/codec/PolygonCodec.java b/src/main/java/org/mariadb/jdbc/plugin/codec/PolygonCodec.java index e0fda8fd9..d5200c41b 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/PolygonCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/PolygonCodec.java @@ -9,6 +9,7 @@ import java.util.Calendar; import org.mariadb.jdbc.client.*; import org.mariadb.jdbc.client.socket.Writer; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.plugin.Codec; import org.mariadb.jdbc.type.Geometry; import org.mariadb.jdbc.type.LineString; @@ -34,23 +35,25 @@ public boolean canEncode(Object value) { } @Override - public Polygon decodeText(ReadableByteBuf buf, int length, ColumnDecoder column, Calendar cal) + public Polygon decodeText( + ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) throws SQLDataException { return decodeBinary(buf, length, column, cal); } @Override - public Polygon decodeBinary(ReadableByteBuf buf, int length, ColumnDecoder column, Calendar cal) + public Polygon decodeBinary( + ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) throws SQLDataException { if (column.getType() == DataType.GEOMETRY) { buf.skip(4); // SRID - Geometry geo = Geometry.getGeometry(buf, length - 4, column); + Geometry geo = Geometry.getGeometry(buf, length.get() - 4, column); if (geo instanceof Polygon) return (Polygon) geo; throw new SQLDataException( String.format( "Geometric type %s cannot be decoded as Polygon", geo.getClass().getName())); } - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Polygon", column.getType())); } diff --git a/src/main/java/org/mariadb/jdbc/plugin/codec/ReaderCodec.java b/src/main/java/org/mariadb/jdbc/plugin/codec/ReaderCodec.java index 2b782dc02..e159aea68 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/ReaderCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/ReaderCodec.java @@ -11,6 +11,7 @@ import java.util.EnumSet; import org.mariadb.jdbc.client.*; import org.mariadb.jdbc.client.socket.Writer; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.plugin.Codec; import org.mariadb.jdbc.util.constants.ServerStatus; @@ -40,7 +41,8 @@ public String className() { @Override @SuppressWarnings("fallthrough") - public Reader decodeText(ReadableByteBuf buf, int length, ColumnDecoder column, Calendar cal) + public Reader decodeText( + ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) throws SQLDataException { switch (column.getType()) { case BLOB: @@ -48,7 +50,7 @@ public Reader decodeText(ReadableByteBuf buf, int length, ColumnDecoder column, case MEDIUMBLOB: case LONGBLOB: if (column.isBinary()) { - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Reader", column.getType())); } @@ -58,17 +60,18 @@ public Reader decodeText(ReadableByteBuf buf, int length, ColumnDecoder column, case STRING: case VARCHAR: case VARSTRING: - return new StringReader(buf.readString(length)); + return new StringReader(buf.readString(length.get())); default: - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Reader", column.getType())); } } @Override - public Reader decodeBinary(ReadableByteBuf buf, int length, ColumnDecoder column, Calendar cal) + public Reader decodeBinary( + ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) throws SQLDataException { return decodeText(buf, length, column, cal); } diff --git a/src/main/java/org/mariadb/jdbc/plugin/codec/ShortCodec.java b/src/main/java/org/mariadb/jdbc/plugin/codec/ShortCodec.java index 5dfcb93ce..876b5d16a 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/ShortCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/ShortCodec.java @@ -10,6 +10,7 @@ import java.util.EnumSet; import org.mariadb.jdbc.client.*; import org.mariadb.jdbc.client.socket.Writer; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.plugin.Codec; /** Short codec */ @@ -56,7 +57,7 @@ public String className() { @Override public Short decodeText( final ReadableByteBuf buffer, - final int length, + final MutableInt length, final ColumnDecoder column, final Calendar cal) throws SQLDataException { @@ -66,7 +67,7 @@ public Short decodeText( @Override public Short decodeBinary( final ReadableByteBuf buffer, - final int length, + final MutableInt length, final ColumnDecoder column, final Calendar cal) throws SQLDataException { diff --git a/src/main/java/org/mariadb/jdbc/plugin/codec/StreamCodec.java b/src/main/java/org/mariadb/jdbc/plugin/codec/StreamCodec.java index 06a5a874c..1ccf3637d 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/StreamCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/StreamCodec.java @@ -10,6 +10,7 @@ import java.util.EnumSet; import org.mariadb.jdbc.client.*; import org.mariadb.jdbc.client.socket.Writer; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.plugin.Codec; import org.mariadb.jdbc.util.constants.ServerStatus; @@ -38,7 +39,8 @@ public boolean canDecode(ColumnDecoder column, Class type) { } @Override - public InputStream decodeText(ReadableByteBuf buf, int length, ColumnDecoder column, Calendar cal) + public InputStream decodeText( + ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) throws SQLDataException { switch (column.getType()) { case STRING: @@ -48,11 +50,11 @@ public InputStream decodeText(ReadableByteBuf buf, int length, ColumnDecoder col case TINYBLOB: case MEDIUMBLOB: case LONGBLOB: - ByteArrayInputStream is = new ByteArrayInputStream(buf.buf(), buf.pos(), length); - buf.skip(length); + ByteArrayInputStream is = new ByteArrayInputStream(buf.buf(), buf.pos(), length.get()); + buf.skip(length.get()); return is; default: - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Stream", column.getType())); } @@ -60,7 +62,8 @@ public InputStream decodeText(ReadableByteBuf buf, int length, ColumnDecoder col @Override public InputStream decodeBinary( - ReadableByteBuf buf, int length, ColumnDecoder column, Calendar cal) throws SQLDataException { + ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + throws SQLDataException { switch (column.getType()) { case STRING: case VARCHAR: @@ -69,11 +72,11 @@ public InputStream decodeBinary( case TINYBLOB: case MEDIUMBLOB: case LONGBLOB: - ByteArrayInputStream is = new ByteArrayInputStream(buf.buf(), buf.pos(), length); - buf.skip(length); + ByteArrayInputStream is = new ByteArrayInputStream(buf.buf(), buf.pos(), length.get()); + buf.skip(length.get()); return is; default: - buf.skip(length); + buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Stream", column.getType())); } diff --git a/src/main/java/org/mariadb/jdbc/plugin/codec/StringCodec.java b/src/main/java/org/mariadb/jdbc/plugin/codec/StringCodec.java index c09e5d050..d62d3ea35 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/StringCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/StringCodec.java @@ -11,6 +11,7 @@ import java.util.EnumSet; import org.mariadb.jdbc.client.*; import org.mariadb.jdbc.client.socket.Writer; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.plugin.Codec; import org.mariadb.jdbc.util.constants.ServerStatus; @@ -62,13 +63,19 @@ public boolean canEncode(Object value) { } public String decodeText( - final ReadableByteBuf buf, final int length, final ColumnDecoder column, final Calendar cal) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar cal) throws SQLDataException { return column.decodeStringText(buf, length, cal); } public String decodeBinary( - final ReadableByteBuf buf, final int length, final ColumnDecoder column, final Calendar cal) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar cal) throws SQLDataException { return column.decodeStringBinary(buf, length, cal); } diff --git a/src/main/java/org/mariadb/jdbc/plugin/codec/TimeCodec.java b/src/main/java/org/mariadb/jdbc/plugin/codec/TimeCodec.java index 0772bd24a..13944876b 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/TimeCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/TimeCodec.java @@ -13,6 +13,7 @@ import java.util.EnumSet; import org.mariadb.jdbc.client.*; import org.mariadb.jdbc.client.socket.Writer; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.plugin.Codec; /** Time codec */ @@ -52,14 +53,15 @@ public boolean canEncode(Object value) { @Override @SuppressWarnings("fallthrough") - public Time decodeText(ReadableByteBuf buf, int length, ColumnDecoder column, Calendar cal) + public Time decodeText(ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) throws SQLDataException { return column.decodeTimeText(buf, length, cal); } @Override @SuppressWarnings("fallthrough") - public Time decodeBinary(ReadableByteBuf buf, int length, ColumnDecoder column, Calendar calParam) + public Time decodeBinary( + ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar calParam) throws SQLDataException { return column.decodeTimeBinary(buf, length, calParam); } diff --git a/src/main/java/org/mariadb/jdbc/plugin/codec/TimestampCodec.java b/src/main/java/org/mariadb/jdbc/plugin/codec/TimestampCodec.java index b8389bbbd..13fb18dbe 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/TimestampCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/TimestampCodec.java @@ -12,6 +12,7 @@ import java.util.EnumSet; import org.mariadb.jdbc.client.*; import org.mariadb.jdbc.client.socket.Writer; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.plugin.Codec; /** Timestamp codec */ @@ -50,14 +51,16 @@ public boolean canEncode(Object value) { @Override @SuppressWarnings("fallthrough") - public Timestamp decodeText(ReadableByteBuf buf, int length, ColumnDecoder column, Calendar cal) + public Timestamp decodeText( + ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) throws SQLDataException { return column.decodeTimestampText(buf, length, cal); } @Override @SuppressWarnings("fallthrough") - public Timestamp decodeBinary(ReadableByteBuf buf, int length, ColumnDecoder column, Calendar cal) + public Timestamp decodeBinary( + ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) throws SQLDataException { return column.decodeTimestampBinary(buf, length, cal); } diff --git a/src/main/java/org/mariadb/jdbc/plugin/codec/UuidCodec.java b/src/main/java/org/mariadb/jdbc/plugin/codec/UuidCodec.java index 8942fef26..79987c7d2 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/UuidCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/UuidCodec.java @@ -14,6 +14,7 @@ import org.mariadb.jdbc.client.DataType; import org.mariadb.jdbc.client.ReadableByteBuf; import org.mariadb.jdbc.client.socket.Writer; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.plugin.Codec; /** UUID codec */ @@ -38,13 +39,19 @@ public boolean canEncode(Object value) { } public UUID decodeText( - final ReadableByteBuf buf, final int length, final ColumnDecoder column, final Calendar cal) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar cal) throws SQLDataException { return UUID.fromString(column.decodeStringText(buf, length, cal)); } public UUID decodeBinary( - final ReadableByteBuf buf, final int length, final ColumnDecoder column, final Calendar cal) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar cal) throws SQLDataException { return UUID.fromString(column.decodeStringBinary(buf, length, cal)); } diff --git a/src/main/java/org/mariadb/jdbc/plugin/codec/ZonedDateTimeCodec.java b/src/main/java/org/mariadb/jdbc/plugin/codec/ZonedDateTimeCodec.java index df6e12be8..1a3718a3b 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/ZonedDateTimeCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/ZonedDateTimeCodec.java @@ -13,6 +13,7 @@ import java.util.EnumSet; import org.mariadb.jdbc.client.*; import org.mariadb.jdbc.client.socket.Writer; +import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.plugin.Codec; /** ZonedDateTime codec */ @@ -51,7 +52,7 @@ public boolean canEncode(Object value) { @Override public ZonedDateTime decodeText( - ReadableByteBuf buf, int length, ColumnDecoder column, Calendar calParam) + ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar calParam) throws SQLDataException { LocalDateTime localDateTime = LocalDateTimeCodec.INSTANCE.decodeText(buf, length, column, calParam); @@ -62,7 +63,7 @@ public ZonedDateTime decodeText( @Override public ZonedDateTime decodeBinary( - ReadableByteBuf buf, int length, ColumnDecoder column, Calendar calParam) + ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar calParam) throws SQLDataException { LocalDateTime localDateTime = LocalDateTimeCodec.INSTANCE.decodeBinary(buf, length, column, calParam); diff --git a/src/test/java/org/mariadb/jdbc/integration/codec/DateCodecTest.java b/src/test/java/org/mariadb/jdbc/integration/codec/DateCodecTest.java index cc919c1c1..410dea67e 100644 --- a/src/test/java/org/mariadb/jdbc/integration/codec/DateCodecTest.java +++ b/src/test/java/org/mariadb/jdbc/integration/codec/DateCodecTest.java @@ -88,6 +88,17 @@ public void getObject(ResultSet rs) throws SQLException { assertFalse(rs.wasNull()); assertNull(rs.getDate(4)); assertTrue(rs.wasNull()); + if (isMariaDBServer()) { + assertTrue(rs.next()); + assertNull(rs.getObject(1)); + assertTrue(rs.wasNull()); + assertNull(rs.getObject(1, LocalDateTime.class)); + assertTrue(rs.wasNull()); + assertNull(rs.getObject(1, ZonedDateTime.class)); + assertTrue(rs.wasNull()); + assertNull(rs.getObject(1, LocalDate.class)); + assertTrue(rs.wasNull()); + } } @Test diff --git a/src/test/java/org/mariadb/jdbc/integration/codec/DateTimeCodecTest.java b/src/test/java/org/mariadb/jdbc/integration/codec/DateTimeCodecTest.java index 6eacd3861..cd9ac5e23 100644 --- a/src/test/java/org/mariadb/jdbc/integration/codec/DateTimeCodecTest.java +++ b/src/test/java/org/mariadb/jdbc/integration/codec/DateTimeCodecTest.java @@ -103,6 +103,13 @@ public void getObject(ResultSet rs) throws SQLException { assertFalse(rs.wasNull()); assertNull(rs.getDate(4)); assertTrue(rs.wasNull()); + if (isMariaDBServer()) { + assertTrue(rs.next()); + assertNull(rs.getObject(1)); + assertTrue(rs.wasNull()); + assertNull(rs.getObject(1, LocalTime.class)); + assertTrue(rs.wasNull()); + } } @Test @@ -394,6 +401,22 @@ public void getDate(ResultSet rs) throws SQLException { rs.next(); assertNull(rs.getTime(1)); assertNull(rs.getTime(2)); + assertNull(rs.getDate(1)); + assertTrue(rs.wasNull()); + assertNull(rs.getDate(2)); + assertTrue(rs.wasNull()); + assertNull(rs.getTimestamp(1)); + assertTrue(rs.wasNull()); + assertNull(rs.getObject(1)); + assertTrue(rs.wasNull()); + assertNull(rs.getObject(1, LocalDateTime.class)); + assertTrue(rs.wasNull()); + assertNull(rs.getObject(1, ZonedDateTime.class)); + assertTrue(rs.wasNull()); + assertNull(rs.getObject(1, LocalDate.class)); + assertTrue(rs.wasNull()); + assertNull(rs.getObject(1, LocalTime.class)); + assertTrue(rs.wasNull()); } } @@ -759,6 +782,7 @@ public void getTime(ResultSet rs) throws SQLException { if (isMariaDBServer()) { rs.next(); assertNull(rs.getTime(1)); + assertTrue(rs.wasNull()); } } @@ -780,6 +804,7 @@ public void getDuration(ResultSet rs) throws SQLException { if (isMariaDBServer()) { rs.next(); assertNull(rs.getObject(1, Duration.class)); + assertTrue(rs.wasNull()); } } @@ -806,6 +831,9 @@ public void getLocalTime(ResultSet rs) throws SQLException { if (isMariaDBServer()) { rs.next(); assertNull(rs.getTime(1)); + assertTrue(rs.wasNull()); + assertNull(rs.getObject(1, LocalTime.class)); + assertTrue(rs.wasNull()); } } @@ -832,6 +860,9 @@ public void getLocalDate(ResultSet rs) throws SQLException { if (isMariaDBServer()) { rs.next(); assertNull(rs.getObject(1, LocalDate.class)); + assertTrue(rs.wasNull()); + assertNull(rs.getDate(1)); + assertTrue(rs.wasNull()); } } @@ -861,7 +892,11 @@ public void getTimestamp(ResultSet rs) throws SQLException { if (isMariaDBServer()) { rs.next(); assertNull(rs.getTimestamp(1)); + assertTrue(rs.wasNull()); assertNull(rs.getTimestamp(2)); + assertTrue(rs.wasNull()); + assertNull(rs.getObject(2, Timestamp.class)); + assertTrue(rs.wasNull()); assertEquals( Timestamp.valueOf("9999-12-31 00:00:00.00").getTime(), rs.getTimestamp(3).getTime()); } @@ -893,9 +928,13 @@ public void getLocalDateTime(ResultSet rs) throws SQLException { if (isMariaDBServer()) { rs.next(); assertNull(rs.getTimestamp(1)); + assertTrue(rs.wasNull()); assertNull(rs.getTimestamp(2)); + assertTrue(rs.wasNull()); assertNull(rs.getObject(1, LocalDateTime.class)); + assertTrue(rs.wasNull()); assertNull(rs.getObject(2, LocalDateTime.class)); + assertTrue(rs.wasNull()); assertEquals( LocalDateTime.parse("9999-12-31T00:00:00.00"), rs.getObject(3, LocalDateTime.class)); } @@ -934,7 +973,9 @@ public void getInstant(ResultSet rs) throws SQLException { if (isMariaDBServer()) { rs.next(); assertNull(rs.getTimestamp(1)); + assertTrue(rs.wasNull()); assertNull(rs.getTimestamp(2)); + assertTrue(rs.wasNull()); assertEquals( ZonedDateTime.of(LocalDateTime.parse("9999-12-31T00:00:00.00"), ZoneId.systemDefault()) .toInstant(), @@ -977,7 +1018,9 @@ public void getOffsetDateTime(ResultSet rs) throws SQLException { if (isMariaDBServer()) { rs.next(); assertNull(rs.getObject(1, OffsetDateTime.class)); + assertTrue(rs.wasNull()); assertNull(rs.getObject(2, OffsetDateTime.class)); + assertTrue(rs.wasNull()); assertEquals( OffsetDateTime.ofInstant( Timestamp.valueOf("9999-12-31 00:00:00.00").toInstant(), ZoneId.systemDefault()), diff --git a/src/test/java/org/mariadb/jdbc/integration/codec/VarcharCodecTest.java b/src/test/java/org/mariadb/jdbc/integration/codec/VarcharCodecTest.java index 08c1bfe21..57a99420c 100644 --- a/src/test/java/org/mariadb/jdbc/integration/codec/VarcharCodecTest.java +++ b/src/test/java/org/mariadb/jdbc/integration/codec/VarcharCodecTest.java @@ -569,6 +569,9 @@ public void getTimestamp(ResultSet rs) throws SQLException { + TimeZone.getDefault().getDSTSavings(), rs.getTimestamp("t2alias", Calendar.getInstance(TimeZone.getTimeZone("UTC"))).getTime()); assertFalse(rs.wasNull()); + rs.next(); + assertNull(rs.getTimestamp(2)); + assertTrue(rs.wasNull()); } @Test @@ -598,6 +601,7 @@ public void getLocalDateTime(ResultSet rs) throws SQLException { () -> rs.getObject(1, LocalDateTime.class), "value 'aaaa-bb-cc' (VARSTRING) cannot be decoded as LocalDateTime"); assertNull(rs.getObject(2, LocalDateTime.class)); + assertTrue(rs.wasNull()); } @Test @@ -626,6 +630,7 @@ public void getLocalDate(ResultSet rs) throws SQLException { () -> rs.getObject(1, LocalDate.class), "value 'aaaa-bb-cc' (VARSTRING) cannot be decoded as Date"); assertNull(rs.getObject(2, LocalDate.class)); + assertTrue(rs.wasNull()); } @Test From fa51fd47f4f91ba57a855ce267474b6cdc43f71d Mon Sep 17 00:00:00 2001 From: rusher Date: Wed, 26 Apr 2023 15:46:16 +0200 Subject: [PATCH 7/8] [misc] windows test correction --- .../org/mariadb/jdbc/integration/DataSourceTest.java | 9 +++++---- .../jdbc/integration/GssapiAuthenticationTest.java | 3 ++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/test/java/org/mariadb/jdbc/integration/DataSourceTest.java b/src/test/java/org/mariadb/jdbc/integration/DataSourceTest.java index 0144b0e33..f04fbab03 100644 --- a/src/test/java/org/mariadb/jdbc/integration/DataSourceTest.java +++ b/src/test/java/org/mariadb/jdbc/integration/DataSourceTest.java @@ -25,10 +25,11 @@ public void basic() throws SQLException { ds.setUrl(mDefUrl); testDs(ds); - ds = new MariaDbDataSource(); - ds.setPassword("ttt"); - ds.setUrl(mDefUrl); - assertThrows(SQLException.class, ds::getConnection); + MariaDbDataSource ds2 = new MariaDbDataSource(); + ds2.setUrl(mDefUrl); + ds2.setPassword("ttt"); + ds2.setUser("ttt"); + assertThrows(SQLException.class, ds2::getConnection); } private void testDs(MariaDbDataSource ds) throws SQLException { diff --git a/src/test/java/org/mariadb/jdbc/integration/GssapiAuthenticationTest.java b/src/test/java/org/mariadb/jdbc/integration/GssapiAuthenticationTest.java index d5df69d8d..af4016440 100644 --- a/src/test/java/org/mariadb/jdbc/integration/GssapiAuthenticationTest.java +++ b/src/test/java/org/mariadb/jdbc/integration/GssapiAuthenticationTest.java @@ -19,7 +19,8 @@ public void nativePassword() throws Exception { } catch (SQLException e) { // eat } - System.out.println("user name:" + System.getProperty("user.name")); + + stmt.execute("DROP USER IF EXISTS " + System.getProperty("user.name")); stmt.execute("CREATE USER " + System.getProperty("user.name") + " IDENTIFIED VIA gssapi"); stmt.execute("GRANT ALL PRIVILEGES ON *.* TO " + System.getProperty("user.name")); From 1ea84e3d85a991893344f64bd01f54ecde3026ed Mon Sep 17 00:00:00 2001 From: rusher Date: Wed, 26 Apr 2023 15:46:37 +0200 Subject: [PATCH 8/8] [CONJ-1071] Error during Bulk execution might result in connection wrong state --- .../jdbc/client/impl/StandardClient.java | 21 +++++++- .../jdbc/integration/BulkStmtSplitError.java | 49 +++++++++++++++++++ 2 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 src/test/java/org/mariadb/jdbc/integration/BulkStmtSplitError.java diff --git a/src/main/java/org/mariadb/jdbc/client/impl/StandardClient.java b/src/main/java/org/mariadb/jdbc/client/impl/StandardClient.java index a7cc5b33d..9a91bb3f3 100644 --- a/src/main/java/org/mariadb/jdbc/client/impl/StandardClient.java +++ b/src/main/java/org/mariadb/jdbc/client/impl/StandardClient.java @@ -543,7 +543,7 @@ public List executePipeline( boolean canRedo) throws SQLException { List results = new ArrayList<>(); - + int perMsgCounter = 0; int readCounter = 0; int[] responseMsg = new int[messages.length]; try { @@ -566,7 +566,7 @@ public List executePipeline( } while (readCounter < messages.length) { readCounter++; - for (int j = 0; j < responseMsg[readCounter - 1]; j++) { + for (perMsgCounter = 0; perMsgCounter < responseMsg[readCounter - 1]; perMsgCounter++) { results.addAll( readResponse( stmt, @@ -583,6 +583,23 @@ public List executePipeline( } catch (SQLException sqlException) { if (!closed) { // read remaining results + perMsgCounter++; + for (; perMsgCounter < responseMsg[readCounter - 1]; perMsgCounter++) { + try { + results.addAll( + readResponse( + stmt, + messages[readCounter - 1], + fetchSize, + maxRows, + resultSetConcurrency, + resultSetType, + closeOnCompletion)); + } catch (SQLException e) { + // eat + } + } + for (int i = readCounter; i < messages.length; i++) { for (int j = 0; j < responseMsg[i]; j++) { try { diff --git a/src/test/java/org/mariadb/jdbc/integration/BulkStmtSplitError.java b/src/test/java/org/mariadb/jdbc/integration/BulkStmtSplitError.java new file mode 100644 index 000000000..96fb947b7 --- /dev/null +++ b/src/test/java/org/mariadb/jdbc/integration/BulkStmtSplitError.java @@ -0,0 +1,49 @@ +package org.mariadb.jdbc.integration; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Types; +import org.junit.jupiter.api.*; +import org.mariadb.jdbc.Statement; + +public class BulkStmtSplitError extends Common { + + @AfterAll + public static void drop() throws SQLException { + Statement stmt = sharedConn.createStatement(); + stmt.execute("DROP TABLE IF EXISTS BulkStmtSplitError"); + } + + @BeforeAll + public static void beforeAll2() throws SQLException { + drop(); + Statement stmt = sharedConn.createStatement(); + stmt.execute( + "CREATE TABLE BulkStmtSplitError (t1 int not null primary key, field varchar(300))"); + stmt.execute("FLUSH TABLES"); + } + + @Test + public void BulkStmtSplitError() throws SQLException { + Statement stmt = sharedConn.createStatement(); + stmt.execute("INSERT INTO BulkStmtSplitError VALUES (1, 'tt'), (2, 'tt2')"); + + try (PreparedStatement prep = + sharedConn.prepareStatement("insert into BulkStmtSplitError values (?, ?)")) { + prep.setInt(1, 1); + prep.setNull(2, Types.VARCHAR); + prep.addBatch(); + prep.setInt(1, 2); + prep.setString(2, "Kenny"); + prep.addBatch(); + prep.executeBatch(); + } catch (SQLException e) { + // eat + } + + ResultSet rs = stmt.executeQuery("SELECT count(*) FROM agent"); + Assertions.assertTrue(rs.next()); + Assertions.assertEquals(2, rs.getInt(1)); + } +}