diff --git a/src/main/java/org/mariadb/jdbc/Configuration.java b/src/main/java/org/mariadb/jdbc/Configuration.java index 686dda5e7..3737ba451 100644 --- a/src/main/java/org/mariadb/jdbc/Configuration.java +++ b/src/main/java/org/mariadb/jdbc/Configuration.java @@ -63,6 +63,9 @@ public class Configuration { // various private String timezone = null; + private String connectionTimeZone = null; + private boolean forceConnectionTimeZoneToSession = true; + private boolean preserveInstants; private Boolean autocommit = null; private boolean useMysqlMetadata = false; private boolean nullDatabaseMeansCurrent = false; @@ -169,6 +172,9 @@ private Configuration( HaMode haMode, Properties nonMappedOptions, String timezone, + String connectionTimeZone, + boolean forceConnectionTimeZoneToSession, + boolean preserveInstants, Boolean autocommit, boolean useMysqlMetadata, boolean nullDatabaseMeansCurrent, @@ -252,6 +258,9 @@ private Configuration( this.haMode = haMode; this.nonMappedOptions = nonMappedOptions; this.timezone = timezone; + this.connectionTimeZone = connectionTimeZone; + this.forceConnectionTimeZoneToSession = forceConnectionTimeZoneToSession; + this.preserveInstants = preserveInstants; this.autocommit = autocommit; this.useMysqlMetadata = useMysqlMetadata; this.nullDatabaseMeansCurrent = nullDatabaseMeansCurrent; @@ -365,6 +374,9 @@ private Configuration( Boolean transformedBitIsBoolean, Boolean yearIsDateType, String timezone, + String connectionTimeZone, + Boolean forceConnectionTimeZoneToSession, + Boolean preserveInstants, Boolean dumpQueriesOnException, Integer prepStmtCacheSize, Boolean useAffectedRows, @@ -458,6 +470,10 @@ private Configuration( if (transformedBitIsBoolean != null) this.transformedBitIsBoolean = transformedBitIsBoolean; if (yearIsDateType != null) this.yearIsDateType = yearIsDateType; this.timezone = timezone; + if (connectionTimeZone != null) this.connectionTimeZone = connectionTimeZone; + if (forceConnectionTimeZoneToSession != null) + this.forceConnectionTimeZoneToSession = forceConnectionTimeZoneToSession; + if (preserveInstants != null) this.preserveInstants = preserveInstants; if (dumpQueriesOnException != null) this.dumpQueriesOnException = dumpQueriesOnException; if (prepStmtCacheSize != null) this.prepStmtCacheSize = prepStmtCacheSize; if (useAffectedRows != null) this.useAffectedRows = useAffectedRows; @@ -548,6 +564,22 @@ private Configuration( first = false; } + // ************************************************************* + // timezone verification + // ************************************************************* + if (this.timezone != null) { + if (this.connectionTimeZone == null) { + if ("disable".equalsIgnoreCase(this.timezone)) { + this.forceConnectionTimeZoneToSession = false; + } else { + this.forceConnectionTimeZoneToSession = true; + if (!"auto".equalsIgnoreCase(this.timezone)) { + this.connectionTimeZone = this.timezone; + } + } + } + } + // ************************************************************* // option value verification // ************************************************************* @@ -578,6 +610,9 @@ public Builder toBuilder() { .addresses(this.addresses == null ? null : this.addresses.toArray(new HostAddress[0])) .haMode(this.haMode) .timezone(this.timezone) + .connectionTimeZone(this.connectionTimeZone) + .forceConnectionTimeZoneToSession(this.forceConnectionTimeZoneToSession) + .preserveInstants(this.preserveInstants) .autocommit(this.autocommit) .useMysqlMetadata(this.useMysqlMetadata) .nullDatabaseMeansCurrent(this.nullDatabaseMeansCurrent) @@ -1078,10 +1113,13 @@ protected static String buildUrl(Configuration conf) { } if (field.getType().equals(String.class)) { - sb.append(first ? '?' : '&'); - first = false; - sb.append(field.getName()).append('='); - sb.append((String) obj); + String defaultValue = (String) field.get(defaultConf); + if (!obj.equals(defaultValue)) { + sb.append(first ? '?' : '&'); + first = false; + sb.append(field.getName()).append('='); + sb.append((String) obj); + } } else if (field.getType().equals(boolean.class)) { boolean defaultValue = field.getBoolean(defaultConf); if (!obj.equals(defaultValue)) { @@ -1548,6 +1586,28 @@ public String timezone() { return timezone; } + /** + * Set connectionTimeZone + * + * @return connectionTimeZone + */ + public String connectionTimeZone() { + return connectionTimeZone; + } + + /** + * forceConnectionTimeZoneToSession must connection timezone be forced + * + * @return forceConnectionTimeZoneToSession + */ + public boolean forceConnectionTimeZoneToSession() { + return forceConnectionTimeZoneToSession; + } + + public boolean preserveInstants() { + return preserveInstants; + } + /** * Must query by logged on exception. * @@ -2009,6 +2069,9 @@ public static final class Builder implements Cloneable { // various private String timezone; + private String connectionTimeZone; + private Boolean forceConnectionTimeZoneToSession; + private Boolean preserveInstants; private Boolean autocommit; private Boolean useMysqlMetadata; private Boolean nullDatabaseMeansCurrent; @@ -2613,6 +2676,40 @@ public Builder timezone(String timezone) { return this; } + /** + * indicate what timestamp timezone to use in exchanges with server possible value are + * LOCAL|SERVER|user-defined time zone + * + * @param connectionTimeZone default timezone + * @return this {@link Builder} + */ + public Builder connectionTimeZone(String connectionTimeZone) { + this.connectionTimeZone = nullOrEmpty(connectionTimeZone); + return this; + } + + /** + * Indicate if connectionTimeZone must be forced to session + * + * @param forceConnectionTimeZoneToSession must connector force connection timezone + * @return this {@link Builder} + */ + public Builder forceConnectionTimeZoneToSession(Boolean forceConnectionTimeZoneToSession) { + this.forceConnectionTimeZoneToSession = forceConnectionTimeZoneToSession; + return this; + } + + /** + * Indicate if connection must preserve instants + * + * @param preserveInstants must connector preserve instants + * @return this {@link Builder} + */ + public Builder preserveInstants(Boolean preserveInstants) { + this.preserveInstants = preserveInstants; + return this; + } + /** * Must queries be dump on exception stracktrace. * @@ -3121,6 +3218,9 @@ public Configuration build() throws SQLException { this.transformedBitIsBoolean, this.yearIsDateType, this.timezone, + this.connectionTimeZone, + this.forceConnectionTimeZoneToSession, + this.preserveInstants, this.dumpQueriesOnException, this.prepStmtCacheSize, this.useAffectedRows, diff --git a/src/main/java/org/mariadb/jdbc/client/ColumnDecoder.java b/src/main/java/org/mariadb/jdbc/client/ColumnDecoder.java index 068fb487c..76127a95b 100644 --- a/src/main/java/org/mariadb/jdbc/client/ColumnDecoder.java +++ b/src/main/java/org/mariadb/jdbc/client/ColumnDecoder.java @@ -202,27 +202,25 @@ default int getPrecision() { /** * Return default Object text encoded * - * @param conf configuration * @param buf row buffer * @param length data length + * @param context connection context * @return default Object * @throws SQLDataException if any decoding error occurs */ - Object getDefaultText( - final Configuration conf, final ReadableByteBuf buf, final MutableInt length) + Object getDefaultText(final ReadableByteBuf buf, final MutableInt length, Context context) throws SQLDataException; /** * Return default Object binary encoded * - * @param conf configuration * @param buf row buffer * @param length data length + * @param context connection context * @return default Object * @throws SQLDataException if any decoding error occurs */ - Object getDefaultBinary( - final Configuration conf, final ReadableByteBuf buf, final MutableInt length) + Object getDefaultBinary(final ReadableByteBuf buf, final MutableInt length, Context context) throws SQLDataException; /** @@ -231,10 +229,12 @@ Object getDefaultBinary( * @param buf row buffer * @param length data length * @param cal calendar + * @param context connection context * @return String value * @throws SQLDataException if any decoding error occurs */ - String decodeStringText(final ReadableByteBuf buf, final MutableInt length, final Calendar cal) + String decodeStringText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException; /** @@ -246,7 +246,8 @@ String decodeStringText(final ReadableByteBuf buf, final MutableInt length, fina * @return String value * @throws SQLDataException if any decoding error occurs */ - String decodeStringBinary(final ReadableByteBuf buf, final MutableInt length, final Calendar cal) + String decodeStringBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException; /** @@ -275,10 +276,12 @@ String decodeStringBinary(final ReadableByteBuf buf, final MutableInt length, fi * @param buf row buffer * @param length data length * @param cal calendar + * @param context connection Context * @return date value * @throws SQLDataException if any decoding error occurs */ - Date decodeDateText(final ReadableByteBuf buf, final MutableInt length, Calendar cal) + Date decodeDateText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException; /** @@ -287,10 +290,12 @@ Date decodeDateText(final ReadableByteBuf buf, final MutableInt length, Calendar * @param buf row buffer * @param length data length * @param cal calendar + * @param context connection Context * @return date value * @throws SQLDataException if any decoding error occurs */ - Date decodeDateBinary(final ReadableByteBuf buf, final MutableInt length, Calendar cal) + Date decodeDateBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException; /** @@ -299,10 +304,12 @@ Date decodeDateBinary(final ReadableByteBuf buf, final MutableInt length, Calend * @param buf row buffer * @param length data length * @param cal calendar + * @param context connection context * @return time value * @throws SQLDataException if any decoding error occurs */ - Time decodeTimeText(final ReadableByteBuf buf, final MutableInt length, Calendar cal) + Time decodeTimeText( + final ReadableByteBuf buf, final MutableInt length, Calendar cal, Context context) throws SQLDataException; /** @@ -311,10 +318,12 @@ Time decodeTimeText(final ReadableByteBuf buf, final MutableInt length, Calendar * @param buf row buffer * @param length data length * @param cal calendar + * @param context connection context * @return time value * @throws SQLDataException if any decoding error occurs */ - Time decodeTimeBinary(final ReadableByteBuf buf, final MutableInt length, Calendar cal) + Time decodeTimeBinary( + final ReadableByteBuf buf, final MutableInt length, Calendar cal, Context context) throws SQLDataException; /** @@ -323,10 +332,12 @@ Time decodeTimeBinary(final ReadableByteBuf buf, final MutableInt length, Calend * @param buf row buffer * @param length data length * @param cal calendar + * @param context connection context * @return timestamp value * @throws SQLDataException if any decoding error occurs */ - Timestamp decodeTimestampText(final ReadableByteBuf buf, final MutableInt length, Calendar cal) + Timestamp decodeTimestampText( + final ReadableByteBuf buf, final MutableInt length, Calendar cal, Context context) throws SQLDataException; /** @@ -335,10 +346,12 @@ Timestamp decodeTimestampText(final ReadableByteBuf buf, final MutableInt length * @param buf row buffer * @param length data length * @param cal calendar + * @param context connection context * @return timestamp value * @throws SQLDataException if any decoding error occurs */ - Timestamp decodeTimestampBinary(final ReadableByteBuf buf, final MutableInt length, Calendar cal) + Timestamp decodeTimestampBinary( + final ReadableByteBuf buf, final MutableInt length, Calendar cal, Context context) throws SQLDataException; /** diff --git a/src/main/java/org/mariadb/jdbc/client/Context.java b/src/main/java/org/mariadb/jdbc/client/Context.java index 2c4b11679..ac3834f78 100644 --- a/src/main/java/org/mariadb/jdbc/client/Context.java +++ b/src/main/java/org/mariadb/jdbc/client/Context.java @@ -3,6 +3,8 @@ // Copyright (c) 2015-2024 MariaDB Corporation Ab package org.mariadb.jdbc.client; +import java.util.Calendar; +import java.util.TimeZone; import java.util.function.Function; import org.mariadb.jdbc.Configuration; import org.mariadb.jdbc.export.ExceptionFactory; @@ -228,4 +230,15 @@ public interface Context { * @param charset server charset */ void setCharset(String charset); + + TimeZone getConnectionTimeZone(); + + void setConnectionTimeZone(TimeZone connectionTimeZone); + + /** + * Get calendar depending on configuration + * + * @return calendar + */ + Calendar getDefaultCalendar(); } 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 1dc4faeec..eab3bfcc2 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/BigDecimalColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/BigDecimalColumn.java @@ -9,6 +9,7 @@ import java.util.Calendar; import org.mariadb.jdbc.Configuration; import org.mariadb.jdbc.client.ColumnDecoder; +import org.mariadb.jdbc.client.Context; import org.mariadb.jdbc.client.DataType; import org.mariadb.jdbc.client.ReadableByteBuf; import org.mariadb.jdbc.client.util.MutableInt; @@ -32,15 +33,15 @@ public class BigDecimalColumn extends ColumnDefinitionPacket implements ColumnDe * @param extTypeFormat extended type format */ public BigDecimalColumn( - ReadableByteBuf buf, - int charset, - long length, - DataType dataType, - byte decimals, - int flags, - int[] stringPos, - String extTypeName, - String extTypeFormat) { + final ReadableByteBuf buf, + final int charset, + final long length, + final DataType dataType, + final byte decimals, + final int flags, + final int[] stringPos, + final String extTypeName, + final String extTypeFormat) { super( buf, charset, @@ -63,15 +64,15 @@ public BigDecimalColumn useAliasAsName() { return new BigDecimalColumn(this); } - public String defaultClassname(Configuration conf) { + public String defaultClassname(final Configuration conf) { return BigDecimal.class.getName(); } - public int getColumnType(Configuration conf) { + public int getColumnType(final Configuration conf) { return Types.DECIMAL; } - public String getColumnTypeName(Configuration conf) { + public String getColumnTypeName(final Configuration conf) { if (isSigned()) return "DECIMAL"; return "DECIMAL UNSIGNED"; } @@ -89,30 +90,34 @@ public int getPrecision() { } @Override - public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, MutableInt length) + public Object getDefaultText( + final ReadableByteBuf buf, final MutableInt length, final Context context) throws SQLDataException { return new BigDecimal(buf.readAscii(length.get())); } @Override - public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, MutableInt length) + public Object getDefaultBinary( + final ReadableByteBuf buf, final MutableInt length, final Context context) throws SQLDataException { return new BigDecimal(buf.readAscii(length.get())); } @Override - public boolean decodeBooleanText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public boolean decodeBooleanText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return new BigDecimal(buf.readAscii(length.get())).intValue() != 0; } @Override - public boolean decodeBooleanBinary(ReadableByteBuf buf, MutableInt length) + public boolean decodeBooleanBinary(final ReadableByteBuf buf, final MutableInt length) throws SQLDataException { return decodeBooleanText(buf, length); } @Override - public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public byte decodeByteText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { String str = buf.readString(length.get()); try { return new BigDecimal(str).setScale(0, RoundingMode.DOWN).byteValueExact(); @@ -123,24 +128,28 @@ public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDat } @Override - public byte decodeByteBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public byte decodeByteBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return decodeByteText(buf, length); } @Override - public String decodeStringText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public String decodeStringText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { return buf.readString(length.get()); } @Override - public String decodeStringBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public String decodeStringBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { return buf.readString(length.get()); } @Override - public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public short decodeShortText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result; String str = buf.readString(length.get()); try { @@ -155,12 +164,14 @@ public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLD } @Override - public short decodeShortBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public short decodeShortBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return decodeShortText(buf, length); } @Override - public int decodeIntText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public int decodeIntText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result; String str = buf.readString(length.get()); try { @@ -176,12 +187,14 @@ public int decodeIntText(ReadableByteBuf buf, MutableInt length) throws SQLDataE } @Override - public int decodeIntBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public int decodeIntBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return decodeIntText(buf, length); } @Override - public long decodeLongText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public long decodeLongText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { String str2 = buf.readAscii(length.get()); try { return new BigDecimal(str2).setScale(0, RoundingMode.DOWN).longValueExact(); @@ -191,7 +204,8 @@ public long decodeLongText(ReadableByteBuf buf, MutableInt length) throws SQLDat } @Override - public long decodeLongBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public long decodeLongBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { String str = buf.readString(length.get()); try { return new BigDecimal(str).setScale(0, RoundingMode.DOWN).longValueExact(); @@ -201,55 +215,64 @@ public long decodeLongBinary(ReadableByteBuf buf, MutableInt length) throws SQLD } @Override - public float decodeFloatText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public float decodeFloatText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return Float.parseFloat(buf.readAscii(length.get())); } @Override - public float decodeFloatBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public float decodeFloatBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return new BigDecimal(buf.readAscii(length.get())).floatValue(); } @Override - public double decodeDoubleText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public double decodeDoubleText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return Double.parseDouble(buf.readAscii(length.get())); } @Override - public double decodeDoubleBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public double decodeDoubleBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return new BigDecimal(buf.readAscii(length.get())).doubleValue(); } @Override - public Date decodeDateText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Date decodeDateText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Date decodeDateBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Date decodeDateBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Time decodeTimeText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Time decodeTimeText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Time decodeTimeBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Time decodeTimeBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Timestamp decodeTimestampText( + final ReadableByteBuf buf, final MutableInt length, Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException( @@ -257,7 +280,8 @@ public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Cal } @Override - public Timestamp decodeTimestampBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Timestamp decodeTimestampBinary( + final ReadableByteBuf buf, final MutableInt length, Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException( 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 84357c8b3..733047dbb 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/BitColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/BitColumn.java @@ -7,6 +7,7 @@ import java.util.Calendar; import org.mariadb.jdbc.Configuration; import org.mariadb.jdbc.client.ColumnDecoder; +import org.mariadb.jdbc.client.Context; import org.mariadb.jdbc.client.DataType; import org.mariadb.jdbc.client.ReadableByteBuf; import org.mariadb.jdbc.client.util.MutableInt; @@ -30,15 +31,15 @@ public class BitColumn extends ColumnDefinitionPacket implements ColumnDecoder { * @param extTypeFormat extended type format */ public BitColumn( - ReadableByteBuf buf, - int charset, - long length, - DataType dataType, - byte decimals, - int flags, - int[] stringPos, - String extTypeName, - String extTypeFormat) { + final ReadableByteBuf buf, + final int charset, + final long length, + final DataType dataType, + final byte decimals, + final int flags, + final int[] stringPos, + final String extTypeName, + final String extTypeFormat) { super( buf, charset, @@ -61,15 +62,15 @@ public BitColumn useAliasAsName() { return new BitColumn(this); } - public String defaultClassname(Configuration conf) { + public String defaultClassname(final Configuration conf) { return columnLength == 1 && conf.transformedBitIsBoolean() ? Boolean.class.getName() : "byte[]"; } - public int getColumnType(Configuration conf) { + public int getColumnType(final Configuration conf) { return columnLength == 1 && conf.transformedBitIsBoolean() ? Types.BOOLEAN : Types.BIT; } - public String getColumnTypeName(Configuration conf) { + public String getColumnTypeName(final Configuration conf) { return "BIT"; } @@ -78,9 +79,10 @@ public int getPrecision() { } @Override - public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, MutableInt length) + public Object getDefaultText( + final ReadableByteBuf buf, final MutableInt length, final Context context) throws SQLDataException { - if (columnLength == 1 && conf.transformedBitIsBoolean()) { + if (columnLength == 1 && context.getConf().transformedBitIsBoolean()) { return ByteCodec.parseBit(buf, length) != 0; } byte[] arr = new byte[length.get()]; @@ -89,36 +91,41 @@ public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, Muta } @Override - public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, MutableInt length) + public Object getDefaultBinary( + final ReadableByteBuf buf, final MutableInt length, final Context context) throws SQLDataException { - return getDefaultText(conf, buf, length); + return getDefaultText(buf, length, context); } @Override - public boolean decodeBooleanText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public boolean decodeBooleanText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return ByteCodec.parseBit(buf, length) != 0; } @Override - public boolean decodeBooleanBinary(ReadableByteBuf buf, MutableInt length) + public boolean decodeBooleanBinary(final ReadableByteBuf buf, final MutableInt length) throws SQLDataException { return ByteCodec.parseBit(buf, length) != 0; } @Override - public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public byte decodeByteText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { byte val = buf.readByte(); if (length.get() > 1) buf.skip(length.get() - 1); return val; } @Override - public byte decodeByteBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public byte decodeByteBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return decodeByteText(buf, length); } @Override - public String decodeStringText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public String decodeStringText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { byte[] bytes = new byte[length.get()]; buf.readBytes(bytes); @@ -139,7 +146,8 @@ public String decodeStringText(ReadableByteBuf buf, MutableInt length, Calendar } @Override - public String decodeStringBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public String decodeStringBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { byte[] bytes = new byte[length.get()]; buf.readBytes(bytes); @@ -160,7 +168,8 @@ public String decodeStringBinary(ReadableByteBuf buf, MutableInt length, Calenda } @Override - public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public short decodeShortText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result = 0; for (int i = 0; i < length.get(); i++) { byte b = buf.readByte(); @@ -173,12 +182,14 @@ public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLD } @Override - public short decodeShortBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public short decodeShortBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return decodeShortText(buf, length); } @Override - public int decodeIntText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public int decodeIntText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result = 0; for (int i = 0; i < length.get(); i++) { byte b = buf.readByte(); @@ -192,7 +203,8 @@ public int decodeIntText(ReadableByteBuf buf, MutableInt length) throws SQLDataE } @Override - public int decodeIntBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public int decodeIntBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result = 0; for (int i = 0; i < length.get(); i++) { byte b = buf.readByte(); @@ -208,7 +220,8 @@ public int decodeIntBinary(ReadableByteBuf buf, MutableInt length) throws SQLDat } @Override - public long decodeLongText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public long decodeLongText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result = 0; for (int i = 0; i < length.get(); i++) { byte b = buf.readByte(); @@ -218,64 +231,74 @@ public long decodeLongText(ReadableByteBuf buf, MutableInt length) throws SQLDat } @Override - public long decodeLongBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public long decodeLongBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return decodeLongText(buf, length); } @Override - public float decodeFloatText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public float decodeFloatText(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public float decodeFloatBinary(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public double decodeDoubleText(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public double decodeDoubleBinary(final ReadableByteBuf buf, final 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, MutableInt length, Calendar cal) + public Date decodeDateText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Date decodeDateBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Date decodeDateBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Time decodeTimeText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Time decodeTimeText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Time decodeTimeBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Time decodeTimeBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Timestamp decodeTimestampText( + final ReadableByteBuf buf, final MutableInt length, Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException( @@ -283,7 +306,8 @@ public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Cal } @Override - public Timestamp decodeTimestampBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Timestamp decodeTimestampBinary( + final ReadableByteBuf buf, final MutableInt length, Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException( 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 630f262b4..9248cf428 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/BlobColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/BlobColumn.java @@ -10,6 +10,7 @@ import java.util.Locale; import org.mariadb.jdbc.Configuration; import org.mariadb.jdbc.client.ColumnDecoder; +import org.mariadb.jdbc.client.Context; import org.mariadb.jdbc.client.DataType; import org.mariadb.jdbc.client.ReadableByteBuf; import org.mariadb.jdbc.client.util.MutableInt; @@ -32,15 +33,15 @@ public class BlobColumn extends StringColumn implements ColumnDecoder { * @param extTypeFormat extended type format */ public BlobColumn( - ReadableByteBuf buf, - int charset, - long length, - DataType dataType, - byte decimals, - int flags, - int[] stringPos, - String extTypeName, - String extTypeFormat) { + final ReadableByteBuf buf, + final int charset, + final long length, + final DataType dataType, + final byte decimals, + final int flags, + final int[] stringPos, + final String extTypeName, + final String extTypeFormat) { super(buf, charset, length, dataType, decimals, flags, stringPos, extTypeName, extTypeFormat); } @@ -53,11 +54,11 @@ public BlobColumn useAliasAsName() { return new BlobColumn(this); } - public String defaultClassname(Configuration conf) { + public String defaultClassname(final Configuration conf) { return isBinary() ? Blob.class.getName() : String.class.getName(); } - public int getColumnType(Configuration conf) { + public int getColumnType(final Configuration conf) { if (columnLength <= 0 || getDisplaySize() > 16777215) { return isBinary() ? Types.LONGVARBINARY : Types.LONGVARCHAR; } else { @@ -68,7 +69,7 @@ public int getColumnType(Configuration conf) { } } - public String getColumnTypeName(Configuration conf) { + public String getColumnTypeName(final Configuration conf) { /* map to different blob types based on datatype length see https://mariadb.com/kb/en/library/data-types/ @@ -113,7 +114,8 @@ public int getPrecision() { } @Override - public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, MutableInt length) + public Object getDefaultText( + final ReadableByteBuf buf, final MutableInt length, final Context context) throws SQLDataException { if (isBinary()) { return buf.readBlob(length.get()); @@ -122,13 +124,15 @@ public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, Muta } @Override - public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, MutableInt length) + public Object getDefaultBinary( + final ReadableByteBuf buf, final MutableInt length, final Context context) throws SQLDataException { - return getDefaultText(conf, buf, length); + return getDefaultText(buf, length, context); } @Override - public boolean decodeBooleanText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public boolean decodeBooleanText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { if (isBinary()) { buf.skip(length.get()); throw new SQLDataException( @@ -139,13 +143,14 @@ public boolean decodeBooleanText(ReadableByteBuf buf, MutableInt length) throws } @Override - public boolean decodeBooleanBinary(ReadableByteBuf buf, MutableInt length) + public boolean decodeBooleanBinary(final ReadableByteBuf buf, final MutableInt length) throws SQLDataException { return decodeBooleanText(buf, length); } @Override - public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public byte decodeByteText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result; if (!isBinary()) { // TEXT column @@ -171,24 +176,28 @@ public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDat } @Override - public byte decodeByteBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public byte decodeByteBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return decodeByteText(buf, length); } @Override - public String decodeStringText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public String decodeStringText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { return buf.readString(length.get()); } @Override - public String decodeStringBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public String decodeStringBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { return buf.readString(length.get()); } @Override - public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public short decodeShortText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { if (isBinary()) { buf.skip(length.get()); throw new SQLDataException( @@ -198,7 +207,8 @@ public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLD } @Override - public short decodeShortBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public short decodeShortBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { if (isBinary()) { buf.skip(length.get()); throw new SQLDataException( @@ -208,7 +218,8 @@ public short decodeShortBinary(ReadableByteBuf buf, MutableInt length) throws SQ } @Override - public int decodeIntText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public int decodeIntText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { if (isBinary()) { buf.skip(length.get()); throw new SQLDataException( @@ -218,7 +229,8 @@ public int decodeIntText(ReadableByteBuf buf, MutableInt length) throws SQLDataE } @Override - public int decodeIntBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public int decodeIntBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { if (isBinary()) { buf.skip(length.get()); throw new SQLDataException( @@ -228,7 +240,8 @@ public int decodeIntBinary(ReadableByteBuf buf, MutableInt length) throws SQLDat } @Override - public long decodeLongText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public long decodeLongText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { if (isBinary()) { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Long", dataType)); @@ -237,7 +250,8 @@ public long decodeLongText(ReadableByteBuf buf, MutableInt length) throws SQLDat } @Override - public long decodeLongBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public long decodeLongBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { if (isBinary()) { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Long", dataType)); @@ -246,7 +260,8 @@ public long decodeLongBinary(ReadableByteBuf buf, MutableInt length) throws SQLD } @Override - public float decodeFloatText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public float decodeFloatText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { if (isBinary()) { buf.skip(length.get()); throw new SQLDataException( @@ -256,7 +271,8 @@ public float decodeFloatText(ReadableByteBuf buf, MutableInt length) throws SQLD } @Override - public float decodeFloatBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public float decodeFloatBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { if (isBinary()) { buf.skip(length.get()); throw new SQLDataException( @@ -266,7 +282,8 @@ public float decodeFloatBinary(ReadableByteBuf buf, MutableInt length) throws SQ } @Override - public double decodeDoubleText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public double decodeDoubleText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { if (isBinary()) { buf.skip(length.get()); throw new SQLDataException( @@ -276,7 +293,8 @@ public double decodeDoubleText(ReadableByteBuf buf, MutableInt length) throws SQ } @Override - public double decodeDoubleBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public double decodeDoubleBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { if (isBinary()) { buf.skip(length.get()); throw new SQLDataException( @@ -286,64 +304,70 @@ public double decodeDoubleBinary(ReadableByteBuf buf, MutableInt length) throws } @Override - public Date decodeDateText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Date decodeDateText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { if (isBinary()) { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } - return super.decodeDateText(buf, length, cal); + return super.decodeDateText(buf, length, cal, context); } @Override - public Date decodeDateBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Date decodeDateBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { if (isBinary()) { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } - return super.decodeDateBinary(buf, length, cal); + return super.decodeDateBinary(buf, length, cal, context); } @Override - public Time decodeTimeText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Time decodeTimeText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { if (isBinary()) { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } - return super.decodeTimeText(buf, length, cal); + return super.decodeTimeText(buf, length, cal, context); } @Override - public Time decodeTimeBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Time decodeTimeBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { if (isBinary()) { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } - return super.decodeTimeBinary(buf, length, cal); + return super.decodeTimeBinary(buf, length, cal, context); } @Override - public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Timestamp decodeTimestampText( + final ReadableByteBuf buf, final MutableInt length, Calendar cal, final Context context) throws SQLDataException { if (isBinary()) { buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Timestamp", dataType)); } - return super.decodeTimestampText(buf, length, cal); + return super.decodeTimestampText(buf, length, cal, context); } @Override - public Timestamp decodeTimestampBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Timestamp decodeTimestampBinary( + final ReadableByteBuf buf, final MutableInt length, Calendar cal, final Context context) throws SQLDataException { if (isBinary()) { buf.skip(length.get()); throw new SQLDataException( String.format("Data type %s cannot be decoded as Timestamp", dataType)); } - return super.decodeTimestampBinary(buf, length, cal); + return super.decodeTimestampBinary(buf, length, cal, context); } } 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 9fbc42108..1d727781b 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/DateColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/DateColumn.java @@ -11,6 +11,7 @@ import java.util.TimeZone; import org.mariadb.jdbc.Configuration; import org.mariadb.jdbc.client.ColumnDecoder; +import org.mariadb.jdbc.client.Context; import org.mariadb.jdbc.client.DataType; import org.mariadb.jdbc.client.ReadableByteBuf; import org.mariadb.jdbc.client.util.MutableInt; @@ -33,15 +34,15 @@ public class DateColumn extends ColumnDefinitionPacket implements ColumnDecoder * @param extTypeFormat extended type format */ public DateColumn( - ReadableByteBuf buf, - int charset, - long length, - DataType dataType, - byte decimals, - int flags, - int[] stringPos, - String extTypeName, - String extTypeFormat) { + final ReadableByteBuf buf, + final int charset, + final long length, + final DataType dataType, + final byte decimals, + final int flags, + final int[] stringPos, + final String extTypeName, + final String extTypeFormat) { super( buf, charset, @@ -64,39 +65,42 @@ public DateColumn useAliasAsName() { return new DateColumn(this); } - public String defaultClassname(Configuration conf) { + public String defaultClassname(final Configuration conf) { return Date.class.getName(); } - public int getColumnType(Configuration conf) { + public int getColumnType(final Configuration conf) { return Types.DATE; } - public String getColumnTypeName(Configuration conf) { + public String getColumnTypeName(final Configuration conf) { return "DATE"; } @Override - public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, MutableInt length) + public Object getDefaultText( + final ReadableByteBuf buf, final MutableInt length, final Context context) throws SQLDataException { - return decodeDateText(buf, length, null); + return decodeDateText(buf, length, null, context); } @Override - public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, MutableInt length) + public Object getDefaultBinary( + final ReadableByteBuf buf, final MutableInt length, final Context context) throws SQLDataException { - return decodeDateBinary(buf, length, null); + return decodeDateBinary(buf, length, null, context); } @Override - public boolean decodeBooleanText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public boolean decodeBooleanText(final ReadableByteBuf buf, final 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, MutableInt length) + public boolean decodeBooleanBinary(final ReadableByteBuf buf, final MutableInt length) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException( @@ -104,25 +108,29 @@ public boolean decodeBooleanBinary(ReadableByteBuf buf, MutableInt length) } @Override - public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public byte decodeByteText(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public byte decodeByteBinary(final ReadableByteBuf buf, final 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, MutableInt length, Calendar cal) + public String decodeStringText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { return buf.readString(length.get()); } @Override - public String decodeStringBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public String decodeStringBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { if (length.get() == 0) return "0000-00-00"; int dateYear = buf.readUnsignedShort(); @@ -132,69 +140,80 @@ public String decodeStringBinary(ReadableByteBuf buf, MutableInt length, Calenda } @Override - public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public short decodeShortText(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public short decodeShortBinary(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public int decodeIntText(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public int decodeIntBinary(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public long decodeLongText(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public long decodeLongBinary(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public float decodeFloatText(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public float decodeFloatBinary(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public double decodeDoubleText(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public double decodeDoubleBinary(final ReadableByteBuf buf, final 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, MutableInt length, Calendar cal) + public Date decodeDateText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { int year = (int) buf.atoull(4); buf.skip(1); @@ -225,7 +244,8 @@ public Date decodeDateText(ReadableByteBuf buf, MutableInt length, Calendar cal) } @Override - public Date decodeDateBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Date decodeDateBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { if (length.get() == 0) { length.set(NULL_LENGTH); @@ -251,21 +271,24 @@ public Date decodeDateBinary(ReadableByteBuf buf, MutableInt length, Calendar ca } @Override - public Time decodeTimeText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Time decodeTimeText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Time decodeTimeBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Time decodeTimeBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Calendar calParam) + public Timestamp decodeTimestampText( + final ReadableByteBuf buf, final MutableInt length, Calendar calParam, final Context context) throws SQLDataException { if (calParam == null || calParam.getTimeZone().equals(TimeZone.getDefault())) { String s = buf.readAscii(length.get()); @@ -288,7 +311,8 @@ public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Cal } @Override - public Timestamp decodeTimestampBinary(ReadableByteBuf buf, MutableInt length, Calendar calParam) + public Timestamp decodeTimestampBinary( + final ReadableByteBuf buf, final MutableInt length, Calendar calParam, final Context context) throws SQLDataException { if (length.get() == 0) { length.set(NULL_LENGTH); 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 dcf866352..855ac5167 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/DoubleColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/DoubleColumn.java @@ -9,6 +9,7 @@ import java.util.Calendar; import org.mariadb.jdbc.Configuration; import org.mariadb.jdbc.client.ColumnDecoder; +import org.mariadb.jdbc.client.Context; import org.mariadb.jdbc.client.DataType; import org.mariadb.jdbc.client.ReadableByteBuf; import org.mariadb.jdbc.client.util.MutableInt; @@ -31,15 +32,15 @@ public class DoubleColumn extends ColumnDefinitionPacket implements ColumnDecode * @param extTypeFormat extended type format */ public DoubleColumn( - ReadableByteBuf buf, - int charset, - long length, - DataType dataType, - byte decimals, - int flags, - int[] stringPos, - String extTypeName, - String extTypeFormat) { + final ReadableByteBuf buf, + final int charset, + final long length, + final DataType dataType, + final byte decimals, + final int flags, + final int[] stringPos, + final String extTypeName, + final String extTypeFormat) { super( buf, charset, @@ -62,45 +63,49 @@ public DoubleColumn useAliasAsName() { return new DoubleColumn(this); } - public String defaultClassname(Configuration conf) { + public String defaultClassname(final Configuration conf) { return Double.class.getName(); } - public int getColumnType(Configuration conf) { + public int getColumnType(final Configuration conf) { return Types.DOUBLE; } - public String getColumnTypeName(Configuration conf) { + public String getColumnTypeName(final Configuration conf) { if (isSigned()) return "DOUBLE"; return "DOUBLE UNSIGNED"; } @Override - public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, MutableInt length) + public Object getDefaultText( + final ReadableByteBuf buf, final MutableInt length, final Context context) throws SQLDataException { return Double.parseDouble(buf.readAscii(length.get())); } @Override - public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, MutableInt length) + public Object getDefaultBinary( + final ReadableByteBuf buf, final MutableInt length, final Context context) throws SQLDataException { return buf.readDouble(); } @Override - public boolean decodeBooleanText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public boolean decodeBooleanText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { String s = buf.readAscii(length.get()); return !"0".equals(s); } @Override - public boolean decodeBooleanBinary(ReadableByteBuf buf, MutableInt length) + public boolean decodeBooleanBinary(final ReadableByteBuf buf, final MutableInt length) throws SQLDataException { return ((int) buf.readDouble()) != 0; } @Override - public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public byte decodeByteText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result; String str = buf.readString(length.get()); try { @@ -116,7 +121,8 @@ public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDat } @Override - public byte decodeByteBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public byte decodeByteBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result = (long) buf.readDouble(); if ((byte) result != result) { throw new SQLDataException("byte overflow"); @@ -126,19 +132,22 @@ public byte decodeByteBinary(ReadableByteBuf buf, MutableInt length) throws SQLD } @Override - public String decodeStringText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public String decodeStringText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { return buf.readString(length.get()); } @Override - public String decodeStringBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public String decodeStringBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { return String.valueOf(buf.readDouble()); } @Override - public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public short decodeShortText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result; String str = buf.readString(length.get()); try { @@ -154,7 +163,8 @@ public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLD } @Override - public short decodeShortBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public short decodeShortBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result = (long) buf.readDouble(); if ((short) result != result || (result < 0 && !isSigned())) { throw new SQLDataException("Short overflow"); @@ -163,7 +173,8 @@ public short decodeShortBinary(ReadableByteBuf buf, MutableInt length) throws SQ } @Override - public int decodeIntText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public int decodeIntText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result; String str = buf.readString(length.get()); try { @@ -180,7 +191,8 @@ public int decodeIntText(ReadableByteBuf buf, MutableInt length) throws SQLDataE } @Override - public int decodeIntBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public int decodeIntBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result = (long) buf.readDouble(); int res = (int) result; if (res != result) { @@ -190,7 +202,8 @@ public int decodeIntBinary(ReadableByteBuf buf, MutableInt length) throws SQLDat } @Override - public long decodeLongText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public long decodeLongText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { String str2 = buf.readAscii(length.get()); try { return new BigDecimal(str2).setScale(0, RoundingMode.DOWN).longValueExact(); @@ -200,60 +213,70 @@ public long decodeLongText(ReadableByteBuf buf, MutableInt length) throws SQLDat } @Override - public long decodeLongBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public long decodeLongBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return (long) buf.readDouble(); } @Override - public float decodeFloatText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public float decodeFloatText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return Float.parseFloat(buf.readAscii(length.get())); } @Override - public float decodeFloatBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public float decodeFloatBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return (float) buf.readDouble(); } @Override - public double decodeDoubleText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public double decodeDoubleText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return Double.parseDouble(buf.readAscii(length.get())); } @Override - public double decodeDoubleBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public double decodeDoubleBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return buf.readDouble(); } @Override - public Date decodeDateText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Date decodeDateText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Date decodeDateBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Date decodeDateBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Time decodeTimeText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Time decodeTimeText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Time decodeTimeBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Time decodeTimeBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Timestamp decodeTimestampText( + final ReadableByteBuf buf, final MutableInt length, Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException( @@ -261,7 +284,8 @@ public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Cal } @Override - public Timestamp decodeTimestampBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Timestamp decodeTimestampBinary( + final ReadableByteBuf buf, final MutableInt length, Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException( 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 f4d3064cc..8bca1fabf 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/FloatColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/FloatColumn.java @@ -9,6 +9,7 @@ import java.util.Calendar; import org.mariadb.jdbc.Configuration; import org.mariadb.jdbc.client.ColumnDecoder; +import org.mariadb.jdbc.client.Context; import org.mariadb.jdbc.client.DataType; import org.mariadb.jdbc.client.ReadableByteBuf; import org.mariadb.jdbc.client.util.MutableInt; @@ -31,15 +32,15 @@ public class FloatColumn extends ColumnDefinitionPacket implements ColumnDecoder * @param extTypeFormat extended type format */ public FloatColumn( - ReadableByteBuf buf, - int charset, - long length, - DataType dataType, - byte decimals, - int flags, - int[] stringPos, - String extTypeName, - String extTypeFormat) { + final ReadableByteBuf buf, + final int charset, + final long length, + final DataType dataType, + final byte decimals, + final int flags, + final int[] stringPos, + final String extTypeName, + final String extTypeFormat) { super( buf, charset, @@ -62,45 +63,49 @@ public FloatColumn useAliasAsName() { return new FloatColumn(this); } - public String defaultClassname(Configuration conf) { + public String defaultClassname(final Configuration conf) { return Float.class.getName(); } - public int getColumnType(Configuration conf) { + public int getColumnType(final Configuration conf) { return Types.REAL; } - public String getColumnTypeName(Configuration conf) { + public String getColumnTypeName(final Configuration conf) { if (isSigned()) return "FLOAT"; return "FLOAT UNSIGNED"; } @Override - public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, MutableInt length) + public Object getDefaultText( + final ReadableByteBuf buf, final MutableInt length, final Context context) throws SQLDataException { return Float.parseFloat(buf.readAscii(length.get())); } @Override - public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, MutableInt length) + public Object getDefaultBinary( + final ReadableByteBuf buf, final MutableInt length, final Context context) throws SQLDataException { return buf.readFloat(); } @Override - public boolean decodeBooleanText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public boolean decodeBooleanText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { String s = buf.readAscii(length.get()); return !"0".equals(s); } @Override - public boolean decodeBooleanBinary(ReadableByteBuf buf, MutableInt length) + public boolean decodeBooleanBinary(final ReadableByteBuf buf, final MutableInt length) throws SQLDataException { return ((int) buf.readFloat()) != 0; } @Override - public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public byte decodeByteText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result; String str = buf.readString(length.get()); try { @@ -116,7 +121,8 @@ public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDat } @Override - public byte decodeByteBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public byte decodeByteBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result = (long) buf.readFloat(); if ((byte) result != result) { throw new SQLDataException("byte overflow"); @@ -126,19 +132,22 @@ public byte decodeByteBinary(ReadableByteBuf buf, MutableInt length) throws SQLD } @Override - public String decodeStringText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public String decodeStringText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { return buf.readString(length.get()); } @Override - public String decodeStringBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public String decodeStringBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { return String.valueOf(buf.readFloat()); } @Override - public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public short decodeShortText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result; String str = buf.readString(length.get()); try { @@ -154,7 +163,8 @@ public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLD } @Override - public short decodeShortBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public short decodeShortBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result = (long) buf.readFloat(); if ((short) result != result || (result < 0 && !isSigned())) { throw new SQLDataException("Short overflow"); @@ -163,7 +173,8 @@ public short decodeShortBinary(ReadableByteBuf buf, MutableInt length) throws SQ } @Override - public int decodeIntText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public int decodeIntText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result; String str = buf.readString(length.get()); try { @@ -180,7 +191,8 @@ public int decodeIntText(ReadableByteBuf buf, MutableInt length) throws SQLDataE } @Override - public int decodeIntBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public int decodeIntBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result = (long) buf.readFloat(); int res = (int) result; if (res != result) { @@ -190,7 +202,8 @@ public int decodeIntBinary(ReadableByteBuf buf, MutableInt length) throws SQLDat } @Override - public long decodeLongText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public long decodeLongText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { String str2 = buf.readAscii(length.get()); try { return new BigDecimal(str2).setScale(0, RoundingMode.DOWN).longValueExact(); @@ -200,60 +213,70 @@ public long decodeLongText(ReadableByteBuf buf, MutableInt length) throws SQLDat } @Override - public long decodeLongBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public long decodeLongBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return (long) buf.readFloat(); } @Override - public float decodeFloatText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public float decodeFloatText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return Float.parseFloat(buf.readAscii(length.get())); } @Override - public float decodeFloatBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public float decodeFloatBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return buf.readFloat(); } @Override - public double decodeDoubleText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public double decodeDoubleText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return Double.parseDouble(buf.readAscii(length.get())); } @Override - public double decodeDoubleBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public double decodeDoubleBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return buf.readFloat(); } @Override - public Date decodeDateText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Date decodeDateText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Date decodeDateBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Date decodeDateBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Time decodeTimeText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Time decodeTimeText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Time decodeTimeBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Time decodeTimeBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Timestamp decodeTimestampText( + final ReadableByteBuf buf, final MutableInt length, Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException( @@ -261,7 +284,8 @@ public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Cal } @Override - public Timestamp decodeTimestampBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Timestamp decodeTimestampBinary( + final ReadableByteBuf buf, final MutableInt length, Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException( 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 720ffe1d2..0ad550087 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/GeometryColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/GeometryColumn.java @@ -7,6 +7,7 @@ import java.util.Calendar; import java.util.Locale; import org.mariadb.jdbc.Configuration; +import org.mariadb.jdbc.client.Context; import org.mariadb.jdbc.client.DataType; import org.mariadb.jdbc.client.ReadableByteBuf; import org.mariadb.jdbc.client.util.MutableInt; @@ -30,15 +31,15 @@ public class GeometryColumn extends BlobColumn { * @param extTypeFormat extended type format */ public GeometryColumn( - ReadableByteBuf buf, - int charset, - long length, - DataType dataType, - byte decimals, - int flags, - int[] stringPos, - String extTypeName, - String extTypeFormat) { + final ReadableByteBuf buf, + final int charset, + final long length, + final DataType dataType, + final byte decimals, + final int flags, + final int[] stringPos, + final String extTypeName, + final String extTypeFormat) { super(buf, charset, length, dataType, decimals, flags, stringPos, extTypeName, extTypeFormat); } @@ -51,7 +52,7 @@ public GeometryColumn useAliasAsName() { return new GeometryColumn(this); } - public String defaultClassname(Configuration conf) { + public String defaultClassname(final Configuration conf) { if (conf.geometryDefaultType() != null && "default".equals(conf.geometryDefaultType())) { if (extTypeName != null) { switch (extTypeName) { @@ -76,11 +77,11 @@ public String defaultClassname(Configuration conf) { return "byte[]"; } - public int getColumnType(Configuration conf) { + public int getColumnType(final Configuration conf) { return Types.VARBINARY; } - public String getColumnTypeName(Configuration conf) { + public String getColumnTypeName(final Configuration conf) { if (extTypeName != null) { return extTypeName.toUpperCase(Locale.ROOT); } @@ -88,9 +89,11 @@ public String getColumnTypeName(Configuration conf) { } @Override - public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, MutableInt length) + public Object getDefaultText( + final ReadableByteBuf buf, final MutableInt length, final Context context) throws SQLDataException { - if (conf.geometryDefaultType() != null && "default".equals(conf.geometryDefaultType())) { + if (context.getConf().geometryDefaultType() != null + && "default".equals(context.getConf().geometryDefaultType())) { buf.skip(4); // SRID return Geometry.getGeometry(buf, length.get() - 4, this); } @@ -100,13 +103,15 @@ public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, Muta } @Override - public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, MutableInt length) + public Object getDefaultBinary( + final ReadableByteBuf buf, final MutableInt length, final Context context) throws SQLDataException { - return getDefaultText(conf, buf, length); + return getDefaultText(buf, length, context); } @Override - public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Timestamp decodeTimestampText( + final ReadableByteBuf buf, final MutableInt length, Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException( @@ -114,7 +119,8 @@ public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Cal } @Override - public Timestamp decodeTimestampBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Timestamp decodeTimestampBinary( + final ReadableByteBuf buf, final MutableInt length, Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException( diff --git a/src/main/java/org/mariadb/jdbc/client/column/JsonColumn.java b/src/main/java/org/mariadb/jdbc/client/column/JsonColumn.java index d6c1e7b91..a0c88108c 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/JsonColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/JsonColumn.java @@ -26,15 +26,15 @@ public class JsonColumn extends StringColumn implements ColumnDecoder { * @param extTypeFormat extended type format */ public JsonColumn( - ReadableByteBuf buf, - int charset, - long length, - DataType dataType, - byte decimals, - int flags, - int[] stringPos, - String extTypeName, - String extTypeFormat) { + final ReadableByteBuf buf, + final int charset, + final long length, + final DataType dataType, + final byte decimals, + final int flags, + final int[] stringPos, + final String extTypeName, + final String extTypeFormat) { super(buf, charset, length, dataType, decimals, flags, stringPos, extTypeName, extTypeFormat); } @@ -47,15 +47,15 @@ public JsonColumn useAliasAsName() { return new JsonColumn(this); } - public String defaultClassname(Configuration conf) { + public String defaultClassname(final Configuration conf) { return String.class.getName(); } - public int getColumnType(Configuration conf) { + public int getColumnType(final Configuration conf) { return Types.LONGVARCHAR; } - public String getColumnTypeName(Configuration conf) { + public String getColumnTypeName(final Configuration conf) { return "JSON"; } } 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 08d469b65..024b5f9c9 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/SignedBigIntColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/SignedBigIntColumn.java @@ -8,6 +8,7 @@ import java.util.Calendar; import org.mariadb.jdbc.Configuration; import org.mariadb.jdbc.client.ColumnDecoder; +import org.mariadb.jdbc.client.Context; import org.mariadb.jdbc.client.DataType; import org.mariadb.jdbc.client.ReadableByteBuf; import org.mariadb.jdbc.client.util.MutableInt; @@ -30,15 +31,15 @@ public class SignedBigIntColumn extends ColumnDefinitionPacket implements Column * @param extTypeFormat extended type format */ public SignedBigIntColumn( - ReadableByteBuf buf, - int charset, - long length, - DataType dataType, - byte decimals, - int flags, - int[] stringPos, - String extTypeName, - String extTypeFormat) { + final ReadableByteBuf buf, + final int charset, + final long length, + final DataType dataType, + final byte decimals, + final int flags, + final int[] stringPos, + final String extTypeName, + final String extTypeFormat) { super( buf, charset, @@ -61,44 +62,48 @@ public SignedBigIntColumn useAliasAsName() { return new SignedBigIntColumn(this); } - public String defaultClassname(Configuration conf) { + public String defaultClassname(final Configuration conf) { return Long.class.getName(); } - public int getColumnType(Configuration conf) { + public int getColumnType(final Configuration conf) { return Types.BIGINT; } - public String getColumnTypeName(Configuration conf) { + public String getColumnTypeName(final Configuration conf) { return "BIGINT"; } @Override - public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, MutableInt length) + public Object getDefaultText( + final ReadableByteBuf buf, final MutableInt length, final Context context) throws SQLDataException { return buf.atoll(length.get()); } @Override - public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, MutableInt length) + public Object getDefaultBinary( + final ReadableByteBuf buf, final MutableInt length, final Context context) throws SQLDataException { return buf.readLong(); } @Override - public boolean decodeBooleanText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public boolean decodeBooleanText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { String s = buf.readAscii(length.get()); return !"0".equals(s); } @Override - public boolean decodeBooleanBinary(ReadableByteBuf buf, MutableInt length) + public boolean decodeBooleanBinary(final ReadableByteBuf buf, final MutableInt length) throws SQLDataException { return buf.readLong() != 0; } @Override - public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public byte decodeByteText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result = buf.atoll(length.get()); if ((byte) result != result) { throw new SQLDataException("byte overflow"); @@ -107,7 +112,8 @@ public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDat } @Override - public byte decodeByteBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public byte decodeByteBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result = buf.readLong(); if ((byte) result != result) { throw new SQLDataException("byte overflow"); @@ -117,19 +123,22 @@ public byte decodeByteBinary(ReadableByteBuf buf, MutableInt length) throws SQLD } @Override - public String decodeStringText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public String decodeStringText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { return buf.readString(length.get()); } @Override - public String decodeStringBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public String decodeStringBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { return BigInteger.valueOf(buf.readLong()).toString(); } @Override - public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public short decodeShortText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result = buf.atoll(length.get()); if ((short) result != result) { throw new SQLDataException("Short overflow"); @@ -138,7 +147,8 @@ public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLD } @Override - public short decodeShortBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public short decodeShortBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result = buf.readLong(); if ((short) result != result) { throw new SQLDataException("Short overflow"); @@ -147,7 +157,8 @@ public short decodeShortBinary(ReadableByteBuf buf, MutableInt length) throws SQ } @Override - public int decodeIntText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public int decodeIntText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result = buf.atoll(length.get()); int res = (int) result; if (res != result) { @@ -157,7 +168,8 @@ public int decodeIntText(ReadableByteBuf buf, MutableInt length) throws SQLDataE } @Override - public int decodeIntBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public int decodeIntBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result = buf.readLong(); int res = (int) result; if (res != result) { @@ -167,65 +179,76 @@ public int decodeIntBinary(ReadableByteBuf buf, MutableInt length) throws SQLDat } @Override - public long decodeLongText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public long decodeLongText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return buf.atoll(length.get()); } @Override - public long decodeLongBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public long decodeLongBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return buf.readLong(); } @Override - public float decodeFloatText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public float decodeFloatText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return Float.parseFloat(buf.readAscii(length.get())); } @Override - public float decodeFloatBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public float decodeFloatBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return (float) buf.readLong(); } @Override - public double decodeDoubleText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public double decodeDoubleText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return Double.parseDouble(buf.readAscii(length.get())); } @Override - public double decodeDoubleBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public double decodeDoubleBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return buf.readLong(); } @Override - public Date decodeDateText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Date decodeDateText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Date decodeDateBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Date decodeDateBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Time decodeTimeText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Time decodeTimeText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Time decodeTimeBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Time decodeTimeBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Timestamp decodeTimestampText( + final ReadableByteBuf buf, final MutableInt length, Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException( @@ -233,7 +256,8 @@ public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Cal } @Override - public Timestamp decodeTimestampBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Timestamp decodeTimestampBinary( + final ReadableByteBuf buf, final MutableInt length, Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException( 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 3d0b91982..33a8cc092 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/SignedIntColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/SignedIntColumn.java @@ -7,6 +7,7 @@ import java.util.Calendar; import org.mariadb.jdbc.Configuration; import org.mariadb.jdbc.client.ColumnDecoder; +import org.mariadb.jdbc.client.Context; import org.mariadb.jdbc.client.DataType; import org.mariadb.jdbc.client.ReadableByteBuf; import org.mariadb.jdbc.client.util.MutableInt; @@ -29,15 +30,15 @@ public class SignedIntColumn extends ColumnDefinitionPacket implements ColumnDec * @param extTypeFormat extended type format */ public SignedIntColumn( - ReadableByteBuf buf, - int charset, - long length, - DataType dataType, - byte decimals, - int flags, - int[] stringPos, - String extTypeName, - String extTypeFormat) { + final ReadableByteBuf buf, + final int charset, + final long length, + final DataType dataType, + final byte decimals, + final int flags, + final int[] stringPos, + final String extTypeName, + final String extTypeFormat) { super( buf, charset, @@ -60,44 +61,48 @@ public SignedIntColumn useAliasAsName() { return new SignedIntColumn(this); } - public String defaultClassname(Configuration conf) { + public String defaultClassname(final Configuration conf) { return Integer.class.getName(); } - public int getColumnType(Configuration conf) { + public int getColumnType(final Configuration conf) { return Types.INTEGER; } - public String getColumnTypeName(Configuration conf) { + public String getColumnTypeName(final Configuration conf) { return "INTEGER"; } @Override - public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, MutableInt length) + public Object getDefaultText( + final ReadableByteBuf buf, final MutableInt length, final Context context) throws SQLDataException { return (int) buf.atoll(length.get()); } @Override - public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, MutableInt length) + public Object getDefaultBinary( + final ReadableByteBuf buf, final MutableInt length, final Context context) throws SQLDataException { return buf.readInt(); } @Override - public boolean decodeBooleanText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public boolean decodeBooleanText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { String s = buf.readAscii(length.get()); return !"0".equals(s); } @Override - public boolean decodeBooleanBinary(ReadableByteBuf buf, MutableInt length) + public boolean decodeBooleanBinary(final ReadableByteBuf buf, final MutableInt length) throws SQLDataException { return buf.readInt() != 0; } @Override - public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public byte decodeByteText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result = buf.atoll(length.get()); if ((byte) result != result || (result < 0 && !isSigned())) { throw new SQLDataException("byte overflow"); @@ -106,7 +111,8 @@ public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDat } @Override - public byte decodeByteBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public byte decodeByteBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result = buf.readInt(); if ((byte) result != result) { throw new SQLDataException("byte overflow"); @@ -116,19 +122,22 @@ public byte decodeByteBinary(ReadableByteBuf buf, MutableInt length) throws SQLD } @Override - public String decodeStringText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public String decodeStringText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { return buf.readString(length.get()); } @Override - public String decodeStringBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public String decodeStringBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { return String.valueOf(buf.readInt()); } @Override - public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public short decodeShortText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result = buf.atoll(length.get()); if ((short) result != result) { throw new SQLDataException("Short overflow"); @@ -137,7 +146,8 @@ public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLD } @Override - public short decodeShortBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public short decodeShortBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result = isSigned() ? buf.readInt() : buf.readUnsignedInt(); if ((short) result != result) { throw new SQLDataException("Short overflow"); @@ -146,75 +156,88 @@ public short decodeShortBinary(ReadableByteBuf buf, MutableInt length) throws SQ } @Override - public int decodeIntText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public int decodeIntText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return (int) buf.atoll(length.get()); } @Override - public int decodeIntBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public int decodeIntBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return buf.readInt(); } @Override - public long decodeLongText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public long decodeLongText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return buf.atoll(length.get()); } @Override - public long decodeLongBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public long decodeLongBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return buf.readInt(); } @Override - public float decodeFloatText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public float decodeFloatText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return Float.parseFloat(buf.readAscii(length.get())); } @Override - public float decodeFloatBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public float decodeFloatBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return (float) buf.readInt(); } @Override - public double decodeDoubleText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public double decodeDoubleText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return Double.parseDouble(buf.readAscii(length.get())); } @Override - public double decodeDoubleBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public double decodeDoubleBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return buf.readInt(); } @Override - public Date decodeDateText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Date decodeDateText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Date decodeDateBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Date decodeDateBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Time decodeTimeText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Time decodeTimeText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Time decodeTimeBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Time decodeTimeBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Timestamp decodeTimestampText( + final ReadableByteBuf buf, final MutableInt length, Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException( @@ -222,7 +245,8 @@ public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Cal } @Override - public Timestamp decodeTimestampBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Timestamp decodeTimestampBinary( + final ReadableByteBuf buf, final MutableInt length, Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException( 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 6124e5051..8b484ca13 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/SignedMediumIntColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/SignedMediumIntColumn.java @@ -7,6 +7,7 @@ import java.util.Calendar; import org.mariadb.jdbc.Configuration; import org.mariadb.jdbc.client.ColumnDecoder; +import org.mariadb.jdbc.client.Context; import org.mariadb.jdbc.client.DataType; import org.mariadb.jdbc.client.ReadableByteBuf; import org.mariadb.jdbc.client.util.MutableInt; @@ -29,15 +30,15 @@ public class SignedMediumIntColumn extends ColumnDefinitionPacket implements Col * @param extTypeFormat extended type format */ public SignedMediumIntColumn( - ReadableByteBuf buf, - int charset, - long length, - DataType dataType, - byte decimals, - int flags, - int[] stringPos, - String extTypeName, - String extTypeFormat) { + final ReadableByteBuf buf, + final int charset, + final long length, + final DataType dataType, + final byte decimals, + final int flags, + final int[] stringPos, + final String extTypeName, + final String extTypeFormat) { super( buf, charset, @@ -60,44 +61,48 @@ public SignedMediumIntColumn useAliasAsName() { return new SignedMediumIntColumn(this); } - public String defaultClassname(Configuration conf) { + public String defaultClassname(final Configuration conf) { return Integer.class.getName(); } - public int getColumnType(Configuration conf) { + public int getColumnType(final Configuration conf) { return Types.INTEGER; } - public String getColumnTypeName(Configuration conf) { + public String getColumnTypeName(final Configuration conf) { return "MEDIUMINT"; } @Override - public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, MutableInt length) + public Object getDefaultText( + final ReadableByteBuf buf, final MutableInt length, final Context context) throws SQLDataException { return decodeIntText(buf, length); } @Override - public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, MutableInt length) + public Object getDefaultBinary( + final ReadableByteBuf buf, final MutableInt length, final Context context) throws SQLDataException { return decodeIntBinary(buf, length); } @Override - public boolean decodeBooleanText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public boolean decodeBooleanText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { String s = buf.readAscii(length.get()); return !"0".equals(s); } @Override - public boolean decodeBooleanBinary(ReadableByteBuf buf, MutableInt length) + public boolean decodeBooleanBinary(final ReadableByteBuf buf, final MutableInt length) throws SQLDataException { return buf.readInt() != 0; } @Override - public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public byte decodeByteText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result = buf.atoll(length.get()); if ((byte) result != result) { throw new SQLDataException("byte overflow"); @@ -106,7 +111,8 @@ public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDat } @Override - public byte decodeByteBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public byte decodeByteBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result = buf.readMedium(); buf.skip(); // MEDIUMINT is encoded on 4 bytes in exchanges ! if ((byte) result != result) { @@ -117,13 +123,15 @@ public byte decodeByteBinary(ReadableByteBuf buf, MutableInt length) throws SQLD } @Override - public String decodeStringText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public String decodeStringText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { return buf.readString(length.get()); } @Override - public String decodeStringBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public String decodeStringBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { String mediumStr = String.valueOf(isSigned() ? buf.readMedium() : buf.readUnsignedMedium()); buf.skip(); // MEDIUMINT is encoded on 4 bytes in exchanges ! @@ -131,7 +139,8 @@ public String decodeStringBinary(ReadableByteBuf buf, MutableInt length, Calenda } @Override - public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public short decodeShortText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result = buf.atoll(length.get()); if ((short) result != result) { throw new SQLDataException("Short overflow"); @@ -140,7 +149,8 @@ public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLD } @Override - public short decodeShortBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public short decodeShortBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result = buf.readMedium(); buf.skip(); // MEDIUMINT is encoded on 4 bytes in exchanges ! if ((short) result != result) { @@ -150,83 +160,96 @@ public short decodeShortBinary(ReadableByteBuf buf, MutableInt length) throws SQ } @Override - public int decodeIntText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public int decodeIntText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return (int) buf.atoll(length.get()); } @Override - public int decodeIntBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public int decodeIntBinary(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public long decodeLongText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return buf.atoll(length.get()); } @Override - public long decodeLongBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public long decodeLongBinary(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public float decodeFloatText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return Float.parseFloat(buf.readAscii(length.get())); } @Override - public float decodeFloatBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public float decodeFloatBinary(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public double decodeDoubleText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return Double.parseDouble(buf.readAscii(length.get())); } @Override - public double decodeDoubleBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public double decodeDoubleBinary(final ReadableByteBuf buf, final 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, MutableInt length, Calendar cal) + public Date decodeDateText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Date decodeDateBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Date decodeDateBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Time decodeTimeText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Time decodeTimeText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Time decodeTimeBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Time decodeTimeBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Timestamp decodeTimestampText( + final ReadableByteBuf buf, final MutableInt length, Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException( @@ -234,7 +257,8 @@ public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Cal } @Override - public Timestamp decodeTimestampBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Timestamp decodeTimestampBinary( + final ReadableByteBuf buf, final MutableInt length, Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException( 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 3f441aa8e..50f6fb9f3 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/SignedSmallIntColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/SignedSmallIntColumn.java @@ -7,6 +7,7 @@ import java.util.Calendar; import org.mariadb.jdbc.Configuration; import org.mariadb.jdbc.client.ColumnDecoder; +import org.mariadb.jdbc.client.Context; import org.mariadb.jdbc.client.DataType; import org.mariadb.jdbc.client.ReadableByteBuf; import org.mariadb.jdbc.client.util.MutableInt; @@ -29,15 +30,15 @@ public class SignedSmallIntColumn extends ColumnDefinitionPacket implements Colu * @param extTypeFormat extended type format */ public SignedSmallIntColumn( - ReadableByteBuf buf, - int charset, - long length, - DataType dataType, - byte decimals, - int flags, - int[] stringPos, - String extTypeName, - String extTypeFormat) { + final ReadableByteBuf buf, + final int charset, + final long length, + final DataType dataType, + final byte decimals, + final int flags, + final int[] stringPos, + final String extTypeName, + final String extTypeFormat) { super( buf, charset, @@ -60,44 +61,48 @@ public SignedSmallIntColumn useAliasAsName() { return new SignedSmallIntColumn(this); } - public String defaultClassname(Configuration conf) { + public String defaultClassname(final Configuration conf) { return Short.class.getName(); } - public int getColumnType(Configuration conf) { + public int getColumnType(final Configuration conf) { return Types.SMALLINT; } - public String getColumnTypeName(Configuration conf) { + public String getColumnTypeName(final Configuration conf) { return isSigned() ? "SMALLINT" : "SMALLINT UNSIGNED"; } @Override - public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, MutableInt length) + public Object getDefaultText( + final ReadableByteBuf buf, final MutableInt length, final Context context) throws SQLDataException { return (short) buf.atoll(length.get()); } @Override - public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, MutableInt length) + public Object getDefaultBinary( + final ReadableByteBuf buf, final MutableInt length, final Context context) throws SQLDataException { return buf.readShort(); } @Override - public boolean decodeBooleanText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public boolean decodeBooleanText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { String s = buf.readAscii(length.get()); return !"0".equals(s); } @Override - public boolean decodeBooleanBinary(ReadableByteBuf buf, MutableInt length) + public boolean decodeBooleanBinary(final ReadableByteBuf buf, final MutableInt length) throws SQLDataException { return buf.readShort() != 0; } @Override - public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public byte decodeByteText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result = buf.atoll(length.get()); if ((byte) result != result) { throw new SQLDataException("byte overflow"); @@ -106,7 +111,8 @@ public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDat } @Override - public byte decodeByteBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public byte decodeByteBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result = buf.readShort(); if ((byte) result != result) { throw new SQLDataException("byte overflow"); @@ -115,97 +121,114 @@ public byte decodeByteBinary(ReadableByteBuf buf, MutableInt length) throws SQLD } @Override - public String decodeStringText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public String decodeStringText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { return buf.readString(length.get()); } @Override - public String decodeStringBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public String decodeStringBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { return String.valueOf(buf.readShort()); } @Override - public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public short decodeShortText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return (short) buf.atoll(length.get()); } @Override - public short decodeShortBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public short decodeShortBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return buf.readShort(); } @Override - public int decodeIntText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public int decodeIntText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return (int) buf.atoll(length.get()); } @Override - public int decodeIntBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public int decodeIntBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return buf.readShort(); } @Override - public long decodeLongText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public long decodeLongText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return buf.atoll(length.get()); } @Override - public long decodeLongBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public long decodeLongBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return buf.readShort(); } @Override - public float decodeFloatText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public float decodeFloatText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return Float.parseFloat(buf.readAscii(length.get())); } @Override - public float decodeFloatBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public float decodeFloatBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return buf.readShort(); } @Override - public double decodeDoubleText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public double decodeDoubleText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return Double.parseDouble(buf.readAscii(length.get())); } @Override - public double decodeDoubleBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public double decodeDoubleBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return buf.readShort(); } @Override - public Date decodeDateText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Date decodeDateText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Date decodeDateBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Date decodeDateBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Time decodeTimeText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Time decodeTimeText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Time decodeTimeBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Time decodeTimeBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Timestamp decodeTimestampText( + final ReadableByteBuf buf, final MutableInt length, Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException( @@ -213,7 +236,8 @@ public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Cal } @Override - public Timestamp decodeTimestampBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Timestamp decodeTimestampBinary( + final ReadableByteBuf buf, final MutableInt length, Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException( 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 4b5ba792e..c06233563 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/SignedTinyIntColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/SignedTinyIntColumn.java @@ -7,6 +7,7 @@ import java.util.Calendar; import org.mariadb.jdbc.Configuration; import org.mariadb.jdbc.client.ColumnDecoder; +import org.mariadb.jdbc.client.Context; import org.mariadb.jdbc.client.DataType; import org.mariadb.jdbc.client.ReadableByteBuf; import org.mariadb.jdbc.client.util.MutableInt; @@ -29,15 +30,15 @@ public class SignedTinyIntColumn extends ColumnDefinitionPacket implements Colum * @param extTypeFormat extended type format */ public SignedTinyIntColumn( - ReadableByteBuf buf, - int charset, - long length, - DataType dataType, - byte decimals, - int flags, - int[] stringPos, - String extTypeName, - String extTypeFormat) { + final ReadableByteBuf buf, + final int charset, + final long length, + final DataType dataType, + final byte decimals, + final int flags, + final int[] stringPos, + final String extTypeName, + final String extTypeFormat) { super( buf, charset, @@ -60,19 +61,19 @@ public SignedTinyIntColumn useAliasAsName() { return new SignedTinyIntColumn(this); } - public String defaultClassname(Configuration conf) { + public String defaultClassname(final Configuration conf) { if (conf.tinyInt1isBit() && columnLength == 1) return Boolean.class.getName(); return Integer.class.getName(); } - public int getColumnType(Configuration conf) { + public int getColumnType(final Configuration conf) { if (conf.tinyInt1isBit() && columnLength == 1) { return conf.transformedBitIsBoolean() ? Types.BOOLEAN : Types.BIT; } return isSigned() ? Types.TINYINT : Types.SMALLINT; } - public String getColumnTypeName(Configuration conf) { + public String getColumnTypeName(final Configuration conf) { if (conf.tinyInt1isBit() && columnLength == 1) { return conf.transformedBitIsBoolean() ? "BOOLEAN" : "BIT"; } @@ -80,18 +81,20 @@ public String getColumnTypeName(Configuration conf) { } @Override - public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, MutableInt length) + public Object getDefaultText( + final ReadableByteBuf buf, final MutableInt length, final Context context) throws SQLDataException { - if (conf.tinyInt1isBit() && columnLength == 1) { + if (context.getConf().tinyInt1isBit() && columnLength == 1) { return decodeBooleanText(buf, length); } return (int) buf.atoll(length.get()); } @Override - public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, MutableInt length) + public Object getDefaultBinary( + final ReadableByteBuf buf, final MutableInt length, final Context context) throws SQLDataException { - if (conf.tinyInt1isBit() && columnLength == 1) { + if (context.getConf().tinyInt1isBit() && columnLength == 1) { return decodeBooleanBinary(buf, length); } if (isSigned()) { @@ -101,19 +104,21 @@ public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, Mu } @Override - public boolean decodeBooleanText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public boolean decodeBooleanText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { String s = buf.readAscii(length.get()); return !"0".equals(s); } @Override - public boolean decodeBooleanBinary(ReadableByteBuf buf, MutableInt length) + public boolean decodeBooleanBinary(final ReadableByteBuf buf, final MutableInt length) throws SQLDataException { return buf.readByte() != 0; } @Override - public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public byte decodeByteText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result = buf.atoll(length.get()); if ((byte) result != result) { throw new SQLDataException("byte overflow"); @@ -122,7 +127,8 @@ public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDat } @Override - public byte decodeByteBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public byte decodeByteBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { if (isSigned()) return buf.readByte(); long result = buf.readUnsignedByte(); @@ -133,13 +139,15 @@ public byte decodeByteBinary(ReadableByteBuf buf, MutableInt length) throws SQLD } @Override - public String decodeStringText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public String decodeStringText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { return buf.readString(length.get()); } @Override - public String decodeStringBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public String decodeStringBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { if (!isSigned()) { return String.valueOf(buf.readUnsignedByte()); @@ -148,32 +156,38 @@ public String decodeStringBinary(ReadableByteBuf buf, MutableInt length, Calenda } @Override - public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public short decodeShortText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return (short) buf.atoll(length.get()); } @Override - public short decodeShortBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public short decodeShortBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return (isSigned() ? buf.readByte() : buf.readUnsignedByte()); } @Override - public int decodeIntText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public int decodeIntText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return (int) buf.atoll(length.get()); } @Override - public int decodeIntBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public int decodeIntBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return (isSigned() ? buf.readByte() : buf.readUnsignedByte()); } @Override - public long decodeLongText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public long decodeLongText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return buf.atoll(length.get()); } @Override - public long decodeLongBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public long decodeLongBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { if (!isSigned()) { return buf.readUnsignedByte(); } @@ -181,12 +195,14 @@ public long decodeLongBinary(ReadableByteBuf buf, MutableInt length) throws SQLD } @Override - public float decodeFloatText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public float decodeFloatText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return Float.parseFloat(buf.readAscii(length.get())); } @Override - public float decodeFloatBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public float decodeFloatBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { if (!isSigned()) { return buf.readUnsignedByte(); } @@ -194,12 +210,14 @@ public float decodeFloatBinary(ReadableByteBuf buf, MutableInt length) throws SQ } @Override - public double decodeDoubleText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public double decodeDoubleText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return Double.parseDouble(buf.readAscii(length.get())); } @Override - public double decodeDoubleBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public double decodeDoubleBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { if (!isSigned()) { return buf.readUnsignedByte(); } @@ -207,35 +225,40 @@ public double decodeDoubleBinary(ReadableByteBuf buf, MutableInt length) throws } @Override - public Date decodeDateText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Date decodeDateText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Date decodeDateBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Date decodeDateBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Time decodeTimeText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Time decodeTimeText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Time decodeTimeBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Time decodeTimeBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Timestamp decodeTimestampText( + final ReadableByteBuf buf, final MutableInt length, Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException( @@ -243,7 +266,8 @@ public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Cal } @Override - public Timestamp decodeTimestampBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Timestamp decodeTimestampBinary( + final ReadableByteBuf buf, final MutableInt length, Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException( 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 85b8b4921..cdcdd5601 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/StringColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/StringColumn.java @@ -13,6 +13,7 @@ import java.util.Calendar; import org.mariadb.jdbc.Configuration; import org.mariadb.jdbc.client.ColumnDecoder; +import org.mariadb.jdbc.client.Context; import org.mariadb.jdbc.client.DataType; import org.mariadb.jdbc.client.ReadableByteBuf; import org.mariadb.jdbc.client.util.MutableInt; @@ -38,15 +39,15 @@ public class StringColumn extends ColumnDefinitionPacket implements ColumnDecode * @param extTypeFormat extended type format */ public StringColumn( - ReadableByteBuf buf, - int charset, - long length, - DataType dataType, - byte decimals, - int flags, - int[] stringPos, - String extTypeName, - String extTypeFormat) { + final ReadableByteBuf buf, + final int charset, + final long length, + final DataType dataType, + final byte decimals, + final int flags, + final int[] stringPos, + final String extTypeName, + final String extTypeFormat) { super( buf, charset, @@ -69,11 +70,11 @@ public StringColumn useAliasAsName() { return new StringColumn(this); } - public String defaultClassname(Configuration conf) { + public String defaultClassname(final Configuration conf) { return isBinary() ? "byte[]" : String.class.getName(); } - public int getColumnType(Configuration conf) { + public int getColumnType(final Configuration conf) { if (dataType == DataType.NULL) { return Types.NULL; } @@ -86,7 +87,7 @@ public int getColumnType(Configuration conf) { return isBinary() ? Types.VARBINARY : Types.VARCHAR; } - public String getColumnTypeName(Configuration conf) { + public String getColumnTypeName(final Configuration conf) { switch (dataType) { case STRING: if (isBinary()) { @@ -123,7 +124,8 @@ public int getPrecision() { } @Override - public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, MutableInt length) + public Object getDefaultText( + final ReadableByteBuf buf, final MutableInt length, final Context context) throws SQLDataException { if (isBinary()) { byte[] arr = new byte[length.get()]; @@ -134,7 +136,8 @@ public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, Muta } @Override - public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, MutableInt length) + public Object getDefaultBinary( + final ReadableByteBuf buf, final MutableInt length, final Context context) throws SQLDataException { if (isBinary()) { byte[] arr = new byte[length.get()]; @@ -145,18 +148,20 @@ public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, Mu } @Override - public boolean decodeBooleanText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public boolean decodeBooleanText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return !"0".equals(buf.readAscii(length.get())); } @Override - public boolean decodeBooleanBinary(ReadableByteBuf buf, MutableInt length) + public boolean decodeBooleanBinary(final ReadableByteBuf buf, final MutableInt length) throws SQLDataException { return !"0".equals(buf.readAscii(length.get())); } @Override - public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public byte decodeByteText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { if (isBinary()) { byte b = buf.readByte(); if (length.get() > 1) buf.skip(length.get() - 1); @@ -177,24 +182,28 @@ public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDat } @Override - public byte decodeByteBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public byte decodeByteBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return decodeByteText(buf, length); } @Override - public String decodeStringText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public String decodeStringText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { return buf.readString(length.get()); } @Override - public String decodeStringBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public String decodeStringBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { return buf.readString(length.get()); } @Override - public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public short decodeShortText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { String str = buf.readString(length.get()); try { return new BigDecimal(str).setScale(0, RoundingMode.DOWN).shortValueExact(); @@ -204,12 +213,14 @@ public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLD } @Override - public short decodeShortBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public short decodeShortBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return decodeShortText(buf, length); } @Override - public int decodeIntText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public int decodeIntText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { String str = buf.readString(length.get()); try { return new BigDecimal(str).setScale(0, RoundingMode.DOWN).intValueExact(); @@ -219,12 +230,14 @@ public int decodeIntText(ReadableByteBuf buf, MutableInt length) throws SQLDataE } @Override - public int decodeIntBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public int decodeIntBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return decodeIntText(buf, length); } @Override - public long decodeLongText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public long decodeLongText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { String str = buf.readString(length.get()); try { return new BigInteger(str).longValueExact(); @@ -234,12 +247,14 @@ public long decodeLongText(ReadableByteBuf buf, MutableInt length) throws SQLDat } @Override - public long decodeLongBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public long decodeLongBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return decodeLongText(buf, length); } @Override - public float decodeFloatText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public float decodeFloatText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { String val = buf.readString(length.get()); try { return Float.parseFloat(val); @@ -249,12 +264,14 @@ public float decodeFloatText(ReadableByteBuf buf, MutableInt length) throws SQLD } @Override - public float decodeFloatBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public float decodeFloatBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return decodeFloatText(buf, length); } @Override - public double decodeDoubleText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public double decodeDoubleText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { String str2 = buf.readString(length.get()); try { return Double.parseDouble(str2); @@ -264,12 +281,14 @@ public double decodeDoubleText(ReadableByteBuf buf, MutableInt length) throws SQ } @Override - public double decodeDoubleBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public double decodeDoubleBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return decodeDoubleText(buf, length); } @Override - public Date decodeDateText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Date decodeDateText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { String val = buf.readString(length.get()); if ("0000-00-00".equals(val)) return null; @@ -307,13 +326,15 @@ public Date decodeDateText(ReadableByteBuf buf, MutableInt length, Calendar cal) } @Override - public Date decodeDateBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Date decodeDateBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { - return decodeDateText(buf, length, cal); + return decodeDateText(buf, length, cal, context); } @Override - public Time decodeTimeText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Time decodeTimeText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { Calendar c = cal == null ? Calendar.getInstance() : cal; int offset = c.getTimeZone().getOffset(0); @@ -326,7 +347,11 @@ public Time decodeTimeText(ReadableByteBuf buf, MutableInt length, Calendar cal) } @Override - public Time decodeTimeBinary(ReadableByteBuf buf, MutableInt length, Calendar calParam) + public Time decodeTimeBinary( + final ReadableByteBuf buf, + final MutableInt length, + final Calendar calParam, + final Context context) throws SQLDataException { int[] parts = LocalTimeCodec.parseTime(buf, length, this); Time t; @@ -372,7 +397,8 @@ public Time decodeTimeBinary(ReadableByteBuf buf, MutableInt length, Calendar ca } @Override - public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Calendar calParam) + public Timestamp decodeTimestampText( + final ReadableByteBuf buf, final MutableInt length, Calendar calParam, final Context context) throws SQLDataException { int pos = buf.pos(); int nanoBegin = -1; @@ -446,7 +472,8 @@ public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Cal } @Override - public Timestamp decodeTimestampBinary(ReadableByteBuf buf, MutableInt length, Calendar calParam) + public Timestamp decodeTimestampBinary( + final ReadableByteBuf buf, final MutableInt length, Calendar calParam, final Context context) throws SQLDataException { String val = buf.readString(length.get()); try { 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 8795edd1b..0c934b0c6 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/TimeColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/TimeColumn.java @@ -7,6 +7,7 @@ import java.util.Calendar; import org.mariadb.jdbc.Configuration; import org.mariadb.jdbc.client.ColumnDecoder; +import org.mariadb.jdbc.client.Context; import org.mariadb.jdbc.client.DataType; import org.mariadb.jdbc.client.ReadableByteBuf; import org.mariadb.jdbc.client.util.MutableInt; @@ -30,15 +31,15 @@ public class TimeColumn extends ColumnDefinitionPacket implements ColumnDecoder * @param extTypeFormat extended type format */ public TimeColumn( - ReadableByteBuf buf, - int charset, - long length, - DataType dataType, - byte decimals, - int flags, - int[] stringPos, - String extTypeName, - String extTypeFormat) { + final ReadableByteBuf buf, + final int charset, + final long length, + final DataType dataType, + final byte decimals, + final int flags, + final int[] stringPos, + final String extTypeName, + final String extTypeFormat) { super( buf, charset, @@ -61,20 +62,21 @@ public TimeColumn useAliasAsName() { return new TimeColumn(this); } - public String defaultClassname(Configuration conf) { + public String defaultClassname(final Configuration conf) { return Time.class.getName(); } - public int getColumnType(Configuration conf) { + public int getColumnType(final Configuration conf) { return Types.TIME; } - public String getColumnTypeName(Configuration conf) { + public String getColumnTypeName(final Configuration conf) { return "TIME"; } @Override - public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, MutableInt length) + public Object getDefaultText( + final ReadableByteBuf buf, final MutableInt length, final Context context) throws SQLDataException { Calendar c = Calendar.getInstance(); int offset = c.getTimeZone().getOffset(0); @@ -87,7 +89,8 @@ public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, Muta } @Override - public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, MutableInt length) + public Object getDefaultBinary( + final ReadableByteBuf buf, final MutableInt length, final Context context) throws SQLDataException { boolean negate = false; Calendar cal = Calendar.getInstance(); @@ -119,26 +122,29 @@ public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, Mu } @Override - public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public byte decodeByteText(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public byte decodeByteBinary(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public boolean decodeBooleanText(final ReadableByteBuf buf, final 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, MutableInt length) + public boolean decodeBooleanBinary(final ReadableByteBuf buf, final MutableInt length) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException( @@ -146,13 +152,15 @@ public boolean decodeBooleanBinary(ReadableByteBuf buf, MutableInt length) } @Override - public String decodeStringText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public String decodeStringText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { return buf.readString(length.get()); } @Override - public String decodeStringBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public String decodeStringBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { long tDays = 0; int tHours = 0; @@ -208,83 +216,96 @@ public String decodeStringBinary(ReadableByteBuf buf, MutableInt length, Calenda } @Override - public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public short decodeShortText(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public short decodeShortBinary(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public int decodeIntText(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public int decodeIntBinary(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public long decodeLongText(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public long decodeLongBinary(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public float decodeFloatText(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public float decodeFloatBinary(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public double decodeDoubleText(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public double decodeDoubleBinary(final ReadableByteBuf buf, final 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, MutableInt length, Calendar cal) + public Date decodeDateText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Date decodeDateBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Date decodeDateBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Time decodeTimeText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Time decodeTimeText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { Calendar c = cal == null ? Calendar.getInstance() : cal; int offset = c.getTimeZone().getOffset(0); @@ -297,7 +318,11 @@ public Time decodeTimeText(ReadableByteBuf buf, MutableInt length, Calendar cal) } @Override - public Time decodeTimeBinary(ReadableByteBuf buf, MutableInt length, Calendar calParam) + public Time decodeTimeBinary( + final ReadableByteBuf buf, + final MutableInt length, + final Calendar calParam, + final Context context) throws SQLDataException { Calendar cal = calParam == null ? Calendar.getInstance() : calParam; long dayOfMonth = 0; @@ -329,7 +354,8 @@ public Time decodeTimeBinary(ReadableByteBuf buf, MutableInt length, Calendar ca } @Override - public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Calendar calParam) + public Timestamp decodeTimestampText( + final ReadableByteBuf buf, final MutableInt length, Calendar calParam, final Context context) throws SQLDataException { int[] parts = LocalTimeCodec.parseTime(buf, length, this); Timestamp t; @@ -379,7 +405,8 @@ public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Cal } @Override - public Timestamp decodeTimestampBinary(ReadableByteBuf buf, MutableInt length, Calendar calParam) + public Timestamp decodeTimestampBinary( + final ReadableByteBuf buf, final MutableInt length, Calendar calParam, final Context context) throws SQLDataException { Calendar cal = calParam == null ? Calendar.getInstance() : calParam; long microseconds = 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 64c3f6f9f..d563e4b1c 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/TimestampColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/TimestampColumn.java @@ -6,21 +6,27 @@ import static org.mariadb.jdbc.client.result.Result.NULL_LENGTH; import java.sql.*; +import java.text.SimpleDateFormat; import java.time.LocalDateTime; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.util.Calendar; +import java.util.Locale; +import java.util.TimeZone; import org.mariadb.jdbc.Configuration; import org.mariadb.jdbc.client.ColumnDecoder; +import org.mariadb.jdbc.client.Context; 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; /** Column metadata definition */ public class TimestampColumn extends ColumnDefinitionPacket implements ColumnDecoder { + private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + private static final DateTimeFormatter dateTimeFormatter = + DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); /** * TIMESTAMP metadata type decoder @@ -36,15 +42,15 @@ public class TimestampColumn extends ColumnDefinitionPacket implements ColumnDec * @param extTypeFormat extended type format */ public TimestampColumn( - ReadableByteBuf buf, - int charset, - long length, - DataType dataType, - byte decimals, - int flags, - int[] stringPos, - String extTypeName, - String extTypeFormat) { + final ReadableByteBuf buf, + final int charset, + final long length, + final DataType dataType, + final byte decimals, + final int flags, + final int[] stringPos, + final String extTypeName, + final String extTypeFormat) { super( buf, charset, @@ -67,39 +73,42 @@ public TimestampColumn useAliasAsName() { return new TimestampColumn(this); } - public String defaultClassname(Configuration conf) { + public String defaultClassname(final Configuration conf) { return Timestamp.class.getName(); } - public int getColumnType(Configuration conf) { + public int getColumnType(final Configuration conf) { return Types.TIMESTAMP; } - public String getColumnTypeName(Configuration conf) { + public String getColumnTypeName(final Configuration conf) { return dataType.name(); } @Override - public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, MutableInt length) + public Object getDefaultText( + final ReadableByteBuf buf, final MutableInt length, final Context context) throws SQLDataException { - return decodeTimestampText(buf, length, null); + return decodeTimestampText(buf, length, null, context); } @Override - public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, MutableInt length) + public Object getDefaultBinary( + final ReadableByteBuf buf, final MutableInt length, final Context context) throws SQLDataException { - return decodeTimestampBinary(buf, length, null); + return decodeTimestampBinary(buf, length, null, context); } @Override - public boolean decodeBooleanText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public boolean decodeBooleanText(final ReadableByteBuf buf, final 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, MutableInt length) + public boolean decodeBooleanBinary(final ReadableByteBuf buf, final MutableInt length) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException( @@ -107,401 +116,224 @@ public boolean decodeBooleanBinary(ReadableByteBuf buf, MutableInt length) } @Override - public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public byte decodeByteText(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public byte decodeByteBinary(final ReadableByteBuf buf, final 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, MutableInt length, Calendar cal) - throws SQLDataException { - return buf.readString(length.get()); - } - - @Override - public String decodeStringBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public String decodeStringText( + final ReadableByteBuf buf, + final MutableInt length, + final Calendar providedCal, + final Context context) throws SQLDataException { - if (length.get() == 0) { - StringBuilder zeroValue = new StringBuilder("0000-00-00 00:00:00"); - if (getDecimals() > 0) { - zeroValue.append("."); - for (int i = 0; i < getDecimals(); i++) zeroValue.append("0"); - } - return zeroValue.toString(); + if (length.get() == 0) return buildZeroDate(); + int initialLength = length.get(); + LocalDateTime ldt = parseText(buf, length); + if (ldt == null) { + if (initialLength > 0) return buildZeroDate(); + return null; } - int year = buf.readUnsignedShort(); - int month = buf.readByte(); - int day = buf.readByte(); - int hour = 0; - int minutes = 0; - int seconds = 0; - long microseconds = 0; - - if (length.get() > 4) { - hour = buf.readByte(); - minutes = buf.readByte(); - seconds = buf.readByte(); - - if (length.get() > 7) { - microseconds = buf.readUnsignedInt(); - } + LocalDateTime modifiedLdt = + localDateTimeToZoneDateTime(ldt, providedCal, context).toLocalDateTime(); + if (this.decimals > 0) { + return dateTimeFormatter.format(modifiedLdt) + + "." + + String.format(Locale.US, "%0" + this.decimals + "d", modifiedLdt.getNano() / 1000); } + return dateTimeFormatter.format(modifiedLdt); + } - // xpand workaround https://jira.mariadb.org/browse/XPT-274 - if (year == 0 && month == 0 && day == 0) { - return "0000-00-00 00:00:00"; + private String buildZeroDate() { + StringBuilder zeroValue = new StringBuilder("0000-00-00 00:00:00"); + if (this.decimals > 0) { + zeroValue.append("."); + for (int i = 0; i < this.decimals; i++) zeroValue.append("0"); } + return zeroValue.toString(); + } - LocalDateTime dateTime = - LocalDateTime.of(year, month, day, hour, minutes, seconds).plusNanos(microseconds * 1000); - - StringBuilder microSecPattern = new StringBuilder(); - if (getDecimals() > 0 || microseconds > 0) { - int decimal = getDecimals() & 0xff; - if (decimal == 0) decimal = 6; - microSecPattern.append("."); - for (int i = 0; i < decimal; i++) microSecPattern.append("S"); + @Override + public String decodeStringBinary( + final ReadableByteBuf buf, + final MutableInt length, + final Calendar providedCal, + final Context context) + throws SQLDataException { + if (length.get() == 0) return buildZeroDate(); + int initialLength = length.get(); + LocalDateTime ldt = parseBinary(buf, length); + if (ldt == null) { + if (initialLength > 0) return buildZeroDate(); + return null; } - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss" + microSecPattern); - return dateTime.toLocalDate().toString() + ' ' + dateTime.toLocalTime().format(formatter); + LocalDateTime modifiedLdt = + localDateTimeToZoneDateTime(ldt, providedCal, context).toLocalDateTime(); + if (this.decimals > 0) { + return dateTimeFormatter.format(modifiedLdt) + + "." + + String.format(Locale.US, "%0" + this.decimals + "d", modifiedLdt.getNano() / 1000); + } + return dateTimeFormatter.format(modifiedLdt); } @Override - public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public short decodeShortText(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public short decodeShortBinary(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public int decodeIntText(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public int decodeIntBinary(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public long decodeLongText(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public long decodeLongBinary(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public float decodeFloatText(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public float decodeFloatBinary(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public double decodeDoubleText(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public double decodeDoubleBinary(final ReadableByteBuf buf, final 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, MutableInt length, Calendar cal) + public Date decodeDateText( + final ReadableByteBuf buf, final MutableInt length, Calendar calParam, Context context) 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.get(); begin++) { - byte b = buf.readByte(); - if (b == '-' || b == ' ' || b == ':') { - partIdx++; - continue; - } - if (b == '.') { - partIdx++; - nanoBegin = begin; - continue; - } - if (b < '0' || b > '9') { - buf.pos(pos); - throw new SQLDataException( - String.format( - "value '%s' (%s) cannot be decoded as Timestamp", - buf.readString(length.get()), dataType)); - } - - timestampsPart[partIdx] = timestampsPart[partIdx] * 10 + b - 48; - } - if (timestampsPart[0] == 0 - && timestampsPart[1] == 0 - && timestampsPart[2] == 0 - && timestampsPart[3] == 0 - && 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.get() - nanoBegin - 1); begin++) { - timestampsPart[6] = timestampsPart[6] * 10; - } - } - - Timestamp timestamp; - if (cal == null) { - Calendar c = Calendar.getInstance(); - c.set( - timestampsPart[0], - timestampsPart[1] - 1, - timestampsPart[2], - timestampsPart[3], - timestampsPart[4], - timestampsPart[5]); - timestamp = new Timestamp(c.getTime().getTime()); - timestamp.setNanos(timestampsPart[6] * 1000); - } else { - synchronized (cal) { - cal.clear(); - cal.set( - timestampsPart[0], - timestampsPart[1] - 1, - timestampsPart[2], - timestampsPart[3], - timestampsPart[4], - timestampsPart[5]); - timestamp = new Timestamp(cal.getTime().getTime()); - timestamp.setNanos(timestampsPart[6] * 1000); - } - } - - String st = timestamp.toString(); - return Date.valueOf(st.substring(0, 10)); + LocalDateTime ldt = parseText(buf, length); + if (ldt == null) return null; + return new Date(localDateTimeToInstant(ldt, calParam, context) + ldt.getNano() / 1_000_000); } @Override - public Date decodeDateBinary(ReadableByteBuf buf, MutableInt length, Calendar calParam) + public Date decodeDateBinary( + final ReadableByteBuf buf, + final MutableInt length, + final Calendar calParam, + final Context context) throws SQLDataException { - if (length.get() == 0) { - length.set(NULL_LENGTH); - return null; - } - int year = buf.readUnsignedShort(); - int month = buf.readByte(); - int dayOfMonth = buf.readByte(); - int hour = 0; - int minutes = 0; - int seconds = 0; - long microseconds = 0; - - if (length.get() > 4) { - hour = buf.readByte(); - minutes = buf.readByte(); - seconds = buf.readByte(); - - 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 - && microseconds == 0) { - length.set(NULL_LENGTH); - return null; - } - - Timestamp timestamp; - if (calParam == null) { - Calendar cal = Calendar.getInstance(); - cal.clear(); - cal.set(year, month - 1, dayOfMonth, hour, minutes, seconds); - timestamp = new Timestamp(cal.getTimeInMillis()); - } else { - synchronized (calParam) { - calParam.clear(); - calParam.set(year, month - 1, dayOfMonth, hour, minutes, seconds); - timestamp = new Timestamp(calParam.getTimeInMillis()); - } - } - timestamp.setNanos((int) (microseconds * 1000)); - String st = timestamp.toString(); - return Date.valueOf(st.substring(0, 10)); + LocalDateTime ldt = parseBinary(buf, length); + if (ldt == null) return null; + return new Date(localDateTimeToInstant(ldt, calParam, context) + ldt.getNano() / 1_000_000); } @Override - public Time decodeTimeText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Time decodeTimeText( + final ReadableByteBuf buf, final MutableInt length, Calendar calParam, Context context) throws SQLDataException { - LocalDateTime lt = LocalDateTimeCodec.INSTANCE.decodeText(buf, length, this, cal); - if (lt == null) return null; - Calendar cc = cal == null ? Calendar.getInstance() : cal; - ZonedDateTime d = - TimeCodec.EPOCH_DATE.atTime(lt.toLocalTime()).atZone(cc.getTimeZone().toZoneId()); - return new Time(d.toEpochSecond() * 1000 + d.getNano() / 1_000_000); + LocalDateTime ldt = parseText(buf, length); + if (ldt == null) return null; + return new Time( + localDateTimeToInstant(ldt.withYear(1970).withMonth(1).withDayOfMonth(1), calParam, context) + + ldt.getNano() / 1_000_000); } @Override - public Time decodeTimeBinary(ReadableByteBuf buf, MutableInt length, Calendar calParam) + public Time decodeTimeBinary( + final ReadableByteBuf buf, final MutableInt length, Calendar calParam, Context context) throws SQLDataException { - if (length.get() == 0) { - length.set(NULL_LENGTH); - return null; - } - - int year = buf.readUnsignedShort(); - int month = buf.readByte(); - int dayOfMonth = buf.readByte(); - - int hour = 0; - int minutes = 0; - int seconds = 0; - long microseconds = 0; - - if (length.get() > 4) { - hour = buf.readByte(); - minutes = buf.readByte(); - seconds = buf.readByte(); - - 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; - } + LocalDateTime ldt = parseBinary(buf, length); + if (ldt == null) return null; + return new Time( + localDateTimeToInstant(ldt.withYear(1970).withMonth(1).withDayOfMonth(1), calParam, context) + + ldt.getNano() / 1_000_000); + } - if (calParam == null) { - Calendar cal = Calendar.getInstance(); - cal.clear(); - cal.set(1970, Calendar.JANUARY, 1, hour, minutes, seconds); - return new Time(cal.getTimeInMillis() + microseconds / 1_000); - } else { - synchronized (calParam) { - calParam.clear(); - calParam.set(1970, Calendar.JANUARY, 1, hour, minutes, seconds); - return new Time(calParam.getTimeInMillis() + microseconds / 1_000); - } - } + @Override + public Timestamp decodeTimestampText( + final ReadableByteBuf buf, final MutableInt length, Calendar calParam, final Context context) + throws SQLDataException { + LocalDateTime ldt = parseText(buf, length); + if (ldt == null) return null; + Timestamp res = new Timestamp(localDateTimeToInstant(ldt, calParam, context)); + res.setNanos(ldt.getNano()); + return res; } @Override - public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Calendar calParam) + public Timestamp decodeTimestampBinary( + final ReadableByteBuf buf, final MutableInt length, Calendar calParam, final Context context) 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.get(); begin++) { - byte b = buf.readByte(); - if (b == '-' || b == ' ' || b == ':') { - partIdx++; - continue; - } - if (b == '.') { - partIdx++; - nanoBegin = begin; - continue; - } - if (b < '0' || b > '9') { - buf.pos(pos); - throw new SQLDataException( - String.format( - "value '%s' (%s) cannot be decoded as Timestamp", - buf.readString(length.get()), dataType)); - } + LocalDateTime ldt = parseBinary(buf, length); + if (ldt == null) return null; + Timestamp res = new Timestamp(localDateTimeToInstant(ldt, calParam, context)); + res.setNanos(ldt.getNano()); + return res; + } - timestampsPart[partIdx] = timestampsPart[partIdx] * 10 + b - 48; - } - if (timestampsPart[0] == 0 - && timestampsPart[1] == 0 - && timestampsPart[2] == 0 - && timestampsPart[3] == 0 - && timestampsPart[4] == 0 - && timestampsPart[5] == 0 - && timestampsPart[6] == 0) { + private LocalDateTime parseText(final ReadableByteBuf buf, final MutableInt length) { + int[] parts = LocalDateTimeCodec.parseTimestamp(buf.readAscii(length.get())); + if (parts == null) { length.set(NULL_LENGTH); return null; } - - // fix non-leading tray for nanoseconds - if (nanoBegin > 0) { - for (int begin = 0; begin < 6 - (length.get() - nanoBegin - 1); begin++) { - timestampsPart[6] = timestampsPart[6] * 10; - } - } - - Timestamp timestamp; - if (calParam == null) { - Calendar c = Calendar.getInstance(); - c.set( - timestampsPart[0], - timestampsPart[1] - 1, - timestampsPart[2], - timestampsPart[3], - timestampsPart[4], - timestampsPart[5]); - timestamp = new Timestamp(c.getTime().getTime()); - timestamp.setNanos(timestampsPart[6] * 1000); - } else { - synchronized (calParam) { - calParam.clear(); - calParam.set( - timestampsPart[0], - timestampsPart[1] - 1, - timestampsPart[2], - timestampsPart[3], - timestampsPart[4], - timestampsPart[5]); - timestamp = new Timestamp(calParam.getTime().getTime()); - timestamp.setNanos(timestampsPart[6] * 1000); - } - } - return timestamp; + return LocalDateTime.of(parts[0], parts[1], parts[2], parts[3], parts[4], parts[5]) + .plusNanos(parts[6]); } - @Override - public Timestamp decodeTimestampBinary(ReadableByteBuf buf, MutableInt length, Calendar calParam) - throws SQLDataException { + private LocalDateTime parseBinary(final ReadableByteBuf buf, final MutableInt length) { if (length.get() == 0) { length.set(NULL_LENGTH); return null; @@ -536,20 +368,50 @@ public Timestamp decodeTimestampBinary(ReadableByteBuf buf, MutableInt length, C length.set(NULL_LENGTH); return null; } - Timestamp timestamp; + return LocalDateTime.of(year, month, dayOfMonth, hour, minutes, seconds) + .plusNanos(microseconds * 1000); + } + + public static long localDateTimeToInstant( + final LocalDateTime ldt, final Calendar calParam, final Context context) { + if (calParam == null) { + Calendar cal = context.getDefaultCalendar(); + cal.set( + ldt.getYear(), + ldt.getMonthValue() - 1, + ldt.getDayOfMonth(), + ldt.getHour(), + ldt.getMinute(), + ldt.getSecond()); + cal.set(Calendar.MILLISECOND, 0); + return cal.getTimeInMillis(); + } + synchronized (calParam) { + calParam.clear(); + calParam.set( + ldt.getYear(), + ldt.getMonthValue() - 1, + ldt.getDayOfMonth(), + ldt.getHour(), + ldt.getMinute(), + ldt.getSecond()); + return calParam.getTimeInMillis(); + } + } + + public static ZonedDateTime localDateTimeToZoneDateTime( + final LocalDateTime ldt, final Calendar calParam, final Context context) { if (calParam == null) { - Calendar cal = Calendar.getInstance(); - cal.clear(); - cal.set(year, month - 1, dayOfMonth, hour, minutes, seconds); - timestamp = new Timestamp(cal.getTimeInMillis()); - } else { - synchronized (calParam) { - calParam.clear(); - calParam.set(year, month - 1, dayOfMonth, hour, minutes, seconds); - timestamp = new Timestamp(calParam.getTimeInMillis()); + if (context.getConf().preserveInstants()) { + ZonedDateTime zdt = ldt.atZone(context.getConnectionTimeZone().toZoneId()); + ZonedDateTime zdt2 = + zdt.withZoneSameInstant( + (calParam == null ? TimeZone.getDefault() : calParam.getTimeZone()).toZoneId()); + LocalDateTime sss = zdt2.toLocalDateTime(); + return zdt2; } + return ldt.atZone(TimeZone.getDefault().toZoneId()); } - timestamp.setNanos((int) (microseconds * 1000)); - return timestamp; + return ldt.atZone(calParam.getTimeZone().toZoneId()); } } 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 4a8f453f6..47744ae6d 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/UnsignedBigIntColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/UnsignedBigIntColumn.java @@ -8,6 +8,7 @@ import java.util.Calendar; import org.mariadb.jdbc.Configuration; import org.mariadb.jdbc.client.ColumnDecoder; +import org.mariadb.jdbc.client.Context; import org.mariadb.jdbc.client.DataType; import org.mariadb.jdbc.client.ReadableByteBuf; import org.mariadb.jdbc.client.util.MutableInt; @@ -30,15 +31,15 @@ public class UnsignedBigIntColumn extends ColumnDefinitionPacket implements Colu * @param extTypeFormat extended type format */ public UnsignedBigIntColumn( - ReadableByteBuf buf, - int charset, - long length, - DataType dataType, - byte decimals, - int flags, - int[] stringPos, - String extTypeName, - String extTypeFormat) { + final ReadableByteBuf buf, + final int charset, + final long length, + final DataType dataType, + final byte decimals, + final int flags, + final int[] stringPos, + final String extTypeName, + final String extTypeFormat) { super( buf, charset, @@ -61,26 +62,28 @@ public UnsignedBigIntColumn useAliasAsName() { return new UnsignedBigIntColumn(this); } - public String defaultClassname(Configuration conf) { + public String defaultClassname(final Configuration conf) { return BigInteger.class.getName(); } - public int getColumnType(Configuration conf) { + public int getColumnType(final Configuration conf) { return Types.BIGINT; } - public String getColumnTypeName(Configuration conf) { + public String getColumnTypeName(final Configuration conf) { return "BIGINT UNSIGNED"; } @Override - public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, MutableInt length) + public Object getDefaultText( + final ReadableByteBuf buf, final MutableInt length, final Context context) throws SQLDataException { return new BigInteger(buf.readAscii(length.get())); } @Override - public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, MutableInt length) + public Object getDefaultBinary( + final ReadableByteBuf buf, final MutableInt length, final Context context) throws SQLDataException { // need BIG ENDIAN, so reverse order byte[] bb = new byte[8]; @@ -91,19 +94,21 @@ public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, Mu } @Override - public boolean decodeBooleanText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public boolean decodeBooleanText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { String s = buf.readAscii(length.get()); return !"0".equals(s); } @Override - public boolean decodeBooleanBinary(ReadableByteBuf buf, MutableInt length) + public boolean decodeBooleanBinary(final ReadableByteBuf buf, final MutableInt length) throws SQLDataException { return buf.readLong() != 0; } @Override - public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public byte decodeByteText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result = buf.atoull(length.get()); if ((byte) result != result || result < 0) { throw new SQLDataException("byte overflow"); @@ -112,7 +117,8 @@ public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDat } @Override - public byte decodeByteBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public byte decodeByteBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { // need BIG ENDIAN, so reverse order byte[] bb = new byte[8]; for (int i = 7; i >= 0; i--) { @@ -128,13 +134,15 @@ public byte decodeByteBinary(ReadableByteBuf buf, MutableInt length) throws SQLD } @Override - public String decodeStringText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public String decodeStringText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { return buf.readString(length.get()); } @Override - public String decodeStringBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public String decodeStringBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { // need BIG ENDIAN, so reverse order byte[] bb = new byte[8]; @@ -145,7 +153,8 @@ public String decodeStringBinary(ReadableByteBuf buf, MutableInt length, Calenda } @Override - public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public short decodeShortText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result = buf.atoull(length.get()); if ((short) result != result || result < 0) { throw new SQLDataException("Short overflow"); @@ -154,7 +163,8 @@ public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLD } @Override - public short decodeShortBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public short decodeShortBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result = buf.readLong(); if ((short) result != result || result < 0) { throw new SQLDataException("Short overflow"); @@ -163,7 +173,8 @@ public short decodeShortBinary(ReadableByteBuf buf, MutableInt length) throws SQ } @Override - public int decodeIntText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public int decodeIntText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result = buf.atoull(length.get()); int res = (int) result; if (res != result || result < 0) { @@ -173,7 +184,8 @@ public int decodeIntText(ReadableByteBuf buf, MutableInt length) throws SQLDataE } @Override - public int decodeIntBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public int decodeIntBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { // need BIG ENDIAN, so reverse order byte[] bb = new byte[8]; @@ -189,7 +201,8 @@ public int decodeIntBinary(ReadableByteBuf buf, MutableInt length) throws SQLDat } @Override - public long decodeLongText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public long decodeLongText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { if (length.get() < 10) return buf.atoull(length.get()); BigInteger val = new BigInteger(buf.readAscii(length.get())); try { @@ -200,7 +213,8 @@ public long decodeLongText(ReadableByteBuf buf, MutableInt length) throws SQLDat } @Override - public long decodeLongBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public long decodeLongBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { if ((buf.getByte(buf.pos() + 7) & 0x80) == 0) { return buf.readLong(); } else { @@ -219,12 +233,14 @@ public long decodeLongBinary(ReadableByteBuf buf, MutableInt length) throws SQLD } @Override - public float decodeFloatText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public float decodeFloatText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return Float.parseFloat(buf.readAscii(length.get())); } @Override - public float decodeFloatBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public float decodeFloatBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { // need BIG ENDIAN, so reverse order byte[] bb = new byte[8]; for (int i = 7; i >= 0; i--) { @@ -234,12 +250,14 @@ public float decodeFloatBinary(ReadableByteBuf buf, MutableInt length) throws SQ } @Override - public double decodeDoubleText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public double decodeDoubleText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return Double.parseDouble(buf.readAscii(length.get())); } @Override - public double decodeDoubleBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public double decodeDoubleBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { // need BIG ENDIAN, so reverse order byte[] bb = new byte[8]; for (int i = 7; i >= 0; i--) { @@ -249,35 +267,40 @@ public double decodeDoubleBinary(ReadableByteBuf buf, MutableInt length) throws } @Override - public Date decodeDateText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Date decodeDateText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Date decodeDateBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Date decodeDateBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Time decodeTimeText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Time decodeTimeText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Time decodeTimeBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Time decodeTimeBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Timestamp decodeTimestampText( + final ReadableByteBuf buf, final MutableInt length, Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException( @@ -285,7 +308,8 @@ public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Cal } @Override - public Timestamp decodeTimestampBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Timestamp decodeTimestampBinary( + final ReadableByteBuf buf, final MutableInt length, Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException( 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 1b94bfe94..74bb95c08 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/UnsignedIntColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/UnsignedIntColumn.java @@ -7,6 +7,7 @@ import java.util.Calendar; import org.mariadb.jdbc.Configuration; import org.mariadb.jdbc.client.ColumnDecoder; +import org.mariadb.jdbc.client.Context; import org.mariadb.jdbc.client.DataType; import org.mariadb.jdbc.client.ReadableByteBuf; import org.mariadb.jdbc.client.util.MutableInt; @@ -29,15 +30,15 @@ public class UnsignedIntColumn extends ColumnDefinitionPacket implements ColumnD * @param extTypeFormat extended type format */ public UnsignedIntColumn( - ReadableByteBuf buf, - int charset, - long length, - DataType dataType, - byte decimals, - int flags, - int[] stringPos, - String extTypeName, - String extTypeFormat) { + final ReadableByteBuf buf, + final int charset, + final long length, + final DataType dataType, + final byte decimals, + final int flags, + final int[] stringPos, + final String extTypeName, + final String extTypeFormat) { super( buf, charset, @@ -60,44 +61,48 @@ public UnsignedIntColumn useAliasAsName() { return new UnsignedIntColumn(this); } - public String defaultClassname(Configuration conf) { + public String defaultClassname(final Configuration conf) { return Long.class.getName(); } - public int getColumnType(Configuration conf) { + public int getColumnType(final Configuration conf) { return Types.BIGINT; } - public String getColumnTypeName(Configuration conf) { + public String getColumnTypeName(final Configuration conf) { return "INTEGER UNSIGNED"; } @Override - public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, MutableInt length) + public Object getDefaultText( + final ReadableByteBuf buf, final MutableInt length, final Context context) throws SQLDataException { return buf.atoull(length.get()); } @Override - public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, MutableInt length) + public Object getDefaultBinary( + final ReadableByteBuf buf, final MutableInt length, final Context context) throws SQLDataException { return buf.readUnsignedInt(); } @Override - public boolean decodeBooleanText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public boolean decodeBooleanText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { String s = buf.readAscii(length.get()); return !"0".equals(s); } @Override - public boolean decodeBooleanBinary(ReadableByteBuf buf, MutableInt length) + public boolean decodeBooleanBinary(final ReadableByteBuf buf, final MutableInt length) throws SQLDataException { return buf.readInt() != 0; } @Override - public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public byte decodeByteText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result = buf.atoull(length.get()); if ((byte) result != result || (result < 0 && !isSigned())) { throw new SQLDataException("byte overflow"); @@ -106,7 +111,8 @@ public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDat } @Override - public byte decodeByteBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public byte decodeByteBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result = buf.readUnsignedInt(); if ((byte) result != result) { throw new SQLDataException("byte overflow"); @@ -116,19 +122,22 @@ public byte decodeByteBinary(ReadableByteBuf buf, MutableInt length) throws SQLD } @Override - public String decodeStringText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public String decodeStringText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { return buf.readString(length.get()); } @Override - public String decodeStringBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public String decodeStringBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { return String.valueOf(buf.readUnsignedInt()); } @Override - public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public short decodeShortText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result = buf.atoll(length.get()); if ((short) result != result || result < 0) { throw new SQLDataException("Short overflow"); @@ -137,7 +146,8 @@ public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLD } @Override - public short decodeShortBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public short decodeShortBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result = buf.readUnsignedInt(); if ((short) result != result || result < 0) { throw new SQLDataException("Short overflow"); @@ -146,7 +156,8 @@ public short decodeShortBinary(ReadableByteBuf buf, MutableInt length) throws SQ } @Override - public int decodeIntText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public int decodeIntText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result = buf.atoull(length.get()); int res = (int) result; if (res != result || result < 0) { @@ -156,7 +167,8 @@ public int decodeIntText(ReadableByteBuf buf, MutableInt length) throws SQLDataE } @Override - public int decodeIntBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public int decodeIntBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result = buf.readUnsignedInt(); int res = (int) result; if (res != result) { @@ -167,65 +179,76 @@ public int decodeIntBinary(ReadableByteBuf buf, MutableInt length) throws SQLDat } @Override - public long decodeLongText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public long decodeLongText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return buf.atoull(length.get()); } @Override - public long decodeLongBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public long decodeLongBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return buf.readUnsignedInt(); } @Override - public float decodeFloatText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public float decodeFloatText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return Float.parseFloat(buf.readAscii(length.get())); } @Override - public float decodeFloatBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public float decodeFloatBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return (float) buf.readUnsignedInt(); } @Override - public double decodeDoubleText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public double decodeDoubleText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return Double.parseDouble(buf.readAscii(length.get())); } @Override - public double decodeDoubleBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public double decodeDoubleBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return buf.readUnsignedInt(); } @Override - public Date decodeDateText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Date decodeDateText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Date decodeDateBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Date decodeDateBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Time decodeTimeText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Time decodeTimeText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Time decodeTimeBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Time decodeTimeBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Timestamp decodeTimestampText( + final ReadableByteBuf buf, final MutableInt length, Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException( @@ -233,7 +256,8 @@ public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Cal } @Override - public Timestamp decodeTimestampBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Timestamp decodeTimestampBinary( + final ReadableByteBuf buf, final MutableInt length, Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException( 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 101aa0526..1529cf813 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/UnsignedMediumIntColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/UnsignedMediumIntColumn.java @@ -7,6 +7,7 @@ import java.util.Calendar; import org.mariadb.jdbc.Configuration; import org.mariadb.jdbc.client.ColumnDecoder; +import org.mariadb.jdbc.client.Context; import org.mariadb.jdbc.client.DataType; import org.mariadb.jdbc.client.ReadableByteBuf; import org.mariadb.jdbc.client.util.MutableInt; @@ -29,15 +30,15 @@ public class UnsignedMediumIntColumn extends ColumnDefinitionPacket implements C * @param extTypeFormat extended type format */ public UnsignedMediumIntColumn( - ReadableByteBuf buf, - int charset, - long length, - DataType dataType, - byte decimals, - int flags, - int[] stringPos, - String extTypeName, - String extTypeFormat) { + final ReadableByteBuf buf, + final int charset, + final long length, + final DataType dataType, + final byte decimals, + final int flags, + final int[] stringPos, + final String extTypeName, + final String extTypeFormat) { super( buf, charset, @@ -60,44 +61,48 @@ public UnsignedMediumIntColumn useAliasAsName() { return new UnsignedMediumIntColumn(this); } - public String defaultClassname(Configuration conf) { + public String defaultClassname(final Configuration conf) { return Integer.class.getName(); } - public int getColumnType(Configuration conf) { + public int getColumnType(final Configuration conf) { return Types.INTEGER; } - public String getColumnTypeName(Configuration conf) { + public String getColumnTypeName(final Configuration conf) { return "MEDIUMINT UNSIGNED"; } @Override - public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, MutableInt length) + public Object getDefaultText( + final ReadableByteBuf buf, final MutableInt length, final Context context) throws SQLDataException { return decodeIntText(buf, length); } @Override - public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, MutableInt length) + public Object getDefaultBinary( + final ReadableByteBuf buf, final MutableInt length, final Context context) throws SQLDataException { return decodeIntBinary(buf, length); } @Override - public boolean decodeBooleanText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public boolean decodeBooleanText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { String s = buf.readAscii(length.get()); return !"0".equals(s); } @Override - public boolean decodeBooleanBinary(ReadableByteBuf buf, MutableInt length) + public boolean decodeBooleanBinary(final ReadableByteBuf buf, final MutableInt length) throws SQLDataException { return buf.readInt() != 0; } @Override - public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public byte decodeByteText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result = buf.atoull(length.get()); if ((byte) result != result || result < 0) { throw new SQLDataException("byte overflow"); @@ -106,7 +111,8 @@ public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDat } @Override - public byte decodeByteBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public byte decodeByteBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result = buf.readUnsignedMedium(); buf.skip(); // MEDIUMINT is encoded on 4 bytes in exchanges ! if ((byte) result != result) { @@ -117,13 +123,15 @@ public byte decodeByteBinary(ReadableByteBuf buf, MutableInt length) throws SQLD } @Override - public String decodeStringText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public String decodeStringText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { return buf.readString(length.get()); } @Override - public String decodeStringBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public String decodeStringBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { String mediumStr = String.valueOf(buf.readUnsignedMedium()); buf.skip(); // MEDIUMINT is encoded on 4 bytes in exchanges ! @@ -131,7 +139,8 @@ public String decodeStringBinary(ReadableByteBuf buf, MutableInt length, Calenda } @Override - public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public short decodeShortText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result = buf.atoull(length.get()); if ((short) result != result || result < 0) { throw new SQLDataException("Short overflow"); @@ -140,7 +149,8 @@ public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLD } @Override - public short decodeShortBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public short decodeShortBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result = buf.readUnsignedMedium(); buf.skip(); // MEDIUMINT is encoded on 4 bytes in exchanges ! if ((short) result != result || result < 0) { @@ -150,83 +160,96 @@ public short decodeShortBinary(ReadableByteBuf buf, MutableInt length) throws SQ } @Override - public int decodeIntText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public int decodeIntText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return (int) buf.atoll(length.get()); } @Override - public int decodeIntBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public int decodeIntBinary(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public long decodeLongText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return buf.atoull(length.get()); } @Override - public long decodeLongBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public long decodeLongBinary(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public float decodeFloatText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return Float.parseFloat(buf.readAscii(length.get())); } @Override - public float decodeFloatBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public float decodeFloatBinary(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public double decodeDoubleText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return Double.parseDouble(buf.readAscii(length.get())); } @Override - public double decodeDoubleBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public double decodeDoubleBinary(final ReadableByteBuf buf, final 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, MutableInt length, Calendar cal) + public Date decodeDateText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Date decodeDateBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Date decodeDateBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Time decodeTimeText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Time decodeTimeText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Time decodeTimeBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Time decodeTimeBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Timestamp decodeTimestampText( + final ReadableByteBuf buf, final MutableInt length, Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException( @@ -234,7 +257,8 @@ public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Cal } @Override - public Timestamp decodeTimestampBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Timestamp decodeTimestampBinary( + final ReadableByteBuf buf, final MutableInt length, Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException( 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 05ba1d457..e80a54f33 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/UnsignedSmallIntColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/UnsignedSmallIntColumn.java @@ -7,6 +7,7 @@ import java.util.Calendar; import org.mariadb.jdbc.Configuration; import org.mariadb.jdbc.client.ColumnDecoder; +import org.mariadb.jdbc.client.Context; import org.mariadb.jdbc.client.DataType; import org.mariadb.jdbc.client.ReadableByteBuf; import org.mariadb.jdbc.client.util.MutableInt; @@ -29,15 +30,15 @@ public class UnsignedSmallIntColumn extends ColumnDefinitionPacket implements Co * @param extTypeFormat extended type format */ public UnsignedSmallIntColumn( - ReadableByteBuf buf, - int charset, - long length, - DataType dataType, - byte decimals, - int flags, - int[] stringPos, - String extTypeName, - String extTypeFormat) { + final ReadableByteBuf buf, + final int charset, + final long length, + final DataType dataType, + final byte decimals, + final int flags, + final int[] stringPos, + final String extTypeName, + final String extTypeFormat) { super( buf, charset, @@ -60,44 +61,48 @@ public UnsignedSmallIntColumn useAliasAsName() { return new UnsignedSmallIntColumn(this); } - public String defaultClassname(Configuration conf) { + public String defaultClassname(final Configuration conf) { return Integer.class.getName(); } - public int getColumnType(Configuration conf) { + public int getColumnType(final Configuration conf) { return Types.INTEGER; } - public String getColumnTypeName(Configuration conf) { + public String getColumnTypeName(final Configuration conf) { return "SMALLINT UNSIGNED"; } @Override - public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, MutableInt length) + public Object getDefaultText( + final ReadableByteBuf buf, final MutableInt length, final Context context) throws SQLDataException { return (int) buf.atoull(length.get()); } @Override - public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, MutableInt length) + public Object getDefaultBinary( + final ReadableByteBuf buf, final MutableInt length, final Context context) throws SQLDataException { return buf.readUnsignedShort(); } @Override - public boolean decodeBooleanText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public boolean decodeBooleanText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { String s = buf.readAscii(length.get()); return !"0".equals(s); } @Override - public boolean decodeBooleanBinary(ReadableByteBuf buf, MutableInt length) + public boolean decodeBooleanBinary(final ReadableByteBuf buf, final MutableInt length) throws SQLDataException { return buf.readShort() != 0; } @Override - public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public byte decodeByteText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result = buf.atoull(length.get()); if ((byte) result != result || result < 0) { throw new SQLDataException("byte overflow"); @@ -106,7 +111,8 @@ public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDat } @Override - public byte decodeByteBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public byte decodeByteBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result = buf.readUnsignedShort(); if ((byte) result != result) { throw new SQLDataException("byte overflow"); @@ -115,19 +121,22 @@ public byte decodeByteBinary(ReadableByteBuf buf, MutableInt length) throws SQLD } @Override - public String decodeStringText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public String decodeStringText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { return buf.readString(length.get()); } @Override - public String decodeStringBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public String decodeStringBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { return String.valueOf(buf.readUnsignedShort()); } @Override - public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public short decodeShortText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result = buf.atoull(length.get()); if ((short) result != result) { throw new SQLDataException("Short overflow"); @@ -136,7 +145,8 @@ public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLD } @Override - public short decodeShortBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public short decodeShortBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { int result = buf.readUnsignedShort(); if ((short) result != result) { throw new SQLDataException("Short overflow"); @@ -145,75 +155,88 @@ public short decodeShortBinary(ReadableByteBuf buf, MutableInt length) throws SQ } @Override - public int decodeIntText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public int decodeIntText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return (int) buf.atoull(length.get()); } @Override - public int decodeIntBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public int decodeIntBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return buf.readUnsignedShort(); } @Override - public long decodeLongText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public long decodeLongText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return buf.atoull(length.get()); } @Override - public long decodeLongBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public long decodeLongBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return buf.readUnsignedShort(); } @Override - public float decodeFloatText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public float decodeFloatText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return Float.parseFloat(buf.readAscii(length.get())); } @Override - public float decodeFloatBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public float decodeFloatBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return (float) buf.readUnsignedShort(); } @Override - public double decodeDoubleText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public double decodeDoubleText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return Double.parseDouble(buf.readAscii(length.get())); } @Override - public double decodeDoubleBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public double decodeDoubleBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return buf.readUnsignedShort(); } @Override - public Date decodeDateText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Date decodeDateText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Date decodeDateBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Date decodeDateBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Time decodeTimeText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Time decodeTimeText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Time decodeTimeBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Time decodeTimeBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Timestamp decodeTimestampText( + final ReadableByteBuf buf, final MutableInt length, Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException( @@ -221,7 +244,8 @@ public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Cal } @Override - public Timestamp decodeTimestampBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Timestamp decodeTimestampBinary( + final ReadableByteBuf buf, final MutableInt length, Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException( 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 5a8474858..8fc4cfa81 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/UnsignedTinyIntColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/UnsignedTinyIntColumn.java @@ -7,6 +7,7 @@ import java.util.Calendar; import org.mariadb.jdbc.Configuration; import org.mariadb.jdbc.client.ColumnDecoder; +import org.mariadb.jdbc.client.Context; import org.mariadb.jdbc.client.DataType; import org.mariadb.jdbc.client.ReadableByteBuf; import org.mariadb.jdbc.client.util.MutableInt; @@ -29,15 +30,15 @@ public class UnsignedTinyIntColumn extends ColumnDefinitionPacket implements Col * @param extTypeFormat extended type format */ public UnsignedTinyIntColumn( - ReadableByteBuf buf, - int charset, - long length, - DataType dataType, - byte decimals, - int flags, - int[] stringPos, - String extTypeName, - String extTypeFormat) { + final ReadableByteBuf buf, + final int charset, + final long length, + final DataType dataType, + final byte decimals, + final int flags, + final int[] stringPos, + final String extTypeName, + final String extTypeFormat) { super( buf, charset, @@ -60,19 +61,19 @@ public UnsignedTinyIntColumn useAliasAsName() { return new UnsignedTinyIntColumn(this); } - public String defaultClassname(Configuration conf) { + public String defaultClassname(final Configuration conf) { if (conf.tinyInt1isBit() && columnLength == 1) return Boolean.class.getName(); return Integer.class.getName(); } - public int getColumnType(Configuration conf) { + public int getColumnType(final Configuration conf) { if (conf.tinyInt1isBit() && columnLength == 1) { return conf.transformedBitIsBoolean() ? Types.BOOLEAN : Types.BIT; } return Types.SMALLINT; } - public String getColumnTypeName(Configuration conf) { + public String getColumnTypeName(final Configuration conf) { if (conf.tinyInt1isBit() && columnLength == 1) { return conf.transformedBitIsBoolean() ? "BOOLEAN" : "BIT"; } @@ -80,37 +81,41 @@ public String getColumnTypeName(Configuration conf) { } @Override - public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, MutableInt length) + public Object getDefaultText( + final ReadableByteBuf buf, final MutableInt length, final Context context) throws SQLDataException { - if (conf.tinyInt1isBit() && columnLength == 1) { + if (context.getConf().tinyInt1isBit() && columnLength == 1) { return decodeBooleanText(buf, length); } return (int) buf.atoull(length.get()); } @Override - public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, MutableInt length) + public Object getDefaultBinary( + final ReadableByteBuf buf, final MutableInt length, final Context context) throws SQLDataException { - if (conf.tinyInt1isBit() && columnLength == 1) { + if (context.getConf().tinyInt1isBit() && columnLength == 1) { return decodeBooleanBinary(buf, length); } return (int) buf.readUnsignedByte(); } @Override - public boolean decodeBooleanText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public boolean decodeBooleanText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { String s = buf.readAscii(length.get()); return !"0".equals(s); } @Override - public boolean decodeBooleanBinary(ReadableByteBuf buf, MutableInt length) + public boolean decodeBooleanBinary(final ReadableByteBuf buf, final MutableInt length) throws SQLDataException { return buf.readByte() != 0; } @Override - public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public byte decodeByteText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result = buf.atoull(length.get()); if ((byte) result != result) { throw new SQLDataException("byte overflow"); @@ -119,7 +124,8 @@ public byte decodeByteText(ReadableByteBuf buf, MutableInt length) throws SQLDat } @Override - public byte decodeByteBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public byte decodeByteBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { long result = buf.readUnsignedByte(); if ((byte) result != result) { throw new SQLDataException("byte overflow"); @@ -128,97 +134,114 @@ public byte decodeByteBinary(ReadableByteBuf buf, MutableInt length) throws SQLD } @Override - public String decodeStringText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public String decodeStringText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { return buf.readString(length.get()); } @Override - public String decodeStringBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public String decodeStringBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { return String.valueOf(buf.readUnsignedByte()); } @Override - public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public short decodeShortText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return (short) buf.atoull(length.get()); } @Override - public short decodeShortBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public short decodeShortBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return buf.readUnsignedByte(); } @Override - public int decodeIntText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public int decodeIntText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return (int) buf.atoull(length.get()); } @Override - public int decodeIntBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public int decodeIntBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return buf.readUnsignedByte(); } @Override - public long decodeLongText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public long decodeLongText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return buf.atoull(length.get()); } @Override - public long decodeLongBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public long decodeLongBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return buf.readUnsignedByte(); } @Override - public float decodeFloatText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public float decodeFloatText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return Float.parseFloat(buf.readAscii(length.get())); } @Override - public float decodeFloatBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public float decodeFloatBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return buf.readUnsignedByte(); } @Override - public double decodeDoubleText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public double decodeDoubleText(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return Double.parseDouble(buf.readAscii(length.get())); } @Override - public double decodeDoubleBinary(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public double decodeDoubleBinary(final ReadableByteBuf buf, final MutableInt length) + throws SQLDataException { return buf.readUnsignedByte(); } @Override - public Date decodeDateText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Date decodeDateText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Date decodeDateBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Date decodeDateBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Date", dataType)); } @Override - public Time decodeTimeText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Time decodeTimeText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Time decodeTimeBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Time decodeTimeBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException(String.format("Data type %s cannot be decoded as Time", dataType)); } @Override - public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Timestamp decodeTimestampText( + final ReadableByteBuf buf, final MutableInt length, Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException( @@ -226,7 +249,8 @@ public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Cal } @Override - public Timestamp decodeTimestampBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Timestamp decodeTimestampBinary( + final ReadableByteBuf buf, final MutableInt length, Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException( 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 9bc72bd95..aed7ceea6 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/UuidColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/UuidColumn.java @@ -8,6 +8,7 @@ import java.util.UUID; import org.mariadb.jdbc.Configuration; import org.mariadb.jdbc.client.ColumnDecoder; +import org.mariadb.jdbc.client.Context; import org.mariadb.jdbc.client.DataType; import org.mariadb.jdbc.client.ReadableByteBuf; import org.mariadb.jdbc.client.util.MutableInt; @@ -31,15 +32,15 @@ public class UuidColumn extends ColumnDefinitionPacket implements ColumnDecoder * @param extTypeFormat extended type format */ public UuidColumn( - ReadableByteBuf buf, - int charset, - long length, - DataType dataType, - byte decimals, - int flags, - int[] stringPos, - String extTypeName, - String extTypeFormat) { + final ReadableByteBuf buf, + final int charset, + final long length, + final DataType dataType, + final byte decimals, + final int flags, + final int[] stringPos, + final String extTypeName, + final String extTypeFormat) { super( buf, charset, @@ -62,15 +63,15 @@ public UuidColumn useAliasAsName() { return new UuidColumn(this); } - public String defaultClassname(Configuration conf) { + public String defaultClassname(final Configuration conf) { return conf.uuidAsString() ? String.class.getName() : UUID.class.getName(); } - public int getColumnType(Configuration conf) { + public int getColumnType(final Configuration conf) { return conf.uuidAsString() ? Types.CHAR : Types.OTHER; } - public String getColumnTypeName(Configuration conf) { + public String getColumnTypeName(final Configuration conf) { return "uuid"; } @@ -83,155 +84,184 @@ public int getPrecision() { } @Override - public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, MutableInt length) + public Object getDefaultText( + final ReadableByteBuf buf, final MutableInt length, final Context context) throws SQLDataException { - return conf.uuidAsString() + return context.getConf().uuidAsString() ? buf.readString(length.get()) : UUID.fromString(buf.readAscii(length.get())); } @Override - public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, MutableInt length) + public Object getDefaultBinary( + final ReadableByteBuf buf, final MutableInt length, final Context context) throws SQLDataException { - return conf.uuidAsString() + return context.getConf().uuidAsString() ? buf.readString(length.get()) : UUID.fromString(buf.readAscii(length.get())); } @Override - public boolean decodeBooleanText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public boolean decodeBooleanText(final ReadableByteBuf buf, final 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, MutableInt length) + public boolean decodeBooleanBinary(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public byte decodeByteText(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public byte decodeByteBinary(final ReadableByteBuf buf, final 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, MutableInt length, Calendar cal) + public String decodeStringText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { return buf.readString(length.get()); } @Override - public String decodeStringBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public String decodeStringBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { return buf.readString(length.get()); } @Override - public short decodeShortText(ReadableByteBuf buf, MutableInt length) throws SQLDataException { + public short decodeShortText(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public short decodeShortBinary(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public int decodeIntText(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public int decodeIntBinary(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public long decodeLongText(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public long decodeLongBinary(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public float decodeFloatText(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public float decodeFloatBinary(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public double decodeDoubleText(final ReadableByteBuf buf, final 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, MutableInt length) throws SQLDataException { + public double decodeDoubleBinary(final ReadableByteBuf buf, final 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, MutableInt length, Calendar cal) + public Date decodeDateText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException("Data type UUID cannot be decoded as Date"); } @Override - public Date decodeDateBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Date decodeDateBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException("Data type UUID cannot be decoded as Date"); } @Override - public Time decodeTimeText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Time decodeTimeText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException("Data type UUID cannot be decoded as Time"); } @Override - public Time decodeTimeBinary(ReadableByteBuf buf, MutableInt length, Calendar calParam) + public Time decodeTimeBinary( + final ReadableByteBuf buf, + final MutableInt length, + final Calendar calParam, + final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException("Data type UUID cannot be decoded as Time"); } @Override - public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Calendar calParam) + public Timestamp decodeTimestampText( + final ReadableByteBuf buf, + final MutableInt length, + final Calendar calParam, + final Context context) throws SQLDataException { buf.skip(length.get()); throw new SQLDataException("Data type UUID cannot be decoded as Timestamp"); } @Override - public Timestamp decodeTimestampBinary(ReadableByteBuf buf, MutableInt length, Calendar calParam) + public Timestamp decodeTimestampBinary( + final ReadableByteBuf buf, final MutableInt length, Calendar calParam, final Context context) throws SQLDataException { 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 a5509e66d..2d73a8c6a 100644 --- a/src/main/java/org/mariadb/jdbc/client/column/YearColumn.java +++ b/src/main/java/org/mariadb/jdbc/client/column/YearColumn.java @@ -6,6 +6,7 @@ import java.sql.*; import java.util.Calendar; import org.mariadb.jdbc.Configuration; +import org.mariadb.jdbc.client.Context; import org.mariadb.jdbc.client.DataType; import org.mariadb.jdbc.client.ReadableByteBuf; import org.mariadb.jdbc.client.util.MutableInt; @@ -27,15 +28,15 @@ public class YearColumn extends UnsignedSmallIntColumn { * @param extTypeFormat extended type format */ public YearColumn( - ReadableByteBuf buf, - int charset, - long length, - DataType dataType, - byte decimals, - int flags, - int[] stringPos, - String extTypeName, - String extTypeFormat) { + final ReadableByteBuf buf, + final int charset, + final long length, + final DataType dataType, + final byte decimals, + final int flags, + final int[] stringPos, + final String extTypeName, + final String extTypeFormat) { super(buf, charset, length, dataType, decimals, flags, stringPos, extTypeName, extTypeFormat); } @@ -48,22 +49,23 @@ public YearColumn useAliasAsName() { return new YearColumn(this); } - public String defaultClassname(Configuration conf) { + public String defaultClassname(final Configuration conf) { return conf.yearIsDateType() ? Date.class.getName() : Short.class.getName(); } - public int getColumnType(Configuration conf) { + public int getColumnType(final Configuration conf) { return conf.yearIsDateType() ? Types.DATE : Types.SMALLINT; } - public String getColumnTypeName(Configuration conf) { + public String getColumnTypeName(final Configuration conf) { return "YEAR"; } @Override - public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, MutableInt length) + public Object getDefaultText( + final ReadableByteBuf buf, final MutableInt length, final Context context) throws SQLDataException { - if (conf.yearIsDateType()) { + if (context.getConf().yearIsDateType()) { short y = (short) buf.atoull(length.get()); if (columnLength == 2) { // YEAR(2) - deprecated @@ -79,9 +81,10 @@ public Object getDefaultText(final Configuration conf, ReadableByteBuf buf, Muta } @Override - public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, MutableInt length) + public Object getDefaultBinary( + final ReadableByteBuf buf, final MutableInt length, final Context context) throws SQLDataException { - if (conf.yearIsDateType()) { + if (context.getConf().yearIsDateType()) { int v = buf.readShort(); if (columnLength == 2) { // YEAR(2) - deprecated @@ -97,7 +100,8 @@ public Object getDefaultBinary(final Configuration conf, ReadableByteBuf buf, Mu } @Override - public Date decodeDateText(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Date decodeDateText( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { short y = (short) buf.atoll(length.get()); if (columnLength == 2) { @@ -112,7 +116,8 @@ public Date decodeDateText(ReadableByteBuf buf, MutableInt length, Calendar cal) } @Override - public Date decodeDateBinary(ReadableByteBuf buf, MutableInt length, Calendar cal) + public Date decodeDateBinary( + final ReadableByteBuf buf, final MutableInt length, final Calendar cal, final Context context) throws SQLDataException { int v = buf.readShort(); @@ -128,13 +133,14 @@ public Date decodeDateBinary(ReadableByteBuf buf, MutableInt length, Calendar ca } @Override - public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Calendar calParam) + public Timestamp decodeTimestampText( + final ReadableByteBuf buf, final MutableInt length, Calendar calParam, final Context context) throws SQLDataException { int year = Integer.parseInt(buf.readAscii(length.get())); if (columnLength <= 2) year += year >= 70 ? 1900 : 2000; if (calParam == null) { - Calendar cal1 = Calendar.getInstance(); + Calendar cal1 = context.getDefaultCalendar(); cal1.clear(); cal1.set(year, Calendar.JANUARY, 1); return new Timestamp(cal1.getTimeInMillis()); @@ -148,14 +154,15 @@ public Timestamp decodeTimestampText(ReadableByteBuf buf, MutableInt length, Cal } @Override - public Timestamp decodeTimestampBinary(ReadableByteBuf buf, MutableInt length, Calendar calParam) + public Timestamp decodeTimestampBinary( + final ReadableByteBuf buf, final MutableInt length, Calendar calParam, final Context context) throws SQLDataException { int year = buf.readUnsignedShort(); if (columnLength <= 2) year += year >= 70 ? 1900 : 2000; Timestamp timestamp; if (calParam == null) { - Calendar cal = Calendar.getInstance(); + Calendar cal = context.getDefaultCalendar(); cal.clear(); cal.set(year, 0, 1, 0, 0, 0); timestamp = new Timestamp(cal.getTimeInMillis()); diff --git a/src/main/java/org/mariadb/jdbc/client/context/BaseContext.java b/src/main/java/org/mariadb/jdbc/client/context/BaseContext.java index 16971d095..ed0f0fc1d 100644 --- a/src/main/java/org/mariadb/jdbc/client/context/BaseContext.java +++ b/src/main/java/org/mariadb/jdbc/client/context/BaseContext.java @@ -5,6 +5,8 @@ import static org.mariadb.jdbc.util.constants.Capabilities.STMT_BULK_OPERATIONS; +import java.util.Calendar; +import java.util.TimeZone; import java.util.function.Function; import org.mariadb.jdbc.Configuration; import org.mariadb.jdbc.HostAddress; @@ -54,6 +56,8 @@ public class BaseContext implements Context { private String redirectUrl = null; + private TimeZone connectionTimeZone = null; + /** * Constructor of connection context * @@ -231,4 +235,18 @@ public boolean canUseTransactionIsolation() { public void setRedirectUrl(String redirectUrl) { this.redirectUrl = redirectUrl; } + + public TimeZone getConnectionTimeZone() { + return connectionTimeZone; + } + + public void setConnectionTimeZone(TimeZone connectionTimeZone) { + this.connectionTimeZone = connectionTimeZone; + } + + public Calendar getDefaultCalendar() { + return conf.preserveInstants() + ? Calendar.getInstance(connectionTimeZone) + : Calendar.getInstance(); + } } 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 bc978e983..aeb73bbc2 100644 --- a/src/main/java/org/mariadb/jdbc/client/impl/StandardClient.java +++ b/src/main/java/org/mariadb/jdbc/client/impl/StandardClient.java @@ -568,43 +568,57 @@ protected void destroySocket() { * * @throws SQLException if any socket error. */ - private String handleTimezone() throws SQLException { - if (!"disable".equalsIgnoreCase(conf.timezone())) { - String timeZone = null; - try { - Result res = - (Result) - execute(new QueryPacket("SELECT @@time_zone, @@system_time_zone"), true).get(0); - res.next(); - timeZone = res.getString(1); - if ("SYSTEM".equals(timeZone)) { - timeZone = res.getString(2); - } - } catch (SQLException sqle) { - Result res = - (Result) - execute( - new QueryPacket( - "SHOW VARIABLES WHERE Variable_name in (" - + "'system_time_zone'," - + "'time_zone')"), - true) - .get(0); - String systemTimeZone = null; - while (res.next()) { - if ("system_time_zone".equals(res.getString(1))) { - systemTimeZone = res.getString(2); - } else { - timeZone = res.getString(2); + private void handleTimezone() throws SQLException { + if (conf.connectionTimeZone() == null || "LOCAL".equalsIgnoreCase(conf.connectionTimeZone())) { + context.setConnectionTimeZone(TimeZone.getDefault()); + } else { + String zoneId = conf.connectionTimeZone(); + if ("SERVER".equalsIgnoreCase(zoneId)) { + try { + Result res = + (Result) + execute(new QueryPacket("SELECT @@time_zone, @@system_time_zone"), true).get(0); + res.next(); + zoneId = res.getString(1); + if ("SYSTEM".equals(zoneId)) { + zoneId = res.getString(2); + } + } catch (SQLException sqle) { + Result res = + (Result) + execute( + new QueryPacket( + "SHOW VARIABLES WHERE Variable_name in (" + + "'system_time_zone'," + + "'time_zone')"), + true) + .get(0); + String systemTimeZone = null; + while (res.next()) { + if ("system_time_zone".equals(res.getString(1))) { + systemTimeZone = res.getString(2); + } else { + zoneId = res.getString(2); + } + } + if ("SYSTEM".equals(zoneId)) { + zoneId = systemTimeZone; } } - if ("SYSTEM".equals(timeZone)) { - timeZone = systemTimeZone; + } + + try { + context.setConnectionTimeZone(TimeZone.getTimeZone(ZoneId.of(zoneId).normalized())); + } catch (DateTimeException e) { + try { + context.setConnectionTimeZone( + TimeZone.getTimeZone(ZoneId.of(zoneId, ZoneId.SHORT_IDS).normalized())); + } catch (DateTimeException e2) { + // unknown zone id + throw new SQLException(String.format("Unknown zoneId %s", zoneId), e); } } - return timeZone; } - return null; } private void postConnectionQueries() throws SQLException { @@ -620,9 +634,8 @@ private void postConnectionQueries() throws SQLException { && !galeraAllowedStates.isEmpty()) { commands.add("show status like 'wsrep_local_state'"); } - - String serverTz = conf.timezone() != null ? handleTimezone() : null; - String sessionVariableQuery = createSessionVariableQuery(serverTz, context); + handleTimezone(); + String sessionVariableQuery = createSessionVariableQuery(context); if (sessionVariableQuery != null) commands.add(sessionVariableQuery); if (conf.database() != null @@ -706,11 +719,10 @@ private void postConnectionQueries() throws SQLException { /** * Create session variable if configuration requires additional commands. * - * @param serverTz server timezone * @param context context * @return sql setting session command */ - public String createSessionVariableQuery(String serverTz, Context context) { + public String createSessionVariableQuery(Context context) { List sessionCommands = new ArrayList<>(); // In JDBC, connection must start in autocommit mode @@ -756,28 +768,14 @@ public String createSessionVariableQuery(String serverTz, Context context) { } // force client timezone to connection to ensure result of now(), ... - if (conf.timezone() != null && !"disable".equalsIgnoreCase(conf.timezone())) { - boolean mustSetTimezone = true; - TimeZone connectionTz = - "auto".equalsIgnoreCase(conf.timezone()) - ? TimeZone.getDefault() - : TimeZone.getTimeZone(ZoneId.of(conf.timezone()).normalized()); - ZoneId clientZoneId = connectionTz.toZoneId(); + if (conf.forceConnectionTimeZoneToSession()) { + TimeZone connectionTz = context.getConnectionTimeZone(); + ZoneId connectionZoneId = connectionTz.toZoneId(); // try to avoid timezone consideration if server use the same one - try { - ZoneId serverZoneId = ZoneId.of(serverTz); - if (serverZoneId.normalized().equals(clientZoneId) - || ZoneId.of(serverTz, ZoneId.SHORT_IDS).equals(clientZoneId)) { - mustSetTimezone = false; - } - } catch (DateTimeException e) { - // eat - } - - if (mustSetTimezone) { - if (clientZoneId.getRules().isFixedOffset()) { - ZoneOffset zoneOffset = clientZoneId.getRules().getOffset(Instant.now()); + if (!connectionZoneId.normalized().equals(TimeZone.getDefault().toZoneId())) { + if (connectionZoneId.getRules().isFixedOffset()) { + ZoneOffset zoneOffset = connectionZoneId.getRules().getOffset(Instant.now()); if (zoneOffset.getTotalSeconds() == 0) { // specific for UTC timezone, server permitting only SYSTEM/UTC offset or named time // zone @@ -787,7 +785,7 @@ public String createSessionVariableQuery(String serverTz, Context context) { sessionCommands.add("time_zone='" + zoneOffset.getId() + "'"); } } else { - sessionCommands.add("time_zone='" + clientZoneId.normalized() + "'"); + sessionCommands.add("time_zone='" + connectionZoneId.normalized() + "'"); } } } 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 1bbf23587..d498e71d7 100644 --- a/src/main/java/org/mariadb/jdbc/client/result/Result.java +++ b/src/main/java/org/mariadb/jdbc/client/result/Result.java @@ -446,7 +446,7 @@ public String getString(int columnIndex) throws SQLException { if (fieldLength.get() == NULL_LENGTH) { return null; } - return rowDecoder.decodeString(metadataList, fieldIndex, rowBuf, fieldLength); + return rowDecoder.decodeString(metadataList, fieldIndex, rowBuf, fieldLength, context); } @Override @@ -526,7 +526,7 @@ public BigInteger getBigInteger(int columnIndex) throws SQLException { return null; } return rowDecoder.decode( - BigIntegerCodec.INSTANCE, null, rowBuf, fieldLength, metadataList, fieldIndex); + BigIntegerCodec.INSTANCE, null, rowBuf, fieldLength, metadataList, fieldIndex, context); } /** @@ -577,7 +577,7 @@ public BigDecimal getBigDecimal(int columnIndex, int scale) throws SQLException } BigDecimal d = rowDecoder.decode( - BigDecimalCodec.INSTANCE, null, rowBuf, fieldLength, metadataList, fieldIndex); + BigDecimalCodec.INSTANCE, null, rowBuf, fieldLength, metadataList, fieldIndex, context); if (d == null) return null; return d.setScale(scale, RoundingMode.HALF_DOWN); } @@ -592,7 +592,7 @@ public byte[] getBytes(int columnIndex) throws SQLException { return null; } return rowDecoder.decode( - ByteArrayCodec.INSTANCE, null, rowBuf, fieldLength, metadataList, fieldIndex); + ByteArrayCodec.INSTANCE, null, rowBuf, fieldLength, metadataList, fieldIndex, context); } @Override @@ -604,7 +604,7 @@ public Date getDate(int columnIndex) throws SQLException { if (fieldLength.get() == NULL_LENGTH) { return null; } - return rowDecoder.decodeDate(metadataList, fieldIndex, rowBuf, fieldLength, null); + return rowDecoder.decodeDate(metadataList, fieldIndex, rowBuf, fieldLength, null, context); } @Override @@ -616,7 +616,7 @@ public Time getTime(int columnIndex) throws SQLException { if (fieldLength.get() == NULL_LENGTH) { return null; } - return rowDecoder.decodeTime(metadataList, fieldIndex, rowBuf, fieldLength, null); + return rowDecoder.decodeTime(metadataList, fieldIndex, rowBuf, fieldLength, null, context); } @Override @@ -628,7 +628,7 @@ public Timestamp getTimestamp(int columnIndex) throws SQLException { if (fieldLength.get() == NULL_LENGTH) { return null; } - return rowDecoder.decodeTimestamp(metadataList, fieldIndex, rowBuf, fieldLength, null); + return rowDecoder.decodeTimestamp(metadataList, fieldIndex, rowBuf, fieldLength, null, context); } @Override @@ -641,7 +641,7 @@ public InputStream getAsciiStream(int columnIndex) throws SQLException { return null; } return rowDecoder.decode( - StreamCodec.INSTANCE, null, rowBuf, fieldLength, metadataList, fieldIndex); + StreamCodec.INSTANCE, null, rowBuf, fieldLength, metadataList, fieldIndex, context); } @Override @@ -655,7 +655,7 @@ public InputStream getUnicodeStream(int columnIndex) throws SQLException { return null; } return rowDecoder.decode( - StreamCodec.INSTANCE, null, rowBuf, fieldLength, metadataList, fieldIndex); + StreamCodec.INSTANCE, null, rowBuf, fieldLength, metadataList, fieldIndex, context); } @Override @@ -668,7 +668,7 @@ public InputStream getBinaryStream(int columnIndex) throws SQLException { return null; } return rowDecoder.decode( - StreamCodec.INSTANCE, null, rowBuf, fieldLength, metadataList, fieldIndex); + StreamCodec.INSTANCE, null, rowBuf, fieldLength, metadataList, fieldIndex, context); } @Override @@ -787,8 +787,7 @@ public Object getObject(int columnIndex) throws SQLException { if (fieldLength.get() == NULL_LENGTH) { return null; } - return rowDecoder.defaultDecode( - context.getConf(), metadataList, fieldIndex, rowBuf, fieldLength); + return rowDecoder.defaultDecode(metadataList, fieldIndex, rowBuf, fieldLength, context); } @Override @@ -806,7 +805,7 @@ public Reader getCharacterStream(int columnIndex) throws SQLException { return null; } return rowDecoder.decode( - ReaderCodec.INSTANCE, null, rowBuf, fieldLength, metadataList, fieldIndex); + ReaderCodec.INSTANCE, null, rowBuf, fieldLength, metadataList, fieldIndex, context); } @Override @@ -824,7 +823,7 @@ public BigDecimal getBigDecimal(int columnIndex) throws SQLException { return null; } return rowDecoder.decode( - BigDecimalCodec.INSTANCE, null, rowBuf, fieldLength, metadataList, fieldIndex); + BigDecimalCodec.INSTANCE, null, rowBuf, fieldLength, metadataList, fieldIndex, context); } @Override @@ -1208,7 +1207,7 @@ public Blob getBlob(int columnIndex) throws SQLException { return null; } return rowDecoder.decode( - BlobCodec.INSTANCE, null, rowBuf, fieldLength, metadataList, fieldIndex); + BlobCodec.INSTANCE, null, rowBuf, fieldLength, metadataList, fieldIndex, context); } @Override @@ -1221,7 +1220,7 @@ public Clob getClob(int columnIndex) throws SQLException { return null; } return rowDecoder.decode( - ClobCodec.INSTANCE, null, rowBuf, fieldLength, metadataList, fieldIndex); + ClobCodec.INSTANCE, null, rowBuf, fieldLength, metadataList, fieldIndex, context); } @Override @@ -1267,7 +1266,7 @@ public Date getDate(int columnIndex, Calendar cal) throws SQLException { if (fieldLength.get() == NULL_LENGTH) { return null; } - return rowDecoder.decodeDate(metadataList, fieldIndex, rowBuf, fieldLength, cal); + return rowDecoder.decodeDate(metadataList, fieldIndex, rowBuf, fieldLength, cal, context); } @Override @@ -1284,7 +1283,7 @@ public Time getTime(int columnIndex, Calendar cal) throws SQLException { if (fieldLength.get() == NULL_LENGTH) { return null; } - return rowDecoder.decodeTime(metadataList, fieldIndex, rowBuf, fieldLength, cal); + return rowDecoder.decodeTime(metadataList, fieldIndex, rowBuf, fieldLength, cal, context); } @Override @@ -1301,7 +1300,7 @@ public Timestamp getTimestamp(int columnIndex, Calendar cal) throws SQLException if (fieldLength.get() == NULL_LENGTH) { return null; } - return rowDecoder.decodeTimestamp(metadataList, fieldIndex, rowBuf, fieldLength, cal); + return rowDecoder.decodeTimestamp(metadataList, fieldIndex, rowBuf, fieldLength, cal, context); } @Override @@ -1321,7 +1320,7 @@ public URL getURL(int columnIndex) throws SQLException { String s = rowDecoder.decode( - StringCodec.INSTANCE, null, rowBuf, fieldLength, metadataList, fieldIndex); + StringCodec.INSTANCE, null, rowBuf, fieldLength, metadataList, fieldIndex, context); if (s == null) return null; try { return new URI(s).toURL(); @@ -1435,7 +1434,8 @@ public NClob getNClob(int columnIndex) throws SQLException { return null; } return (NClob) - rowDecoder.decode(ClobCodec.INSTANCE, null, rowBuf, fieldLength, metadataList, fieldIndex); + rowDecoder.decode( + ClobCodec.INSTANCE, null, rowBuf, fieldLength, metadataList, fieldIndex, context); } @Override @@ -1483,7 +1483,7 @@ public Reader getNCharacterStream(int columnIndex) throws SQLException { return null; } return rowDecoder.decode( - ReaderCodec.INSTANCE, null, rowBuf, fieldLength, metadataList, fieldIndex); + ReaderCodec.INSTANCE, null, rowBuf, fieldLength, metadataList, fieldIndex, context); } @Override @@ -1652,17 +1652,17 @@ public T getObject(int columnIndex, Class type) throws SQLException { } return null; } - Configuration conf = context.getConf(); ColumnDecoder column = metadataList[columnIndex - 1]; // type generic, return "natural" java type if (Object.class.equals(type) || type == null) { - return (T) rowDecoder.defaultDecode(conf, metadataList, fieldIndex, rowBuf, fieldLength); + return (T) rowDecoder.defaultDecode(metadataList, fieldIndex, rowBuf, fieldLength, context); } + Configuration conf = context.getConf(); for (Codec codec : conf.codecs()) { if (codec.canDecode(column, type)) { return rowDecoder.decode( - (Codec) codec, calendar, rowBuf, fieldLength, metadataList, fieldIndex); + (Codec) codec, calendar, rowBuf, fieldLength, metadataList, fieldIndex, context); } } rowBuf.skip(fieldLength.get()); 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 a23bfca0f..defb9996d 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 @@ -10,8 +10,8 @@ import java.sql.Time; import java.sql.Timestamp; import java.util.Calendar; -import org.mariadb.jdbc.Configuration; import org.mariadb.jdbc.client.ColumnDecoder; +import org.mariadb.jdbc.client.Context; import org.mariadb.jdbc.client.impl.StandardReadableByteBuf; import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.plugin.Codec; @@ -28,135 +28,142 @@ public class BinaryRowDecoder implements RowDecoder { * @param fieldLength field length * @param metadataList metadatas * @param fieldIndex field index + * @param context connection context * @return default object according to metadata * @param Codec default return type * @throws SQLException if any decoding error occurs */ public T decode( - Codec codec, - Calendar cal, - StandardReadableByteBuf rowBuf, - MutableInt fieldLength, - ColumnDecoder[] metadataList, - final MutableInt fieldIndex) + final Codec codec, + final Calendar cal, + final StandardReadableByteBuf rowBuf, + final MutableInt fieldLength, + final ColumnDecoder[] metadataList, + final MutableInt fieldIndex, + final Context context) throws SQLException { - return codec.decodeBinary(rowBuf, fieldLength, metadataList[fieldIndex.get()], cal); + return codec.decodeBinary(rowBuf, fieldLength, metadataList[fieldIndex.get()], cal, context); } @Override public Object defaultDecode( - Configuration conf, - ColumnDecoder[] metadataList, + final ColumnDecoder[] metadataList, final MutableInt fieldIndex, - StandardReadableByteBuf rowBuf, - MutableInt fieldLength) + final StandardReadableByteBuf rowBuf, + final MutableInt fieldLength, + final Context context) throws SQLException { - return metadataList[fieldIndex.get()].getDefaultBinary(conf, rowBuf, fieldLength); + return metadataList[fieldIndex.get()].getDefaultBinary(rowBuf, fieldLength, context); } public String decodeString( - ColumnDecoder[] metadataList, + final ColumnDecoder[] metadataList, final MutableInt fieldIndex, - StandardReadableByteBuf rowBuf, - MutableInt fieldLength) + final StandardReadableByteBuf rowBuf, + final MutableInt fieldLength, + final Context context) throws SQLException { - return metadataList[fieldIndex.get()].decodeStringBinary(rowBuf, fieldLength, null); + return metadataList[fieldIndex.get()].decodeStringBinary(rowBuf, fieldLength, null, context); } public byte decodeByte( - ColumnDecoder[] metadataList, + final ColumnDecoder[] metadataList, final MutableInt fieldIndex, - StandardReadableByteBuf rowBuf, - MutableInt fieldLength) + final StandardReadableByteBuf rowBuf, + final MutableInt fieldLength) throws SQLException { return metadataList[fieldIndex.get()].decodeByteBinary(rowBuf, fieldLength); } public boolean decodeBoolean( - ColumnDecoder[] metadataList, + final ColumnDecoder[] metadataList, final MutableInt fieldIndex, - StandardReadableByteBuf rowBuf, - MutableInt fieldLength) + final StandardReadableByteBuf rowBuf, + final MutableInt fieldLength) throws SQLException { return metadataList[fieldIndex.get()].decodeBooleanBinary(rowBuf, fieldLength); } public Date decodeDate( - ColumnDecoder[] metadataList, - MutableInt fieldIndex, - StandardReadableByteBuf rowBuf, - MutableInt fieldLength, - Calendar cal) + final ColumnDecoder[] metadataList, + final MutableInt fieldIndex, + final StandardReadableByteBuf rowBuf, + final MutableInt fieldLength, + final Calendar cal, + final Context context) throws SQLException { - return metadataList[fieldIndex.get()].decodeDateBinary(rowBuf, fieldLength, cal); + return metadataList[fieldIndex.get()].decodeDateBinary(rowBuf, fieldLength, cal, context); } public Time decodeTime( - ColumnDecoder[] metadataList, - MutableInt fieldIndex, - StandardReadableByteBuf rowBuf, - MutableInt fieldLength, - Calendar cal) + final ColumnDecoder[] metadataList, + final MutableInt fieldIndex, + final StandardReadableByteBuf rowBuf, + final MutableInt fieldLength, + final Calendar cal, + final Context context) throws SQLException { - return metadataList[fieldIndex.get()].decodeTimeBinary(rowBuf, fieldLength, cal); + return metadataList[fieldIndex.get()].decodeTimeBinary(rowBuf, fieldLength, cal, context); } public Timestamp decodeTimestamp( - ColumnDecoder[] metadataList, - MutableInt fieldIndex, - StandardReadableByteBuf rowBuf, - MutableInt fieldLength, - Calendar cal) + final ColumnDecoder[] metadataList, + final MutableInt fieldIndex, + final StandardReadableByteBuf rowBuf, + final MutableInt fieldLength, + final Calendar cal, + final Context context) throws SQLException { - return metadataList[fieldIndex.get()].decodeTimestampBinary(rowBuf, fieldLength, cal); + return metadataList[fieldIndex.get()].decodeTimestampBinary(rowBuf, fieldLength, cal, context); } public short decodeShort( - ColumnDecoder[] metadataList, + final ColumnDecoder[] metadataList, final MutableInt fieldIndex, - StandardReadableByteBuf rowBuf, - MutableInt fieldLength) + final StandardReadableByteBuf rowBuf, + final MutableInt fieldLength) throws SQLException { return metadataList[fieldIndex.get()].decodeShortBinary(rowBuf, fieldLength); } public int decodeInt( - ColumnDecoder[] metadataList, + final ColumnDecoder[] metadataList, final MutableInt fieldIndex, - StandardReadableByteBuf rowBuf, - MutableInt fieldLength) + final StandardReadableByteBuf rowBuf, + final MutableInt fieldLength) throws SQLException { return metadataList[fieldIndex.get()].decodeIntBinary(rowBuf, fieldLength); } public long decodeLong( - ColumnDecoder[] metadataList, + final ColumnDecoder[] metadataList, final MutableInt fieldIndex, - StandardReadableByteBuf rowBuf, - MutableInt fieldLength) + final StandardReadableByteBuf rowBuf, + final MutableInt fieldLength) throws SQLException { return metadataList[fieldIndex.get()].decodeLongBinary(rowBuf, fieldLength); } public float decodeFloat( - ColumnDecoder[] metadataList, + final ColumnDecoder[] metadataList, final MutableInt fieldIndex, - StandardReadableByteBuf rowBuf, - MutableInt fieldLength) + final StandardReadableByteBuf rowBuf, + final MutableInt fieldLength) throws SQLException { return metadataList[fieldIndex.get()].decodeFloatBinary(rowBuf, fieldLength); } public double decodeDouble( - ColumnDecoder[] metadataList, + final ColumnDecoder[] metadataList, final MutableInt fieldIndex, - StandardReadableByteBuf rowBuf, - MutableInt fieldLength) + final StandardReadableByteBuf rowBuf, + final MutableInt fieldLength) throws SQLException { return metadataList[fieldIndex.get()].decodeDoubleBinary(rowBuf, fieldLength); } - public boolean wasNull(byte[] nullBitmap, final MutableInt fieldIndex, MutableInt fieldLength) { + public boolean wasNull( + final byte[] nullBitmap, final MutableInt fieldIndex, final MutableInt fieldLength) { return (nullBitmap[(fieldIndex.get() + 2) / 8] & (1 << ((fieldIndex.get() + 2) % 8))) > 0 || fieldLength.get() == NULL_LENGTH; } @@ -168,12 +175,12 @@ public boolean wasNull(byte[] nullBitmap, final MutableInt fieldIndex, MutableIn */ @Override public int setPosition( - int newIndex, + final int newIndex, final MutableInt fieldIndex, - int maxIndex, - StandardReadableByteBuf rowBuf, - byte[] nullBitmap, - ColumnDecoder[] metadataList) { + final int maxIndex, + final StandardReadableByteBuf rowBuf, + final byte[] nullBitmap, + final ColumnDecoder[] metadataList) { if (fieldIndex.get() >= newIndex) { fieldIndex.set(0); 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 1fc59ca4a..8b0f8f460 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 @@ -8,8 +8,8 @@ import java.sql.Time; import java.sql.Timestamp; import java.util.Calendar; -import org.mariadb.jdbc.Configuration; import org.mariadb.jdbc.client.ColumnDecoder; +import org.mariadb.jdbc.client.Context; import org.mariadb.jdbc.client.impl.StandardReadableByteBuf; import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.plugin.Codec; @@ -54,6 +54,7 @@ int setPosition( * @param fieldLength field length * @param metadataList metadatas * @param fieldIndex field index + * @param context connection context * @return default object according to metadata * @param Codec default return type * @throws SQLException if any decoding error occurs @@ -64,26 +65,27 @@ T decode( StandardReadableByteBuf rowBuf, MutableInt fieldLength, ColumnDecoder[] metadataList, - MutableInt fieldIndex) + MutableInt fieldIndex, + Context context) throws SQLException; /** * Decode data according to data type. * - * @param conf configuration * @param metadataList metadata list * @param fieldIndex field index * @param rowBuf row buffer * @param fieldLength field length + * @param context connection context * @return data * @throws SQLException if any decoding error occurs */ Object defaultDecode( - Configuration conf, ColumnDecoder[] metadataList, MutableInt fieldIndex, StandardReadableByteBuf rowBuf, - MutableInt fieldLength) + MutableInt fieldLength, + Context context) throws SQLException; /** @@ -136,7 +138,8 @@ Date decodeDate( MutableInt fieldIndex, StandardReadableByteBuf rowBuf, MutableInt fieldLength, - Calendar cal) + Calendar cal, + Context context) throws SQLException; /** @@ -147,6 +150,7 @@ Date decodeDate( * @param rowBuf row buffer * @param fieldLength field length * @param cal calendar + * @param context connection context * @return data * @throws SQLException if data type cannot be decoded to Time value */ @@ -155,7 +159,8 @@ Time decodeTime( MutableInt fieldIndex, StandardReadableByteBuf rowBuf, MutableInt fieldLength, - Calendar cal) + Calendar cal, + Context context) throws SQLException; /** @@ -166,6 +171,7 @@ Time decodeTime( * @param rowBuf row buffer * @param fieldLength field length * @param cal calendar + * @param context connection context * @return data * @throws SQLException if data type cannot be decoded to Timestamp value */ @@ -174,7 +180,8 @@ Timestamp decodeTimestamp( MutableInt fieldIndex, StandardReadableByteBuf rowBuf, MutableInt fieldLength, - Calendar cal) + Calendar cal, + Context context) throws SQLException; /** @@ -218,6 +225,7 @@ int decodeInt( * @param fieldIndex field index * @param rowBuf row buffer * @param fieldLength field length + * @param context connection Context * @return data * @throws SQLException if data type cannot be decoded to String value */ @@ -225,7 +233,8 @@ String decodeString( ColumnDecoder[] metadataList, MutableInt fieldIndex, StandardReadableByteBuf rowBuf, - MutableInt fieldLength) + MutableInt fieldLength, + Context context) 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 5712c6d60..be1e7cc75 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 @@ -10,8 +10,8 @@ import java.sql.Time; import java.sql.Timestamp; import java.util.Calendar; -import org.mariadb.jdbc.Configuration; import org.mariadb.jdbc.client.ColumnDecoder; +import org.mariadb.jdbc.client.Context; import org.mariadb.jdbc.client.impl.StandardReadableByteBuf; import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.plugin.Codec; @@ -21,131 +21,137 @@ public class TextRowDecoder implements RowDecoder { @Override public T decode( - Codec codec, - Calendar cal, - StandardReadableByteBuf rowBuf, - MutableInt fieldLength, - ColumnDecoder[] metadataList, - MutableInt fieldIndex) + final Codec codec, + final Calendar cal, + final StandardReadableByteBuf rowBuf, + final MutableInt fieldLength, + final ColumnDecoder[] metadataList, + final MutableInt fieldIndex, + final Context context) throws SQLException { - return codec.decodeText(rowBuf, fieldLength, metadataList[fieldIndex.get()], cal); + return codec.decodeText(rowBuf, fieldLength, metadataList[fieldIndex.get()], cal, context); } @Override public Object defaultDecode( - Configuration conf, - ColumnDecoder[] metadataList, - MutableInt fieldIndex, - StandardReadableByteBuf rowBuf, - MutableInt fieldLength) + final ColumnDecoder[] metadataList, + final MutableInt fieldIndex, + final StandardReadableByteBuf rowBuf, + final MutableInt fieldLength, + final Context context) throws SQLException { - return metadataList[fieldIndex.get()].getDefaultText(conf, rowBuf, fieldLength); + return metadataList[fieldIndex.get()].getDefaultText(rowBuf, fieldLength, context); } @Override public String decodeString( - ColumnDecoder[] metadataList, - MutableInt fieldIndex, - StandardReadableByteBuf rowBuf, - MutableInt fieldLength) + final ColumnDecoder[] metadataList, + final MutableInt fieldIndex, + final StandardReadableByteBuf rowBuf, + final MutableInt fieldLength, + final Context context) throws SQLException { - return metadataList[fieldIndex.get()].decodeStringText(rowBuf, fieldLength, null); + return metadataList[fieldIndex.get()].decodeStringText(rowBuf, fieldLength, null, context); } public byte decodeByte( - ColumnDecoder[] metadataList, - MutableInt fieldIndex, - StandardReadableByteBuf rowBuf, - MutableInt fieldLength) + final ColumnDecoder[] metadataList, + final MutableInt fieldIndex, + final StandardReadableByteBuf rowBuf, + final MutableInt fieldLength) throws SQLException { return metadataList[fieldIndex.get()].decodeByteText(rowBuf, fieldLength); } public boolean decodeBoolean( - ColumnDecoder[] metadataList, - MutableInt fieldIndex, - StandardReadableByteBuf rowBuf, - MutableInt fieldLength) + final ColumnDecoder[] metadataList, + final MutableInt fieldIndex, + final StandardReadableByteBuf rowBuf, + final MutableInt fieldLength) throws SQLException { return metadataList[fieldIndex.get()].decodeBooleanText(rowBuf, fieldLength); } public Date decodeDate( - ColumnDecoder[] metadataList, - MutableInt fieldIndex, - StandardReadableByteBuf rowBuf, - MutableInt fieldLength, - Calendar cal) + final ColumnDecoder[] metadataList, + final MutableInt fieldIndex, + final StandardReadableByteBuf rowBuf, + final MutableInt fieldLength, + final Calendar cal, + final Context context) throws SQLException { - return metadataList[fieldIndex.get()].decodeDateText(rowBuf, fieldLength, cal); + return metadataList[fieldIndex.get()].decodeDateText(rowBuf, fieldLength, cal, context); } public Time decodeTime( - ColumnDecoder[] metadataList, - MutableInt fieldIndex, - StandardReadableByteBuf rowBuf, - MutableInt fieldLength, - Calendar cal) + final ColumnDecoder[] metadataList, + final MutableInt fieldIndex, + final StandardReadableByteBuf rowBuf, + final MutableInt fieldLength, + final Calendar cal, + final Context context) throws SQLException { - return metadataList[fieldIndex.get()].decodeTimeText(rowBuf, fieldLength, cal); + return metadataList[fieldIndex.get()].decodeTimeText(rowBuf, fieldLength, cal, context); } public Timestamp decodeTimestamp( - ColumnDecoder[] metadataList, - MutableInt fieldIndex, - StandardReadableByteBuf rowBuf, - MutableInt fieldLength, - Calendar cal) + final ColumnDecoder[] metadataList, + final MutableInt fieldIndex, + final StandardReadableByteBuf rowBuf, + final MutableInt fieldLength, + final Calendar cal, + final Context context) throws SQLException { - return metadataList[fieldIndex.get()].decodeTimestampText(rowBuf, fieldLength, cal); + return metadataList[fieldIndex.get()].decodeTimestampText(rowBuf, fieldLength, cal, context); } public short decodeShort( - ColumnDecoder[] metadataList, - MutableInt fieldIndex, - StandardReadableByteBuf rowBuf, - MutableInt fieldLength) + final ColumnDecoder[] metadataList, + final MutableInt fieldIndex, + final StandardReadableByteBuf rowBuf, + final MutableInt fieldLength) throws SQLException { return metadataList[fieldIndex.get()].decodeShortText(rowBuf, fieldLength); } public int decodeInt( - ColumnDecoder[] metadataList, - MutableInt fieldIndex, - StandardReadableByteBuf rowBuf, - MutableInt fieldLength) + final ColumnDecoder[] metadataList, + final MutableInt fieldIndex, + final StandardReadableByteBuf rowBuf, + final MutableInt fieldLength) throws SQLException { return metadataList[fieldIndex.get()].decodeIntText(rowBuf, fieldLength); } public long decodeLong( - ColumnDecoder[] metadataList, - MutableInt fieldIndex, - StandardReadableByteBuf rowBuf, - MutableInt fieldLength) + final ColumnDecoder[] metadataList, + final MutableInt fieldIndex, + final StandardReadableByteBuf rowBuf, + final MutableInt fieldLength) throws SQLException { return metadataList[fieldIndex.get()].decodeLongText(rowBuf, fieldLength); } public float decodeFloat( - ColumnDecoder[] metadataList, - MutableInt fieldIndex, - StandardReadableByteBuf rowBuf, - MutableInt fieldLength) + final ColumnDecoder[] metadataList, + final MutableInt fieldIndex, + final StandardReadableByteBuf rowBuf, + final MutableInt fieldLength) throws SQLException { return metadataList[fieldIndex.get()].decodeFloatText(rowBuf, fieldLength); } public double decodeDouble( - ColumnDecoder[] metadataList, - MutableInt fieldIndex, - StandardReadableByteBuf rowBuf, - MutableInt fieldLength) + final ColumnDecoder[] metadataList, + final MutableInt fieldIndex, + final StandardReadableByteBuf rowBuf, + final MutableInt fieldLength) throws SQLException { return metadataList[fieldIndex.get()].decodeDoubleText(rowBuf, fieldLength); } - public boolean wasNull(byte[] nullBitmap, MutableInt fieldIndex, MutableInt fieldLength) { + public boolean wasNull( + final byte[] nullBitmap, final MutableInt fieldIndex, final MutableInt fieldLength) { return fieldLength.get() == NULL_LENGTH; } @@ -156,7 +162,7 @@ public boolean wasNull(byte[] nullBitmap, MutableInt fieldIndex, MutableInt fiel */ @Override public int setPosition( - int newIndex, + final int newIndex, final MutableInt fieldIndex, final int maxIndex, final StandardReadableByteBuf rowBuf, diff --git a/src/main/java/org/mariadb/jdbc/client/util/Parameter.java b/src/main/java/org/mariadb/jdbc/client/util/Parameter.java index cecda5b1b..ab3cfb70b 100644 --- a/src/main/java/org/mariadb/jdbc/client/util/Parameter.java +++ b/src/main/java/org/mariadb/jdbc/client/util/Parameter.java @@ -25,10 +25,11 @@ public interface Parameter { * Encode parameter in binary format * * @param encoder packet writer + * @param context connection context * @throws IOException if socket error occurs * @throws SQLException if other kind of error occurs */ - void encodeBinary(Writer encoder) throws IOException, SQLException; + void encodeBinary(Writer encoder, Context context) throws IOException, SQLException; /** * Encode parameter in binary long format diff --git a/src/main/java/org/mariadb/jdbc/codec/Parameter.java b/src/main/java/org/mariadb/jdbc/codec/Parameter.java index f64203166..c0281e1e8 100644 --- a/src/main/java/org/mariadb/jdbc/codec/Parameter.java +++ b/src/main/java/org/mariadb/jdbc/codec/Parameter.java @@ -53,8 +53,8 @@ public void encodeText(Writer encoder, Context context) throws IOException, SQLE } } - public void encodeBinary(Writer encoder) throws IOException, SQLException { - codec.encodeBinary(encoder, this.value, null, length); + public void encodeBinary(Writer encoder, Context context) throws IOException, SQLException { + codec.encodeBinary(encoder, context, this.value, null, length); } public void encodeLongData(Writer encoder) throws IOException, SQLException { diff --git a/src/main/java/org/mariadb/jdbc/codec/ParameterWithCal.java b/src/main/java/org/mariadb/jdbc/codec/ParameterWithCal.java index 255839e8e..7e601cdc9 100644 --- a/src/main/java/org/mariadb/jdbc/codec/ParameterWithCal.java +++ b/src/main/java/org/mariadb/jdbc/codec/ParameterWithCal.java @@ -25,7 +25,7 @@ public void encodeText(Writer encoder, Context context) throws IOException, SQLE } @Override - public void encodeBinary(Writer encoder) throws IOException, SQLException { - codec.encodeBinary(encoder, this.value, this.cal, length); + public void encodeBinary(Writer encoder, Context context) throws IOException, SQLException { + codec.encodeBinary(encoder, context, this.value, this.cal, length); } } diff --git a/src/main/java/org/mariadb/jdbc/message/client/BulkExecutePacket.java b/src/main/java/org/mariadb/jdbc/message/client/BulkExecutePacket.java index 3c5973641..05e338832 100644 --- a/src/main/java/org/mariadb/jdbc/message/client/BulkExecutePacket.java +++ b/src/main/java/org/mariadb/jdbc/message/client/BulkExecutePacket.java @@ -117,7 +117,7 @@ public int encode(Writer writer, Context context, Prepare newPrepareResult) writer.writeByte(0x01); // value is null } else { writer.writeByte(0x00); // value follow - param.encodeBinary(writer); + param.encodeBinary(writer, context); } } diff --git a/src/main/java/org/mariadb/jdbc/message/client/ExecutePacket.java b/src/main/java/org/mariadb/jdbc/message/client/ExecutePacket.java index 749e88d3b..1df1cc595 100644 --- a/src/main/java/org/mariadb/jdbc/message/client/ExecutePacket.java +++ b/src/main/java/org/mariadb/jdbc/message/client/ExecutePacket.java @@ -118,7 +118,7 @@ public int encode(Writer writer, Context context, Prepare newPrepareResult) for (int i = 0; i < parameterCount; i++) { Parameter p = parameters.get(i); if (!p.isNull() && !p.canEncodeLongData()) { - p.encodeBinary(writer); + p.encodeBinary(writer, context); } } } diff --git a/src/main/java/org/mariadb/jdbc/message/client/PrepareExecutePacket.java b/src/main/java/org/mariadb/jdbc/message/client/PrepareExecutePacket.java index 2606df993..f6ff0d63b 100644 --- a/src/main/java/org/mariadb/jdbc/message/client/PrepareExecutePacket.java +++ b/src/main/java/org/mariadb/jdbc/message/client/PrepareExecutePacket.java @@ -115,7 +115,7 @@ public int encode(Writer writer, Context context, Prepare newPrepareResult) for (int i = 0; i < parameterCount; i++) { Parameter p = parameters.get(i); if (!p.isNull() && !p.canEncodeLongData()) { - p.encodeBinary(writer); + p.encodeBinary(writer, context); } } } diff --git a/src/main/java/org/mariadb/jdbc/plugin/Codec.java b/src/main/java/org/mariadb/jdbc/plugin/Codec.java index 83783ee8c..ae347f0d4 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/Codec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/Codec.java @@ -51,6 +51,7 @@ public interface Codec { * @param fieldLength encoded value length * @param column server column metadata * @param cal calendar + * @param context connection context * @return decoded value * @throws SQLDataException if unexpected error occurs during decoding */ @@ -58,7 +59,8 @@ T decodeText( final ReadableByteBuf buffer, final MutableInt fieldLength, final ColumnDecoder column, - final Calendar cal) + final Calendar cal, + final Context context) throws SQLDataException; /** @@ -68,6 +70,7 @@ T decodeText( * @param fieldLength encoded value length * @param column server column metadata * @param cal calendar + * @param context connection context * @return decoded value * @throws SQLDataException if unexpected error occurs during decoding */ @@ -75,7 +78,8 @@ T decodeBinary( final ReadableByteBuf buffer, final MutableInt fieldLength, final ColumnDecoder column, - final Calendar cal) + final Calendar cal, + final Context context) throws SQLDataException; /** @@ -96,13 +100,14 @@ void encodeText(Writer encoder, Context context, Object value, Calendar cal, Lon * Binary encode value to writer * * @param encoder writer + * @param context connection context * @param value value to encode * @param cal calendar * @param length maximum value length * @throws IOException if any socket error occurs * @throws SQLException if encoding error occurs */ - void encodeBinary(Writer encoder, Object value, Calendar cal, Long length) + void encodeBinary(Writer encoder, Context context, Object value, Calendar cal, Long length) throws IOException, SQLException; /** 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 b8257a3ba..5c9845e60 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/BigDecimalCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/BigDecimalCodec.java @@ -72,7 +72,11 @@ public boolean canEncode(Object value) { @Override @SuppressWarnings("fallthrough") public BigDecimal decodeText( - ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar cal, + final Context context) throws SQLDataException { switch (column.getType()) { case TINYINT: @@ -128,7 +132,11 @@ public BigDecimal decodeText( @Override @SuppressWarnings("fallthrough") public BigDecimal decodeBinary( - ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar cal, + final Context context) throws SQLDataException { switch (column.getType()) { @@ -211,13 +219,23 @@ public BigDecimal decodeBinary( } @Override - public void encodeText(Writer encoder, Context context, Object value, Calendar cal, Long length) + public void encodeText( + final Writer encoder, + final Context context, + final Object value, + final Calendar cal, + final Long length) throws IOException { encoder.writeAscii(((BigDecimal) value).toPlainString()); } @Override - public void encodeBinary(Writer encoder, Object value, Calendar cal, Long maxLength) + public void encodeBinary( + final Writer encoder, + final Context context, + final Object value, + final Calendar cal, + final Long maxLength) throws IOException { String asciiFormat = ((BigDecimal) value).toPlainString(); encoder.writeLength(asciiFormat.length()); 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 ea3cdb8f4..f4ac2d038 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/BigIntegerCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/BigIntegerCodec.java @@ -56,7 +56,11 @@ public boolean canEncode(Object value) { @Override @SuppressWarnings("fallthrough") public BigInteger decodeText( - ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar cal, + final Context context) throws SQLDataException { switch (column.getType()) { @@ -115,7 +119,11 @@ public BigInteger decodeText( @Override @SuppressWarnings("fallthrough") public BigInteger decodeBinary( - ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar cal, + final Context context) throws SQLDataException { switch (column.getType()) { @@ -203,13 +211,23 @@ public BigInteger decodeBinary( } @Override - public void encodeText(Writer encoder, Context context, Object value, Calendar cal, Long length) + public void encodeText( + final Writer encoder, + final Context context, + final Object value, + final Calendar cal, + final Long length) throws IOException { encoder.writeAscii(value.toString()); } @Override - public void encodeBinary(Writer encoder, Object value, Calendar cal, Long maxLength) + public void encodeBinary( + final Writer encoder, + final Context context, + final Object value, + final Calendar cal, + final Long maxLength) throws IOException { String asciiFormat = value.toString(); encoder.writeLength(asciiFormat.length()); 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 c87164dd1..22c819c44 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/BitSetCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/BitSetCodec.java @@ -59,13 +59,21 @@ public boolean canDecode(ColumnDecoder column, Class type) { @Override public BitSet decodeText( - ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) { + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar cal, + final Context context) { return parseBit(buf, length); } @Override public BitSet decodeBinary( - ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) { + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar cal, + final Context context) { return parseBit(buf, length); } @@ -74,7 +82,12 @@ public boolean canEncode(Object value) { } @Override - public void encodeText(Writer encoder, Context context, Object value, Calendar cal, Long length) + public void encodeText( + final Writer encoder, + final Context context, + final Object value, + final Calendar cal, + final Long length) throws IOException { byte[] bytes = ((BitSet) value).toByteArray(); revertOrder(bytes); @@ -88,7 +101,12 @@ public void encodeText(Writer encoder, Context context, Object value, Calendar c } @Override - public void encodeBinary(Writer encoder, Object value, Calendar cal, Long maxLength) + public void encodeBinary( + final Writer encoder, + final Context context, + final Object value, + final Calendar cal, + final Long maxLength) throws IOException { byte[] bytes = ((BitSet) value).toByteArray(); revertOrder(bytes); 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 b869ec9af..065802790 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/BlobCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/BlobCodec.java @@ -50,7 +50,12 @@ public boolean canEncode(Object value) { @Override @SuppressWarnings("fallthrough") - public Blob decodeText(ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + public Blob decodeText( + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar cal, + final Context context) throws SQLDataException { switch (column.getType()) { case STRING: @@ -74,7 +79,11 @@ public Blob decodeText(ReadableByteBuf buf, MutableInt length, ColumnDecoder col @Override @SuppressWarnings("fallthrough") public Blob decodeBinary( - ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar cal, + final Context context) throws SQLDataException { switch (column.getType()) { case STRING: @@ -98,7 +107,11 @@ public Blob decodeBinary( @Override public void encodeText( - Writer encoder, Context context, Object value, Calendar cal, Long maxLength) + final Writer encoder, + final Context context, + final Object value, + final Calendar cal, + final Long maxLength) throws IOException, SQLException { encoder.writeBytes(ByteArrayCodec.BINARY_PREFIX); byte[] array = new byte[4096]; @@ -124,7 +137,12 @@ public void encodeText( } @Override - public void encodeBinary(Writer encoder, Object value, Calendar cal, Long maxLength) + public void encodeBinary( + final Writer encoder, + final Context context, + final Object value, + final Calendar cal, + final Long maxLength) throws IOException, SQLException { long length; InputStream is = ((Blob) value).getBinaryStream(); 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 ea7294528..5d043df17 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/BooleanCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/BooleanCodec.java @@ -56,7 +56,8 @@ public Boolean decodeText( final ReadableByteBuf buffer, final MutableInt length, final ColumnDecoder column, - final Calendar cal) + final Calendar cal, + final Context context) throws SQLDataException { return column.decodeBooleanText(buffer, length); } @@ -65,20 +66,30 @@ public Boolean decodeBinary( final ReadableByteBuf buffer, final MutableInt length, final ColumnDecoder column, - final Calendar cal) + final Calendar cal, + final Context context) throws SQLDataException { return column.decodeBooleanBinary(buffer, length); } @Override public void encodeText( - Writer encoder, Context context, Object value, Calendar cal, Long maxLength) + final Writer encoder, + final Context context, + final Object value, + final Calendar cal, + final Long maxLength) throws IOException { encoder.writeAscii(((Boolean) value) ? "1" : "0"); } @Override - public void encodeBinary(Writer encoder, Object value, Calendar cal, Long maxLength) + public void encodeBinary( + final Writer encoder, + final Context context, + final Object value, + final Calendar cal, + final Long maxLength) throws IOException { encoder.writeByte(((Boolean) value) ? 1 : 0); } 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 c0f35106a..d71a5181b 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/ByteArrayCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/ByteArrayCodec.java @@ -50,7 +50,11 @@ public boolean canEncode(Object value) { @Override public byte[] decodeText( - ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar cal, + final Context context) throws SQLDataException { return getBytes(buf, length, column); } @@ -80,14 +84,22 @@ private byte[] getBytes(ReadableByteBuf buf, MutableInt length, ColumnDecoder co @Override public byte[] decodeBinary( - ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar cal, + final Context context) throws SQLDataException { return getBytes(buf, length, column); } @Override public void encodeText( - Writer encoder, Context context, Object value, Calendar cal, Long maxLength) + final Writer encoder, + final Context context, + final Object value, + final Calendar cal, + final Long maxLength) throws IOException { int length = ((byte[]) value).length; @@ -100,7 +112,12 @@ public void encodeText( } @Override - public void encodeBinary(Writer encoder, Object value, Calendar cal, Long maxLength) + public void encodeBinary( + final Writer encoder, + final Context context, + final Object value, + final Calendar cal, + final Long maxLength) throws IOException { int length = ((byte[]) value).length; if (maxLength != null) length = Math.min(length, maxLength.intValue()); 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 e269cf56a..10b9aa82b 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/ByteCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/ByteCodec.java @@ -75,33 +75,44 @@ public boolean canEncode(Object value) { @Override public Byte decodeText( - final ReadableByteBuf buffer, + final ReadableByteBuf buf, final MutableInt length, final ColumnDecoder column, - final Calendar cal) + final Calendar cal, + final Context context) throws SQLDataException { - return column.decodeByteText(buffer, length); + return column.decodeByteText(buf, length); } @Override public Byte decodeBinary( - final ReadableByteBuf buffer, + final ReadableByteBuf buf, final MutableInt length, final ColumnDecoder column, - final Calendar cal) + final Calendar cal, + final Context context) throws SQLDataException { - return column.decodeByteBinary(buffer, length); + return column.decodeByteBinary(buf, length); } @Override public void encodeText( - Writer encoder, Context context, Object value, Calendar cal, Long maxLength) + final Writer encoder, + final Context context, + final Object value, + final Calendar cal, + final Long maxLength) throws IOException { encoder.writeAscii(Integer.toString((Byte) value)); } @Override - public void encodeBinary(Writer encoder, Object value, Calendar cal, Long maxLength) + public void encodeBinary( + final Writer encoder, + final Context context, + final Object value, + final Calendar cal, + final Long maxLength) throws IOException { encoder.writeByte((byte) value); } 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 391744d00..0111771fb 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/ClobCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/ClobCodec.java @@ -47,7 +47,12 @@ public boolean canEncode(Object value) { } @Override - public Clob decodeText(ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + public Clob decodeText( + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar cal, + final Context context) throws SQLDataException { return getClob(buf, length, column); } @@ -84,14 +89,22 @@ private Clob getClob(ReadableByteBuf buf, MutableInt length, ColumnDecoder colum @Override public Clob decodeBinary( - ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar cal, + final Context context) throws SQLDataException { return getClob(buf, length, column); } @Override public void encodeText( - Writer encoder, Context context, Object value, Calendar cal, Long maxLength) + final Writer encoder, + final Context context, + final Object value, + final Calendar cal, + final Long maxLength) throws IOException, SQLException { Reader reader = ((Clob) value).getCharacterStream(); char[] buf = new char[4096]; @@ -109,7 +122,12 @@ public void encodeText( } @Override - public void encodeBinary(Writer encoder, Object value, Calendar cal, Long maxLength) + public void encodeBinary( + final Writer encoder, + final Context context, + final Object value, + final Calendar cal, + final Long maxLength) throws IOException, SQLException { // prefer use of encodeLongData, because length is unknown Reader reader = ((Clob) value).getCharacterStream(); 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 ffd0ad518..59343dcda 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/DateCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/DateCodec.java @@ -49,24 +49,33 @@ public boolean canEncode(Object value) { @Override @SuppressWarnings("fallthrough") - public Date decodeText(ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + public Date decodeText( + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar cal, + final Context context) throws SQLDataException { - return column.decodeDateText(buf, length, cal); + return column.decodeDateText(buf, length, cal, context); } @Override @SuppressWarnings("fallthrough") public Date decodeBinary( - ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar cal, + final Context context) throws SQLDataException { - return column.decodeDateBinary(buf, length, cal); + return column.decodeDateBinary(buf, length, cal, context); } @Override public void encodeText( Writer encoder, Context context, Object val, Calendar providedCal, Long maxLen) throws IOException { - Calendar cal = providedCal == null ? Calendar.getInstance() : providedCal; + Calendar cal = providedCal == null ? context.getDefaultCalendar() : providedCal; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); sdf.setTimeZone(cal.getTimeZone()); String dateString = sdf.format(val); @@ -77,7 +86,8 @@ public void encodeText( } @Override - public void encodeBinary(Writer encoder, Object value, Calendar providedCal, Long maxLength) + public void encodeBinary( + Writer encoder, Context context, Object value, Calendar providedCal, Long maxLength) throws IOException { if (providedCal == null) { Calendar cal = Calendar.getInstance(); 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 48dfc23d6..19b5ac42e 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/DoubleCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/DoubleCodec.java @@ -54,7 +54,11 @@ public boolean canEncode(Object value) { @Override @SuppressWarnings("fallthrough") public Double decodeText( - ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar cal, + final Context context) throws SQLDataException { return column.decodeDoubleText(buf, length); } @@ -62,7 +66,11 @@ public Double decodeText( @Override @SuppressWarnings("fallthrough") public Double decodeBinary( - ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar cal, + final Context context) throws SQLDataException { return column.decodeDoubleBinary(buf, length); } @@ -74,7 +82,12 @@ public void encodeText(Writer encoder, Context context, Object value, Calendar c } @Override - public void encodeBinary(Writer encoder, Object value, Calendar cal, Long maxLength) + public void encodeBinary( + final Writer encoder, + final Context context, + final Object value, + final Calendar cal, + final Long maxLength) throws IOException { encoder.writeDouble((Double) value); } 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 07431dc85..eb82ff82b 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/DurationCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/DurationCodec.java @@ -49,7 +49,11 @@ public boolean canEncode(Object value) { @Override @SuppressWarnings("fallthrough") public Duration decodeText( - ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar cal, + final Context context) throws SQLDataException { int[] parts; @@ -104,7 +108,11 @@ public Duration decodeText( @Override @SuppressWarnings("fallthrough") public Duration decodeBinary( - ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar cal, + final Context context) throws SQLDataException { long days = 0; @@ -208,7 +216,8 @@ public void encodeText(Writer encoder, Context context, Object val, Calendar cal } @Override - public void encodeBinary(Writer encoder, Object val, Calendar cal, Long maxLength) + public void encodeBinary( + Writer encoder, Context context, Object val, Calendar cal, Long maxLength) throws IOException { int nano = ((Duration) val).getNano(); if (nano > 0) { 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 2fb9aa488..ed31ebc10 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/FloatCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/FloatCodec.java @@ -53,22 +53,24 @@ public boolean canEncode(Object value) { @Override public Float decodeText( - final ReadableByteBuf buffer, + final ReadableByteBuf buf, final MutableInt length, final ColumnDecoder column, - final Calendar cal) + final Calendar cal, + final Context context) throws SQLDataException { - return column.decodeFloatText(buffer, length); + return column.decodeFloatText(buf, length); } @Override public Float decodeBinary( - final ReadableByteBuf buffer, + final ReadableByteBuf buf, final MutableInt length, final ColumnDecoder column, - final Calendar cal) + final Calendar cal, + final Context context) throws SQLDataException { - return column.decodeFloatBinary(buffer, length); + return column.decodeFloatBinary(buf, length); } @Override @@ -78,7 +80,12 @@ public void encodeText(Writer encoder, Context context, Object value, Calendar c } @Override - public void encodeBinary(Writer encoder, Object value, Calendar cal, Long maxLength) + public void encodeBinary( + final Writer encoder, + final Context context, + final Object value, + final Calendar cal, + final Long maxLength) throws IOException { encoder.writeFloat((Float) value); } 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 6ef9ef337..df00e4206 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/GeometryCollectionCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/GeometryCollectionCodec.java @@ -32,14 +32,22 @@ public boolean canEncode(Object value) { @Override public GeometryCollection decodeText( - ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar cal, + final Context context) throws SQLDataException { - return decodeBinary(buf, length, column, cal); + return decodeBinary(buf, length, column, cal, context); } @Override public GeometryCollection decodeBinary( - ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar cal, + final Context context) throws SQLDataException { if (column.getType() == DataType.GEOMETRY) { buf.skip(4); // SRID @@ -57,13 +65,22 @@ public GeometryCollection decodeBinary( @Override public void encodeText( - Writer encoder, Context context, Object value, Calendar cal, Long maxLength) + final Writer encoder, + final Context context, + final Object value, + final Calendar cal, + final Long maxLength) throws IOException { encoder.writeBytes(("ST_GeomCollFromText('" + value.toString() + "')").getBytes()); } @Override - public void encodeBinary(Writer encoder, Object value, Calendar cal, Long maxLength) + public void encodeBinary( + final Writer encoder, + final Context context, + final Object value, + final Calendar cal, + final Long maxLength) throws IOException { GeometryCollection geometryCollection = (GeometryCollection) value; 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 2e3ae94dc..164ed15af 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/InstantCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/InstantCodec.java @@ -48,20 +48,28 @@ public boolean canEncode(Object value) { @Override public Instant decodeText( - ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar calParam) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar calParam, + final Context context) throws SQLDataException { LocalDateTime localDateTime = - LocalDateTimeCodec.INSTANCE.decodeText(buf, length, column, calParam); + LocalDateTimeCodec.INSTANCE.decodeText(buf, length, column, calParam, context); if (localDateTime == null) return null; return localDateTime.atZone(ZoneId.systemDefault()).toInstant(); } @Override public Instant decodeBinary( - ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar calParam) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar calParam, + final Context context) throws SQLDataException { LocalDateTime localDateTime = - LocalDateTimeCodec.INSTANCE.decodeBinary(buf, length, column, calParam); + LocalDateTimeCodec.INSTANCE.decodeBinary(buf, length, column, calParam, context); if (localDateTime == null) return null; return localDateTime.atZone(ZoneId.systemDefault()).toInstant(); } @@ -92,7 +100,8 @@ public void encodeText( } @Override - public void encodeBinary(Writer encoder, Object value, Calendar calParam, Long maxLength) + public void encodeBinary( + Writer encoder, Context context, Object value, Calendar calParam, Long maxLength) throws IOException { Instant instant = (Instant) value; ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, ZoneId.systemDefault()); 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 c08e99262..de0de13ac 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/IntCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/IntCodec.java @@ -55,22 +55,24 @@ public boolean canEncode(Object value) { @Override public Integer decodeText( - final ReadableByteBuf buffer, + final ReadableByteBuf buf, final MutableInt length, final ColumnDecoder column, - final Calendar cal) + final Calendar cal, + final Context context) throws SQLDataException { - return column.decodeIntText(buffer, length); + return column.decodeIntText(buf, length); } @Override public Integer decodeBinary( - final ReadableByteBuf buffer, + final ReadableByteBuf buf, final MutableInt length, final ColumnDecoder column, - final Calendar cal) + final Calendar cal, + final Context context) throws SQLDataException { - return column.decodeIntBinary(buffer, length); + return column.decodeIntBinary(buf, length); } @Override @@ -80,7 +82,12 @@ public void encodeText(Writer encoder, Context context, Object value, Calendar c } @Override - public void encodeBinary(Writer encoder, Object value, Calendar cal, Long maxLength) + public void encodeBinary( + final Writer encoder, + final Context context, + final Object value, + final Calendar cal, + final Long maxLength) throws IOException { encoder.writeInt((Integer) value); } 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 4a0ad5b39..2d0cbc2ff 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/LineStringCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/LineStringCodec.java @@ -32,14 +32,22 @@ public boolean canEncode(Object value) { @Override public LineString decodeText( - ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar cal, + final Context context) throws SQLDataException { - return decodeBinary(buf, length, column, cal); + return decodeBinary(buf, length, column, cal, context); } @Override public LineString decodeBinary( - ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar cal, + final Context context) throws SQLDataException { if (column.getType() == DataType.GEOMETRY) { buf.skip(4); // SRID @@ -56,13 +64,22 @@ public LineString decodeBinary( @Override public void encodeText( - Writer encoder, Context context, Object value, Calendar cal, Long maxLength) + final Writer encoder, + final Context context, + final Object value, + final Calendar cal, + final Long maxLength) throws IOException { encoder.writeBytes(("ST_LineFromText('" + value.toString() + "')").getBytes()); } @Override - public void encodeBinary(Writer encoder, Object value, Calendar cal, Long maxLength) + public void encodeBinary( + final Writer encoder, + final Context context, + final Object value, + final Calendar cal, + final Long maxLength) throws IOException { LineString line = (LineString) value; 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 8aa55eeb2..d4273ae88 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/LocalDateCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/LocalDateCodec.java @@ -80,7 +80,11 @@ public boolean canEncode(Object value) { @Override @SuppressWarnings("fallthrough") public LocalDate decodeText( - ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar cal, + final Context context) throws SQLDataException { int[] parts; @@ -159,7 +163,11 @@ public LocalDate decodeText( @Override @SuppressWarnings("fallthrough") public LocalDate decodeBinary( - ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar cal, + final Context context) throws SQLDataException { int year; @@ -270,7 +278,8 @@ public void encodeText(Writer encoder, Context context, Object val, Calendar cal } @Override - public void encodeBinary(Writer encoder, Object value, Calendar providedCal, Long maxLength) + public void encodeBinary( + Writer encoder, Context context, Object value, Calendar providedCal, Long maxLength) throws IOException { LocalDate val = (LocalDate) value; encoder.writeByte(7); // length 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 4587a9b59..d2159dbbc 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/LocalDateTimeCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/LocalDateTimeCodec.java @@ -3,12 +3,11 @@ // Copyright (c) 2015-2024 MariaDB Corporation Ab 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; import java.time.LocalDateTime; +import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatterBuilder; import java.util.Calendar; @@ -121,194 +120,29 @@ public boolean canEncode(Object value) { @Override @SuppressWarnings("fallthrough") public LocalDateTime decodeText( - ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar cal, + final Context context) throws SQLDataException { - int[] parts; - switch (column.getType()) { - case BLOB: - case TINYBLOB: - case MEDIUMBLOB: - case LONGBLOB: - if (column.isBinary()) { - buf.skip(length.get()); - throw new SQLDataException( - String.format("Data type %s cannot be decoded as LocalDateTime", column.getType())); - } - // expected fallthrough - // BLOB is considered as String if it has a collation (this is TEXT column) - - case STRING: - case VARCHAR: - case VARSTRING: - String val = buf.readString(length.get()); - try { - parts = parseTimestamp(val); - 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) { - throw new SQLDataException( - String.format( - "value '%s' (%s) cannot be decoded as LocalDateTime", val, column.getType())); - } - - case DATE: - parts = LocalDateCodec.parseDate(buf, length); - 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.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]); - - case TIME: - parts = LocalTimeCodec.parseTime(buf, length, column); - if (parts[0] == -1) { - return LocalDateTime.of(1970, 1, 1, 0, 0) - .minusHours(parts[1] % 24) - .minusMinutes(parts[2]) - .minusSeconds(parts[3]) - .minusNanos(parts[4]); - } - 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.get())); - if (column.getColumnLength() <= 2) year += year >= 70 ? 1900 : 2000; - return LocalDateTime.of(year, 1, 1, 0, 0); - - default: - buf.skip(length.get()); - throw new SQLDataException( - String.format("Data type %s cannot be decoded as LocalDateTime", column.getType())); - } + ZonedDateTime zdt = ZonedDateTimeCodec.INSTANCE.decodeText(buf, length, column, cal, context); + if (zdt == null) return null; + return zdt.toLocalDateTime(); } @Override @SuppressWarnings("fallthrough") public LocalDateTime decodeBinary( - ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar cal, + final Context context) throws SQLDataException { - int year = 1970; - int month = 1; - long dayOfMonth = 1; - int hour = 0; - int minutes = 0; - int seconds = 0; - long microseconds = 0; - - switch (column.getType()) { - case TIME: - 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.get() > 8) { - microseconds = buf.readUnsignedInt(); - } - - if (negate) { - return LocalDateTime.of(1970, 1, 1, 0, 0) - .minusDays(day) - .minusHours(hour) - .minusMinutes(minutes) - .minusSeconds(seconds) - .minusNanos(microseconds * 1000); - } - } - break; - - case BLOB: - case TINYBLOB: - case MEDIUMBLOB: - case LONGBLOB: - if (column.isBinary()) { - buf.skip(length.get()); - throw new SQLDataException( - String.format("Data type %s cannot be decoded as LocalDateTime", column.getType())); - } - // expected fallthrough - // BLOB is considered as String if it has a collation (this is TEXT column) - - case STRING: - case VARCHAR: - case VARSTRING: - String val = buf.readString(length.get()); - try { - int[] parts = parseTimestamp(val); - 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) { - throw new SQLDataException( - String.format( - "value '%s' (%s) cannot be decoded as LocalDateTime", val, column.getType())); - } - - case DATE: - case TIMESTAMP: - case DATETIME: - if (length.get() == 0) { - length.set(NULL_LENGTH); - return null; - } - year = buf.readUnsignedShort(); - month = buf.readByte(); - dayOfMonth = buf.readByte(); - - if (length.get() > 4) { - hour = buf.readByte(); - minutes = buf.readByte(); - seconds = buf.readByte(); - - 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) { - length.set(NULL_LENGTH); - return null; - } - - break; - - case YEAR: - year = buf.readUnsignedShort(); - if (column.getColumnLength() <= 2) year += year >= 70 ? 1900 : 2000; - break; - default: - buf.skip(length.get()); - throw new SQLDataException( - String.format("Data type %s cannot be decoded as LocalDateTime", column.getType())); - } - - return LocalDateTime.of(year, month, (int) dayOfMonth, hour, minutes, seconds) - .plusNanos(microseconds * 1000); + ZonedDateTime zdt = ZonedDateTimeCodec.INSTANCE.decodeBinary(buf, length, column, cal, context); + if (zdt == null) return null; + return zdt.toLocalDateTime(); } @Override @@ -322,7 +156,12 @@ public void encodeText(Writer encoder, Context context, Object value, Calendar c } @Override - public void encodeBinary(Writer encoder, Object value, Calendar cal, Long maxLength) + public void encodeBinary( + final Writer encoder, + final Context context, + final Object value, + final Calendar cal, + final Long maxLength) throws IOException { LocalDateTime val = (LocalDateTime) value; int nano = val.getNano(); 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 86d318582..0884f34ef 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/LocalTimeCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/LocalTimeCodec.java @@ -110,7 +110,11 @@ public boolean canEncode(Object value) { @Override @SuppressWarnings("fallthrough") public LocalTime decodeText( - ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar cal, + final Context context) throws SQLDataException { int[] parts; @@ -175,7 +179,11 @@ public LocalTime decodeText( @Override @SuppressWarnings("fallthrough") public LocalTime decodeBinary( - ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar cal, + final Context context) throws SQLDataException { int hour = 0; @@ -306,7 +314,12 @@ public void encodeText(Writer encoder, Context context, Object value, Calendar c } @Override - public void encodeBinary(Writer encoder, Object value, Calendar cal, Long maxLength) + public void encodeBinary( + final Writer encoder, + final Context context, + final Object value, + final Calendar cal, + final Long maxLength) throws IOException { LocalTime val = (LocalTime) value; int nano = val.getNano(); 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 242a5b8b0..657571b54 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/LongCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/LongCodec.java @@ -55,22 +55,24 @@ public boolean canEncode(Object value) { @Override public Long decodeText( - final ReadableByteBuf buffer, + final ReadableByteBuf buf, final MutableInt length, final ColumnDecoder column, - final Calendar cal) + final Calendar cal, + final Context context) throws SQLDataException { - return column.decodeLongText(buffer, length); + return column.decodeLongText(buf, length); } @Override public Long decodeBinary( - final ReadableByteBuf buffer, + final ReadableByteBuf buf, final MutableInt length, final ColumnDecoder column, - final Calendar cal) + final Calendar cal, + final Context context) throws SQLDataException { - return column.decodeLongBinary(buffer, length); + return column.decodeLongBinary(buf, length); } @Override @@ -80,7 +82,12 @@ public void encodeText(Writer encoder, Context context, Object value, Calendar c } @Override - public void encodeBinary(Writer encoder, Object value, Calendar cal, Long maxLength) + public void encodeBinary( + final Writer encoder, + final Context context, + final Object value, + final Calendar cal, + final Long maxLength) throws IOException { encoder.writeLong((Long) value); } 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 6e7ccd039..f8077cdfd 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/MultiLinestringCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/MultiLinestringCodec.java @@ -35,14 +35,22 @@ public boolean canEncode(Object value) { @Override public MultiLineString decodeText( - ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar cal, + final Context context) throws SQLDataException { - return decodeBinary(buf, length, column, cal); + return decodeBinary(buf, length, column, cal, context); } @Override public MultiLineString decodeBinary( - ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar cal, + final Context context) throws SQLDataException { if (column.getType() == DataType.GEOMETRY) { buf.skip(4); // SRID @@ -59,13 +67,22 @@ public MultiLineString decodeBinary( @Override public void encodeText( - Writer encoder, Context context, Object value, Calendar cal, Long maxLength) + final Writer encoder, + final Context context, + final Object value, + final Calendar cal, + final Long maxLength) throws IOException { encoder.writeBytes(("ST_MLineFromText('" + value.toString() + "')").getBytes()); } @Override - public void encodeBinary(Writer encoder, Object value, Calendar cal, Long maxLength) + public void encodeBinary( + final Writer encoder, + final Context context, + final Object value, + final Calendar cal, + final Long maxLength) throws IOException { MultiLineString mp = (MultiLineString) value; 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 15596b454..a71b2b223 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/MultiPointCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/MultiPointCodec.java @@ -34,14 +34,22 @@ public boolean canEncode(Object value) { @Override public MultiPoint decodeText( - ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar cal, + final Context context) throws SQLDataException { - return decodeBinary(buf, length, column, cal); + return decodeBinary(buf, length, column, cal, context); } @Override public MultiPoint decodeBinary( - ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar cal, + final Context context) throws SQLDataException { if (column.getType() == DataType.GEOMETRY) { buf.skip(4); // SRID @@ -58,13 +66,22 @@ public MultiPoint decodeBinary( @Override public void encodeText( - Writer encoder, Context context, Object value, Calendar cal, Long maxLength) + final Writer encoder, + final Context context, + final Object value, + final Calendar cal, + final Long maxLength) throws IOException { encoder.writeBytes(("ST_MPointFromText('" + value.toString() + "')").getBytes()); } @Override - public void encodeBinary(Writer encoder, Object value, Calendar cal, Long maxLength) + public void encodeBinary( + final Writer encoder, + final Context context, + final Object value, + final Calendar cal, + final Long maxLength) throws IOException { MultiPoint mp = (MultiPoint) value; encoder.writeLength(13 + mp.getPoints().length * 21L); 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 9db1822a8..148f71380 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/MultiPolygonCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/MultiPolygonCodec.java @@ -32,14 +32,22 @@ public boolean canEncode(Object value) { @Override public MultiPolygon decodeText( - ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar cal, + final Context context) throws SQLDataException { - return decodeBinary(buf, length, column, cal); + return decodeBinary(buf, length, column, cal, context); } @Override public MultiPolygon decodeBinary( - ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar cal, + final Context context) throws SQLDataException { if (column.getType() == DataType.GEOMETRY) { buf.skip(4); // SRID @@ -56,13 +64,22 @@ public MultiPolygon decodeBinary( @Override public void encodeText( - Writer encoder, Context context, Object value, Calendar cal, Long maxLength) + final Writer encoder, + final Context context, + final Object value, + final Calendar cal, + final Long maxLength) throws IOException { encoder.writeBytes(("ST_MPolyFromText('" + value.toString() + "')").getBytes()); } @Override - public void encodeBinary(Writer encoder, Object value, Calendar cal, Long maxLength) + public void encodeBinary( + final Writer encoder, + final Context context, + final Object value, + final Calendar cal, + final Long maxLength) throws IOException { MultiPolygon mariadbMultiPolygon = (MultiPolygon) value; 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 8acd0f951..26c3c29d4 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/OffsetDateTimeCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/OffsetDateTimeCodec.java @@ -3,6 +3,8 @@ // Copyright (c) 2015-2024 MariaDB Corporation Ab 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; @@ -11,6 +13,7 @@ import java.util.Calendar; import java.util.EnumSet; import org.mariadb.jdbc.client.*; +import org.mariadb.jdbc.client.column.TimestampColumn; import org.mariadb.jdbc.client.socket.Writer; import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.plugin.Codec; @@ -51,25 +54,42 @@ public boolean canEncode(Object value) { @Override public OffsetDateTime decodeText( - ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar calParam) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar calParam, + final Context context) throws SQLDataException { switch (column.getType()) { case DATETIME: case TIMESTAMP: - LocalDateTime localDateTime = - LocalDateTimeCodec.INSTANCE.decodeText(buf, length, column, calParam); - if (localDateTime == null) return null; - Calendar cal = calParam == null ? Calendar.getInstance() : calParam; - return localDateTime.atZone(cal.getTimeZone().toZoneId()).toOffsetDateTime(); + ZonedDateTime zdt = + ZonedDateTimeCodec.INSTANCE.decodeText(buf, length, column, calParam, context); + if (zdt == null) return null; + return zdt.toOffsetDateTime(); case STRING: case VARCHAR: case VARSTRING: String val = buf.readString(length.get()); try { - return OffsetDateTime.parse(val); + int[] parts = LocalDateTimeCodec.parseTimestamp(val); + if (parts == null) { + length.set(NULL_LENGTH); + return null; + } + return TimestampColumn.localDateTimeToZoneDateTime( + LocalDateTime.of(parts[0], parts[1], parts[2], parts[3], parts[4], parts[5]) + .plusNanos(parts[6]), + calParam, + context) + .toOffsetDateTime(); } catch (Throwable e) { - // eat + try { + return OffsetDateTime.parse(val); + } catch (Throwable ee) { + // eat + } } throw new SQLDataException( String.format( @@ -84,25 +104,42 @@ public OffsetDateTime decodeText( @Override public OffsetDateTime decodeBinary( - ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar calParam) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar calParam, + final Context context) throws SQLDataException { switch (column.getType()) { case DATETIME: case TIMESTAMP: - LocalDateTime localDateTime = - LocalDateTimeCodec.INSTANCE.decodeBinary(buf, length, column, calParam); - if (localDateTime == null) return null; - Calendar cal = calParam == null ? Calendar.getInstance() : calParam; - return localDateTime.atZone(cal.getTimeZone().toZoneId()).toOffsetDateTime(); + ZonedDateTime zdt = + ZonedDateTimeCodec.INSTANCE.decodeBinary(buf, length, column, calParam, context); + if (zdt == null) return null; + return zdt.toOffsetDateTime(); case STRING: case VARCHAR: case VARSTRING: String val = buf.readString(length.get()); try { - return OffsetDateTime.parse(val); + int[] parts = LocalDateTimeCodec.parseTimestamp(val); + if (parts == null) { + length.set(NULL_LENGTH); + return null; + } + return TimestampColumn.localDateTimeToZoneDateTime( + LocalDateTime.of(parts[0], parts[1], parts[2], parts[3], parts[4], parts[5]) + .plusNanos(parts[6]), + calParam, + context) + .toOffsetDateTime(); } catch (Throwable e) { - // eat + try { + return OffsetDateTime.parse(val); + } catch (Throwable ee) { + // eat + } } throw new SQLDataException( String.format( @@ -121,7 +158,7 @@ public void encodeText( Writer encoder, Context context, Object val, Calendar calParam, Long maxLen) throws IOException { OffsetDateTime zdt = (OffsetDateTime) val; - Calendar cal = calParam == null ? Calendar.getInstance() : calParam; + Calendar cal = calParam == null ? context.getDefaultCalendar() : calParam; encoder.writeByte('\''); encoder.writeAscii( zdt.atZoneSameInstant(cal.getTimeZone().toZoneId()) @@ -133,10 +170,11 @@ public void encodeText( } @Override - public void encodeBinary(Writer encoder, Object value, Calendar calParam, Long maxLength) + public void encodeBinary( + Writer encoder, Context context, Object value, Calendar calParam, Long maxLength) throws IOException { OffsetDateTime zdt = (OffsetDateTime) value; - Calendar cal = calParam == null ? Calendar.getInstance() : calParam; + Calendar cal = calParam == null ? context.getDefaultCalendar() : calParam; ZonedDateTime convertedZdt = zdt.atZoneSameInstant(cal.getTimeZone().toZoneId()); int nano = convertedZdt.getNano(); if (nano > 0) { 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 6472406a6..41d43d7b5 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/PointCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/PointCodec.java @@ -32,14 +32,22 @@ public boolean canEncode(Object value) { @Override public Point decodeText( - ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar cal, + final Context context) throws SQLDataException { - return decodeBinary(buf, length, column, cal); + return decodeBinary(buf, length, column, cal, context); } @Override public Point decodeBinary( - ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar cal, + final Context context) throws SQLDataException { if (column.getType() == DataType.GEOMETRY) { buf.skip(4); // SRID @@ -55,13 +63,22 @@ public Point decodeBinary( @Override public void encodeText( - Writer encoder, Context context, Object value, Calendar cal, Long maxLength) + final Writer encoder, + final Context context, + final Object value, + final Calendar cal, + final Long maxLength) throws IOException { encoder.writeBytes(("ST_PointFromText('" + value.toString() + "')").getBytes()); } @Override - public void encodeBinary(Writer encoder, Object value, Calendar cal, Long maxLength) + public void encodeBinary( + final Writer encoder, + final Context context, + final Object value, + final Calendar cal, + final Long maxLength) throws IOException { Point pt = (Point) value; encoder.writeLength(25); 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 06c436a2e..20a871e95 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/PolygonCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/PolygonCodec.java @@ -35,14 +35,22 @@ public boolean canEncode(Object value) { @Override public Polygon decodeText( - ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar cal, + final Context context) throws SQLDataException { - return decodeBinary(buf, length, column, cal); + return decodeBinary(buf, length, column, cal, context); } @Override public Polygon decodeBinary( - ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar cal, + final Context context) throws SQLDataException { if (column.getType() == DataType.GEOMETRY) { buf.skip(4); // SRID @@ -59,13 +67,22 @@ public Polygon decodeBinary( @Override public void encodeText( - Writer encoder, Context context, Object value, Calendar cal, Long maxLength) + final Writer encoder, + final Context context, + final Object value, + final Calendar cal, + final Long maxLength) throws IOException { encoder.writeBytes(("ST_PolyFromText('" + value.toString() + "')").getBytes()); } @Override - public void encodeBinary(Writer encoder, Object value, Calendar cal, Long maxLength) + public void encodeBinary( + final Writer encoder, + final Context context, + final Object value, + final Calendar cal, + final Long maxLength) throws IOException { Polygon poly = (Polygon) value; 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 2ba90ad27..7c97e2151 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/ReaderCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/ReaderCodec.java @@ -41,7 +41,11 @@ public String className() { @Override @SuppressWarnings("fallthrough") public Reader decodeText( - ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar cal, + final Context context) throws SQLDataException { switch (column.getType()) { case BLOB: @@ -70,9 +74,13 @@ public Reader decodeText( @Override public Reader decodeBinary( - ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar cal, + final Context context) throws SQLDataException { - return decodeText(buf, length, column, cal); + return decodeText(buf, length, column, cal, context); } public boolean canEncode(Object value) { @@ -109,7 +117,8 @@ public void encodeText(Writer encoder, Context context, Object val, Calendar cal } @Override - public void encodeBinary(Writer encoder, Object val, Calendar cal, Long maxLength) + public void encodeBinary( + Writer encoder, Context context, Object val, Calendar cal, Long maxLength) throws IOException { // prefer use of encodeLongData, because length is unknown byte[] clobBytes = new byte[4096]; 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 454da9fe3..2867204d6 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/ShortCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/ShortCodec.java @@ -55,22 +55,24 @@ public String className() { @Override public Short decodeText( - final ReadableByteBuf buffer, + final ReadableByteBuf buf, final MutableInt length, final ColumnDecoder column, - final Calendar cal) + final Calendar cal, + final Context context) throws SQLDataException { - return column.decodeShortText(buffer, length); + return column.decodeShortText(buf, length); } @Override public Short decodeBinary( - final ReadableByteBuf buffer, + final ReadableByteBuf buf, final MutableInt length, final ColumnDecoder column, - final Calendar cal) + final Calendar cal, + final Context context) throws SQLDataException { - return column.decodeShortBinary(buffer, length); + return column.decodeShortBinary(buf, length); } @Override @@ -80,7 +82,12 @@ public void encodeText(Writer encoder, Context context, Object value, Calendar c } @Override - public void encodeBinary(Writer encoder, Object value, Calendar cal, Long maxLength) + public void encodeBinary( + final Writer encoder, + final Context context, + final Object value, + final Calendar cal, + final Long maxLength) throws IOException { encoder.writeShort((Short) value); } 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 966bc0de1..e205f056f 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/StreamCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/StreamCodec.java @@ -39,7 +39,11 @@ public boolean canDecode(ColumnDecoder column, Class type) { @Override public InputStream decodeText( - ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar cal, + final Context context) throws SQLDataException { switch (column.getType()) { case STRING: @@ -61,7 +65,11 @@ public InputStream decodeText( @Override public InputStream decodeBinary( - ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar cal, + final Context context) throws SQLDataException { switch (column.getType()) { case STRING: @@ -111,7 +119,12 @@ public void encodeText(Writer encoder, Context context, Object value, Calendar c } @Override - public void encodeBinary(Writer encoder, Object value, Calendar cal, Long maxLength) + public void encodeBinary( + final Writer encoder, + final Context context, + final Object value, + final Calendar cal, + final Long maxLength) throws IOException { // length is not known byte[] blobBytes = new byte[4096]; 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 d111d1900..f609511db 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/StringCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/StringCodec.java @@ -65,18 +65,20 @@ public String decodeText( final ReadableByteBuf buf, final MutableInt length, final ColumnDecoder column, - final Calendar cal) + final Calendar cal, + final Context context) throws SQLDataException { - return column.decodeStringText(buf, length, cal); + return column.decodeStringText(buf, length, cal, context); } public String decodeBinary( final ReadableByteBuf buf, final MutableInt length, final ColumnDecoder column, - final Calendar cal) + final Calendar cal, + final Context context) throws SQLDataException { - return column.decodeStringBinary(buf, length, cal); + return column.decodeStringBinary(buf, length, cal, context); } public void encodeText(Writer encoder, Context context, Object value, Calendar cal, Long maxLen) @@ -88,7 +90,8 @@ public void encodeText(Writer encoder, Context context, Object value, Calendar c encoder.writeByte('\''); } - public void encodeBinary(Writer writer, Object value, Calendar cal, Long maxLength) + public void encodeBinary( + Writer writer, Context context, Object value, Calendar cal, Long maxLength) throws IOException { byte[] b = value.toString().getBytes(StandardCharsets.UTF_8); int len = maxLength != null ? Math.min(maxLength.intValue(), b.length) : b.length; 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 c148f1204..ef2eb17d3 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/TimeCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/TimeCodec.java @@ -53,17 +53,26 @@ public boolean canEncode(Object value) { @Override @SuppressWarnings("fallthrough") - public Time decodeText(ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + public Time decodeText( + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar cal, + final Context context) throws SQLDataException { - return column.decodeTimeText(buf, length, cal); + return column.decodeTimeText(buf, length, cal, context); } @Override @SuppressWarnings("fallthrough") public Time decodeBinary( - ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar calParam) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar calParam, + final Context context) throws SQLDataException { - return column.decodeTimeBinary(buf, length, calParam); + return column.decodeTimeBinary(buf, length, calParam, context); } @Override @@ -81,7 +90,8 @@ public void encodeText( } @Override - public void encodeBinary(Writer encoder, Object value, Calendar providedCal, Long maxLength) + public void encodeBinary( + Writer encoder, Context context, Object value, Calendar providedCal, Long maxLength) throws IOException { if (providedCal == null) { Calendar cal = Calendar.getInstance(); 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 d865c4e14..b3b5f45fa 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/TimestampCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/TimestampCodec.java @@ -51,17 +51,25 @@ public boolean canEncode(Object value) { @Override @SuppressWarnings("fallthrough") public Timestamp decodeText( - ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar cal, + final Context context) throws SQLDataException { - return column.decodeTimestampText(buf, length, cal); + return column.decodeTimestampText(buf, length, cal, context); } @Override @SuppressWarnings("fallthrough") public Timestamp decodeBinary( - ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar cal) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar cal, + final Context context) throws SQLDataException { - return column.decodeTimestampBinary(buf, length, cal); + return column.decodeTimestampBinary(buf, length, cal, context); } @Override @@ -69,7 +77,7 @@ public void encodeText( Writer encoder, Context context, Object val, Calendar providedCal, Long maxLen) throws IOException { Timestamp ts = (Timestamp) val; - Calendar cal = providedCal == null ? Calendar.getInstance() : providedCal; + Calendar cal = providedCal == null ? context.getDefaultCalendar() : providedCal; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); sdf.setTimeZone(cal.getTimeZone()); String dateString = sdf.format(ts); @@ -90,11 +98,12 @@ public void encodeText( } @Override - public void encodeBinary(Writer encoder, Object value, Calendar providedCal, Long maxLength) + public void encodeBinary( + Writer encoder, Context context, Object value, Calendar providedCal, Long maxLength) throws IOException { Timestamp ts = (Timestamp) value; if (providedCal == null) { - Calendar cal = Calendar.getInstance(); + Calendar cal = context.getDefaultCalendar(); cal.clear(); cal.setTimeInMillis(ts.getTime()); if (ts.getNanos() == 0) { 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 10296fc84..fe32ca90e 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/UuidCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/UuidCodec.java @@ -41,18 +41,20 @@ public UUID decodeText( final ReadableByteBuf buf, final MutableInt length, final ColumnDecoder column, - final Calendar cal) + final Calendar cal, + final Context context) throws SQLDataException { - return UUID.fromString(column.decodeStringText(buf, length, cal)); + return UUID.fromString(column.decodeStringText(buf, length, cal, context)); } public UUID decodeBinary( final ReadableByteBuf buf, final MutableInt length, final ColumnDecoder column, - final Calendar cal) + final Calendar cal, + final Context context) throws SQLDataException { - return UUID.fromString(column.decodeStringBinary(buf, length, cal)); + return UUID.fromString(column.decodeStringBinary(buf, length, cal, context)); } public void encodeText(Writer encoder, Context context, Object value, Calendar cal, Long maxLen) @@ -62,7 +64,8 @@ public void encodeText(Writer encoder, Context context, Object value, Calendar c encoder.writeByte('\''); } - public void encodeBinary(Writer writer, Object value, Calendar cal, Long maxLength) + public void encodeBinary( + Writer writer, Context context, Object value, Calendar cal, Long maxLength) throws IOException { String valueSt = value.toString(); writer.writeLength(valueSt.length()); 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 b88881633..79ce6c643 100644 --- a/src/main/java/org/mariadb/jdbc/plugin/codec/ZonedDateTimeCodec.java +++ b/src/main/java/org/mariadb/jdbc/plugin/codec/ZonedDateTimeCodec.java @@ -3,13 +3,18 @@ // Copyright (c) 2015-2024 MariaDB Corporation Ab 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; import java.time.LocalDateTime; import java.time.ZonedDateTime; import java.util.Calendar; import java.util.EnumSet; +import java.util.TimeZone; import org.mariadb.jdbc.client.*; +import org.mariadb.jdbc.client.column.TimestampColumn; import org.mariadb.jdbc.client.socket.Writer; import org.mariadb.jdbc.client.util.MutableInt; import org.mariadb.jdbc.plugin.Codec; @@ -50,24 +55,236 @@ public boolean canEncode(Object value) { @Override public ZonedDateTime decodeText( - ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar calParam) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar calParam, + final Context context) throws SQLDataException { - LocalDateTime localDateTime = - LocalDateTimeCodec.INSTANCE.decodeText(buf, length, column, calParam); - if (localDateTime == null) return null; - Calendar cal = calParam == null ? Calendar.getInstance() : calParam; - return localDateTime.atZone(cal.getTimeZone().toZoneId()); + int[] parts; + switch (column.getType()) { + case BLOB: + case TINYBLOB: + case MEDIUMBLOB: + case LONGBLOB: + if (column.isBinary()) { + buf.skip(length.get()); + throw new SQLDataException( + String.format("Data type %s cannot be decoded as ZoneDateTime", column.getType())); + } + // expected fallthrough + // BLOB is considered as String if it has a collation (this is TEXT column) + + case STRING: + case VARCHAR: + case VARSTRING: + String val = buf.readString(length.get()); + try { + parts = LocalDateTimeCodec.parseTimestamp(val); + if (parts == null) { + length.set(NULL_LENGTH); + return null; + } + return TimestampColumn.localDateTimeToZoneDateTime( + LocalDateTime.of(parts[0], parts[1], parts[2], parts[3], parts[4], parts[5]) + .plusNanos(parts[6]), + calParam, + context); + } catch (DateTimeException dte) { + throw new SQLDataException( + String.format( + "value '%s' (%s) cannot be decoded as ZoneDateTime", val, column.getType())); + } + + case DATE: + parts = LocalDateCodec.parseDate(buf, length); + if (parts == null) { + length.set(NULL_LENGTH); + return null; + } + TimeZone tz = calParam == null ? TimeZone.getDefault() : calParam.getTimeZone(); + return LocalDateTime.of(parts[0], parts[1], parts[2], 0, 0, 0).atZone(tz.toZoneId()); + + case DATETIME: + case TIMESTAMP: + parts = LocalDateTimeCodec.parseTimestamp(buf.readAscii(length.get())); + if (parts == null) { + length.set(NULL_LENGTH); + return null; + } + LocalDateTime ldt = + LocalDateTime.of(parts[0], parts[1], parts[2], parts[3], parts[4], parts[5]) + .plusNanos(parts[6]); + return TimestampColumn.localDateTimeToZoneDateTime(ldt, calParam, context); + + case TIME: + parts = LocalTimeCodec.parseTime(buf, length, column); + TimeZone tzTime = calParam == null ? TimeZone.getDefault() : calParam.getTimeZone(); + if (parts[0] == -1) { + return LocalDateTime.of(1970, 1, 1, 0, 0) + .minusHours(parts[1] % 24) + .minusMinutes(parts[2]) + .minusSeconds(parts[3]) + .minusNanos(parts[4]) + .atZone(tzTime.toZoneId()); + } + return LocalDateTime.of(1970, 1, 1, parts[1] % 24, parts[2], parts[3]) + .plusNanos(parts[4]) + .atZone(tzTime.toZoneId()); + + case YEAR: + int year = Integer.parseInt(buf.readAscii(length.get())); + if (column.getColumnLength() <= 2) year += year >= 70 ? 1900 : 2000; + TimeZone tzYear = calParam == null ? TimeZone.getDefault() : calParam.getTimeZone(); + return LocalDateTime.of(year, 1, 1, 0, 0).atZone(tzYear.toZoneId()); + + default: + buf.skip(length.get()); + throw new SQLDataException( + String.format("Data type %s cannot be decoded as ZoneDateTime", column.getType())); + } } @Override public ZonedDateTime decodeBinary( - ReadableByteBuf buf, MutableInt length, ColumnDecoder column, Calendar calParam) + final ReadableByteBuf buf, + final MutableInt length, + final ColumnDecoder column, + final Calendar calParam, + final Context context) throws SQLDataException { - LocalDateTime localDateTime = - LocalDateTimeCodec.INSTANCE.decodeBinary(buf, length, column, calParam); - if (localDateTime == null) return null; - Calendar cal = calParam == null ? Calendar.getInstance() : calParam; - return localDateTime.atZone(cal.getTimeZone().toZoneId()); + int year = 1970; + int month = 1; + long dayOfMonth = 1; + int hour = 0; + int minutes = 0; + int seconds = 0; + long microseconds = 0; + + switch (column.getType()) { + case TIME: + TimeZone tzTime = calParam == null ? TimeZone.getDefault() : calParam.getTimeZone(); + 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.get() > 8) { + microseconds = buf.readUnsignedInt(); + } + if (negate) { + return LocalDateTime.of(1970, 1, 1, 0, 0) + .minusDays(day) + .minusHours(hour) + .minusMinutes(minutes) + .minusSeconds(seconds) + .minusNanos(microseconds * 1000) + .atZone(tzTime.toZoneId()); + } + } + return LocalDateTime.of(year, month, (int) dayOfMonth, hour, minutes, seconds) + .plusNanos(microseconds * 1000) + .atZone(tzTime.toZoneId()); + + case BLOB: + case TINYBLOB: + case MEDIUMBLOB: + case LONGBLOB: + if (column.isBinary()) { + buf.skip(length.get()); + throw new SQLDataException( + String.format("Data type %s cannot be decoded as ZoneDateTime", column.getType())); + } + // expected fallthrough + // BLOB is considered as String if it has a collation (this is TEXT column) + + case STRING: + case VARCHAR: + case VARSTRING: + String val = buf.readString(length.get()); + try { + int[] parts = LocalDateTimeCodec.parseTimestamp(val); + if (parts == null) { + length.set(NULL_LENGTH); + return null; + } + return TimestampColumn.localDateTimeToZoneDateTime( + LocalDateTime.of(parts[0], parts[1], parts[2], parts[3], parts[4], parts[5]) + .plusNanos(parts[6]), + calParam, + context); + } catch (DateTimeException dte) { + throw new SQLDataException( + String.format( + "value '%s' (%s) cannot be decoded as ZoneDateTime", val, column.getType())); + } + + case DATE: + if (length.get() == 0) { + length.set(NULL_LENGTH); + return null; + } + year = buf.readUnsignedShort(); + month = buf.readByte(); + dayOfMonth = buf.readByte(); + + // xpand workaround https://jira.mariadb.org/browse/XPT-274 + if (year == 0 && month == 0 && dayOfMonth == 0) { + length.set(NULL_LENGTH); + return null; + } + TimeZone tz = calParam == null ? TimeZone.getDefault() : calParam.getTimeZone(); + return LocalDateTime.of(year, month, (int) dayOfMonth, 0, 0, 0).atZone(tz.toZoneId()); + + case TIMESTAMP: + case DATETIME: + if (length.get() == 0) { + length.set(NULL_LENGTH); + return null; + } + year = buf.readUnsignedShort(); + month = buf.readByte(); + dayOfMonth = buf.readByte(); + + if (length.get() > 4) { + hour = buf.readByte(); + minutes = buf.readByte(); + seconds = buf.readByte(); + + 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) { + length.set(NULL_LENGTH); + return null; + } + + LocalDateTime ldt = + LocalDateTime.of(year, month, (int) dayOfMonth, hour, minutes, seconds) + .plusNanos(microseconds * 1000); + return TimestampColumn.localDateTimeToZoneDateTime(ldt, calParam, context); + + case YEAR: + year = buf.readUnsignedShort(); + if (column.getColumnLength() <= 2) year += year >= 70 ? 1900 : 2000; + TimeZone tzYear = calParam == null ? TimeZone.getDefault() : calParam.getTimeZone(); + return LocalDateTime.of(year, 1, 1, 0, 0).atZone(tzYear.toZoneId()); + + default: + buf.skip(length.get()); + throw new SQLDataException( + String.format("Data type %s cannot be decoded as LocalDateTime", column.getType())); + } } @Override @@ -75,7 +292,7 @@ public void encodeText( Writer encoder, Context context, Object val, Calendar calParam, Long maxLen) throws IOException { ZonedDateTime zdt = (ZonedDateTime) val; - Calendar cal = calParam == null ? Calendar.getInstance() : calParam; + Calendar cal = calParam == null ? context.getDefaultCalendar() : calParam; encoder.writeByte('\''); encoder.writeAscii( zdt.withZoneSameInstant(cal.getTimeZone().toZoneId()) @@ -87,10 +304,11 @@ public void encodeText( } @Override - public void encodeBinary(Writer encoder, Object value, Calendar calParam, Long maxLength) + public void encodeBinary( + Writer encoder, Context context, Object value, Calendar calParam, Long maxLength) throws IOException { ZonedDateTime zdt = (ZonedDateTime) value; - Calendar cal = calParam == null ? Calendar.getInstance() : calParam; + Calendar cal = calParam == null ? context.getDefaultCalendar() : calParam; ZonedDateTime convertedZdt = zdt.withZoneSameInstant(cal.getTimeZone().toZoneId()); int nano = convertedZdt.getNano(); if (nano > 0) { diff --git a/src/main/resources/driver.properties b/src/main/resources/driver.properties index 21c10acf2..c8a1141ea 100644 --- a/src/main/resources/driver.properties +++ b/src/main/resources/driver.properties @@ -78,3 +78,6 @@ permitRedirect=permit server redirection. Default is true fallbackToSystemKeyStore=Indicate if keystore default implementation can be used. Connector use keystore option if set or if not, use javax.net.ssl.keyStore* system properties to load keystore if this option is enable fallbackToSystemTrustStore=Indicate if truststore default implementation can be used. Connector use serverSslCert option if set or if not, use java default truststore if this option is enable nullDatabaseMeansCurrent=When enable, in DatabaseMetadata, will handle null database/schema (depending on UseCatalog=catalog/schema) as current. (Alias nullCatalogMeansCurrent) +preserveInstants= +connectionTimeZone= +forceConnectionTimeZoneToSession= \ No newline at end of file diff --git a/src/test/java/org/mariadb/jdbc/integration/ConnectionTest.java b/src/test/java/org/mariadb/jdbc/integration/ConnectionTest.java index c88c84e1d..5e6031357 100644 --- a/src/test/java/org/mariadb/jdbc/integration/ConnectionTest.java +++ b/src/test/java/org/mariadb/jdbc/integration/ConnectionTest.java @@ -8,10 +8,7 @@ import java.sql.*; import java.sql.Connection; import java.sql.Statement; -import java.util.Locale; -import java.util.Properties; -import java.util.Timer; -import java.util.TimerTask; +import java.util.*; import java.util.concurrent.Executor; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; diff --git a/src/test/java/org/mariadb/jdbc/integration/ErrorTest.java b/src/test/java/org/mariadb/jdbc/integration/ErrorTest.java index 55489767c..94cd132fc 100644 --- a/src/test/java/org/mariadb/jdbc/integration/ErrorTest.java +++ b/src/test/java/org/mariadb/jdbc/integration/ErrorTest.java @@ -6,12 +6,14 @@ import static org.junit.jupiter.api.Assertions.*; import java.sql.*; +import java.time.ZonedDateTime; import org.junit.jupiter.api.*; public class ErrorTest extends Common { @BeforeAll public static void begin() throws SQLException { + System.out.println(ZonedDateTime.now()); drop(); Statement stmt = sharedConn.createStatement(); stmt.execute("CREATE TABLE deadlock(a int primary key)"); diff --git a/src/test/java/org/mariadb/jdbc/integration/PreparedStatementParametersTest.java b/src/test/java/org/mariadb/jdbc/integration/PreparedStatementParametersTest.java index 2fcf412bd..7159495bd 100644 --- a/src/test/java/org/mariadb/jdbc/integration/PreparedStatementParametersTest.java +++ b/src/test/java/org/mariadb/jdbc/integration/PreparedStatementParametersTest.java @@ -12,6 +12,8 @@ import java.net.URL; import java.nio.charset.StandardCharsets; import java.sql.*; +import java.time.LocalDateTime; +import java.time.ZonedDateTime; import java.util.Calendar; import java.util.TimeZone; import org.junit.jupiter.api.AfterAll; @@ -150,14 +152,14 @@ public void checkParameters(org.mariadb.jdbc.Connection con, boolean text) throw rs -> assertEquals(Date.valueOf("2010-05-25"), rs.getDate(1)), con); boolean minus = TimeZone.getDefault().getOffset(System.currentTimeMillis()) > 0; + + ZonedDateTime zdt = + LocalDateTime.parse((minus ? "2010-01-11" : "2010-01-12") + "T00:00:00.0") + .atZone(TimeZone.getTimeZone("UTC").toZoneId()); + checkSendTimestamp( ps -> ps.setDate(1, Date.valueOf("2010-01-12"), utcCal), - rs -> - assertEquals(minus ? 1263164400000L : 1263254400000L, rs.getDate(1, utcCal).getTime()), - con); - checkSendTimestamp( - ps -> ps.setDate(1, Date.valueOf("2010-01-12"), utcCal), - rs -> assertEquals(minus ? "2010-01-11" : "2010-01-12", rs.getDate(1, utcCal).toString()), + rs -> assertEquals(zdt.toEpochSecond() * 1000, rs.getDate(1, utcCal).getTime()), con); checkSendTimestamp( ps -> ps.setDate(1, Date.valueOf("2010-05-25")), diff --git a/src/test/java/org/mariadb/jdbc/integration/codec/BinaryCodecTest.java b/src/test/java/org/mariadb/jdbc/integration/codec/BinaryCodecTest.java index baed2bfc9..289d98bd4 100644 --- a/src/test/java/org/mariadb/jdbc/integration/codec/BinaryCodecTest.java +++ b/src/test/java/org/mariadb/jdbc/integration/codec/BinaryCodecTest.java @@ -132,11 +132,13 @@ public void getObjectType(ResultSet rs) throws Exception { testObject(rs, Date.class, Date.valueOf("2011-01-01")); Timestamp tt = Timestamp.valueOf("2010-12-31 23:59:59"); testObject(rs, Timestamp.class, new Timestamp(tt.getTime() + 152), 2); + ZonedDateTime zdt = LocalDateTime.parse("2011-01-01T00:00").atZone(ZoneId.systemDefault()); + testObject(rs, ZonedDateTime.class, zdt); testObject( rs, - ZonedDateTime.class, - LocalDateTime.parse("2011-01-01T00:00").atZone(ZoneId.systemDefault())); - testErrObject(rs, OffsetDateTime.class); + OffsetDateTime.class, + OffsetDateTime.of(LocalDateTime.parse("2010-12-31T23:59:59.152"), zdt.getOffset()), + 2); testErrObject(rs, OffsetTime.class); } 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 155c7fa46..6ad4217b7 100644 --- a/src/test/java/org/mariadb/jdbc/integration/codec/CharCodecTest.java +++ b/src/test/java/org/mariadb/jdbc/integration/codec/CharCodecTest.java @@ -130,11 +130,14 @@ public void getObjectType(ResultSet rs) throws Exception { testObject(rs, Date.class, Date.valueOf("2011-01-01")); Timestamp tt = Timestamp.valueOf("2010-12-31 23:59:59"); testObject(rs, Timestamp.class, new Timestamp(tt.getTime() + 152), 2); + ZonedDateTime zdt = LocalDateTime.parse("2011-01-01T00:00").atZone(ZoneId.systemDefault()); + testObject(rs, ZonedDateTime.class, zdt); testObject( rs, - ZonedDateTime.class, - LocalDateTime.parse("2011-01-01T00:00").atZone(ZoneId.systemDefault())); - testErrObject(rs, OffsetDateTime.class); + OffsetDateTime.class, + OffsetDateTime.of(LocalDateTime.parse("2010-12-31T23:59:59.152"), zdt.getOffset()), + 2); + testErrObject(rs, OffsetTime.class); testObject(rs, java.util.Date.class, Date.valueOf("2010-12-31"), 2); } @@ -492,10 +495,10 @@ public void getOffsetDateTimePrepare() throws SQLException { public void getOffsetDateTime(ResultSet rs) throws SQLException { assertTrue(rs.next()); - Common.assertThrowsContains( - SQLException.class, - () -> rs.getObject(1, OffsetDateTime.class), - "cannot be decoded as OffsetDateTime"); + // Common.assertThrowsContains( + // SQLException.class, + // () -> rs.getObject(1, OffsetDateTime.class), + // "cannot be decoded as OffsetDateTime"); assertTrue(rs.next()); assertEquals( OffsetDateTime.parse("2010-12-31T23:59:59.152+01:00"), 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 8858a1803..5563ecb8c 100644 --- a/src/test/java/org/mariadb/jdbc/integration/codec/DateTimeCodecTest.java +++ b/src/test/java/org/mariadb/jdbc/integration/codec/DateTimeCodecTest.java @@ -14,6 +14,7 @@ import java.util.Calendar; import java.util.TimeZone; import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.Assumptions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.mariadb.jdbc.Statement; @@ -87,6 +88,112 @@ public void getObjectPrepare() throws SQLException { getObject(getPrepare(sharedConnBinary)); } + @Test + public void getObjectPrepareTimeZone() throws SQLException { + + int hourOffset = + TimeZone.getDefault() + .getOffset( + LocalDateTime.parse("2010-01-12T01:55:12") + .atZone(TimeZone.getDefault().toZoneId()) + .toEpochSecond() + * 1000) + / 3600000; + Assumptions.assumeTrue(hourOffset != 0); + + Statement stmt = sharedConn.createStatement(); + ResultSet rs1 = stmt.executeQuery("SELECT @@global.time_zone"); + rs1.next(); + String srvTz = rs1.getString(1); + String currOffset = + (hourOffset < 0 ? "-" : "+") + + (Math.abs(hourOffset) < 10 ? "0" : "") + + Math.abs(hourOffset) + + ":00"; + try { + setTz(stmt, hourOffset, 1); + checkTz("2010-01-12 00:55:12", "2010-01-12", "00:55:12", false, currOffset, hourOffset); + checkTz("2010-01-12 00:55:12", "2010-01-12", "00:55:12", true, currOffset, hourOffset); + + setTz(stmt, hourOffset, 2); + checkTz("2010-01-11 23:55:12", "2010-01-11", "23:55:12", false, currOffset, hourOffset); + checkTz("2010-01-11 23:55:12", "2010-01-11", "23:55:12", true, currOffset, hourOffset); + + } finally { + stmt.execute("SET @@global.time_zone='" + srvTz + "'"); + } + } + + private void checkTz( + String expectedTimestamp, + String expectedDate, + String expectedTime, + boolean usePrepare, + String srvTz, + int hourOffset) + throws SQLException { + Calendar utcCalendar = Calendar.getInstance(TimeZone.getTimeZone("UTC")); + try (Connection conn = + createCon( + "connectionTimeZone=SERVER&forceConnectionTimeZoneToSession=true&preserveInstants=true&useServerPrepStmts=" + + usePrepare)) { + ResultSet rs = getPrepare(conn); + assertEquals(Timestamp.valueOf(expectedTimestamp).getTime(), rs.getTimestamp(1).getTime()); + assertEquals(expectedTimestamp + ".0", rs.getTimestamp(1).toString()); + assertEquals(Timestamp.valueOf(expectedTimestamp).getTime(), rs.getTimestamp(1).getTime()); + assertEquals(expectedTimestamp, rs.getString(1)); + assertEquals( + ZonedDateTime.of( + LocalDateTime.parse(expectedDate + "T" + expectedTime), + ZoneId.systemDefault()) + .toEpochSecond() + * 1000, + rs.getDate(1).getTime()); + assertEquals(expectedTime, rs.getTime(1).toString()); + + ZonedDateTime dateUtc = + LocalDateTime.parse("2010-01-12T01:55:12") + .atZone(TimeZone.getTimeZone("UTC").toZoneId()) + .withZoneSameInstant(TimeZone.getDefault().toZoneId()); + + assertEquals(dateUtc.toLocalTime().toString(), rs.getTime(1, utcCalendar).toString()); + assertEquals( + dateUtc.toLocalDateTime().toString().replace("T", " ") + ".0", + rs.getTimestamp(1, utcCalendar).toString()); + + assertEquals( + expectedTimestamp.replace(" ", "T") + srvTz, + rs.getObject(1, OffsetDateTime.class).toString()); + String sdt = rs.getObject(1, ZonedDateTime.class).toString(); + assertEquals(expectedTimestamp.replace(" ", "T") + srvTz, sdt.substring(0, sdt.indexOf('['))); + assertEquals( + expectedTimestamp.replace(" ", "T"), rs.getObject(1, LocalDateTime.class).toString()); + } + try (Connection conn = + createCon( + "connectionTimeZone=SERVER&forceConnectionTimeZoneToSession=true&preserveInstants=false&useServerPrepStmts=" + + usePrepare)) { + ResultSet rs = getPrepare(conn); + assertEquals( + Timestamp.valueOf("2010-01-12 01:55:12").getTime(), + ((Timestamp) rs.getObject(1)).getTime()); + assertEquals("2010-01-12 01:55:12", rs.getString(1)); + assertEquals("2010-01-12", rs.getDate(1).toString()); + assertEquals("01:55:12", rs.getTime(1).toString()); + } + } + + private String setTz(Statement stmt, int hourOffset, int offset) throws SQLException { + hourOffset += offset; + String newTz = + (hourOffset < 0 ? "-" : "+") + + (Math.abs(hourOffset) < 10 ? "0" : "") + + Math.abs(hourOffset) + + ":00"; + stmt.execute("SET @@global.time_zone='" + newTz + "'"); + return newTz; + } + public void getObject(ResultSet rs) throws SQLException { assertFalse(rs.wasNull()); assertEquals( @@ -158,7 +265,15 @@ public void getObjectType(ResultSet rs) throws Exception { rs, ZonedDateTime.class, LocalDateTime.parse("2010-01-12T01:55:12").atZone(ZoneId.systemDefault())); - testObject(rs, java.util.Date.class, Date.valueOf("2010-01-12")); + testObject( + rs, + java.util.Date.class, + new Date( + ZonedDateTime.of( + LocalDateTime.parse("2010-01-12T01:55:12.0"), + TimeZone.getDefault().toZoneId()) + .toEpochSecond() + * 1000)); } @Test @@ -185,14 +300,8 @@ public void getString(ResultSet rs, boolean text) throws SQLException { if (isMariaDBServer()) { rs.next(); assertEquals("0000-00-00 00:00:00", rs.getString(1)); - if (isXpand() && !text) { - // https://jira.mariadb.org/browse/XPT-273 - assertEquals("0000-00-00 00:00:00", rs.getString(2)); - assertEquals("9999-12-31 00:00:00", rs.getString(3)); - } else { - assertEquals("0000-00-00 00:00:00.000000", rs.getString(2)); - assertEquals("9999-12-31 00:00:00.000000", rs.getString(3)); - } + assertEquals("0000-00-00 00:00:00.000000", rs.getString(2)); + assertEquals("9999-12-31 00:00:00.000000", rs.getString(3)); } } @@ -369,30 +478,31 @@ public void getDatePrepare() throws SQLException { public void getDate(ResultSet rs) throws SQLException { assertEquals( - 1263254400000L - - TimeZone.getDefault().getOffset(Timestamp.valueOf("2010-01-12 01:55:12").getTime()), + ZonedDateTime.of(LocalDateTime.parse("2010-01-12T01:55:12.0"), ZoneId.of("UTC")) + .toEpochSecond() + * 1000, rs.getDate(1, Calendar.getInstance(TimeZone.getTimeZone("UTC"))).getTime()); assertFalse(rs.wasNull()); assertEquals( - 1263254400000L - - TimeZone.getDefault() - .getOffset(Timestamp.valueOf("2010-01-12 01:55:12.123456").getTime()), + ZonedDateTime.of( + LocalDateTime.parse("2010-01-12T01:55:12.0"), TimeZone.getDefault().toZoneId()) + .toEpochSecond() + * 1000, rs.getDate(1).getTime()); assertFalse(rs.wasNull()); assertEquals( - -30609792000000L - - TimeZone.getDefault().getOffset(Timestamp.valueOf("1000-01-01 01:55:13").getTime()), - rs.getDate(2, Calendar.getInstance(TimeZone.getTimeZone("UTC"))).getTime()); + "1000-01-01", rs.getDate(2, Calendar.getInstance(TimeZone.getTimeZone("UTC"))).toString()); assertFalse(rs.wasNull()); - assertEquals( - -30609792000000L - - TimeZone.getDefault().getOffset(Timestamp.valueOf("1000-01-01 01:55:13").getTime()), - rs.getDate(2).getTime()); + assertEquals(Timestamp.valueOf("1000-01-01 01:55:13.21234").getTime(), rs.getDate(2).getTime()); assertFalse(rs.wasNull()); assertEquals( - 253402214400000L - - TimeZone.getDefault().getOffset(Timestamp.valueOf("9999-12-31 18:30:12").getTime()), + ZonedDateTime.of( + LocalDateTime.parse("9999-12-31T18:30:12.55"), + TimeZone.getDefault().toZoneId()) + .toEpochSecond() + * 1000 + + 550, rs.getDate(3).getTime()); assertFalse(rs.wasNull()); assertNull(rs.getDate(4)); @@ -427,6 +537,11 @@ public void getDateTimezoneTest() throws SQLException { TimeZone.setDefault(TimeZone.getTimeZone("GMT+8")); try (Connection conGmt8 = createCon("timezone=auto")) { getDateTimezoneTestGmt8(conGmt8, getPrepare(conGmt8), TimeZone.getTimeZone("GMT+8")); + try (Connection conGmt8Preserve = createCon("timezone=auto&preserveInstants=true")) { + getDateTimezoneTestGmt8preserveInstants( + conGmt8Preserve, getPrepare(conGmt8Preserve), TimeZone.getTimeZone("GMT+8")); + } + TimeZone.setDefault(TimeZone.getTimeZone("GMT-8")); try (Connection conGmtm8 = createCon("timezone=auto")) { getDateTimezoneTestGmtm8(conGmtm8, getPrepare(conGmtm8), TimeZone.getTimeZone("GMT-8")); @@ -534,6 +649,89 @@ public void getDateTimezoneTestGmt8(Connection conGmt8, ResultSet rs, TimeZone t conGmt8.rollback(); } + public void getDateTimezoneTestGmt8preserveInstants(Connection conGmt8, ResultSet rs, TimeZone tz) + throws SQLException { + + assertEquals("2010-01-12T01:55:12+08:00", 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:12+08:00")); + 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:12+08:00", rs.getObject(2, OffsetDateTime.class).toString()); + assertEquals("2010-01-12 01:55:12.000000", rs.getString(2)); + + rs.next(); + assertEquals("2010-01-12T01:55:12+08:00", rs.getObject(2, OffsetDateTime.class).toString()); + assertEquals("2010-01-12 01:55:12.0", rs.getTimestamp(2).toString()); + assertEquals(1263232512000L, rs.getTimestamp(2).getTime()); + assertEquals( + "2010-01-12 09:55:12.0", + rs.getTimestamp(2, Calendar.getInstance(TimeZone.getTimeZone("UTC"))).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-12T08:55:12+08:00", rs.getObject(2, OffsetDateTime.class).toString()); + assertEquals("2010-01-12 08:55:12.0", rs.getTimestamp(2).toString()); + assertEquals(1263257712000L, rs.getTimestamp(2).getTime()); + assertEquals("2010-01-12 08:55:12.000000", rs.getString(2)); + assertEquals("2010-01-12", rs.getDate(2).toString()); + + rs.next(); + assertEquals("2010-01-12T09:55:12+08:00", rs.getObject(2, OffsetDateTime.class).toString()); + assertEquals("2010-01-12 09:55:12.0", rs.getTimestamp(2).toString()); + assertEquals(1263261312000L, rs.getTimestamp(2).getTime()); + assertEquals("2010-01-12 09:55:12.000000", rs.getString(2)); + assertEquals("2010-01-12", rs.getDate(2).toString()); + + rs.next(); + assertEquals("2010-01-13T05:55:12+08:00", rs.getObject(2, OffsetDateTime.class).toString()); + assertEquals("2010-01-13 05:55:12.0", rs.getTimestamp(2).toString()); + 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(); + } + public void getDateTimezoneTestGmtm8(Connection conGmt8, ResultSet rs, TimeZone tz) throws SQLException { @@ -763,11 +961,22 @@ public void getTimePrepare() throws SQLException { } public void getTime(ResultSet rs) throws SQLException { + int hourOffset = + TimeZone.getDefault() + .getOffset( + LocalDateTime.parse("2010-01-12T01:55:12") + .atZone(TimeZone.getDefault().toZoneId()) + .toEpochSecond() + * 1000) + / 3600000; + int hourPlusOffset = (25 + hourOffset) % 24; assertEquals( - 6912000, rs.getTime(1, Calendar.getInstance(TimeZone.getTimeZone("UTC"))).getTime()); + Time.valueOf((hourPlusOffset < 10 ? "0" + hourPlusOffset : hourPlusOffset) + ":55:12") + .toString(), + rs.getTime(1, Calendar.getInstance(TimeZone.getTimeZone("UTC"))).toString()); assertFalse(rs.wasNull()); - assertEquals(Time.valueOf("01:55:12").getTime(), rs.getTime(1).getTime()); + assertEquals(Time.valueOf("01:55:12").toString(), rs.getTime(1).toString()); assertFalse(rs.wasNull()); assertEquals( @@ -881,6 +1090,9 @@ public void getTimestamp(ResultSet rs) throws SQLException { assertFalse(rs.wasNull()); assertEquals(Timestamp.valueOf("2010-01-12 01:55:12").getTime(), rs.getTimestamp(1).getTime()); assertFalse(rs.wasNull()); + + Timestamp t1 = Timestamp.valueOf("1000-01-01 01:55:13.212345"); + Timestamp t2 = rs.getTimestamp(2); assertEquals( Timestamp.valueOf("1000-01-01 01:55:13.212345").getTime(), rs.getTimestamp(2).getTime()); assertFalse(rs.wasNull()); 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 ee84942e8..f2288b9ab 100644 --- a/src/test/java/org/mariadb/jdbc/integration/codec/EnumCodecTest.java +++ b/src/test/java/org/mariadb/jdbc/integration/codec/EnumCodecTest.java @@ -184,11 +184,14 @@ public void getObjectType(ResultSet rs) throws Exception { testObject(rs, Date.class, Date.valueOf("2011-01-01")); Timestamp tt = Timestamp.valueOf("2010-12-31 23:59:59"); testObject(rs, Timestamp.class, new Timestamp(tt.getTime() + 152), 2); + ZonedDateTime zdt = LocalDateTime.parse("2011-01-01T00:00").atZone(ZoneId.systemDefault()); + testObject(rs, ZonedDateTime.class, zdt); testObject( rs, - ZonedDateTime.class, - LocalDateTime.parse("2011-01-01T00:00").atZone(ZoneId.systemDefault())); - testErrObject(rs, OffsetDateTime.class); + OffsetDateTime.class, + OffsetDateTime.of(LocalDateTime.parse("2010-12-31T23:59:59.152"), zdt.getOffset()), + 2); + testErrObject(rs, OffsetTime.class); testObject(rs, java.util.Date.class, Date.valueOf("2010-12-31"), 2); } diff --git a/src/test/java/org/mariadb/jdbc/integration/codec/VarbinaryCodecTest.java b/src/test/java/org/mariadb/jdbc/integration/codec/VarbinaryCodecTest.java index 059803c7e..de56dfc21 100644 --- a/src/test/java/org/mariadb/jdbc/integration/codec/VarbinaryCodecTest.java +++ b/src/test/java/org/mariadb/jdbc/integration/codec/VarbinaryCodecTest.java @@ -129,11 +129,13 @@ public void getObjectType(ResultSet rs) throws Exception { testObject(rs, Date.class, Date.valueOf("2011-01-01")); Timestamp tt = Timestamp.valueOf("2010-12-31 23:59:59"); testObject(rs, Timestamp.class, new Timestamp(tt.getTime() + 152), 2); + ZonedDateTime zdt = LocalDateTime.parse("2011-01-01T00:00").atZone(ZoneId.systemDefault()); + testObject(rs, ZonedDateTime.class, zdt); testObject( rs, - ZonedDateTime.class, - LocalDateTime.parse("2011-01-01T00:00").atZone(ZoneId.systemDefault())); - testErrObject(rs, OffsetDateTime.class); + OffsetDateTime.class, + OffsetDateTime.of(LocalDateTime.parse("2010-12-31T23:59:59.152"), zdt.getOffset()), + 2); testErrObject(rs, OffsetTime.class); testObject(rs, java.util.Date.class, Date.valueOf("2010-12-31"), 2); } 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 a7d1fdd49..439a299f2 100644 --- a/src/test/java/org/mariadb/jdbc/integration/codec/VarcharCodecTest.java +++ b/src/test/java/org/mariadb/jdbc/integration/codec/VarcharCodecTest.java @@ -143,12 +143,16 @@ public void getObjectType(ResultSet rs) throws Exception { testObject(rs, Date.class, Date.valueOf("2011-01-01")); Timestamp tt = Timestamp.valueOf("2010-12-31 23:59:59"); testObject(rs, Timestamp.class, new Timestamp(tt.getTime() + 152), 2); + ZonedDateTime zdt = LocalDateTime.parse("2011-01-01T00:00").atZone(ZoneId.systemDefault()); + testObject(rs, ZonedDateTime.class, zdt); + testErrObject(rs, BigInteger.class); + testObject(rs, ZonedDateTime.class, zdt); testObject( rs, - ZonedDateTime.class, - LocalDateTime.parse("2011-01-01T00:00").atZone(ZoneId.systemDefault())); - testErrObject(rs, BigInteger.class); - testErrObject(rs, OffsetDateTime.class); + OffsetDateTime.class, + OffsetDateTime.of(LocalDateTime.parse("2010-12-31T23:59:59.152"), zdt.getOffset()), + 2); + testErrObject(rs, OffsetTime.class); testObject(rs, java.util.Date.class, Date.valueOf("2010-12-31"), 2); } @@ -589,7 +593,7 @@ public void getLocalDateTime(ResultSet rs) throws SQLException { Common.assertThrowsContains( SQLDataException.class, () -> rs.getObject(1, LocalDateTime.class), - "value '0' (VARSTRING) cannot be decoded as LocalDateTime"); + "value '0' (VARSTRING) cannot be decoded"); rs.next(); assertEquals(LocalDateTime.parse("2011-01-01T00:00:00"), rs.getObject(1, LocalDateTime.class)); assertFalse(rs.wasNull()); @@ -599,7 +603,7 @@ public void getLocalDateTime(ResultSet rs) throws SQLException { Common.assertThrowsContains( SQLDataException.class, () -> rs.getObject(1, LocalDateTime.class), - "value 'aaaa-bb-cc' (VARSTRING) cannot be decoded as LocalDateTime"); + "value 'aaaa-bb-cc' (VARSTRING) cannot be decoded"); assertNull(rs.getObject(2, LocalDateTime.class)); assertTrue(rs.wasNull()); } diff --git a/src/test/java/org/mariadb/jdbc/unit/util/ConfigurationTest.java b/src/test/java/org/mariadb/jdbc/unit/util/ConfigurationTest.java index f5464cf4c..24048266b 100644 --- a/src/test/java/org/mariadb/jdbc/unit/util/ConfigurationTest.java +++ b/src/test/java/org/mariadb/jdbc/unit/util/ConfigurationTest.java @@ -867,9 +867,12 @@ public void builder() throws SQLException { .fallbackToSystemTrustStore(false) .initSql("SET @@a='10'") .useCatalogTerm("schema") + .preserveInstants(true) + .connectionTimeZone("SERVER") + .forceConnectionTimeZoneToSession(false) .build(); String expected = - "jdbc:mariadb://host1:3305,address=(host=host2)(port=3307)(type=replica)/db?user=me&password=***&timezone=UTC&autocommit=false&nullDatabaseMeansCurrent=true&useCatalogTerm=SCHEMA&createDatabaseIfNotExist=true&useLocalSessionState=true&returnMultiValuesGeneratedIds=true&permitRedirect=false&transactionIsolation=REPEATABLE_READ&defaultFetchSize=10&maxQuerySizeToLog=100&maxAllowedPacket=8000&geometryDefaultType=default&restrictedAuth=mysql_native_password,client_ed25519&initSql=SET" + "jdbc:mariadb://host1:3305,address=(host=host2)(port=3307)(type=replica)/db?user=me&password=***&timezone=UTC&connectionTimeZone=SERVER&forceConnectionTimeZoneToSession=false&preserveInstants=true&autocommit=false&nullDatabaseMeansCurrent=true&useCatalogTerm=SCHEMA&createDatabaseIfNotExist=true&useLocalSessionState=true&returnMultiValuesGeneratedIds=true&permitRedirect=false&transactionIsolation=REPEATABLE_READ&defaultFetchSize=10&maxQuerySizeToLog=100&maxAllowedPacket=8000&geometryDefaultType=default&restrictedAuth=mysql_native_password,client_ed25519&initSql=SET" + " @@a='10'&socketFactory=someSocketFactory&connectTimeout=22&pipe=pipeName&localSocket=localSocket&uuidAsString=true&tcpKeepAlive=false&tcpKeepIdle=10&tcpKeepCount=50&tcpKeepInterval=50&tcpAbortiveClose=true&localSocketAddress=localSocketAddress&socketTimeout=1000&useReadAheadInput=true&tlsSocketType=TLStype&sslMode=TRUST&serverSslCert=mycertPath&keyStore=/tmp&keyStorePassword=MyPWD&keyStoreType=JKS&trustStoreType=JKS&enabledSslCipherSuites=myCipher,cipher2&enabledSslProtocolSuites=TLSv1.2&fallbackToSystemKeyStore=false&fallbackToSystemTrustStore=false&allowMultiQueries=true&allowLocalInfile=false&useCompression=true&useAffectedRows=true&useBulkStmts=true&disablePipeline=true&cachePrepStmts=false&prepStmtCacheSize=2&useServerPrepStmts=true&credentialType=ENV&sessionVariables=blabla&connectionAttributes=bla=bla&servicePrincipalName=SPN&blankTableNameMeta=true&tinyInt1isBit=false&yearIsDateType=false&dumpQueriesOnException=true&includeInnodbStatusInDeadlockExceptions=true&includeThreadDumpInDeadlockExceptions=true&retriesAllDown=10&galeraAllowedState=A,B&transactionReplay=true&pool=true&poolName=myPool&maxPoolSize=16&minPoolSize=12&maxIdleTime=25000®isterJmxPool=false&poolValidMinDelay=260&useResetConnection=true&serverRsaPublicKeyFile=RSAPath&allowPublicKeyRetrieval=true"; assertEquals(expected, conf.toString()); assertEquals(expected, conf.toBuilder().build().toString()); @@ -938,11 +941,11 @@ public void toConf() throws SQLException { + "default options :")); assertTrue( Configuration.toConf( - "jdbc:mariadb:loadbalance://host1:3305,address=(host=host2)(port=3307)(type=replica)/db?nonExisting&nonExistingWithValue=tt&user=me&password=***&timezone=UTC&autocommit=false&createDatabaseIfNotExist=true&") + "jdbc:mariadb:loadbalance://host1:3305,address=(host=host2)(port=3307)(type=replica)/db?nonExisting&nonExistingWithValue=tt&user=me&password=***&autocommit=false&createDatabaseIfNotExist=true&") .startsWith( "Configuration:\n" + " * resulting Url :" - + " jdbc:mariadb:loadbalance://address=(host=host1)(port=3305)(type=primary),address=(host=host2)(port=3307)(type=replica)/db?user=me&password=***&nonExisting=&nonExistingWithValue=tt&timezone=UTC&autocommit=false&createDatabaseIfNotExist=true\n" + + " jdbc:mariadb:loadbalance://address=(host=host1)(port=3305)(type=primary),address=(host=host2)(port=3307)(type=replica)/db?user=me&password=***&nonExisting=&nonExistingWithValue=tt&autocommit=false&createDatabaseIfNotExist=true\n" + "Unknown options : \n" + " * nonExisting : \n" + " * nonExistingWithValue : tt\n" @@ -955,7 +958,6 @@ public void toConf() throws SQLException { + " * database : db\n" + " * haMode : LOADBALANCE\n" + " * password : ***\n" - + " * timezone : UTC\n" + " * user : me\n" + "\n" + "default options :\n"