提交 25e44117 authored 作者: httpdigest's avatar httpdigest

Check for unsupported column types in PostgreSQL mode

上级 5831e75e
...@@ -4002,8 +4002,14 @@ public class Parser { ...@@ -4002,8 +4002,14 @@ public class Parser {
private Column parseColumnForTable(String columnName, private Column parseColumnForTable(String columnName,
boolean defaultNullable) { boolean defaultNullable) {
Column column; Column column;
boolean isIdentity = false; boolean isIdentity = readIf("IDENTITY");
if (readIf("IDENTITY") || readIf("BIGSERIAL")) { if (isIdentity || readIf("BIGSERIAL")) {
// Check if any of them are disallowed in the current Mode
if (isIdentity && session.getDatabase().getMode().
disallowedTypes.contains("IDENTITY")) {
throw DbException.get(ErrorCode.UNKNOWN_DATA_TYPE_1,
currentToken);
}
column = new Column(columnName, Value.LONG); column = new Column(columnName, Value.LONG);
column.setOriginalSQL("IDENTITY"); column.setOriginalSQL("IDENTITY");
parseAutoIncrement(column); parseAutoIncrement(column);
...@@ -4176,7 +4182,7 @@ public class Parser { ...@@ -4176,7 +4182,7 @@ public class Parser {
enumerators = templateColumn.getEnumerators(); enumerators = templateColumn.getEnumerators();
} else { } else {
dataType = DataType.getTypeByName(original); dataType = DataType.getTypeByName(original);
if (dataType == null) { if (dataType == null || session.getDatabase().getMode().disallowedTypes.contains(original)) {
throw DbException.get(ErrorCode.UNKNOWN_DATA_TYPE_1, throw DbException.get(ErrorCode.UNKNOWN_DATA_TYPE_1,
currentToken); currentToken);
} }
......
...@@ -5,12 +5,14 @@ ...@@ -5,12 +5,14 @@
*/ */
package org.h2.engine; package org.h2.engine;
import org.h2.util.New; import java.util.Collections;
import org.h2.util.StringUtils;
import java.util.HashMap; import java.util.HashMap;
import java.util.Set;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.h2.util.New;
import org.h2.util.StringUtils;
/** /**
* The compatibility modes. There is a fixed set of modes (for example * The compatibility modes. There is a fixed set of modes (for example
* PostgreSQL, MySQL). Each mode has different settings. * PostgreSQL, MySQL). Each mode has different settings.
...@@ -170,6 +172,13 @@ public class Mode { ...@@ -170,6 +172,13 @@ public class Mode {
*/ */
public boolean allowDB2TimestampFormat; public boolean allowDB2TimestampFormat;
/**
* An optional Set of hidden/disallowed column types.
* Certain DBMSs don't support all column types provided by H2, such as
* "NUMBER" when using PostgreSQL mode.
*/
public Set<String> disallowedTypes = Collections.emptySet();
private final String name; private final String name;
static { static {
...@@ -268,6 +277,12 @@ public class Mode { ...@@ -268,6 +277,12 @@ public class Mode {
Pattern.compile("ApplicationName"); Pattern.compile("ApplicationName");
mode.prohibitEmptyInPredicate = true; mode.prohibitEmptyInPredicate = true;
mode.padFixedLengthStrings = true; mode.padFixedLengthStrings = true;
// Enumerate all H2 types NOT supported by PostgreSQL:
Set<String> disallowedTypes = new java.util.HashSet<String>();
disallowedTypes.add("NUMBER");
disallowedTypes.add("IDENTITY");
disallowedTypes.add("TINYINT");
mode.disallowedTypes = disallowedTypes;
add(mode); add(mode);
mode = new Mode("Ignite"); mode = new Mode("Ignite");
......
...@@ -246,6 +246,8 @@ public class TestCompatibility extends TestBase { ...@@ -246,6 +246,8 @@ public class TestCompatibility extends TestBase {
assertResult("ABC", stat, "SELECT SUBSTRING('ABCDEF' FOR 3)"); assertResult("ABC", stat, "SELECT SUBSTRING('ABCDEF' FOR 3)");
assertResult("ABCD", stat, "SELECT SUBSTRING('0ABCDEF' FROM 2 FOR 4)"); assertResult("ABCD", stat, "SELECT SUBSTRING('0ABCDEF' FROM 2 FOR 4)");
/* --------- Behaviour of CHAR(N) --------- */
/* Test right-padding of CHAR(N) at INSERT */ /* Test right-padding of CHAR(N) at INSERT */
stat.execute("CREATE TABLE TEST(CH CHAR(10))"); stat.execute("CREATE TABLE TEST(CH CHAR(10))");
stat.execute("INSERT INTO TEST (CH) VALUES ('Hello')"); stat.execute("INSERT INTO TEST (CH) VALUES ('Hello')");
...@@ -273,6 +275,19 @@ public class TestCompatibility extends TestBase { ...@@ -273,6 +275,19 @@ public class TestCompatibility extends TestBase {
stat.execute("INSERT INTO TEST (CH) VALUES ('1 ')"); stat.execute("INSERT INTO TEST (CH) VALUES ('1 ')");
assertResult("1 ", stat, "SELECT CH FROM TEST"); assertResult("1 ", stat, "SELECT CH FROM TEST");
assertResult("1 ", stat, "SELECT CH FROM TEST WHERE CH = '1 '"); assertResult("1 ", stat, "SELECT CH FROM TEST WHERE CH = '1 '");
/* --------- Disallowed column types --------- */
String[] DISALLOWED_TYPES = {"NUMBER", "IDENTITY", "TINYINT"};
for (String type : DISALLOWED_TYPES) {
stat.execute("DROP TABLE IF EXISTS TEST");
try {
stat.execute("CREATE TABLE TEST(COL " + type + ")");
fail("Expect type " + type + " to not exist in PostgreSQL mode");
} catch (org.h2.jdbc.JdbcSQLException e) {
/* Expected! */
}
}
} }
private void testMySQL() throws SQLException { private void testMySQL() throws SQLException {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论