提交 c75170fd authored 作者: httpdigest's avatar httpdigest

Add correct handling of CHAR = CHAR(1) in PostgreSQL compatibility mode

上级 62ff12af
......@@ -35,9 +35,12 @@ public class ValueStringFixed extends ValueString {
}
private static String trimRight(String s) {
return trimRight(s, 0);
}
private static String trimRight(String s, int minLength) {
int endIndex = s.length() - 1;
int i = endIndex;
while (i >= 0 && s.charAt(i) == ' ') {
while (i >= minLength && s.charAt(i) == ' ') {
i--;
}
s = i == endIndex ? s : s.substring(0, i + 1);
......@@ -87,9 +90,21 @@ public class ValueStringFixed extends ValueString {
* @return the value
*/
public static ValueStringFixed get(String s, int precision, Mode mode) {
if (mode != null && mode.padFixedLengthStrings && precision < Integer.MAX_VALUE) {
s = rightPadWithSpaces(s, precision);
// Should fixed strings be padded?
if (mode != null && mode.padFixedLengthStrings) {
if (precision == Integer.MAX_VALUE) {
// CHAR without a length specification is identical to CHAR(1)
precision = 1;
}
if (s.length() < precision) {
// We have to pad
s = rightPadWithSpaces(s, precision);
} else {
// We should trim, because inserting 'A ' into a CHAR(1) is possible!
s = trimRight(s, precision);
}
} else if (precision != PRECISION_DO_NOT_TRIM) {
// Default behaviour of H2
s = trimRight(s);
}
if (s.length() == 0) {
......
......@@ -253,6 +253,26 @@ public class TestCompatibility extends TestBase {
/* Test that WHERE clauses accept unpadded values and will pad before comparison */
assertResult("Hello ", stat, "SELECT CH FROM TEST WHERE CH = 'Hello'");
/* Test CHAR which is identical to CHAR(1) */
stat.execute("DROP TABLE IF EXISTS TEST");
stat.execute("CREATE TABLE TEST(CH CHAR)");
stat.execute("INSERT INTO TEST (CH) VALUES ('')");
assertResult(" ", stat, "SELECT CH FROM TEST");
assertResult(" ", stat, "SELECT CH FROM TEST WHERE CH = ''");
/* Test that excessive spaces are trimmed */
stat.execute("DELETE FROM TEST");
stat.execute("INSERT INTO TEST (CH) VALUES ('1 ')");
assertResult("1", stat, "SELECT CH FROM TEST");
assertResult("1", stat, "SELECT CH FROM TEST WHERE CH = '1 '");
/* Test that we do not trim too far */
stat.execute("DROP TABLE IF EXISTS TEST");
stat.execute("CREATE TABLE TEST(CH CHAR(2))");
stat.execute("INSERT INTO TEST (CH) VALUES ('1 ')");
assertResult("1 ", stat, "SELECT CH FROM TEST");
assertResult("1 ", stat, "SELECT CH FROM TEST WHERE CH = '1 '");
}
private void testMySQL() throws SQLException {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论