Unverified 提交 00e46bf6 authored 作者: Noel Grandin's avatar Noel Grandin 提交者: GitHub

Merge pull request #741 from katzyn/misc

More cleanups in LocalDateTimeUtils and other minor changes
...@@ -6844,34 +6844,22 @@ public class Parser { ...@@ -6844,34 +6844,22 @@ public class Parser {
* @return the quoted identifier * @return the quoted identifier
*/ */
public static String quoteIdentifier(String s) { public static String quoteIdentifier(String s) {
if (s == null || s.length() == 0) { if (s == null) {
return "\"\""; return "\"\"";
} }
char c = s.charAt(0); if (isSimpleIdentifier(s))
// lowercase a-z is quoted as well
if ((!Character.isLetter(c) && c != '_') || Character.isLowerCase(c)) {
return StringUtils.quoteIdentifier(s);
}
for (int i = 1, length = s.length(); i < length; i++) {
c = s.charAt(i);
if ((!Character.isLetterOrDigit(c) && c != '_') ||
Character.isLowerCase(c)) {
return StringUtils.quoteIdentifier(s);
}
}
if (isKeyword(s, true)) {
return StringUtils.quoteIdentifier(s);
}
return s; return s;
return StringUtils.quoteIdentifier(s);
} }
/** /**
* @param s * @param s
* identifier to check * identifier to check
* @return is specified identifier may be used without quotes * @return is specified identifier may be used without quotes
* @throws NullPointerException if s is {@code null}
*/ */
public static boolean isSimpleIdentifier(String s) { public static boolean isSimpleIdentifier(String s) {
if (s == null || s.length() == 0) { if (s.length() == 0) {
return false; return false;
} }
char c = s.charAt(0); char c = s.charAt(0);
......
...@@ -79,7 +79,7 @@ public class Mode { ...@@ -79,7 +79,7 @@ public class Mode {
* [OFFSET .. ROW|ROWS] [FETCH FIRST .. ROW|ROWS ONLY] * [OFFSET .. ROW|ROWS] [FETCH FIRST .. ROW|ROWS ONLY]
* as an alternative for LIMIT .. OFFSET. * as an alternative for LIMIT .. OFFSET.
*/ */
public boolean supportOffsetFetch = Constants.VERSION_MINOR >= 4 ? true : false; public boolean supportOffsetFetch = Constants.VERSION_MINOR >= 4;
/** /**
* The system columns 'CTID' and 'OID' are supported. * The system columns 'CTID' and 'OID' are supported.
......
...@@ -344,7 +344,7 @@ public class SysProperties { ...@@ -344,7 +344,7 @@ public class SysProperties {
*/ */
public static final boolean OLD_STYLE_OUTER_JOIN = public static final boolean OLD_STYLE_OUTER_JOIN =
Utils.getProperty("h2.oldStyleOuterJoin", Utils.getProperty("h2.oldStyleOuterJoin",
Constants.VERSION_MINOR >= 4 ? false : true); Constants.VERSION_MINOR < 4);
/** /**
* System property <code>h2.pgClientEncoding</code> (default: UTF-8).<br /> * System property <code>h2.pgClientEncoding</code> (default: UTF-8).<br />
...@@ -401,7 +401,7 @@ public class SysProperties { ...@@ -401,7 +401,7 @@ public class SysProperties {
*/ */
public static final boolean SORT_BINARY_UNSIGNED = public static final boolean SORT_BINARY_UNSIGNED =
Utils.getProperty("h2.sortBinaryUnsigned", Utils.getProperty("h2.sortBinaryUnsigned",
Constants.VERSION_MINOR >= 4 ? true : false); Constants.VERSION_MINOR >= 4);
/** /**
* System property <code>h2.sortNullsHigh</code> (default: false).<br /> * System property <code>h2.sortNullsHigh</code> (default: false).<br />
...@@ -455,7 +455,7 @@ public class SysProperties { ...@@ -455,7 +455,7 @@ public class SysProperties {
*/ */
public static final boolean IMPLICIT_RELATIVE_PATH = public static final boolean IMPLICIT_RELATIVE_PATH =
Utils.getProperty("h2.implicitRelativePath", Utils.getProperty("h2.implicitRelativePath",
Constants.VERSION_MINOR >= 4 ? false : true); Constants.VERSION_MINOR < 4);
/** /**
* System property <code>h2.urlMap</code> (default: null).<br /> * System property <code>h2.urlMap</code> (default: null).<br />
......
...@@ -3824,14 +3824,12 @@ public class JdbcResultSet extends TraceObject implements ResultSet, JdbcResultS ...@@ -3824,14 +3824,12 @@ public class JdbcResultSet extends TraceObject implements ResultSet, JdbcResultS
} else if (LocalDateTimeUtils.isLocalTime(type)) { } else if (LocalDateTimeUtils.isLocalTime(type)) {
return type.cast(LocalDateTimeUtils.valueToLocalTime(value)); return type.cast(LocalDateTimeUtils.valueToLocalTime(value));
} else if (LocalDateTimeUtils.isLocalDateTime(type)) { } else if (LocalDateTimeUtils.isLocalDateTime(type)) {
return type.cast(LocalDateTimeUtils.valueToLocalDateTime( return type.cast(LocalDateTimeUtils.valueToLocalDateTime(value));
(ValueTimestamp) value));
} else if (LocalDateTimeUtils.isInstant(type)) { } else if (LocalDateTimeUtils.isInstant(type)) {
return type.cast(LocalDateTimeUtils.valueToInstant(value)); return type.cast(LocalDateTimeUtils.valueToInstant(value));
} else if (LocalDateTimeUtils.isOffsetDateTime(type) && } else if (LocalDateTimeUtils.isOffsetDateTime(type) &&
value instanceof ValueTimestampTimeZone) { value instanceof ValueTimestampTimeZone) {
return type.cast(LocalDateTimeUtils.valueToOffsetDateTime( return type.cast(LocalDateTimeUtils.valueToOffsetDateTime(value));
(ValueTimestampTimeZone) value));
} else { } else {
throw unsupported(type.getName()); throw unsupported(type.getName());
} }
......
...@@ -1332,9 +1332,6 @@ public class JdbcStatement extends TraceObject implements Statement, JdbcStateme ...@@ -1332,9 +1332,6 @@ public class JdbcStatement extends TraceObject implements Statement, JdbcStateme
*/ */
@Override @Override
public boolean isSimpleIdentifier(String identifier) throws SQLException { public boolean isSimpleIdentifier(String identifier) throws SQLException {
if (identifier == null)
// To conform with JDBC specification
throw new NullPointerException();
return Parser.isSimpleIdentifier(identifier); return Parser.isSimpleIdentifier(identifier);
} }
......
...@@ -1099,7 +1099,7 @@ public class WebApp { ...@@ -1099,7 +1099,7 @@ public class WebApp {
if (isBuiltIn(sql, "@best_row_identifier")) { if (isBuiltIn(sql, "@best_row_identifier")) {
String[] p = split(sql); String[] p = split(sql);
int scale = p[4] == null ? 0 : Integer.parseInt(p[4]); int scale = p[4] == null ? 0 : Integer.parseInt(p[4]);
boolean nullable = p[5] == null ? false : Boolean.parseBoolean(p[5]); boolean nullable = Boolean.parseBoolean(p[5]);
return meta.getBestRowIdentifier(p[1], p[2], p[3], scale, nullable); return meta.getBestRowIdentifier(p[1], p[2], p[3], scale, nullable);
} else if (isBuiltIn(sql, "@catalogs")) { } else if (isBuiltIn(sql, "@catalogs")) {
return meta.getCatalogs(); return meta.getCatalogs();
...@@ -1120,8 +1120,8 @@ public class WebApp { ...@@ -1120,8 +1120,8 @@ public class WebApp {
return meta.getImportedKeys(p[1], p[2], p[3]); return meta.getImportedKeys(p[1], p[2], p[3]);
} else if (isBuiltIn(sql, "@index_info")) { } else if (isBuiltIn(sql, "@index_info")) {
String[] p = split(sql); String[] p = split(sql);
boolean unique = p[4] == null ? false : Boolean.parseBoolean(p[4]); boolean unique = Boolean.parseBoolean(p[4]);
boolean approx = p[5] == null ? false : Boolean.parseBoolean(p[5]); boolean approx = Boolean.parseBoolean(p[5]);
return meta.getIndexInfo(p[1], p[2], p[3], unique, approx); return meta.getIndexInfo(p[1], p[2], p[3], unique, approx);
} else if (isBuiltIn(sql, "@primary_keys")) { } else if (isBuiltIn(sql, "@primary_keys")) {
String[] p = split(sql); String[] p = split(sql);
......
...@@ -13,7 +13,6 @@ import java.sql.Timestamp; ...@@ -13,7 +13,6 @@ import java.sql.Timestamp;
import java.util.Arrays; import java.util.Arrays;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.h2.api.TimestampWithTimeZone;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.value.Value; import org.h2.value.Value;
import org.h2.value.ValueDate; import org.h2.value.ValueDate;
...@@ -418,13 +417,12 @@ public class LocalDateTimeUtils { ...@@ -418,13 +417,12 @@ public class LocalDateTimeUtils {
* @param value the value to convert * @param value the value to convert
* @return the LocalDateTime * @return the LocalDateTime
*/ */
public static Object valueToLocalDateTime(ValueTimestamp value) { public static Object valueToLocalDateTime(Value value) {
long dateValue = value.getDateValue(); ValueTimestamp valueTimestamp = (ValueTimestamp) value.convertTo(Value.TIMESTAMP);
long timeNanos = value.getTimeNanos(); long dateValue = valueTimestamp.getDateValue();
long timeNanos = valueTimestamp.getTimeNanos();
try { try {
Object localDate = localDateFromDateValue(dateValue); return localDateTimeFromDateNanos(dateValue, timeNanos);
Object localDateTime = LOCAL_DATE_AT_START_OF_DAY.invoke(localDate);
return LOCAL_DATE_TIME_PLUS_NANOS.invoke(localDateTime, timeNanos);
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
throw DbException.convert(e); throw DbException.convert(e);
} catch (InvocationTargetException e) { } catch (InvocationTargetException e) {
...@@ -458,19 +456,14 @@ public class LocalDateTimeUtils { ...@@ -458,19 +456,14 @@ public class LocalDateTimeUtils {
* @param value the value to convert * @param value the value to convert
* @return the OffsetDateTime * @return the OffsetDateTime
*/ */
public static Object valueToOffsetDateTime(ValueTimestampTimeZone value) { public static Object valueToOffsetDateTime(Value value) {
return timestampWithTimeZoneToOffsetDateTime((TimestampWithTimeZone) value.getObject()); ValueTimestampTimeZone valueTimestampTimeZone = (ValueTimestampTimeZone) value.convertTo(Value.TIMESTAMP_TZ);
} long dateValue = valueTimestampTimeZone.getDateValue();
long timeNanos = valueTimestampTimeZone.getTimeNanos();
private static Object timestampWithTimeZoneToOffsetDateTime(
TimestampWithTimeZone timestampWithTimeZone) {
long dateValue = timestampWithTimeZone.getYMD();
long timeNanos = timestampWithTimeZone.getNanosSinceMidnight();
try { try {
Object localDateTime = localDateTimeFromDateNanos(dateValue, timeNanos); Object localDateTime = localDateTimeFromDateNanos(dateValue, timeNanos);
short timeZoneOffsetMins = timestampWithTimeZone.getTimeZoneOffsetMins(); short timeZoneOffsetMins = valueTimestampTimeZone.getTimeZoneOffsetMins();
int offsetSeconds = (int) TimeUnit.MINUTES.toSeconds(timeZoneOffsetMins); int offsetSeconds = (int) TimeUnit.MINUTES.toSeconds(timeZoneOffsetMins);
Object offset = ZONE_OFFSET_OF_TOTAL_SECONDS.invoke(null, offsetSeconds); Object offset = ZONE_OFFSET_OF_TOTAL_SECONDS.invoke(null, offsetSeconds);
......
...@@ -31,6 +31,12 @@ public class ToChar { ...@@ -31,6 +31,12 @@ public class ToChar {
*/ */
private static final long JULIAN_EPOCH; private static final long JULIAN_EPOCH;
private static final int[] ROMAN_VALUES = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9,
5, 4, 1 };
private static final String[] ROMAN_NUMERALS = { "M", "CM", "D", "CD", "C", "XC",
"L", "XL", "X", "IX", "V", "IV", "I" };
static { static {
GregorianCalendar epoch = new GregorianCalendar(Locale.ENGLISH); GregorianCalendar epoch = new GregorianCalendar(Locale.ENGLISH);
epoch.setGregorianChange(new Date(Long.MAX_VALUE)); epoch.setGregorianChange(new Date(Long.MAX_VALUE));
...@@ -410,14 +416,10 @@ public class ToChar { ...@@ -410,14 +416,10 @@ public class ToChar {
} }
private static String toRomanNumeral(int number) { private static String toRomanNumeral(int number) {
int[] values = new int[] { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9,
5, 4, 1 };
String[] numerals = new String[] { "M", "CM", "D", "CD", "C", "XC",
"L", "XL", "X", "IX", "V", "IV", "I" };
StringBuilder result = new StringBuilder(); StringBuilder result = new StringBuilder();
for (int i = 0; i < values.length; i++) { for (int i = 0; i < ROMAN_VALUES.length; i++) {
int value = values[i]; int value = ROMAN_VALUES[i];
String numeral = numerals[i]; String numeral = ROMAN_NUMERALS[i];
while (number >= value) { while (number >= value) {
result.append(numeral); result.append(numeral);
number -= value; number -= value;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论