Skip to content

Commit

Permalink
[CONJ-1171] timezone new options
Browse files Browse the repository at this point in the history
* connectionTimeZone
* forceConnectionTimeZoneToSession
* preserveInstants
  • Loading branch information
rusher committed Apr 17, 2024
1 parent 74e581b commit 4567d3b
Show file tree
Hide file tree
Showing 84 changed files with 2,990 additions and 1,764 deletions.
108 changes: 104 additions & 4 deletions src/main/java/org/mariadb/jdbc/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -169,6 +172,9 @@ private Configuration(
HaMode haMode,
Properties nonMappedOptions,
String timezone,
String connectionTimeZone,
boolean forceConnectionTimeZoneToSession,
boolean preserveInstants,
Boolean autocommit,
boolean useMysqlMetadata,
boolean nullDatabaseMeansCurrent,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -365,6 +374,9 @@ private Configuration(
Boolean transformedBitIsBoolean,
Boolean yearIsDateType,
String timezone,
String connectionTimeZone,
Boolean forceConnectionTimeZoneToSession,
Boolean preserveInstants,
Boolean dumpQueriesOnException,
Integer prepStmtCacheSize,
Boolean useAffectedRows,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
// *************************************************************
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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,
Expand Down
41 changes: 27 additions & 14 deletions src/main/java/org/mariadb/jdbc/client/ColumnDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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;

/**
Expand All @@ -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;

/**
Expand Down Expand Up @@ -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;

/**
Expand All @@ -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;

/**
Expand All @@ -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;

/**
Expand All @@ -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;

/**
Expand All @@ -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;

/**
Expand All @@ -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;

/**
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/org/mariadb/jdbc/client/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Loading

0 comments on commit 4567d3b

Please sign in to comment.