Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
65eb8d0c
提交
65eb8d0c
authored
7月 28, 2018
作者:
Evgenij Ryazanov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Prefer explicitly defined primary key
上级
006fe1b0
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
43 行增加
和
14 行删除
+43
-14
AlterTableAddConstraint.java
h2/src/main/org/h2/command/ddl/AlterTableAddConstraint.java
+4
-0
CommandWithColumns.java
h2/src/main/org/h2/command/ddl/CommandWithColumns.java
+27
-14
createTable.sql
h2/src/test/org/h2/test/scripts/ddl/createTable.sql
+12
-0
没有找到文件。
h2/src/main/org/h2/command/ddl/AlterTableAddConstraint.java
浏览文件 @
65eb8d0c
...
...
@@ -393,6 +393,10 @@ public class AlterTableAddConstraint extends SchemaCommand {
this
.
constraintName
=
constraintName
;
}
public
String
getConstraintName
()
{
return
constraintName
;
}
public
void
setType
(
int
type
)
{
this
.
type
=
type
;
}
...
...
h2/src/main/org/h2/command/ddl/CommandWithColumns.java
浏览文件 @
65eb8d0c
...
...
@@ -21,7 +21,7 @@ public abstract class CommandWithColumns extends SchemaCommand {
private
ArrayList
<
DefineCommand
>
constraintCommands
;
private
IndexColumn
[]
pkColumns
;
private
AlterTableAddConstraint
primaryKey
;
protected
CommandWithColumns
(
Session
session
,
Schema
schema
)
{
super
(
session
,
schema
);
...
...
@@ -49,7 +49,7 @@ public abstract class CommandWithColumns extends SchemaCommand {
AlterTableAddConstraint
con
=
(
AlterTableAddConstraint
)
command
;
boolean
alreadySet
;
if
(
con
.
getType
()
==
CommandInterface
.
ALTER_TABLE_ADD_CONSTRAINT_PRIMARY_KEY
)
{
alreadySet
=
setPrimaryKey
Columns
(
con
.
getIndexColumns
()
);
alreadySet
=
setPrimaryKey
(
con
);
}
else
{
alreadySet
=
false
;
}
...
...
@@ -66,7 +66,8 @@ public abstract class CommandWithColumns extends SchemaCommand {
* @param columns the list of columns
*/
protected
void
changePrimaryKeysToNotNull
(
ArrayList
<
Column
>
columns
)
{
if
(
pkColumns
!=
null
)
{
if
(
primaryKey
!=
null
)
{
IndexColumn
[]
pkColumns
=
primaryKey
.
getIndexColumns
();
for
(
Column
c
:
columns
)
{
for
(
IndexColumn
idxCol
:
pkColumns
)
{
if
(
c
.
getName
().
equals
(
idxCol
.
columnName
))
{
...
...
@@ -126,27 +127,39 @@ public abstract class CommandWithColumns extends SchemaCommand {
}
/**
* Set
s the primary key columns
, but also check if a primary key with different
* Set
the primary key
, but also check if a primary key with different
* columns is already defined.
* <p>
* If an unnamed primary key with the same columns is already defined it is
* removed from the list of constraints and this method returns
* {@code false}.
* </p>
*
* @param columns
* the primary key columns
* @return true if the same primary key columns where already set
* @param primaryKey
* the primary key
* @return whether another primary key with the same columns was already set
* and the specified primary key should be ignored
*/
private
boolean
setPrimaryKeyColumns
(
IndexColumn
[]
columns
)
{
if
(
pkColumns
!=
null
)
{
int
len
=
columns
.
length
;
if
(
len
!=
pkColumns
.
length
)
{
private
boolean
setPrimaryKey
(
AlterTableAddConstraint
primaryKey
)
{
if
(
this
.
primaryKey
!=
null
)
{
IndexColumn
[]
oldColumns
=
this
.
primaryKey
.
getIndexColumns
();
IndexColumn
[]
newColumns
=
primaryKey
.
getIndexColumns
();
int
len
=
newColumns
.
length
;
if
(
len
!=
oldColumns
.
length
)
{
throw
DbException
.
get
(
ErrorCode
.
SECOND_PRIMARY_KEY
);
}
for
(
int
i
=
0
;
i
<
len
;
i
++)
{
if
(!
columns
[
i
].
columnName
.
equals
(
pk
Columns
[
i
].
columnName
))
{
if
(!
newColumns
[
i
].
columnName
.
equals
(
old
Columns
[
i
].
columnName
))
{
throw
DbException
.
get
(
ErrorCode
.
SECOND_PRIMARY_KEY
);
}
}
return
true
;
if
(
this
.
primaryKey
.
getConstraintName
()
!=
null
)
{
return
true
;
}
// Remove unnamed primary key
constraintCommands
.
remove
(
this
.
primaryKey
);
}
this
.
p
kColumns
=
columns
;
this
.
p
rimaryKey
=
primaryKey
;
return
false
;
}
...
...
h2/src/test/org/h2/test/scripts/ddl/createTable.sql
浏览文件 @
65eb8d0c
...
...
@@ -15,6 +15,18 @@ SELECT CONSTRAINT_NAME, CONSTRAINT_TYPE FROM INFORMATION_SCHEMA.TABLE_CONSTRAINT
DROP
TABLE
TEST
;
>
ok
CREATE
TABLE
TEST
(
ID
IDENTITY
,
CONSTRAINT
PK_1
PRIMARY
KEY
(
ID
));
>
ok
SELECT
CONSTRAINT_NAME
,
CONSTRAINT_TYPE
FROM
INFORMATION_SCHEMA
.
TABLE_CONSTRAINTS
;
>
CONSTRAINT_NAME
CONSTRAINT_TYPE
>
--------------- ---------------
>
PK_1
PRIMARY
KEY
>
rows
:
1
DROP
TABLE
TEST
;
>
ok
CREATE
TABLE
T1
(
ID
INT
PRIMARY
KEY
,
COL2
INT
);
>
ok
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论