repair setTimestamp format parse issue in B dbcompatibility mode

This commit is contained in:
justbk
2023-08-18 18:41:56 +08:00
parent d2ee82c3e3
commit 1fcb648eeb
3 changed files with 26 additions and 12 deletions

View File

@ -537,6 +537,11 @@ public enum PGProperty {
*/ */
HEARTBEAT_PERIOD("heartbeatPeriod", "0", "heartbeat interval time"), HEARTBEAT_PERIOD("heartbeatPeriod", "0", "heartbeat interval time"),
/**
* It is used to change timestamp parameter in TimestampUtils convert.
*/
TIMESTAMP_NANO_FORMAT("timestampNanoFormat", "0", "0 main's already add nana seconds. 1 mains nano<1000 will not add"),
/** /**
* In the scenario where heartbeat maintenance is enabled for the active node, * In the scenario where heartbeat maintenance is enabled for the active node,
* if the active node is down, set the timeout threshold for searching for the active node. * if the active node is down, set the timeout threshold for searching for the active node.

View File

@ -313,6 +313,7 @@ public class PgConnection implements BaseConnection {
return queryExecutor.getTimeZone(); return queryExecutor.getTimeZone();
} }
}); });
timestampUtils.setTimestampNanoFormat(PGProperty.TIMESTAMP_NANO_FORMAT.getInteger(info));
// Initialize common queries. // Initialize common queries.
// isParameterized==true so full parse is performed and the engine knows the query // isParameterized==true so full parse is performed and the engine knows the query

View File

@ -59,6 +59,7 @@ public class TimestampUtils {
private TimeZone prevDefaultZoneFieldValue; private TimeZone prevDefaultZoneFieldValue;
private TimeZone defaultTimeZoneCache; private TimeZone defaultTimeZoneCache;
private int timestampNanoFormat = 0;
static { static {
// The expected maximum value is 60 (seconds), so 64 is used "just in case" // The expected maximum value is 60 (seconds), so 64 is used "just in case"
@ -131,6 +132,10 @@ public class TimestampUtils {
this.timeZoneProvider = timeZoneProvider; this.timeZoneProvider = timeZoneProvider;
} }
public void setTimestampNanoFormat(int format) {
this.timestampNanoFormat = format;
}
private Calendar getCalendar(int sign, int hr, int min, int sec) { private Calendar getCalendar(int sign, int hr, int min, int sec) {
int rawOffset = sign * (((hr * 60 + min) * 60 + sec) * 1000); int rawOffset = sign * (((hr * 60 + min) * 60 + sec) * 1000);
if (calCache != null && calCacheZone == rawOffset) { if (calCache != null && calCacheZone == rawOffset) {
@ -661,7 +666,7 @@ public class TimestampUtils {
appendDate(sbuf, cal); appendDate(sbuf, cal);
sbuf.append(' '); sbuf.append(' ');
appendTime(sbuf, cal, nanos); appendTime(sbuf, cal, nanos, timestampNanoFormat);
if (withTimeZone) { if (withTimeZone) {
appendTimeZone(sbuf, cal); appendTimeZone(sbuf, cal);
} }
@ -708,7 +713,7 @@ public class TimestampUtils {
sbuf.setLength(0); sbuf.setLength(0);
appendTime(sbuf, cal, cal.get(Calendar.MILLISECOND) * 1000000); appendTime(sbuf, cal, cal.get(Calendar.MILLISECOND) * 1000000, timestampNanoFormat);
// The 'time' parser for <= 7.3 doesn't like timezones. // The 'time' parser for <= 7.3 doesn't like timezones.
if (withTimeZone) { if (withTimeZone) {
@ -742,11 +747,11 @@ public class TimestampUtils {
sb.append(NUMBERS[day]); sb.append(NUMBERS[day]);
} }
private static void appendTime(StringBuilder sb, Calendar cal, int nanos) { private static void appendTime(StringBuilder sb, Calendar cal, int nanos, int format) {
int hours = cal.get(Calendar.HOUR_OF_DAY); int hours = cal.get(Calendar.HOUR_OF_DAY);
int minutes = cal.get(Calendar.MINUTE); int minutes = cal.get(Calendar.MINUTE);
int seconds = cal.get(Calendar.SECOND); int seconds = cal.get(Calendar.SECOND);
appendTime(sb, hours, minutes, seconds, nanos); appendTime(sb, hours, minutes, seconds, nanos, format);
} }
/** /**
@ -759,7 +764,7 @@ public class TimestampUtils {
* @param seconds seconds * @param seconds seconds
* @param nanos nanoseconds * @param nanos nanoseconds
*/ */
private static void appendTime(StringBuilder sb, int hours, int minutes, int seconds, int nanos) { private static void appendTime(StringBuilder sb, int hours, int minutes, int seconds, int nanos, int format) {
sb.append(NUMBERS[hours]); sb.append(NUMBERS[hours]);
sb.append(':'); sb.append(':');
@ -773,21 +778,24 @@ public class TimestampUtils {
// a two digit fractional second, but we don't need to support 7.1 // a two digit fractional second, but we don't need to support 7.1
// anymore and getting the version number here is difficult. // anymore and getting the version number here is difficult.
// //
if (nanos < 1000) { if (nanos < 1000 && format != 0) {
return; return;
} }
sb.append('.'); sb.append('.');
int len = sb.length(); int len = sb.length();
sb.append(nanos / 1000); // append microseconds sb.append(nanos / 1000); // append microseconds
int needZeros = 6 - (sb.length() - len); final int NANO_BITS = 6;
int needZeros = NANO_BITS - (sb.length() - len);
if (needZeros > 0) { if (needZeros > 0) {
sb.insert(len, ZEROS, 0, needZeros); sb.insert(len, ZEROS, 0, needZeros);
} }
int end = sb.length() - 1; int end = sb.length() - 1;
while (sb.charAt(end) == '0') { int needDelete = NANO_BITS - 1;
while (sb.charAt(end) == '0' && needDelete != 0) {
sb.deleteCharAt(end); sb.deleteCharAt(end);
end--; end--;
needDelete --;
} }
} }
@ -855,7 +863,7 @@ public class TimestampUtils {
// it relies on the fact that appendTime just truncates 000..999 nanosecond part // it relies on the fact that appendTime just truncates 000..999 nanosecond part
localTime = localTime.plus(ONE_MICROSECOND); localTime = localTime.plus(ONE_MICROSECOND);
} }
appendTime(sbuf, localTime); appendTime(sbuf, localTime, timestampNanoFormat);
return sbuf.toString(); return sbuf.toString();
} }
@ -879,7 +887,7 @@ public class TimestampUtils {
LocalDate localDate = localDateTime.toLocalDate(); LocalDate localDate = localDateTime.toLocalDate();
appendDate(sbuf, localDate); appendDate(sbuf, localDate);
sbuf.append(' '); sbuf.append(' ');
appendTime(sbuf, localDateTime.toLocalTime()); appendTime(sbuf, localDateTime.toLocalTime(), timestampNanoFormat);
appendTimeZone(sbuf, offsetDateTime.getOffset()); appendTimeZone(sbuf, offsetDateTime.getOffset());
appendEra(sbuf, localDate); appendEra(sbuf, localDate);
@ -911,12 +919,12 @@ public class TimestampUtils {
appendDate(sb, year, month, day); appendDate(sb, year, month, day);
} }
private static void appendTime(StringBuilder sb, LocalTime localTime) { private static void appendTime(StringBuilder sb, LocalTime localTime, int format) {
int hours = localTime.getHour(); int hours = localTime.getHour();
int minutes = localTime.getMinute(); int minutes = localTime.getMinute();
int seconds = localTime.getSecond(); int seconds = localTime.getSecond();
int nanos = localTime.getNano(); int nanos = localTime.getNano();
appendTime(sb, hours, minutes, seconds, nanos); appendTime(sb, hours, minutes, seconds, nanos, format);
} }
private void appendTimeZone(StringBuilder sb, java.time.ZoneOffset offset) { private void appendTimeZone(StringBuilder sb, java.time.ZoneOffset offset) {