提交 e5db57c3 authored 作者: plus33's avatar plus33

Changes based on feedback form Noel Grandin

* Renamed enum NULL_CONSTRAINT to NullConstraintType
* Replaced some switches with if-statement
* Check for Oracle-Mode when parsing Oracles-SQLs NOT NULL ENABLE
VALIDATE syntax. NOTE: 
* Added DbTypeEnum-class to Mode to have a String-free dbType check
featuring the method Database.isDbTypeOneOf(DbTypeEnum... dbTypes)
上级 8eb12a7a
...@@ -18,6 +18,7 @@ import java.util.Comparator; ...@@ -18,6 +18,7 @@ import java.util.Comparator;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import org.h2.api.ErrorCode; import org.h2.api.ErrorCode;
import org.h2.api.Trigger; import org.h2.api.Trigger;
import org.h2.command.ddl.AlterIndexRename; import org.h2.command.ddl.AlterIndexRename;
...@@ -93,7 +94,7 @@ import org.h2.engine.Constants; ...@@ -93,7 +94,7 @@ import org.h2.engine.Constants;
import org.h2.engine.Database; import org.h2.engine.Database;
import org.h2.engine.DbObject; import org.h2.engine.DbObject;
import org.h2.engine.FunctionAlias; import org.h2.engine.FunctionAlias;
import org.h2.engine.Mode; import org.h2.engine.Mode.DbTypeEnum;
import org.h2.engine.Procedure; import org.h2.engine.Procedure;
import org.h2.engine.Right; import org.h2.engine.Right;
import org.h2.engine.Session; import org.h2.engine.Session;
...@@ -820,7 +821,7 @@ public class Parser { ...@@ -820,7 +821,7 @@ public class Parser {
} }
currentPrepared = command; currentPrepared = command;
int start = lastParseIndex; int start = lastParseIndex;
if (!readIf("FROM") && database.getMode() == Mode.getMySQL()) { if (!readIf("FROM") && database.isDbTypeOneOf(DbTypeEnum.MySQL)) {
readIdentifierWithSchema(); readIdentifierWithSchema();
read("FROM"); read("FROM");
} }
...@@ -4073,10 +4074,9 @@ public class Parser { ...@@ -4073,10 +4074,9 @@ public class Parser {
} else if (readIf("VISIBLE")) { } else if (readIf("VISIBLE")) {
column.setVisible(true); column.setVisible(true);
} }
NULL_CONSTRAINT nullConstraint = parseNotNullConstraint(); NullConstraintType nullConstraint = parseNotNullConstraint();
switch (nullConstraint) { switch (nullConstraint) {
case NULL_IS_ALLOWED: case NULL_IS_ALLOWED:
column.setNullable(true); column.setNullable(true);
break; break;
case NULL_IS_NOT_ALLOWED: case NULL_IS_NOT_ALLOWED:
...@@ -4121,13 +4121,8 @@ public class Parser { ...@@ -4121,13 +4121,8 @@ public class Parser {
column.setPrimaryKey(true); column.setPrimaryKey(true);
column.setAutoIncrement(true, start, increment); column.setAutoIncrement(true, start, increment);
} }
nullConstraint = parseNotNullConstraint(); if (NullConstraintType.NULL_IS_NOT_ALLOWED == parseNotNullConstraint()) {
switch (nullConstraint) {
case NULL_IS_NOT_ALLOWED:
column.setNullable(false); column.setNullable(false);
break;
default:
// do nothing
} }
if (readIf("AUTO_INCREMENT") || readIf("BIGSERIAL") || readIf("SERIAL")) { if (readIf("AUTO_INCREMENT") || readIf("BIGSERIAL") || readIf("SERIAL")) {
parseAutoIncrement(column); parseAutoIncrement(column);
...@@ -5876,7 +5871,7 @@ public class Parser { ...@@ -5876,7 +5871,7 @@ public class Parser {
readIf("COLUMN"); // optional readIf("COLUMN"); // optional
String columnName = readColumnIdentifier(); String columnName = readColumnIdentifier();
AlterTableAlterColumn command = null; AlterTableAlterColumn command = null;
NULL_CONSTRAINT nullConstraint = parseNotNullConstraint(); NullConstraintType nullConstraint = parseNotNullConstraint();
switch (nullConstraint) { switch (nullConstraint) {
case NULL_IS_ALLOWED: case NULL_IS_ALLOWED:
case NULL_IS_NOT_ALLOWED: case NULL_IS_NOT_ALLOWED:
...@@ -5885,7 +5880,7 @@ public class Parser { ...@@ -5885,7 +5880,7 @@ public class Parser {
command.setIfTableExists(ifTableExists); command.setIfTableExists(ifTableExists);
Column column = columnIfTableExists(schema, tableName, columnName, ifTableExists); Column column = columnIfTableExists(schema, tableName, columnName, ifTableExists);
command.setOldColumn(column); command.setOldColumn(column);
if (nullConstraint == NULL_CONSTRAINT.NULL_IS_ALLOWED) { if (nullConstraint == NullConstraintType.NULL_IS_ALLOWED) {
command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_NULL); command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_NULL);
} else { } else {
command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_NOT_NULL); command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_NOT_NULL);
...@@ -5950,7 +5945,7 @@ public class Parser { ...@@ -5950,7 +5945,7 @@ public class Parser {
command.setTableName(tableName); command.setTableName(tableName);
command.setIfTableExists(ifTableExists); command.setIfTableExists(ifTableExists);
command.setOldColumn(column); command.setOldColumn(column);
NULL_CONSTRAINT nullConstraint = parseNotNullConstraint(); NullConstraintType nullConstraint = parseNotNullConstraint();
switch (nullConstraint) { switch (nullConstraint) {
case NULL_IS_ALLOWED: case NULL_IS_ALLOWED:
command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_NULL); command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_NULL);
...@@ -6372,14 +6367,8 @@ public class Parser { ...@@ -6372,14 +6367,8 @@ public class Parser {
unique.setTableName(tableName); unique.setTableName(tableName);
command.addConstraintCommand(unique); command.addConstraintCommand(unique);
} }
if (NullConstraintType.NULL_IS_NOT_ALLOWED == parseNotNullConstraint()) {
NULL_CONSTRAINT nullConstraint = parseNotNullConstraint();
switch (nullConstraint) {
case NULL_IS_NOT_ALLOWED:
column.setNullable(false); column.setNullable(false);
break;
default:
// do nothing
} }
if (readIf("CHECK")) { if (readIf("CHECK")) {
Expression expr = readExpression(); Expression expr = readExpression();
...@@ -6482,30 +6471,32 @@ public class Parser { ...@@ -6482,30 +6471,32 @@ public class Parser {
return command; return command;
} }
private enum NULL_CONSTRAINT { private enum NullConstraintType {
NULL_IS_ALLOWED, NULL_IS_NOT_ALLOWED, NO_NULL_CONSTRAINT_FOUND NULL_IS_ALLOWED, NULL_IS_NOT_ALLOWED, NO_NULL_CONSTRAINT_FOUND
} }
private NULL_CONSTRAINT parseNotNullConstraint() { private NullConstraintType parseNotNullConstraint() {
NULL_CONSTRAINT nullConstraint = NULL_CONSTRAINT.NO_NULL_CONSTRAINT_FOUND; NullConstraintType nullConstraint = NullConstraintType.NO_NULL_CONSTRAINT_FOUND;
if ((isToken("NOT") || isToken("NULL"))) { if ((isToken("NOT") || isToken("NULL"))) {
if (readIf("NOT")) { if (readIf("NOT")) {
read("NULL"); read("NULL");
nullConstraint = NULL_CONSTRAINT.NULL_IS_NOT_ALLOWED; nullConstraint = NullConstraintType.NULL_IS_NOT_ALLOWED;
} else { } else {
read("NULL"); read("NULL");
nullConstraint = NULL_CONSTRAINT.NULL_IS_ALLOWED; nullConstraint = NullConstraintType.NULL_IS_ALLOWED;
} }
if (readIf("ENABLE")) { if (database.isDbTypeOneOf(DbTypeEnum.Oracle)) {
readIf("VALIDATE"); // Leave constraint 'as is' if (readIf("ENABLE")) {
if (readIf("NOVALIDATE")) { // Turn off constraint, thus allow NULLs readIf("VALIDATE"); // Leave constraint 'as is'
nullConstraint = NULL_CONSTRAINT.NULL_IS_ALLOWED; if (readIf("NOVALIDATE")) { // Turn off constraint, allow NULLs
nullConstraint = NullConstraintType.NULL_IS_ALLOWED;
}
}
if (readIf("DISABLE")) { // Turn off constraint, allow NULLs
nullConstraint = NullConstraintType.NULL_IS_ALLOWED;
readIf("VALIDATE"); // ignore validate
readIf("NOVALIDATE"); // ignore novalidate
} }
}
if (readIf("DISABLE")) { // Turn off constraint, thus allow NULLs
nullConstraint = NULL_CONSTRAINT.NULL_IS_ALLOWED;
readIf("VALIDATE"); // ignore validate
readIf("NOVALIDATE"); // ignore novalidate
} }
} }
return nullConstraint; return nullConstraint;
......
...@@ -18,6 +18,7 @@ import java.util.concurrent.ConcurrentHashMap; ...@@ -18,6 +18,7 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import org.h2.api.DatabaseEventListener; import org.h2.api.DatabaseEventListener;
import org.h2.api.ErrorCode; import org.h2.api.ErrorCode;
import org.h2.api.JavaObjectSerializer; import org.h2.api.JavaObjectSerializer;
...@@ -26,6 +27,7 @@ import org.h2.command.CommandInterface; ...@@ -26,6 +27,7 @@ import org.h2.command.CommandInterface;
import org.h2.command.ddl.CreateTableData; import org.h2.command.ddl.CreateTableData;
import org.h2.command.dml.SetTypes; import org.h2.command.dml.SetTypes;
import org.h2.constraint.Constraint; import org.h2.constraint.Constraint;
import org.h2.engine.Mode.DbTypeEnum;
import org.h2.index.Cursor; import org.h2.index.Cursor;
import org.h2.index.Index; import org.h2.index.Index;
import org.h2.index.IndexType; import org.h2.index.IndexType;
...@@ -2376,6 +2378,16 @@ public class Database implements DataHandler { ...@@ -2376,6 +2378,16 @@ public class Database implements DataHandler {
return mode; return mode;
} }
public boolean isDbTypeOneOf(DbTypeEnum... dbTypes) {
DbTypeEnum currentDbType = mode.getDbTypeEnum();
for (DbTypeEnum dbType : dbTypes) {
if (currentDbType == dbType) {
return true;
}
}
return false;
}
public boolean isMultiThreaded() { public boolean isMultiThreaded() {
return multiThreaded; return multiThreaded;
} }
......
...@@ -23,7 +23,25 @@ public class Mode { ...@@ -23,7 +23,25 @@ public class Mode {
*/ */
static final String REGULAR = "REGULAR"; static final String REGULAR = "REGULAR";
private static final HashMap<String, Mode> MODES = New.hashMap(); public static enum DbTypeEnum {
REGULAR, UNKNOWN, DB2 , Derby, HSQLDB, Ignite, MSSQLServer, MySQL, Oracle, PostgreSQL;
public static DbTypeEnum parse(String modeAsStr) {
DbTypeEnum result = DbTypeEnum.UNKNOWN;
if (modeAsStr != null) {
modeAsStr = StringUtils.toUpperEnglish(modeAsStr.trim());
for (DbTypeEnum m: DbTypeEnum.values()) {
if (m.name().toUpperCase().equals(modeAsStr)) {
result = m;
break;
}
}
}
return result;
}
}
private static final HashMap<DbTypeEnum, Mode> MODES = New.hashMap();
// Modes are also documented in the features section // Modes are also documented in the features section
...@@ -178,14 +196,14 @@ public class Mode { ...@@ -178,14 +196,14 @@ public class Mode {
*/ */
public Set<String> disallowedTypes = Collections.emptySet(); public Set<String> disallowedTypes = Collections.emptySet();
private final String name; private final DbTypeEnum dbTypeEnum;
static { static {
Mode mode = new Mode(REGULAR); Mode mode = new Mode(DbTypeEnum.REGULAR);
mode.nullConcatIsNull = true; mode.nullConcatIsNull = true;
add(mode); add(mode);
mode = new Mode("DB2"); mode = new Mode(DbTypeEnum.DB2);
mode.aliasColumnName = true; mode.aliasColumnName = true;
mode.supportOffsetFetch = true; mode.supportOffsetFetch = true;
mode.sysDummy1 = true; mode.sysDummy1 = true;
...@@ -200,7 +218,7 @@ public class Mode { ...@@ -200,7 +218,7 @@ public class Mode {
mode.allowDB2TimestampFormat = true; mode.allowDB2TimestampFormat = true;
add(mode); add(mode);
mode = new Mode("Derby"); mode = new Mode(DbTypeEnum.Derby);
mode.aliasColumnName = true; mode.aliasColumnName = true;
mode.uniqueIndexSingleNull = true; mode.uniqueIndexSingleNull = true;
mode.supportOffsetFetch = true; mode.supportOffsetFetch = true;
...@@ -210,7 +228,7 @@ public class Mode { ...@@ -210,7 +228,7 @@ public class Mode {
mode.supportedClientInfoPropertiesRegEx = null; mode.supportedClientInfoPropertiesRegEx = null;
add(mode); add(mode);
mode = new Mode("HSQLDB"); mode = new Mode(DbTypeEnum.HSQLDB);
mode.aliasColumnName = true; mode.aliasColumnName = true;
mode.convertOnlyToSmallerScale = true; mode.convertOnlyToSmallerScale = true;
mode.nullConcatIsNull = true; mode.nullConcatIsNull = true;
...@@ -223,7 +241,7 @@ public class Mode { ...@@ -223,7 +241,7 @@ public class Mode {
mode.supportedClientInfoPropertiesRegEx = null; mode.supportedClientInfoPropertiesRegEx = null;
add(mode); add(mode);
mode = new Mode("MSSQLServer"); mode = new Mode(DbTypeEnum.MSSQLServer);
mode.aliasColumnName = true; mode.aliasColumnName = true;
mode.squareBracketQuotedNames = true; mode.squareBracketQuotedNames = true;
mode.uniqueIndexSingleNull = true; mode.uniqueIndexSingleNull = true;
...@@ -235,7 +253,7 @@ public class Mode { ...@@ -235,7 +253,7 @@ public class Mode {
mode.supportedClientInfoPropertiesRegEx = null; mode.supportedClientInfoPropertiesRegEx = null;
add(mode); add(mode);
mode = new Mode("MySQL"); mode = new Mode(DbTypeEnum.MySQL);
mode.convertInsertNullToZero = true; mode.convertInsertNullToZero = true;
mode.indexDefinitionInCreateTable = true; mode.indexDefinitionInCreateTable = true;
mode.lowerCaseIdentifiers = true; mode.lowerCaseIdentifiers = true;
...@@ -249,7 +267,7 @@ public class Mode { ...@@ -249,7 +267,7 @@ public class Mode {
mode.prohibitEmptyInPredicate = true; mode.prohibitEmptyInPredicate = true;
add(mode); add(mode);
mode = new Mode("Oracle"); mode = new Mode(DbTypeEnum.Oracle);
mode.aliasColumnName = true; mode.aliasColumnName = true;
mode.convertOnlyToSmallerScale = true; mode.convertOnlyToSmallerScale = true;
mode.uniqueIndexSingleNullExceptAllColumnsAreNull = true; mode.uniqueIndexSingleNullExceptAllColumnsAreNull = true;
...@@ -262,7 +280,7 @@ public class Mode { ...@@ -262,7 +280,7 @@ public class Mode {
mode.prohibitEmptyInPredicate = true; mode.prohibitEmptyInPredicate = true;
add(mode); add(mode);
mode = new Mode("PostgreSQL"); mode = new Mode(DbTypeEnum.PostgreSQL);
mode.aliasColumnName = true; mode.aliasColumnName = true;
mode.nullConcatIsNull = true; mode.nullConcatIsNull = true;
mode.supportOffsetFetch = true; mode.supportOffsetFetch = true;
...@@ -285,19 +303,19 @@ public class Mode { ...@@ -285,19 +303,19 @@ public class Mode {
mode.disallowedTypes = disallowedTypes; mode.disallowedTypes = disallowedTypes;
add(mode); add(mode);
mode = new Mode("Ignite"); mode = new Mode(DbTypeEnum.Ignite);
mode.nullConcatIsNull = true; mode.nullConcatIsNull = true;
mode.allowAffinityKey = true; mode.allowAffinityKey = true;
mode.indexDefinitionInCreateTable = true; mode.indexDefinitionInCreateTable = true;
add(mode); add(mode);
} }
private Mode(String name) { private Mode(DbTypeEnum dbTypeEnum) {
this.name = name; this.dbTypeEnum = dbTypeEnum;
} }
private static void add(Mode mode) { private static void add(Mode mode) {
MODES.put(StringUtils.toUpperEnglish(mode.name), mode); MODES.put(mode.dbTypeEnum, mode);
} }
/** /**
...@@ -307,15 +325,24 @@ public class Mode { ...@@ -307,15 +325,24 @@ public class Mode {
* @return the mode object * @return the mode object
*/ */
public static Mode getInstance(String name) { public static Mode getInstance(String name) {
return MODES.get(StringUtils.toUpperEnglish(name)); DbTypeEnum dbTypeEnum = DbTypeEnum.parse(name);
return MODES.get(dbTypeEnum);
}
public static Mode getInstance(DbTypeEnum dbTypeEnum) {
return MODES.get(dbTypeEnum);
} }
public static Mode getMySQL() { public static Mode getMySQL() {
return getInstance("MySQL"); return getInstance(DbTypeEnum.MySQL);
}
public DbTypeEnum getDbTypeEnum() {
return dbTypeEnum;
} }
public String getName() { public String getName() {
return name; return dbTypeEnum.name();
} }
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论