Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
b94d69d2
Unverified
提交
b94d69d2
authored
1月 09, 2018
作者:
Noel Grandin
提交者:
GitHub
1月 09, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #720 from ilm-informatique/dropTableRestrict
DROP TABLE RESTRICT shouldn't drop foreign keys in other tables
上级
a318cf11
4c0aa62c
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
98 行增加
和
6 行删除
+98
-6
help.csv
h2/src/docsrc/help/help.csv
+2
-2
DropTable.java
h2/src/main/org/h2/command/ddl/DropTable.java
+19
-2
DbSettings.java
h2/src/main/org/h2/engine/DbSettings.java
+11
-0
TestCases.java
h2/src/test/org/h2/test/db/TestCases.java
+66
-2
没有找到文件。
h2/src/docsrc/help/help.csv
浏览文件 @
b94d69d2
...
@@ -907,8 +907,8 @@ DROP SEQUENCE SEQ_ID
...
@@ -907,8 +907,8 @@ DROP SEQUENCE SEQ_ID
DROP TABLE [ IF EXISTS ] tableName [,...] [ RESTRICT | CASCADE ]
DROP TABLE [ IF EXISTS ] tableName [,...] [ RESTRICT | CASCADE ]
","
","
Drops an existing table, or a list of tables.
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).
The command will fail if dependent
object
s exist and the RESTRICT clause is used (the default).
All dependent views are dropped as well if the CASCADE clause is used.
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.
This command commits an open transaction in this connection.
","
","
DROP TABLE TEST
DROP TABLE TEST
...
...
h2/src/main/org/h2/command/ddl/DropTable.java
浏览文件 @
b94d69d2
...
@@ -5,9 +5,12 @@
...
@@ -5,9 +5,12 @@
*/
*/
package
org
.
h2
.
command
.
ddl
;
package
org
.
h2
.
command
.
ddl
;
import
java.util.List
;
import
java.util.concurrent.CopyOnWriteArrayList
;
import
java.util.concurrent.CopyOnWriteArrayList
;
import
org.h2.api.ErrorCode
;
import
org.h2.api.ErrorCode
;
import
org.h2.command.CommandInterface
;
import
org.h2.command.CommandInterface
;
import
org.h2.constraint.Constraint
;
import
org.h2.constraint.ConstraintReferential
;
import
org.h2.constraint.ConstraintReferential
;
import
org.h2.engine.Database
;
import
org.h2.engine.Database
;
import
org.h2.engine.Right
;
import
org.h2.engine.Right
;
...
@@ -73,15 +76,29 @@ public class DropTable extends SchemaCommand {
...
@@ -73,15 +76,29 @@ public class DropTable extends SchemaCommand {
throw
DbException
.
get
(
ErrorCode
.
CANNOT_DROP_TABLE_1
,
tableName
);
throw
DbException
.
get
(
ErrorCode
.
CANNOT_DROP_TABLE_1
,
tableName
);
}
}
if
(
dropAction
==
ConstraintReferential
.
RESTRICT
)
{
if
(
dropAction
==
ConstraintReferential
.
RESTRICT
)
{
StatementBuilder
buff
=
new
StatementBuilder
();
CopyOnWriteArrayList
<
TableView
>
dependentViews
=
table
.
getDependentViews
();
CopyOnWriteArrayList
<
TableView
>
dependentViews
=
table
.
getDependentViews
();
if
(
dependentViews
!=
null
&&
dependentViews
.
size
()
>
0
)
{
if
(
dependentViews
!=
null
&&
dependentViews
.
size
()
>
0
)
{
StatementBuilder
buff
=
new
StatementBuilder
();
for
(
TableView
v
:
dependentViews
)
{
for
(
TableView
v
:
dependentViews
)
{
buff
.
appendExceptFirst
(
", "
);
buff
.
appendExceptFirst
(
", "
);
buff
.
append
(
v
.
getName
());
buff
.
append
(
v
.
getName
());
}
}
throw
DbException
.
get
(
ErrorCode
.
CANNOT_DROP_2
,
tableName
,
buff
.
toString
());
}
}
if
(
session
.
getDatabase
()
.
getSettings
().
standardDropTableRestrict
)
{
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
);
table
.
lock
(
session
,
true
,
true
);
}
}
...
...
h2/src/main/org/h2/engine/DbSettings.java
浏览文件 @
b94d69d2
...
@@ -350,6 +350,17 @@ public class DbSettings extends SettingsBase {
...
@@ -350,6 +350,17 @@ public class DbSettings extends SettingsBase {
*/
*/
public
final
boolean
multiThreaded
=
get
(
"MULTI_THREADED"
,
false
);
public
final
boolean
multiThreaded
=
get
(
"MULTI_THREADED"
,
false
);
/**
* Database setting <code>STANDARD_DROP_TABLE_RESTRICT</code> (default:
* false).<br />
* <code>true</code> if DROP TABLE RESTRICT should fail if there's any
* foreign key referencing the table to be dropped. <code>false</code> if
* foreign keys referencing the table to be dropped should be silently
* dropped as well.
*/
public
final
boolean
standardDropTableRestrict
=
get
(
"STANDARD_DROP_TABLE_RESTRICT"
,
false
);
private
DbSettings
(
HashMap
<
String
,
String
>
s
)
{
private
DbSettings
(
HashMap
<
String
,
String
>
s
)
{
super
(
s
);
super
(
s
);
}
}
...
...
h2/src/test/org/h2/test/db/TestCases.java
浏览文件 @
b94d69d2
...
@@ -19,7 +19,6 @@ import java.sql.Timestamp;
...
@@ -19,7 +19,6 @@ import java.sql.Timestamp;
import
java.util.List
;
import
java.util.List
;
import
java.util.Random
;
import
java.util.Random
;
import
java.util.concurrent.TimeUnit
;
import
java.util.concurrent.TimeUnit
;
import
org.h2.api.ErrorCode
;
import
org.h2.api.ErrorCode
;
import
org.h2.engine.Constants
;
import
org.h2.engine.Constants
;
import
org.h2.store.fs.FileUtils
;
import
org.h2.store.fs.FileUtils
;
...
@@ -54,6 +53,7 @@ public class TestCases extends TestBase {
...
@@ -54,6 +53,7 @@ public class TestCases extends TestBase {
testGroupSubquery
();
testGroupSubquery
();
testCountDistinctNotNull
();
testCountDistinctNotNull
();
testDependencies
();
testDependencies
();
testDropTable
();
testConvertType
();
testConvertType
();
testSortedSelect
();
testSortedSelect
();
testMaxMemoryRows
();
testMaxMemoryRows
();
...
@@ -150,7 +150,7 @@ public class TestCases extends TestBase {
...
@@ -150,7 +150,7 @@ public class TestCases extends TestBase {
stat
.
execute
(
"alter table b add constraint x "
+
stat
.
execute
(
"alter table b add constraint x "
+
"foreign key(a_id) references a(id)"
);
"foreign key(a_id) references a(id)"
);
stat
.
execute
(
"update a set x=200"
);
stat
.
execute
(
"update a set x=200"
);
stat
.
execute
(
"drop table if exists a, b"
);
stat
.
execute
(
"drop table if exists a, b
cascade
"
);
conn
.
close
();
conn
.
close
();
}
}
...
@@ -278,6 +278,70 @@ public class TestCases extends TestBase {
...
@@ -278,6 +278,70 @@ public class TestCases extends TestBase {
conn
.
close
();
conn
.
close
();
}
}
private
void
testDropTable
()
throws
SQLException
{
trace
(
"testDropTable"
);
final
boolean
[]
booleans
=
new
boolean
[]
{
true
,
false
};
for
(
final
boolean
stdDropTableRestrict
:
booleans
)
{
for
(
final
boolean
restrict
:
booleans
)
{
testDropTableNoReference
(
stdDropTableRestrict
,
restrict
);
testDropTableViewReference
(
stdDropTableRestrict
,
restrict
);
testDropTableForeignKeyReference
(
stdDropTableRestrict
,
restrict
);
}
}
}
private
Statement
createTable
(
final
boolean
stdDropTableRestrict
)
throws
SQLException
{
deleteDb
(
"cases"
);
Connection
conn
=
getConnection
(
"cases;STANDARD_DROP_TABLE_RESTRICT="
+
stdDropTableRestrict
);
Statement
stat
=
conn
.
createStatement
();
stat
.
execute
(
"create table test(id int)"
);
return
stat
;
}
private
void
dropTable
(
final
boolean
restrict
,
Statement
stat
,
final
boolean
expectedDropSuccess
)
throws
SQLException
{
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"
);
}
private
void
testDropTableNoReference
(
final
boolean
stdDropTableRestrict
,
final
boolean
restrict
)
throws
SQLException
{
Statement
stat
=
createTable
(
stdDropTableRestrict
);
// always succeed as there's no reference to the table
dropTable
(
restrict
,
stat
,
true
);
stat
.
getConnection
().
close
();
}
private
void
testDropTableViewReference
(
final
boolean
stdDropTableRestrict
,
final
boolean
restrict
)
throws
SQLException
{
Statement
stat
=
createTable
(
stdDropTableRestrict
);
stat
.
execute
(
"create view abc as select * from test"
);
// drop allowed only if cascade
final
boolean
expectedDropSuccess
=
!
restrict
;
dropTable
(
restrict
,
stat
,
expectedDropSuccess
);
// missing view if the drop succeeded
assertThrows
(
expectedDropSuccess
?
ErrorCode
.
TABLE_OR_VIEW_NOT_FOUND_1
:
0
,
stat
).
execute
(
"select * from abc"
);
stat
.
getConnection
().
close
();
}
private
void
testDropTableForeignKeyReference
(
final
boolean
stdDropTableRestrict
,
final
boolean
restrict
)
throws
SQLException
{
Statement
stat
=
createTable
(
stdDropTableRestrict
);
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 cascade or old style
final
boolean
expectedDropSuccess
=
!
stdDropTableRestrict
||
!
restrict
;
dropTable
(
restrict
,
stat
,
expectedDropSuccess
);
// insertion succeeds if the foreign key was dropped
assertThrows
(
expectedDropSuccess
?
0
:
ErrorCode
.
REFERENTIAL_INTEGRITY_VIOLATED_PARENT_MISSING_1
,
stat
)
.
execute
(
"insert into ref values(1,2)"
);
stat
.
getConnection
().
close
();
}
private
void
testConvertType
()
throws
SQLException
{
private
void
testConvertType
()
throws
SQLException
{
deleteDb
(
"cases"
);
deleteDb
(
"cases"
);
Connection
conn
=
getConnection
(
"cases"
);
Connection
conn
=
getConnection
(
"cases"
);
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论