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