提交 8474e816 authored 作者: Thomas Mueller's avatar Thomas Mueller

The default expression of a column may no longer reference the table, because a…

The default expression of a column may no longer reference the table, because a database with such a table couldn't be opened normally.
上级 00256211
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
package org.h2.command.ddl; package org.h2.command.ddl;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import org.h2.command.CommandInterface; import org.h2.command.CommandInterface;
import org.h2.command.Parser; import org.h2.command.Parser;
import org.h2.command.Prepared; import org.h2.command.Prepared;
...@@ -18,6 +19,7 @@ import org.h2.engine.DbObject; ...@@ -18,6 +19,7 @@ import org.h2.engine.DbObject;
import org.h2.engine.Right; import org.h2.engine.Right;
import org.h2.engine.Session; import org.h2.engine.Session;
import org.h2.expression.Expression; import org.h2.expression.Expression;
import org.h2.expression.ExpressionVisitor;
import org.h2.index.Index; import org.h2.index.Index;
import org.h2.index.IndexType; import org.h2.index.IndexType;
import org.h2.message.DbException; import org.h2.message.DbException;
...@@ -76,6 +78,9 @@ public class AlterTableAlterColumn extends SchemaCommand { ...@@ -76,6 +78,9 @@ public class AlterTableAlterColumn extends SchemaCommand {
table.checkSupportAlter(); table.checkSupportAlter();
table.lock(session, true, true); table.lock(session, true, true);
Sequence sequence = oldColumn == null ? null : oldColumn.getSequence(); Sequence sequence = oldColumn == null ? null : oldColumn.getSequence();
if (newColumn != null) {
checkDefaultReferencesTable(newColumn.getDefaultExpression());
}
switch (type) { switch (type) {
case CommandInterface.ALTER_TABLE_ALTER_COLUMN_NOT_NULL: { case CommandInterface.ALTER_TABLE_ALTER_COLUMN_NOT_NULL: {
if (!oldColumn.isNullable()) { if (!oldColumn.isNullable()) {
...@@ -98,6 +103,7 @@ public class AlterTableAlterColumn extends SchemaCommand { ...@@ -98,6 +103,7 @@ public class AlterTableAlterColumn extends SchemaCommand {
break; break;
} }
case CommandInterface.ALTER_TABLE_ALTER_COLUMN_DEFAULT: { case CommandInterface.ALTER_TABLE_ALTER_COLUMN_DEFAULT: {
checkDefaultReferencesTable(defaultExpression);
oldColumn.setSequence(null); oldColumn.setSequence(null);
oldColumn.setDefaultExpression(session, defaultExpression); oldColumn.setDefaultExpression(session, defaultExpression);
removeSequence(sequence); removeSequence(sequence);
...@@ -149,6 +155,18 @@ public class AlterTableAlterColumn extends SchemaCommand { ...@@ -149,6 +155,18 @@ public class AlterTableAlterColumn extends SchemaCommand {
return 0; return 0;
} }
private void checkDefaultReferencesTable(Expression defaultExpression) {
if (defaultExpression == null) {
return;
}
HashSet<DbObject> dependencies = New.hashSet();
ExpressionVisitor visitor = ExpressionVisitor.getDependenciesVisitor(dependencies);
defaultExpression.isEverything(visitor);
if (dependencies.contains(table)) {
throw DbException.get(ErrorCode.COLUMN_IS_REFERENCED_1, defaultExpression.getSQL());
}
}
private void convertAutoIncrementColumn(Column c) { private void convertAutoIncrementColumn(Column c) {
if (c.isAutoIncrement()) { if (c.isAutoIncrement()) {
if (c.isPrimaryKey()) { if (c.isPrimaryKey()) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论