提交 9b2bbeb2 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Reimplement MONTHNAME and DAYNAME without Calendar and SimpleDateFormat

上级 3c45c3c6
......@@ -15,7 +15,7 @@ import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.text.DateFormatSymbols;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.HashMap;
......@@ -153,6 +153,11 @@ public class Function extends Expression implements FunctionCall {
private static final HashMap<String, Integer> DATE_PART = new HashMap<>();
private static final char[] SOUNDEX_INDEX = new char[128];
/**
* English names of months and week days.
*/
private static volatile String[][] MONTHS_AND_WEEKS;
protected Expression[] args;
private final FunctionInfo info;
......@@ -844,9 +849,8 @@ public class Function extends Expression implements FunctionCall {
database.getMode().treatEmptyStringsAsNull);
break;
case DAY_NAME: {
SimpleDateFormat dayName = new SimpleDateFormat(
"EEEE", Locale.ENGLISH);
result = ValueString.get(dayName.format(v0.getDate()),
int dayOfWeek = DateTimeUtils.getSundayDayOfWeek(DateTimeUtils.dateAndTimeFromValue(v0)[0]);
result = ValueString.get(getMonthsAndWeeks(1)[dayOfWeek],
database.getMode().treatEmptyStringsAsNull);
break;
}
......@@ -866,9 +870,8 @@ public class Function extends Expression implements FunctionCall {
result = ValueInt.get(getIntDatePart(v0, info.type));
break;
case MONTH_NAME: {
SimpleDateFormat monthName = new SimpleDateFormat("MMMM",
Locale.ENGLISH);
result = ValueString.get(monthName.format(v0.getDate()),
int month = DateTimeUtils.monthFromDateValue(DateTimeUtils.dateAndTimeFromValue(v0)[0]);
result = ValueString.get(getMonthsAndWeeks(0)[month - 1],
database.getMode().treatEmptyStringsAsNull);
break;
}
......@@ -2025,6 +2028,18 @@ public class Function extends Expression implements FunctionCall {
return r2 - r1;
}
private static String[] getMonthsAndWeeks(int field) {
String[][] result = MONTHS_AND_WEEKS;
if (result == null) {
result = new String[2][];
DateFormatSymbols dfs = DateFormatSymbols.getInstance(Locale.ENGLISH);
result[0] = dfs.getMonths();
result[1] = dfs.getWeekdays();
MONTHS_AND_WEEKS = result;
}
return result[field];
}
private static String substring(String s, int start, int length) {
int len = s.length();
start--;
......
......@@ -26,6 +26,7 @@ public class TestClearReferences extends TestBase {
"org.h2.compress.CompressLZF.cachedHashTable",
"org.h2.engine.DbSettings.defaultSettings",
"org.h2.engine.SessionRemote.sessionFactory",
"org.h2.expression.Function.MONTHS_AND_WEEKS",
"org.h2.jdbcx.JdbcDataSourceFactory.cachedTraceSystem",
"org.h2.store.RecoverTester.instance",
"org.h2.store.fs.FilePath.defaultProvider",
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论