Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
6888e92a
Unverified
提交
6888e92a
authored
11月 02, 2017
作者:
Noel Grandin
提交者:
GitHub
11月 02, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #659 from pkures/master
fixing incorrect index reuse in AlterTableAddConstraint.java
上级
7e8d5029
b5899708
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
51 行增加
和
27 行删除
+51
-27
AlterTableAddConstraint.java
h2/src/main/org/h2/command/ddl/AlterTableAddConstraint.java
+16
-15
TestCases.java
h2/src/test/org/h2/test/db/TestCases.java
+0
-12
TestScript.java
h2/src/test/org/h2/test/scripts/TestScript.java
+2
-0
altertable-index-reuse.sql
h2/src/test/org/h2/test/scripts/altertable-index-reuse.sql
+33
-0
没有找到文件。
h2/src/main/org/h2/command/ddl/AlterTableAddConstraint.java
浏览文件 @
6888e92a
...
@@ -212,7 +212,7 @@ public class AlterTableAddConstraint extends SchemaCommand {
...
@@ -212,7 +212,7 @@ public class AlterTableAddConstraint extends SchemaCommand {
isOwner
=
true
;
isOwner
=
true
;
index
.
getIndexType
().
setBelongsToConstraint
(
true
);
index
.
getIndexType
().
setBelongsToConstraint
(
true
);
}
else
{
}
else
{
index
=
getIndex
(
table
,
indexColumns
,
tru
e
);
index
=
getIndex
(
table
,
indexColumns
,
fals
e
);
if
(
index
==
null
)
{
if
(
index
==
null
)
{
index
=
createIndex
(
table
,
indexColumns
,
false
);
index
=
createIndex
(
table
,
indexColumns
,
false
);
isOwner
=
true
;
isOwner
=
true
;
...
@@ -329,29 +329,30 @@ public class AlterTableAddConstraint extends SchemaCommand {
...
@@ -329,29 +329,30 @@ public class AlterTableAddConstraint extends SchemaCommand {
}
}
return
null
;
return
null
;
}
}
// all cols must be in the index key, the order doesn't matter and there must be no other fields in the index key
private
static
boolean
canUseUniqueIndex
(
Index
idx
,
Table
table
,
private
static
boolean
canUseUniqueIndex
(
Index
idx
,
Table
table
,
IndexColumn
[]
cols
)
{
IndexColumn
[]
cols
)
{
if
(
idx
.
getTable
()
!=
table
||
!
idx
.
getIndexType
().
isUnique
())
{
if
(
idx
.
getTable
()
!=
table
||
!
idx
.
getIndexType
().
isUnique
())
{
return
false
;
return
false
;
}
}
Column
[]
indexCols
=
idx
.
getColumns
();
Column
[]
indexCols
=
idx
.
getColumns
();
if
(
indexCols
.
length
>
cols
.
length
)
{
return
false
;
HashSet
<
Column
>
indexColsSet
=
New
.
hashSet
();
for
(
Column
c
:
indexCols
)
{
indexColsSet
.
add
(
c
);
}
}
HashSet
<
Column
>
set
=
New
.
hashSet
();
HashSet
<
Column
>
colsSet
=
New
.
hashSet
();
for
(
IndexColumn
c
:
cols
)
{
for
(
IndexColumn
c
:
cols
)
{
s
et
.
add
(
c
.
column
);
colsS
et
.
add
(
c
.
column
);
}
}
for
(
Column
c
:
indexCols
)
{
// all columns of the index must be part of the list,
return
colsSet
.
equals
(
indexColsSet
);
// but not all columns of the list need to be part of the index
}
if
(!
set
.
contains
(
c
))
{
return
false
;
}
}
return
true
;
}
private
static
boolean
canUseIndex
(
Index
existingIndex
,
Table
table
,
private
static
boolean
canUseIndex
(
Index
existingIndex
,
Table
table
,
IndexColumn
[]
cols
,
boolean
moreColumnsOk
)
{
IndexColumn
[]
cols
,
boolean
moreColumnsOk
)
{
...
...
h2/src/test/org/h2/test/db/TestCases.java
浏览文件 @
6888e92a
...
@@ -150,18 +150,6 @@ public class TestCases extends TestBase {
...
@@ -150,18 +150,6 @@ public class TestCases extends TestBase {
"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"
);
stat
.
execute
(
"drop all objects"
);
stat
.
execute
(
"create table parent(id int primary key)"
);
stat
.
execute
(
"create table child(id int, parent_id int, x int)"
);
stat
.
execute
(
"create index y on child(parent_id, x)"
);
stat
.
execute
(
"alter table child add constraint z "
+
"foreign key(parent_id) references parent(id)"
);
ResultSet
rs
=
stat
.
executeQuery
(
"select * from information_schema.indexes where table_name = 'CHILD'"
);
while
(
rs
.
next
())
{
assertEquals
(
"Y"
,
rs
.
getString
(
"index_name"
));
}
conn
.
close
();
conn
.
close
();
}
}
...
...
h2/src/test/org/h2/test/scripts/TestScript.java
浏览文件 @
6888e92a
...
@@ -77,7 +77,9 @@ public class TestScript extends TestBase {
...
@@ -77,7 +77,9 @@ public class TestScript extends TestBase {
return
;
return
;
}
}
reconnectOften
=
!
config
.
memory
&&
config
.
big
;
reconnectOften
=
!
config
.
memory
&&
config
.
big
;
testScript
(
"testScript.sql"
);
testScript
(
"testScript.sql"
);
testScript
(
"altertable-index-reuse.sql"
);
testScript
(
"query-optimisations.sql"
);
testScript
(
"query-optimisations.sql"
);
testScript
(
"commands-dml-script.sql"
);
testScript
(
"commands-dml-script.sql"
);
testScript
(
"commands-dml-create-view.sql"
);
testScript
(
"commands-dml-create-view.sql"
);
...
...
h2/src/test/org/h2/test/scripts/altertable-index-reuse.sql
0 → 100644
浏览文件 @
6888e92a
-- Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0,
-- and the EPL 1.0 (http://h2database.com/html/license.html).
-- Initial Developer: H2 Group
--
CREATE
TABLE
"domains"
(
"id"
bigint
NOT
NULL
auto_increment
PRIMARY
KEY
);
>
ok
CREATE
TABLE
"users"
(
"id"
bigint
NOT
NULL
auto_increment
PRIMARY
KEY
,
"username"
varchar_ignorecase
(
255
),
"domain"
bigint
,
"desc"
varchar_ignorecase
(
255
));
>
ok
-- adds constraint on (domain,username) and generates unique index domainusername_key_INDEX_xxx
ALTER
TABLE
"users"
ADD
CONSTRAINT
"domainusername_key"
UNIQUE
(
"domain"
,
"username"
);
>
ok
-- adds foreign key on domain - if domainusername_key didn't exist it would create unique index on domain, but it reuses the existing index
ALTER
TABLE
"users"
ADD
CONSTRAINT
"udomain_fkey"
FOREIGN
KEY
(
"domain"
)
REFERENCES
"domains"
(
"id"
)
ON
DELETE
RESTRICT
;
>
ok
-- now we drop the domainusername_key, but domainusername_key_INDEX_xxx is used by udomain_fkey and was not being dropped
-- this was an issue, because it's a unique index and still enforcing constraint on (domain,username)
ALTER
TABLE
"users"
DROP
CONSTRAINT
"domainusername_key"
;
>
ok
insert
into
"domains"
(
"id"
)
VALUES
(
1
);
>
update
count
:
1
insert
into
"users"
(
"username"
,
"domain"
,
"desc"
)
VALUES
(
'test'
,
1
,
'first user'
);
>
update
count
:
1
-- should work,because we dropped domainusername_key, but failed: Unique index or primary key violation
INSERT
INTO
"users"
(
"username"
,
"domain"
,
"desc"
)
VALUES
(
'test'
,
1
,
'second user'
);
>
update
count
:
1
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论