Skip to content

Commit

Permalink
Fix for Bug#113599 (Bug#36171571), Contribution: Replace StringBuffer…
Browse files Browse the repository at this point in the history
… with StringBuilder in ValueEncoders.

Change-Id: I94583700015187c68095b93b2bf728082a03fb9e
  • Loading branch information
Axyoan Marcelo committed Feb 7, 2024
1 parent 832994a commit 00449a4
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 67 deletions.
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

Version 8.4.0

- Fix for Bug#113599 (Bug#36171571), Contribution: Replace StringBuffer with StringBuilder in ValueEncoders.
Thanks to Henning Pöttker for his contribution.

- Fix for Bug#91550 (Bug#28297874), DatabaseMetaData specifies incorrect extra name characters.

- Fix for Bug#113129 (Bug#36043145), setting the FetchSize on a Statement object does not affect.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023, Oracle and/or its affiliates.
* Copyright (c) 2022, 2024, Oracle and/or its affiliates.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 2.0, as published by the
Expand Down Expand Up @@ -76,20 +76,20 @@ public String getString(BindValue binding) {
.atZoneSameInstant(this.serverSession.getDefaultTimeZone().toZoneId()).toLocalDateTime()),
binding.getField(), binding.keepOrigNanos());

StringBuffer buf = new StringBuffer();
sb = new StringBuilder();

buf.append(TimeUtil.getSimpleDateFormat(null, "''yyyy-MM-dd HH:mm:ss",
sb.append(TimeUtil.getSimpleDateFormat(null, "''yyyy-MM-dd HH:mm:ss",
binding.getMysqlType() == MysqlType.TIMESTAMP && this.preserveInstants.getValue() ? this.serverSession.getSessionTimeZone()
: this.serverSession.getDefaultTimeZone())
.format(x));

if (this.serverSession.getCapabilities().serverSupportsFracSecs() && x.getNanos() > 0) {
buf.append('.');
buf.append(TimeUtil.formatNanos(x.getNanos(), 6));
sb.append('.');
sb.append(TimeUtil.formatNanos(x.getNanos(), 6));
}
buf.append('\'');
sb.append('\'');

return buf.toString();
return sb.toString();

case YEAR:
return String.valueOf(((Instant) binding.getValue()).atOffset(ZoneOffset.UTC)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023, Oracle and/or its affiliates.
* Copyright (c) 2022, 2024, Oracle and/or its affiliates.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 2.0, as published by the
Expand Down Expand Up @@ -75,20 +75,20 @@ public String getString(BindValue binding) {
((OffsetDateTime) binding.getValue()).atZoneSameInstant(this.serverSession.getDefaultTimeZone().toZoneId()).toLocalDateTime()),
binding.getField(), binding.keepOrigNanos());

StringBuffer buf = new StringBuffer();
sb = new StringBuilder();

buf.append(TimeUtil.getSimpleDateFormat(null, "''yyyy-MM-dd HH:mm:ss",
sb.append(TimeUtil.getSimpleDateFormat(null, "''yyyy-MM-dd HH:mm:ss",
binding.getMysqlType() == MysqlType.TIMESTAMP && this.preserveInstants.getValue() ? this.serverSession.getSessionTimeZone()
: this.serverSession.getDefaultTimeZone())
.format(x));

if (this.serverSession.getCapabilities().serverSupportsFracSecs() && x.getNanos() > 0) {
buf.append('.');
buf.append(TimeUtil.formatNanos(x.getNanos(), 6));
sb.append('.');
sb.append(TimeUtil.formatNanos(x.getNanos(), 6));
}
buf.append('\'');
sb.append('\'');

return buf.toString();
return sb.toString();

case YEAR:
return String.valueOf(odt.atZoneSameInstant(this.serverSession.getDefaultTimeZone().toZoneId()).getYear());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023, Oracle and/or its affiliates.
* Copyright (c) 2022, 2024, Oracle and/or its affiliates.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 2.0, as published by the
Expand Down Expand Up @@ -87,15 +87,15 @@ public String getString(BindValue binding) {
ts = TimeUtil.truncateFractionalSeconds(ts);
}

StringBuffer buf = new StringBuffer();
buf.append(binding.getCalendar() != null ? TimeUtil.getSimpleDateFormat("''yyyy-MM-dd HH:mm:ss", binding.getCalendar()).format(x)
StringBuilder sb = new StringBuilder();
sb.append(binding.getCalendar() != null ? TimeUtil.getSimpleDateFormat("''yyyy-MM-dd HH:mm:ss", binding.getCalendar()).format(x)
: TimeUtil.getSimpleDateFormat(null, "''yyyy-MM-dd HH:mm:ss", this.serverSession.getDefaultTimeZone()).format(x));
if (this.serverSession.getCapabilities().serverSupportsFracSecs() && ts.getNanos() > 0) {
buf.append('.');
buf.append(TimeUtil.formatNanos(ts.getNanos(), 6));
sb.append('.');
sb.append(TimeUtil.formatNanos(ts.getNanos(), 6));
}
buf.append('\'');
return buf.toString();
sb.append('\'');
return sb.toString();
case YEAR:
Calendar cal = Calendar.getInstance();
cal.setTime((java.util.Date) binding.getValue());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023, Oracle and/or its affiliates.
* Copyright (c) 2022, 2024, Oracle and/or its affiliates.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 2.0, as published by the
Expand Down Expand Up @@ -75,30 +75,30 @@ public String getString(BindValue binding) {
case TEXT:
case MEDIUMTEXT:
case LONGTEXT:
StringBuffer buf = new StringBuffer();
StringBuilder sb = new StringBuilder();

if (binding.getCalendar() != null) {
buf.append(TimeUtil.getSimpleDateFormat("''yyyy-MM-dd HH:mm:ss", binding.getCalendar()).format(x));
sb.append(TimeUtil.getSimpleDateFormat("''yyyy-MM-dd HH:mm:ss", binding.getCalendar()).format(x));
} else {
this.tsdf = TimeUtil.getSimpleDateFormat(this.tsdf, "''yyyy-MM-dd HH:mm:ss",
binding.getMysqlType() == MysqlType.TIMESTAMP && this.preserveInstants.getValue() ? this.serverSession.getSessionTimeZone()
: this.serverSession.getDefaultTimeZone());
buf.append(this.tsdf.format(x));
sb.append(this.tsdf.format(x));
}

if (this.serverSession.getCapabilities().serverSupportsFracSecs() && x.getNanos() > 0) {
buf.append('.');
buf.append(TimeUtil.formatNanos(x.getNanos(), 6));
sb.append('.');
sb.append(TimeUtil.formatNanos(x.getNanos(), 6));
}
buf.append('\'');
sb.append('\'');

return buf.toString();
return sb.toString();
case YEAR:
Calendar cal = Calendar.getInstance();
cal.setTime((java.util.Date) binding.getValue());
return String.valueOf(cal.get(Calendar.YEAR));
case TIME:
StringBuilder sb = new StringBuilder("'");
sb = new StringBuilder("'");
sb.append(adjustLocalTime(((Timestamp) binding.getValue()).toLocalDateTime().toLocalTime(), binding.getField())
.format(TimeUtil.TIME_FORMATTER_WITH_OPTIONAL_MICROS));
sb.append("'");
Expand Down Expand Up @@ -157,23 +157,23 @@ public void encodeAsBinary(Message msg, BindValue binding) {
case TEXT:
case MEDIUMTEXT:
case LONGTEXT:
StringBuffer buf = new StringBuffer();
StringBuilder sb = new StringBuilder();

if (binding.getCalendar() != null) {
buf.append(TimeUtil.getSimpleDateFormat("yyyy-MM-dd HH:mm:ss", binding.getCalendar()).format(x));
sb.append(TimeUtil.getSimpleDateFormat("yyyy-MM-dd HH:mm:ss", binding.getCalendar()).format(x));
} else {
this.tsdf = TimeUtil.getSimpleDateFormat(this.tsdf, "yyyy-MM-dd HH:mm:ss",
binding.getMysqlType() == MysqlType.TIMESTAMP && this.preserveInstants.getValue() ? this.serverSession.getSessionTimeZone()
: this.serverSession.getDefaultTimeZone());
buf.append(this.tsdf.format(x));
sb.append(this.tsdf.format(x));
}

if (this.serverSession.getCapabilities().serverSupportsFracSecs() && x.getNanos() > 0) {
buf.append('.');
buf.append(TimeUtil.formatNanos(x.getNanos(), 6));
sb.append('.');
sb.append(TimeUtil.formatNanos(x.getNanos(), 6));
}

intoPacket.writeBytes(StringSelfDataType.STRING_LENENC, StringUtils.getBytes(buf.toString(), this.charEncoding.getValue()));
intoPacket.writeBytes(StringSelfDataType.STRING_LENENC, StringUtils.getBytes(sb.toString(), this.charEncoding.getValue()));
return;
default:
throw ExceptionFactory.createException(WrongArgumentException.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023, Oracle and/or its affiliates.
* Copyright (c) 2022, 2024, Oracle and/or its affiliates.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 2.0, as published by the
Expand Down Expand Up @@ -65,21 +65,21 @@ public String getString(BindValue binding) {
case TIMESTAMP:
Timestamp ts = adjustTimestamp(new java.sql.Timestamp(((Calendar) binding.getValue()).getTimeInMillis()), binding.getField(),
binding.keepOrigNanos());
StringBuffer buf = new StringBuffer();
StringBuilder sb = new StringBuilder();
if (binding.getCalendar() != null) {
buf.append(TimeUtil.getSimpleDateFormat("''yyyy-MM-dd HH:mm:ss", binding.getCalendar()).format(x));
sb.append(TimeUtil.getSimpleDateFormat("''yyyy-MM-dd HH:mm:ss", binding.getCalendar()).format(x));
} else {
buf.append(TimeUtil.getSimpleDateFormat(null, "''yyyy-MM-dd HH:mm:ss",
sb.append(TimeUtil.getSimpleDateFormat(null, "''yyyy-MM-dd HH:mm:ss",
binding.getMysqlType() == MysqlType.TIMESTAMP && this.preserveInstants.getValue() ? this.serverSession.getSessionTimeZone()
: this.serverSession.getDefaultTimeZone())
.format(ts));
}
if (this.serverSession.getCapabilities().serverSupportsFracSecs() && ts.getNanos() > 0) {
buf.append('.');
buf.append(TimeUtil.formatNanos(ts.getNanos(), 6));
sb.append('.');
sb.append(TimeUtil.formatNanos(ts.getNanos(), 6));
}
buf.append('\'');
return buf.toString();
sb.append('\'');
return sb.toString();
case DATETIME:
case CHAR:
case VARCHAR:
Expand All @@ -90,7 +90,7 @@ public String getString(BindValue binding) {
ZonedDateTime zdt = ZonedDateTime.ofInstant(x.toInstant(), x.getTimeZone().toZoneId())
.withZoneSameInstant(this.serverSession.getDefaultTimeZone().toZoneId());

StringBuilder sb = new StringBuilder("'");
sb = new StringBuilder("'");
sb.append(zdt.format(zdt.getNano() > 0 && this.serverSession.getCapabilities().serverSupportsFracSecs() && this.sendFractionalSeconds.getValue()
? TimeUtil.DATETIME_FORMATTER_WITH_MILLIS_NO_OFFSET
: TimeUtil.DATETIME_FORMATTER_NO_FRACT_NO_OFFSET));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023, Oracle and/or its affiliates.
* Copyright (c) 2022, 2024, Oracle and/or its affiliates.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 2.0, as published by the
Expand Down Expand Up @@ -77,30 +77,30 @@ public String getString(BindValue binding) {
case TEXT:
case MEDIUMTEXT:
case LONGTEXT:
StringBuffer buf = new StringBuffer();
StringBuilder sb = new StringBuilder();

if (binding.getCalendar() != null) {
buf.append(TimeUtil.getSimpleDateFormat("''yyyy-MM-dd HH:mm:ss", binding.getCalendar()).format(x));
sb.append(TimeUtil.getSimpleDateFormat("''yyyy-MM-dd HH:mm:ss", binding.getCalendar()).format(x));
} else {
this.tsdf = TimeUtil.getSimpleDateFormat(this.tsdf, "''yyyy-MM-dd HH:mm:ss",
binding.getMysqlType() == MysqlType.TIMESTAMP && this.preserveInstants.getValue() ? this.serverSession.getSessionTimeZone()
: this.serverSession.getDefaultTimeZone());
buf.append(this.tsdf.format(x));
sb.append(this.tsdf.format(x));
}

if (this.serverSession.getCapabilities().serverSupportsFracSecs() && x.getNanos() > 0) {
buf.append('.');
buf.append(TimeUtil.formatNanos(x.getNanos(), 6));
sb.append('.');
sb.append(TimeUtil.formatNanos(x.getNanos(), 6));
}
buf.append('\'');
sb.append('\'');

return buf.toString();
return sb.toString();
case YEAR:
Calendar cal = Calendar.getInstance();
cal.setTime((java.util.Date) binding.getValue());
return String.valueOf(cal.get(Calendar.YEAR));
case TIME:
StringBuilder sb = new StringBuilder("'");
sb = new StringBuilder("'");
sb.append(adjustLocalTime(new Timestamp(((java.util.Date) binding.getValue()).getTime()).toLocalDateTime().toLocalTime(), binding.getField())
.format(TimeUtil.TIME_FORMATTER_WITH_OPTIONAL_MICROS));
sb.append("'");
Expand Down Expand Up @@ -161,23 +161,23 @@ public void encodeAsBinary(Message msg, BindValue binding) {
case TEXT:
case MEDIUMTEXT:
case LONGTEXT:
StringBuffer buf = new StringBuffer();
StringBuilder sb = new StringBuilder();

if (binding.getCalendar() != null) {
buf.append(TimeUtil.getSimpleDateFormat("yyyy-MM-dd HH:mm:ss", binding.getCalendar()).format(x));
sb.append(TimeUtil.getSimpleDateFormat("yyyy-MM-dd HH:mm:ss", binding.getCalendar()).format(x));
} else {
this.tsdf = TimeUtil.getSimpleDateFormat(this.tsdf, "yyyy-MM-dd HH:mm:ss",
binding.getMysqlType() == MysqlType.TIMESTAMP && this.preserveInstants.getValue() ? this.serverSession.getSessionTimeZone()
: this.serverSession.getDefaultTimeZone());
buf.append(this.tsdf.format(x));
sb.append(this.tsdf.format(x));
}

if (this.serverSession.getCapabilities().serverSupportsFracSecs() && x.getNanos() > 0) {
buf.append('.');
buf.append(TimeUtil.formatNanos(x.getNanos(), 6));
sb.append('.');
sb.append(TimeUtil.formatNanos(x.getNanos(), 6));
}

intoPacket.writeBytes(StringSelfDataType.STRING_LENENC, StringUtils.getBytes(buf.toString(), this.charEncoding.getValue()));
intoPacket.writeBytes(StringSelfDataType.STRING_LENENC, StringUtils.getBytes(sb.toString(), this.charEncoding.getValue()));
break;

default:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023, Oracle and/or its affiliates.
* Copyright (c) 2022, 2024, Oracle and/or its affiliates.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 2.0, as published by the
Expand Down Expand Up @@ -75,20 +75,20 @@ public String getString(BindValue binding) {
((ZonedDateTime) binding.getValue()).withZoneSameInstant(this.serverSession.getDefaultTimeZone().toZoneId()).toLocalDateTime()),
binding.getField(), binding.keepOrigNanos());

StringBuffer buf = new StringBuffer();
sb = new StringBuilder();

buf.append(TimeUtil.getSimpleDateFormat(null, "''yyyy-MM-dd HH:mm:ss",
sb.append(TimeUtil.getSimpleDateFormat(null, "''yyyy-MM-dd HH:mm:ss",
binding.getMysqlType() == MysqlType.TIMESTAMP && this.preserveInstants.getValue() ? this.serverSession.getSessionTimeZone()
: this.serverSession.getDefaultTimeZone())
.format(x));

if (this.serverSession.getCapabilities().serverSupportsFracSecs() && x.getNanos() > 0) {
buf.append('.');
buf.append(TimeUtil.formatNanos(x.getNanos(), 6));
sb.append('.');
sb.append(TimeUtil.formatNanos(x.getNanos(), 6));
}
buf.append('\'');
sb.append('\'');

return buf.toString();
return sb.toString();

case YEAR:
return String.valueOf(((ZonedDateTime) binding.getValue()).withZoneSameInstant(this.serverSession.getDefaultTimeZone().toZoneId()).getYear());
Expand Down

0 comments on commit 00449a4

Please sign in to comment.