Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
e1cdf544
提交
e1cdf544
authored
1月 08, 2018
作者:
sylvain-ilm
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
DROP TABLE RESTRICT shouldn't drop foreign constraints
上级
b5341bb5
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
66 行增加
和
4 行删除
+66
-4
help.csv
h2/src/docsrc/help/help.csv
+2
-2
DropTable.java
h2/src/main/org/h2/command/ddl/DropTable.java
+16
-2
TestCases.java
h2/src/test/org/h2/test/db/TestCases.java
+48
-0
没有找到文件。
h2/src/docsrc/help/help.csv
浏览文件 @
e1cdf544
...
...
@@ -907,8 +907,8 @@ DROP SEQUENCE SEQ_ID
DROP TABLE [ IF EXISTS ] tableName [,...] [ RESTRICT | CASCADE ]
","
Drops an existing table, or a list of tables.
The command will fail if dependent
view
s exist and the RESTRICT clause is used (the default).
All dependent views are dropped as well if the CASCADE clause is used.
The command will fail if dependent
object
s exist and the RESTRICT clause is used (the default).
All dependent views a
nd constraints a
re dropped as well if the CASCADE clause is used.
This command commits an open transaction in this connection.
","
DROP TABLE TEST
...
...
h2/src/main/org/h2/command/ddl/DropTable.java
浏览文件 @
e1cdf544
...
...
@@ -5,9 +5,12 @@
*/
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.engine.Database
;
import
org.h2.engine.Right
;
...
...
@@ -73,15 +76,26 @@ public class DropTable extends SchemaCommand {
throw
DbException
.
get
(
ErrorCode
.
CANNOT_DROP_TABLE_1
,
tableName
);
}
if
(
dropAction
==
ConstraintReferential
.
RESTRICT
)
{
StatementBuilder
buff
=
new
StatementBuilder
();
CopyOnWriteArrayList
<
TableView
>
dependentViews
=
table
.
getDependentViews
();
if
(
dependentViews
!=
null
&&
dependentViews
.
size
()
>
0
)
{
StatementBuilder
buff
=
new
StatementBuilder
();
for
(
TableView
v
:
dependentViews
)
{
buff
.
appendExceptFirst
(
", "
);
buff
.
append
(
v
.
getName
());
}
throw
DbException
.
get
(
ErrorCode
.
CANNOT_DROP_2
,
tableName
,
buff
.
toString
());
}
final
List
<
Constraint
>
constraints
=
table
.
getConstraints
();
if
(
constraints
!=
null
&&
constraints
.
size
()
>
0
)
{
for
(
Constraint
c
:
constraints
)
{
if
(
c
.
getTable
()
!=
table
)
{
buff
.
appendExceptFirst
(
", "
);
buff
.
append
(
c
.
getName
());
}
}
}
if
(
buff
.
length
()
>
0
)
throw
DbException
.
get
(
ErrorCode
.
CANNOT_DROP_2
,
tableName
,
buff
.
toString
());
}
table
.
lock
(
session
,
true
,
true
);
}
...
...
h2/src/test/org/h2/test/db/TestCases.java
浏览文件 @
e1cdf544
...
...
@@ -54,6 +54,7 @@ public class TestCases extends TestBase {
testGroupSubquery
();
testCountDistinctNotNull
();
testDependencies
();
testDropTable
();
testConvertType
();
testSortedSelect
();
testMaxMemoryRows
();
...
...
@@ -278,6 +279,53 @@ public class TestCases extends TestBase {
conn
.
close
();
}
private
static
enum
DropDependent
{
NONE
,
VIEW
,
FOREIGN_KEY
;
}
private
void
testDropTable
()
throws
SQLException
{
trace
(
"testDrop"
);
for
(
final
boolean
restrict
:
new
boolean
[]
{
true
,
false
})
{
for
(
final
DropDependent
dropDep
:
DropDependent
.
values
())
{
deleteDb
(
"cases"
);
Connection
conn
=
getConnection
(
"cases"
);
Statement
stat
=
conn
.
createStatement
();
stat
.
execute
(
"create table test(id int)"
);
if
(
dropDep
==
DropDependent
.
VIEW
)
{
stat
.
execute
(
"create view abc as select * from test"
);
}
else
if
(
dropDep
==
DropDependent
.
FOREIGN_KEY
)
{
stat
.
execute
(
"create table ref(id int, id_test int, foreign key (id_test) references test (id)) "
);
// test table is empty so the foreign key forces ref table to be also empty
assertThrows
(
ErrorCode
.
REFERENTIAL_INTEGRITY_VIOLATED_PARENT_MISSING_1
,
stat
).
execute
(
"insert into ref values(1,2)"
);
}
// drop allowed if no references or cascade
final
boolean
expectedDropSuccess
=
dropDep
==
DropDependent
.
NONE
||
!
restrict
;
assertThrows
(
expectedDropSuccess
?
0
:
ErrorCode
.
CANNOT_DROP_2
,
stat
).
execute
(
"drop table test "
+
(
restrict
?
"restrict"
:
"cascade"
));
assertThrows
(
expectedDropSuccess
?
ErrorCode
.
TABLE_OR_VIEW_NOT_FOUND_1
:
0
,
stat
).
execute
(
"select * from test"
);
// missing view if it was never created or if the drop succeeded
assertThrows
(
dropDep
!=
DropDependent
.
VIEW
||
expectedDropSuccess
?
ErrorCode
.
TABLE_OR_VIEW_NOT_FOUND_1
:
0
,
stat
).
execute
(
"select * from abc"
);
final
int
refError
;
if
(
dropDep
!=
DropDependent
.
FOREIGN_KEY
)
{
// never created
refError
=
ErrorCode
.
TABLE_OR_VIEW_NOT_FOUND_1
;
}
else
if
(
expectedDropSuccess
)
{
// foreign key dropped
refError
=
0
;
}
else
{
// foreign key not dropped
refError
=
ErrorCode
.
REFERENTIAL_INTEGRITY_VIOLATED_PARENT_MISSING_1
;
}
assertThrows
(
refError
,
stat
).
execute
(
"insert into ref values(1,2)"
);
conn
.
close
();
}
}
}
private
void
testConvertType
()
throws
SQLException
{
deleteDb
(
"cases"
);
Connection
conn
=
getConnection
(
"cases"
);
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论