提交 426737dd authored 作者: Noel Grandin's avatar Noel Grandin

create enums for ConstraintActionType and UnionType

上级 cad0c35a
......@@ -90,7 +90,7 @@ import org.h2.command.dml.Set;
import org.h2.command.dml.SetTypes;
import org.h2.command.dml.TransactionCommand;
import org.h2.command.dml.Update;
import org.h2.constraint.ConstraintReferential;
import org.h2.constraint.ConstraintActionType;
import org.h2.engine.Constants;
import org.h2.engine.Database;
import org.h2.engine.DbObject;
......@@ -1599,12 +1599,12 @@ public class Parser {
ifExists = readIfExists(ifExists);
command.setIfExists(ifExists);
if (readIf("CASCADE")) {
command.setDropAction(ConstraintReferential.CASCADE);
command.setDropAction(ConstraintActionType.CASCADE);
readIf("CONSTRAINTS");
} else if (readIf("RESTRICT")) {
command.setDropAction(ConstraintReferential.RESTRICT);
command.setDropAction(ConstraintActionType.RESTRICT);
} else if (readIf("IGNORE")) {
command.setDropAction(ConstraintReferential.SET_DEFAULT);
command.setDropAction(ConstraintActionType.SET_DEFAULT);
}
return command;
} else if (readIf("INDEX")) {
......@@ -1654,7 +1654,7 @@ public class Parser {
command.setViewName(viewName);
ifExists = readIfExists(ifExists);
command.setIfExists(ifExists);
Integer dropAction = parseCascadeOrRestrict();
ConstraintActionType dropAction = parseCascadeOrRestrict();
if (dropAction != null) {
command.setDropAction(dropAction);
}
......@@ -1964,21 +1964,21 @@ public class Parser {
if (readIf("UNION")) {
SelectUnion union = new SelectUnion(session, command);
if (readIf("ALL")) {
union.setUnionType(SelectUnion.UNION_ALL);
union.setUnionType(SelectUnion.UnionType.UNION_ALL);
} else {
readIf("DISTINCT");
union.setUnionType(SelectUnion.UNION);
union.setUnionType(SelectUnion.UnionType.UNION);
}
union.setRight(parseSelectSub());
command = union;
} else if (readIf("MINUS") || readIf("EXCEPT")) {
SelectUnion union = new SelectUnion(session, command);
union.setUnionType(SelectUnion.EXCEPT);
union.setUnionType(SelectUnion.UnionType.EXCEPT);
union.setRight(parseSelectSub());
command = union;
} else if (readIf("INTERSECT")) {
SelectUnion union = new SelectUnion(session, command);
union.setUnionType(SelectUnion.INTERSECT);
union.setUnionType(SelectUnion.UnionType.INTERSECT);
union.setRight(parseSelectSub());
command = union;
} else {
......@@ -6382,28 +6382,28 @@ public class Parser {
return command;
}
private int parseAction() {
Integer result = parseCascadeOrRestrict();
private ConstraintActionType parseAction() {
ConstraintActionType result = parseCascadeOrRestrict();
if (result != null) {
return result;
}
if (readIf("NO")) {
read("ACTION");
return ConstraintReferential.RESTRICT;
return ConstraintActionType.RESTRICT;
}
read("SET");
if (readIf("NULL")) {
return ConstraintReferential.SET_NULL;
return ConstraintActionType.SET_NULL;
}
read("DEFAULT");
return ConstraintReferential.SET_DEFAULT;
return ConstraintActionType.SET_DEFAULT;
}
private Integer parseCascadeOrRestrict() {
private ConstraintActionType parseCascadeOrRestrict() {
if (readIf("CASCADE")) {
return ConstraintReferential.CASCADE;
return ConstraintActionType.CASCADE;
} else if (readIf("RESTRICT")) {
return ConstraintReferential.RESTRICT;
return ConstraintActionType.RESTRICT;
} else {
return null;
}
......
......@@ -10,6 +10,7 @@ import java.util.HashSet;
import org.h2.api.ErrorCode;
import org.h2.command.CommandInterface;
import org.h2.constraint.Constraint;
import org.h2.constraint.ConstraintActionType;
import org.h2.constraint.ConstraintCheck;
import org.h2.constraint.ConstraintReferential;
import org.h2.constraint.ConstraintUnique;
......@@ -38,8 +39,8 @@ public class AlterTableAddConstraint extends SchemaCommand {
private String constraintName;
private String tableName;
private IndexColumn[] indexColumns;
private int deleteAction;
private int updateAction;
private ConstraintActionType deleteAction;
private ConstraintActionType updateAction;
private Schema refSchema;
private String refTableName;
private IndexColumn[] refIndexColumns;
......@@ -298,11 +299,11 @@ public class AlterTableAddConstraint extends SchemaCommand {
}
}
public void setDeleteAction(int action) {
public void setDeleteAction(ConstraintActionType action) {
this.deleteAction = action;
}
public void setUpdateAction(int action) {
public void setUpdateAction(ConstraintActionType action) {
this.updateAction = action;
}
......
......@@ -7,11 +7,10 @@ package org.h2.command.ddl;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import org.h2.api.ErrorCode;
import org.h2.command.CommandInterface;
import org.h2.constraint.Constraint;
import org.h2.constraint.ConstraintReferential;
import org.h2.constraint.ConstraintActionType;
import org.h2.engine.Database;
import org.h2.engine.Right;
import org.h2.engine.Session;
......@@ -31,13 +30,13 @@ public class DropTable extends SchemaCommand {
private String tableName;
private Table table;
private DropTable next;
private int dropAction;
private ConstraintActionType dropAction;
public DropTable(Session session, Schema schema) {
super(session, schema);
dropAction = session.getDatabase().getSettings().dropRestrict ?
ConstraintReferential.RESTRICT :
ConstraintReferential.CASCADE;
ConstraintActionType.RESTRICT :
ConstraintActionType.CASCADE;
}
/**
......@@ -75,7 +74,7 @@ public class DropTable extends SchemaCommand {
if (!table.canDrop()) {
throw DbException.get(ErrorCode.CANNOT_DROP_TABLE_1, tableName);
}
if (dropAction == ConstraintReferential.RESTRICT) {
if (dropAction == ConstraintActionType.RESTRICT) {
StatementBuilder buff = new StatementBuilder();
CopyOnWriteArrayList<TableView> dependentViews = table.getDependentViews();
if (dependentViews != null && dependentViews.size() > 0) {
......@@ -131,7 +130,7 @@ public class DropTable extends SchemaCommand {
return 0;
}
public void setDropAction(int dropAction) {
public void setDropAction(ConstraintActionType dropAction) {
this.dropAction = dropAction;
if (next != null) {
next.setDropAction(dropAction);
......
......@@ -8,7 +8,7 @@ package org.h2.command.ddl;
import java.util.ArrayList;
import org.h2.api.ErrorCode;
import org.h2.command.CommandInterface;
import org.h2.constraint.ConstraintReferential;
import org.h2.constraint.ConstraintActionType;
import org.h2.engine.DbObject;
import org.h2.engine.Right;
import org.h2.engine.Session;
......@@ -26,20 +26,20 @@ public class DropView extends SchemaCommand {
private String viewName;
private boolean ifExists;
private int dropAction;
private ConstraintActionType dropAction;
public DropView(Session session, Schema schema) {
super(session, schema);
dropAction = session.getDatabase().getSettings().dropRestrict ?
ConstraintReferential.RESTRICT :
ConstraintReferential.CASCADE;
ConstraintActionType.RESTRICT :
ConstraintActionType.CASCADE;
}
public void setIfExists(boolean b) {
ifExists = b;
}
public void setDropAction(int dropAction) {
public void setDropAction(ConstraintActionType dropAction) {
this.dropAction = dropAction;
}
......@@ -61,7 +61,7 @@ public class DropView extends SchemaCommand {
}
session.getUser().checkRight(view, Right.ALL);
if (dropAction == ConstraintReferential.RESTRICT) {
if (dropAction == ConstraintActionType.RESTRICT) {
for (DbObject child : view.getChildren()) {
if (child instanceof TableView) {
throw DbException.get(ErrorCode.CANNOT_DROP_2, viewName, child.getName());
......
......@@ -38,27 +38,29 @@ import org.h2.value.ValueNull;
*/
public class SelectUnion extends Query {
/**
* The type of a UNION statement.
*/
public static final int UNION = 0;
public enum UnionType {
/**
* The type of a UNION statement.
*/
UNION,
/**
* The type of a UNION ALL statement.
*/
public static final int UNION_ALL = 1;
/**
* The type of a UNION ALL statement.
*/
UNION_ALL,
/**
* The type of an EXCEPT statement.
*/
public static final int EXCEPT = 2;
/**
* The type of an EXCEPT statement.
*/
EXCEPT,
/**
* The type of an INTERSECT statement.
*/
public static final int INTERSECT = 3;
/**
* The type of an INTERSECT statement.
*/
INTERSECT
}
private int unionType;
private UnionType unionType;
/**
* The left hand side of the union (the first subquery).
......@@ -93,11 +95,11 @@ public class SelectUnion extends Query {
right.prepareJoinBatch();
}
public void setUnionType(int type) {
public void setUnionType(UnionType type) {
this.unionType = type;
}
public int getUnionType() {
public UnionType getUnionType() {
return unionType;
}
......@@ -178,7 +180,7 @@ public class SelectUnion extends Query {
limitExpr = ValueExpression.get(ValueInt.get(l));
}
if (session.getDatabase().getSettings().optimizeInsertFromSelect) {
if (unionType == UNION_ALL && target != null) {
if (unionType == UnionType.UNION_ALL && target != null) {
if (sort == null && !distinct && maxRows == 0 &&
offsetExpr == null && limitExpr == null) {
left.query(0, target);
......@@ -188,7 +190,7 @@ public class SelectUnion extends Query {
}
}
int columnCount = left.getColumnCount();
if (session.isLazyQueryExecution() && unionType == UNION_ALL && !distinct &&
if (session.isLazyQueryExecution() && unionType == UnionType.UNION_ALL && !distinct &&
sort == null && !randomAccessResult && !isForUpdate &&
offsetExpr == null && isReadOnly()) {
int limit = -1;
......
/*
* Copyright 2004-2018 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.constraint;
public enum ConstraintActionType {
/**
* The action is to restrict the operation.
*/
RESTRICT,
/**
* The action is to cascade the operation.
*/
CASCADE,
/**
* The action is to set the value to the default value.
*/
SET_DEFAULT,
/**
* The action is to set the value to NULL.
*/
SET_NULL
}
\ No newline at end of file
......@@ -33,30 +33,10 @@ import org.h2.value.ValueNull;
*/
public class ConstraintReferential extends Constraint {
/**
* The action is to restrict the operation.
*/
public static final int RESTRICT = 0;
/**
* The action is to cascade the operation.
*/
public static final int CASCADE = 1;
/**
* The action is to set the value to the default value.
*/
public static final int SET_DEFAULT = 2;
/**
* The action is to set the value to NULL.
*/
public static final int SET_NULL = 3;
private IndexColumn[] columns;
private IndexColumn[] refColumns;
private int deleteAction;
private int updateAction;
private ConstraintActionType deleteAction;
private ConstraintActionType updateAction;
private Table refTable;
private Index index;
private Index refIndex;
......@@ -74,7 +54,7 @@ public class ConstraintReferential extends Constraint {
return Constraint.REFERENTIAL;
}
private static void appendAction(StatementBuilder buff, int action) {
private static void appendAction(StatementBuilder buff, ConstraintActionType action) {
switch (action) {
case CASCADE:
buff.append("CASCADE");
......@@ -154,11 +134,11 @@ public class ConstraintReferential extends Constraint {
if (internalIndex && refIndexOwner && forTable == this.table) {
buff.append(" INDEX ").append(refIndex.getSQL());
}
if (deleteAction != RESTRICT) {
if (deleteAction != ConstraintActionType.RESTRICT) {
buff.append(" ON DELETE ");
appendAction(buff, deleteAction);
}
if (updateAction != RESTRICT) {
if (updateAction != ConstraintActionType.RESTRICT) {
buff.append(" ON UPDATE ");
appendAction(buff, updateAction);
}
......@@ -437,21 +417,21 @@ public class ConstraintReferential extends Constraint {
}
if (newRow == null) {
// this is a delete
if (deleteAction == RESTRICT) {
if (deleteAction == ConstraintActionType.RESTRICT) {
checkRow(session, oldRow);
} else {
int i = deleteAction == CASCADE ? 0 : columns.length;
int i = deleteAction == ConstraintActionType.CASCADE ? 0 : columns.length;
Prepared deleteCommand = getDelete(session);
setWhere(deleteCommand, i, oldRow);
updateWithSkipCheck(deleteCommand);
}
} else {
// this is an update
if (updateAction == RESTRICT) {
if (updateAction == ConstraintActionType.RESTRICT) {
checkRow(session, oldRow);
} else {
Prepared updateCommand = getUpdate(session);
if (updateAction == CASCADE) {
if (updateAction == ConstraintActionType.CASCADE) {
ArrayList<Parameter> params = updateCommand.getParameters();
for (int i = 0, len = columns.length; i < len; i++) {
Parameter param = params.get(i);
......@@ -488,7 +468,7 @@ public class ConstraintReferential extends Constraint {
}
}
public int getDeleteAction() {
public ConstraintActionType getDeleteAction() {
return deleteAction;
}
......@@ -497,11 +477,11 @@ public class ConstraintReferential extends Constraint {
*
* @param action the action
*/
public void setDeleteAction(int action) {
public void setDeleteAction(ConstraintActionType action) {
if (action == deleteAction && deleteSQL == null) {
return;
}
if (deleteAction != RESTRICT) {
if (deleteAction != ConstraintActionType.RESTRICT) {
throw DbException.get(ErrorCode.CONSTRAINT_ALREADY_EXISTS_1, "ON DELETE");
}
this.deleteAction = action;
......@@ -509,11 +489,11 @@ public class ConstraintReferential extends Constraint {
}
private void buildDeleteSQL() {
if (deleteAction == RESTRICT) {
if (deleteAction == ConstraintActionType.RESTRICT) {
return;
}
StatementBuilder buff = new StatementBuilder();
if (deleteAction == CASCADE) {
if (deleteAction == ConstraintActionType.CASCADE) {
buff.append("DELETE FROM ").append(table.getSQL());
} else {
appendUpdate(buff);
......@@ -530,7 +510,7 @@ public class ConstraintReferential extends Constraint {
return prepare(session, deleteSQL, deleteAction);
}
public int getUpdateAction() {
public ConstraintActionType getUpdateAction() {
return updateAction;
}
......@@ -539,11 +519,11 @@ public class ConstraintReferential extends Constraint {
*
* @param action the action
*/
public void setUpdateAction(int action) {
public void setUpdateAction(ConstraintActionType action) {
if (action == updateAction && updateSQL == null) {
return;
}
if (updateAction != RESTRICT) {
if (updateAction != ConstraintActionType.RESTRICT) {
throw DbException.get(ErrorCode.CONSTRAINT_ALREADY_EXISTS_1, "ON UPDATE");
}
this.updateAction = action;
......@@ -551,7 +531,7 @@ public class ConstraintReferential extends Constraint {
}
private void buildUpdateSQL() {
if (updateAction == RESTRICT) {
if (updateAction == ConstraintActionType.RESTRICT) {
return;
}
StatementBuilder buff = new StatementBuilder();
......@@ -566,15 +546,15 @@ public class ConstraintReferential extends Constraint {
buildDeleteSQL();
}
private Prepared prepare(Session session, String sql, int action) {
private Prepared prepare(Session session, String sql, ConstraintActionType action) {
Prepared command = session.prepare(sql);
if (action != CASCADE) {
if (action != ConstraintActionType.CASCADE) {
ArrayList<Parameter> params = command.getParameters();
for (int i = 0, len = columns.length; i < len; i++) {
Column column = columns[i].column;
Parameter param = params.get(i);
Value value;
if (action == SET_NULL) {
if (action == ConstraintActionType.SET_NULL) {
value = ValueNull.INSTANCE;
} else {
Expression expr = column.getDefaultExpression();
......
......@@ -19,6 +19,7 @@ import java.util.HashMap;
import java.util.Locale;
import org.h2.command.Command;
import org.h2.constraint.Constraint;
import org.h2.constraint.ConstraintActionType;
import org.h2.constraint.ConstraintCheck;
import org.h2.constraint.ConstraintReferential;
import org.h2.constraint.ConstraintUnique;
......@@ -1913,15 +1914,15 @@ public class MetaTable extends Table {
return rows;
}
private static int getRefAction(int action) {
private static int getRefAction(ConstraintActionType action) {
switch (action) {
case ConstraintReferential.CASCADE:
case CASCADE:
return DatabaseMetaData.importedKeyCascade;
case ConstraintReferential.RESTRICT:
case RESTRICT:
return DatabaseMetaData.importedKeyRestrict;
case ConstraintReferential.SET_DEFAULT:
case SET_DEFAULT:
return DatabaseMetaData.importedKeySetDefault;
case ConstraintReferential.SET_NULL:
case SET_NULL:
return DatabaseMetaData.importedKeySetNull;
default:
throw DbException.throwInternalError("action="+action);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论