提交 3eba25f8 authored 作者: andrei's avatar andrei

Convert constraint type into enum

上级 e0619d3a
...@@ -120,7 +120,7 @@ public class AlterTableAddConstraint extends SchemaCommand { ...@@ -120,7 +120,7 @@ public class AlterTableAddConstraint extends SchemaCommand {
ArrayList<Constraint> constraints = table.getConstraints(); ArrayList<Constraint> constraints = table.getConstraints();
for (int i = 0; constraints != null && i < constraints.size(); i++) { for (int i = 0; constraints != null && i < constraints.size(); i++) {
Constraint c = constraints.get(i); Constraint c = constraints.get(i);
if (Constraint.PRIMARY_KEY.equals(c.getConstraintType())) { if (Constraint.Type.PRIMARY_KEY == c.getConstraintType()) {
throw DbException.get(ErrorCode.SECOND_PRIMARY_KEY); throw DbException.get(ErrorCode.SECOND_PRIMARY_KEY);
} }
} }
......
...@@ -57,7 +57,7 @@ public class DropIndex extends SchemaCommand { ...@@ -57,7 +57,7 @@ public class DropIndex extends SchemaCommand {
Constraint cons = constraints.get(i); Constraint cons = constraints.get(i);
if (cons.usesIndex(index)) { if (cons.usesIndex(index)) {
// can drop primary key index (for compatibility) // can drop primary key index (for compatibility)
if (Constraint.PRIMARY_KEY.equals(cons.getConstraintType())) { if (Constraint.Type.PRIMARY_KEY == cons.getConstraintType()) {
pkConstraint = cons; pkConstraint = cons;
} else { } else {
throw DbException.get( throw DbException.get(
......
...@@ -284,8 +284,7 @@ public class ScriptCommand extends ScriptBase { ...@@ -284,8 +284,7 @@ public class ScriptCommand extends ScriptBase {
final ArrayList<Constraint> constraints = table.getConstraints(); final ArrayList<Constraint> constraints = table.getConstraints();
if (constraints != null) { if (constraints != null) {
for (Constraint constraint : constraints) { for (Constraint constraint : constraints) {
if (Constraint.PRIMARY_KEY.equals( if (Constraint.Type.PRIMARY_KEY == constraint.getConstraintType()) {
constraint.getConstraintType())) {
add(constraint.getCreateSQLWithoutIndexes(), false); add(constraint.getCreateSQLWithoutIndexes(), false);
} }
} }
...@@ -336,7 +335,7 @@ public class ScriptCommand extends ScriptBase { ...@@ -336,7 +335,7 @@ public class ScriptCommand extends ScriptBase {
if (constraint.getTable().isHidden()) { if (constraint.getTable().isHidden()) {
continue; continue;
} }
if (!Constraint.PRIMARY_KEY.equals(constraint.getConstraintType())) { if (Constraint.Type.PRIMARY_KEY != constraint.getConstraintType()) {
add(constraint.getCreateSQLWithoutIndexes(), false); add(constraint.getCreateSQLWithoutIndexes(), false);
} }
} }
......
...@@ -24,25 +24,24 @@ import org.h2.table.Table; ...@@ -24,25 +24,24 @@ import org.h2.table.Table;
public abstract class Constraint extends SchemaObjectBase implements public abstract class Constraint extends SchemaObjectBase implements
Comparable<Constraint> { Comparable<Constraint> {
/** public enum Type {
* The constraint type name for check constraints. /**
*/ * The constraint type for check constraints.
public static final String CHECK = "CHECK"; */
CHECK,
/** /**
* The constraint type name for referential constraints. * The constraint type for primary key constraints.
*/ */
public static final String REFERENTIAL = "REFERENTIAL"; PRIMARY_KEY,
/**
/** * The constraint type for unique constraints.
* The constraint type name for unique constraints. */
*/ UNIQUE,
public static final String UNIQUE = "UNIQUE"; /**
* The constraint type for referential constraints.
/** */
* The constraint type name for primary key constraints. REFERENTIAL
*/ }
public static final String PRIMARY_KEY = "PRIMARY KEY";
/** /**
* The table for which this constraint is defined. * The table for which this constraint is defined.
...@@ -60,7 +59,7 @@ public abstract class Constraint extends SchemaObjectBase implements ...@@ -60,7 +59,7 @@ public abstract class Constraint extends SchemaObjectBase implements
* *
* @return the name * @return the name
*/ */
public abstract String getConstraintType(); public abstract Type getConstraintType();
/** /**
* Check if this row fulfils the constraint. * Check if this row fulfils the constraint.
...@@ -155,29 +154,12 @@ public abstract class Constraint extends SchemaObjectBase implements ...@@ -155,29 +154,12 @@ public abstract class Constraint extends SchemaObjectBase implements
return null; return null;
} }
private int getConstraintTypeOrder() {
String constraintType = getConstraintType();
if (CHECK.equals(constraintType)) {
return 0;
} else if (PRIMARY_KEY.equals(constraintType)) {
return 1;
} else if (UNIQUE.equals(constraintType)) {
return 2;
} else if (REFERENTIAL.equals(constraintType)) {
return 3;
} else {
throw DbException.throwInternalError("type: " + constraintType);
}
}
@Override @Override
public int compareTo(Constraint other) { public int compareTo(Constraint other) {
if (this == other) { if (this == other) {
return 0; return 0;
} }
int thisType = getConstraintTypeOrder(); return Integer.compare(getConstraintType().ordinal(), other.getConstraintType().ordinal());
int otherType = other.getConstraintTypeOrder();
return thisType - otherType;
} }
@Override @Override
......
...@@ -36,8 +36,8 @@ public class ConstraintCheck extends Constraint { ...@@ -36,8 +36,8 @@ public class ConstraintCheck extends Constraint {
} }
@Override @Override
public String getConstraintType() { public Type getConstraintType() {
return Constraint.CHECK; return Constraint.Type.CHECK;
} }
public void setTableFilter(TableFilter filter) { public void setTableFilter(TableFilter filter) {
......
...@@ -50,8 +50,8 @@ public class ConstraintReferential extends Constraint { ...@@ -50,8 +50,8 @@ public class ConstraintReferential extends Constraint {
} }
@Override @Override
public String getConstraintType() { public Type getConstraintType() {
return Constraint.REFERENTIAL; return Constraint.Type.REFERENTIAL;
} }
private static void appendAction(StatementBuilder buff, ConstraintActionType action) { private static void appendAction(StatementBuilder buff, ConstraintActionType action) {
......
...@@ -34,8 +34,8 @@ public class ConstraintUnique extends Constraint { ...@@ -34,8 +34,8 @@ public class ConstraintUnique extends Constraint {
} }
@Override @Override
public String getConstraintType() { public Type getConstraintType() {
return primaryKey ? Constraint.PRIMARY_KEY : Constraint.UNIQUE; return primaryKey ? Constraint.Type.PRIMARY_KEY : Constraint.Type.UNIQUE;
} }
@Override @Override
......
...@@ -467,7 +467,7 @@ public class MVTable extends TableBase { ...@@ -467,7 +467,7 @@ public class MVTable extends TableBase {
if (constraints != null) { if (constraints != null) {
for (int i = 0, size = constraints.size(); i < size; i++) { for (int i = 0, size = constraints.size(); i < size; i++) {
Constraint c = constraints.get(i); Constraint c = constraints.get(i);
if (!(c.getConstraintType().equals(Constraint.REFERENTIAL))) { if (c.getConstraintType() != Constraint.Type.REFERENTIAL) {
continue; continue;
} }
ConstraintReferential ref = (ConstraintReferential) c; ConstraintReferential ref = (ConstraintReferential) c;
......
...@@ -868,8 +868,7 @@ public class MetaTable extends Table { ...@@ -868,8 +868,7 @@ public class MetaTable extends Table {
Constraint constraint = constraints.get(k); Constraint constraint = constraints.get(k);
if (constraint.usesIndex(index)) { if (constraint.usesIndex(index)) {
if (index.getIndexType().isPrimaryKey()) { if (index.getIndexType().isPrimaryKey()) {
if (constraint.getConstraintType().equals( if (constraint.getConstraintType() == Constraint.Type.PRIMARY_KEY) {
Constraint.PRIMARY_KEY)) {
constraintName = constraint.getName(); constraintName = constraint.getName();
} }
} else { } else {
...@@ -1544,7 +1543,7 @@ public class MetaTable extends Table { ...@@ -1544,7 +1543,7 @@ public class MetaTable extends Table {
for (SchemaObject obj : database.getAllSchemaObjects( for (SchemaObject obj : database.getAllSchemaObjects(
DbObject.CONSTRAINT)) { DbObject.CONSTRAINT)) {
Constraint constraint = (Constraint) obj; Constraint constraint = (Constraint) obj;
if (!(constraint.getConstraintType().equals(Constraint.REFERENTIAL))) { if (constraint.getConstraintType() != Constraint.Type.REFERENTIAL) {
continue; continue;
} }
ConstraintReferential ref = (ConstraintReferential) constraint; ConstraintReferential ref = (ConstraintReferential) constraint;
...@@ -1597,7 +1596,7 @@ public class MetaTable extends Table { ...@@ -1597,7 +1596,7 @@ public class MetaTable extends Table {
for (SchemaObject obj : database.getAllSchemaObjects( for (SchemaObject obj : database.getAllSchemaObjects(
DbObject.CONSTRAINT)) { DbObject.CONSTRAINT)) {
Constraint constraint = (Constraint) obj; Constraint constraint = (Constraint) obj;
String constraintType = constraint.getConstraintType(); Constraint.Type constraintType = constraint.getConstraintType();
String checkExpression = null; String checkExpression = null;
IndexColumn[] indexColumns = null; IndexColumn[] indexColumns = null;
Table table = constraint.getTable(); Table table = constraint.getTable();
...@@ -1613,12 +1612,12 @@ public class MetaTable extends Table { ...@@ -1613,12 +1612,12 @@ public class MetaTable extends Table {
if (!checkIndex(session, tableName, indexFrom, indexTo)) { if (!checkIndex(session, tableName, indexFrom, indexTo)) {
continue; continue;
} }
if (constraintType.equals(Constraint.CHECK)) { if (constraintType == Constraint.Type.CHECK) {
checkExpression = ((ConstraintCheck) constraint).getExpression().getSQL(); checkExpression = ((ConstraintCheck) constraint).getExpression().getSQL();
} else if (constraintType.equals(Constraint.UNIQUE) || } else if (constraintType == Constraint.Type.UNIQUE ||
constraintType.equals(Constraint.PRIMARY_KEY)) { constraintType == Constraint.Type.PRIMARY_KEY) {
indexColumns = ((ConstraintUnique) constraint).getColumns(); indexColumns = ((ConstraintUnique) constraint).getColumns();
} else if (constraintType.equals(Constraint.REFERENTIAL)) { } else if (constraintType == Constraint.Type.REFERENTIAL) {
indexColumns = ((ConstraintReferential) constraint).getColumns(); indexColumns = ((ConstraintReferential) constraint).getColumns();
} }
String columnList = null; String columnList = null;
...@@ -1638,7 +1637,7 @@ public class MetaTable extends Table { ...@@ -1638,7 +1637,7 @@ public class MetaTable extends Table {
// CONSTRAINT_NAME // CONSTRAINT_NAME
identifier(constraint.getName()), identifier(constraint.getName()),
// CONSTRAINT_TYPE // CONSTRAINT_TYPE
constraintType, constraintType.toString(),
// TABLE_CATALOG // TABLE_CATALOG
catalog, catalog,
// TABLE_SCHEMA // TABLE_SCHEMA
......
...@@ -746,7 +746,7 @@ public class RegularTable extends TableBase { ...@@ -746,7 +746,7 @@ public class RegularTable extends TableBase {
if (constraints != null) { if (constraints != null) {
for (int i = 0, size = constraints.size(); i < size; i++) { for (int i = 0, size = constraints.size(); i < size; i++) {
Constraint c = constraints.get(i); Constraint c = constraints.get(i);
if (!(c.getConstraintType().equals(Constraint.REFERENTIAL))) { if (c.getConstraintType() != Constraint.Type.REFERENTIAL) {
continue; continue;
} }
ConstraintReferential ref = (ConstraintReferential) c; ConstraintReferential ref = (ConstraintReferential) c;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论