提交 62ff12af authored 作者: httpdigest's avatar httpdigest

Incorporate review remarks from @grandinj

上级 2eb2a31b
...@@ -163,7 +163,7 @@ public class Mode { ...@@ -163,7 +163,7 @@ public class Mode {
/** /**
* Whether to right-pad fixed strings with spaces. * Whether to right-pad fixed strings with spaces.
*/ */
public boolean padFixedStrings; public boolean padFixedLengthStrings;
private final String name; private final String name;
...@@ -261,7 +261,7 @@ public class Mode { ...@@ -261,7 +261,7 @@ public class Mode {
mode.supportedClientInfoPropertiesRegEx = mode.supportedClientInfoPropertiesRegEx =
Pattern.compile("ApplicationName"); Pattern.compile("ApplicationName");
mode.prohibitEmptyInPredicate = true; mode.prohibitEmptyInPredicate = true;
mode.padFixedStrings = true; mode.padFixedLengthStrings = true;
add(mode); add(mode);
mode = new Mode("Ignite"); mode = new Mode("Ignite");
......
...@@ -535,7 +535,15 @@ public abstract class Value { ...@@ -535,7 +535,15 @@ public abstract class Value {
throw throwUnsupportedExceptionForType("%"); throw throwUnsupportedExceptionForType("%");
} }
/**
* Compare a value to the specified type.
*
* @param targetType the type of the returned value
* @return the converted value
*/
public Value convertTo(int targetType) { public Value convertTo(int targetType) {
// Use -1 to indicate "default behaviour" where value conversion should not
// depend on any datatype precision.
return convertTo(targetType, -1, null); return convertTo(targetType, -1, null);
} }
...@@ -543,6 +551,9 @@ public abstract class Value { ...@@ -543,6 +551,9 @@ public abstract class Value {
* Compare a value to the specified type. * Compare a value to the specified type.
* *
* @param targetType the type of the returned value * @param targetType the type of the returned value
* @param the precision of the column to convert this value to.
* The special constant <code>-1</code> is used to indicate that
* the precision plays no role when converting the value
* @return the converted value * @return the converted value
*/ */
public Value convertTo(int targetType, int precision, Mode mode) { public Value convertTo(int targetType, int precision, Mode mode) {
......
...@@ -50,9 +50,7 @@ public class ValueStringFixed extends ValueString { ...@@ -50,9 +50,7 @@ public class ValueStringFixed extends ValueString {
return s; return s;
} }
char[] res = new char[length]; char[] res = new char[length];
for (int i = 0; i < s.length(); i++) { s.getChars(0, s.length(), res, 0);
res[i] = s.charAt(i);
}
Arrays.fill(res, s.length(), length, ' '); Arrays.fill(res, s.length(), length, ' ');
return new String(res); return new String(res);
} }
...@@ -70,10 +68,26 @@ public class ValueStringFixed extends ValueString { ...@@ -70,10 +68,26 @@ public class ValueStringFixed extends ValueString {
* @return the value * @return the value
*/ */
public static ValueStringFixed get(String s) { public static ValueStringFixed get(String s) {
// Use the special precision constant PRECISION_TRIM to indicate
// default H2 behaviour of trimming the value.
return get(s, PRECISION_TRIM, null); return get(s, PRECISION_TRIM, null);
} }
/**
* Get or create a fixed length string value for the given string.
* <p>
* This method will use a {@link Mode}-specific conversion when <code>mode</code> is not <code>null</code>.
* Otherwise it will use the default H2 behaviour of trimming the given string if <code>precision</code>
* is not {@link #PRECISION_DO_NOT_TRIM}.
*
* @param s the string
* @param precision if the {@link Mode#padFixedLengthStrings} indicates that strings should be padded, this
* defines the overall length of the (potentially padded) string.
* If the special constant {@link #PRECISION_DO_NOT_TRIM} is used the value will not be trimmed.
* @return the value
*/
public static ValueStringFixed get(String s, int precision, Mode mode) { public static ValueStringFixed get(String s, int precision, Mode mode) {
if (mode != null && mode.padFixedStrings && precision < Integer.MAX_VALUE) { if (mode != null && mode.padFixedLengthStrings && precision < Integer.MAX_VALUE) {
s = rightPadWithSpaces(s, precision); s = rightPadWithSpaces(s, precision);
} else if (precision != PRECISION_DO_NOT_TRIM) { } else if (precision != PRECISION_DO_NOT_TRIM) {
s = trimRight(s); s = trimRight(s);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论