提交 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;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import org.h2.api.ErrorCode;
import org.h2.api.Trigger;
import org.h2.command.ddl.AlterIndexRename;
......@@ -93,7 +94,7 @@ import org.h2.engine.Constants;
import org.h2.engine.Database;
import org.h2.engine.DbObject;
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.Right;
import org.h2.engine.Session;
......@@ -820,7 +821,7 @@ public class Parser {
}
currentPrepared = command;
int start = lastParseIndex;
if (!readIf("FROM") && database.getMode() == Mode.getMySQL()) {
if (!readIf("FROM") && database.isDbTypeOneOf(DbTypeEnum.MySQL)) {
readIdentifierWithSchema();
read("FROM");
}
......@@ -4073,10 +4074,9 @@ public class Parser {
} else if (readIf("VISIBLE")) {
column.setVisible(true);
}
NULL_CONSTRAINT nullConstraint = parseNotNullConstraint();
NullConstraintType nullConstraint = parseNotNullConstraint();
switch (nullConstraint) {
case NULL_IS_ALLOWED:
column.setNullable(true);
break;
case NULL_IS_NOT_ALLOWED:
......@@ -4121,13 +4121,8 @@ public class Parser {
column.setPrimaryKey(true);
column.setAutoIncrement(true, start, increment);
}
nullConstraint = parseNotNullConstraint();
switch (nullConstraint) {
case NULL_IS_NOT_ALLOWED:
if (NullConstraintType.NULL_IS_NOT_ALLOWED == parseNotNullConstraint()) {
column.setNullable(false);
break;
default:
// do nothing
}
if (readIf("AUTO_INCREMENT") || readIf("BIGSERIAL") || readIf("SERIAL")) {
parseAutoIncrement(column);
......@@ -5876,7 +5871,7 @@ public class Parser {
readIf("COLUMN"); // optional
String columnName = readColumnIdentifier();
AlterTableAlterColumn command = null;
NULL_CONSTRAINT nullConstraint = parseNotNullConstraint();
NullConstraintType nullConstraint = parseNotNullConstraint();
switch (nullConstraint) {
case NULL_IS_ALLOWED:
case NULL_IS_NOT_ALLOWED:
......@@ -5885,7 +5880,7 @@ public class Parser {
command.setIfTableExists(ifTableExists);
Column column = columnIfTableExists(schema, tableName, columnName, ifTableExists);
command.setOldColumn(column);
if (nullConstraint == NULL_CONSTRAINT.NULL_IS_ALLOWED) {
if (nullConstraint == NullConstraintType.NULL_IS_ALLOWED) {
command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_NULL);
} else {
command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_NOT_NULL);
......@@ -5950,7 +5945,7 @@ public class Parser {
command.setTableName(tableName);
command.setIfTableExists(ifTableExists);
command.setOldColumn(column);
NULL_CONSTRAINT nullConstraint = parseNotNullConstraint();
NullConstraintType nullConstraint = parseNotNullConstraint();
switch (nullConstraint) {
case NULL_IS_ALLOWED:
command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_NULL);
......@@ -6372,14 +6367,8 @@ public class Parser {
unique.setTableName(tableName);
command.addConstraintCommand(unique);
}
NULL_CONSTRAINT nullConstraint = parseNotNullConstraint();
switch (nullConstraint) {
case NULL_IS_NOT_ALLOWED:
if (NullConstraintType.NULL_IS_NOT_ALLOWED == parseNotNullConstraint()) {
column.setNullable(false);
break;
default:
// do nothing
}
if (readIf("CHECK")) {
Expression expr = readExpression();
......@@ -6482,30 +6471,32 @@ public class Parser {
return command;
}
private enum NULL_CONSTRAINT {
private enum NullConstraintType {
NULL_IS_ALLOWED, NULL_IS_NOT_ALLOWED, NO_NULL_CONSTRAINT_FOUND
}
private NULL_CONSTRAINT parseNotNullConstraint() {
NULL_CONSTRAINT nullConstraint = NULL_CONSTRAINT.NO_NULL_CONSTRAINT_FOUND;
private NullConstraintType parseNotNullConstraint() {
NullConstraintType nullConstraint = NullConstraintType.NO_NULL_CONSTRAINT_FOUND;
if ((isToken("NOT") || isToken("NULL"))) {
if (readIf("NOT")) {
read("NULL");
nullConstraint = NULL_CONSTRAINT.NULL_IS_NOT_ALLOWED;
nullConstraint = NullConstraintType.NULL_IS_NOT_ALLOWED;
} else {
read("NULL");
nullConstraint = NULL_CONSTRAINT.NULL_IS_ALLOWED;
nullConstraint = NullConstraintType.NULL_IS_ALLOWED;
}
if (readIf("ENABLE")) {
readIf("VALIDATE"); // Leave constraint 'as is'
if (readIf("NOVALIDATE")) { // Turn off constraint, thus allow NULLs
nullConstraint = NULL_CONSTRAINT.NULL_IS_ALLOWED;
if (database.isDbTypeOneOf(DbTypeEnum.Oracle)) {
if (readIf("ENABLE")) {
readIf("VALIDATE"); // Leave constraint 'as is'
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;
......
......@@ -18,6 +18,7 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import org.h2.api.DatabaseEventListener;
import org.h2.api.ErrorCode;
import org.h2.api.JavaObjectSerializer;
......@@ -26,6 +27,7 @@ import org.h2.command.CommandInterface;
import org.h2.command.ddl.CreateTableData;
import org.h2.command.dml.SetTypes;
import org.h2.constraint.Constraint;
import org.h2.engine.Mode.DbTypeEnum;
import org.h2.index.Cursor;
import org.h2.index.Index;
import org.h2.index.IndexType;
......@@ -2376,6 +2378,16 @@ public class Database implements DataHandler {
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() {
return multiThreaded;
}
......
......@@ -23,7 +23,25 @@ public class Mode {
*/
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
......@@ -178,14 +196,14 @@ public class Mode {
*/
public Set<String> disallowedTypes = Collections.emptySet();
private final String name;
private final DbTypeEnum dbTypeEnum;
static {
Mode mode = new Mode(REGULAR);
Mode mode = new Mode(DbTypeEnum.REGULAR);
mode.nullConcatIsNull = true;
add(mode);
mode = new Mode("DB2");
mode = new Mode(DbTypeEnum.DB2);
mode.aliasColumnName = true;
mode.supportOffsetFetch = true;
mode.sysDummy1 = true;
......@@ -200,7 +218,7 @@ public class Mode {
mode.allowDB2TimestampFormat = true;
add(mode);
mode = new Mode("Derby");
mode = new Mode(DbTypeEnum.Derby);
mode.aliasColumnName = true;
mode.uniqueIndexSingleNull = true;
mode.supportOffsetFetch = true;
......@@ -210,7 +228,7 @@ public class Mode {
mode.supportedClientInfoPropertiesRegEx = null;
add(mode);
mode = new Mode("HSQLDB");
mode = new Mode(DbTypeEnum.HSQLDB);
mode.aliasColumnName = true;
mode.convertOnlyToSmallerScale = true;
mode.nullConcatIsNull = true;
......@@ -223,7 +241,7 @@ public class Mode {
mode.supportedClientInfoPropertiesRegEx = null;
add(mode);
mode = new Mode("MSSQLServer");
mode = new Mode(DbTypeEnum.MSSQLServer);
mode.aliasColumnName = true;
mode.squareBracketQuotedNames = true;
mode.uniqueIndexSingleNull = true;
......@@ -235,7 +253,7 @@ public class Mode {
mode.supportedClientInfoPropertiesRegEx = null;
add(mode);
mode = new Mode("MySQL");
mode = new Mode(DbTypeEnum.MySQL);
mode.convertInsertNullToZero = true;
mode.indexDefinitionInCreateTable = true;
mode.lowerCaseIdentifiers = true;
......@@ -249,7 +267,7 @@ public class Mode {
mode.prohibitEmptyInPredicate = true;
add(mode);
mode = new Mode("Oracle");
mode = new Mode(DbTypeEnum.Oracle);
mode.aliasColumnName = true;
mode.convertOnlyToSmallerScale = true;
mode.uniqueIndexSingleNullExceptAllColumnsAreNull = true;
......@@ -262,7 +280,7 @@ public class Mode {
mode.prohibitEmptyInPredicate = true;
add(mode);
mode = new Mode("PostgreSQL");
mode = new Mode(DbTypeEnum.PostgreSQL);
mode.aliasColumnName = true;
mode.nullConcatIsNull = true;
mode.supportOffsetFetch = true;
......@@ -285,19 +303,19 @@ public class Mode {
mode.disallowedTypes = disallowedTypes;
add(mode);
mode = new Mode("Ignite");
mode = new Mode(DbTypeEnum.Ignite);
mode.nullConcatIsNull = true;
mode.allowAffinityKey = true;
mode.indexDefinitionInCreateTable = true;
add(mode);
}
private Mode(String name) {
this.name = name;
private Mode(DbTypeEnum dbTypeEnum) {
this.dbTypeEnum = dbTypeEnum;
}
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 {
* @return the mode object
*/
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() {
return getInstance("MySQL");
return getInstance(DbTypeEnum.MySQL);
}
public DbTypeEnum getDbTypeEnum() {
return dbTypeEnum;
}
public String getName() {
return name;
return dbTypeEnum.name();
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论