Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
e47fbaa9
Unverified
提交
e47fbaa9
authored
3月 02, 2018
作者:
Noel Grandin
提交者:
GitHub
3月 02, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #916 from katzyn/information_schema
Implement TABLE_CONSTRAINTS and REFERENTIAL_CONSTRAINTS from the SQL standard
上级
9ae7a1de
dd2140c5
显示空白字符变更
内嵌
并排
正在显示
7 个修改的文件
包含
241 行增加
和
38 行删除
+241
-38
Constraint.java
h2/src/main/org/h2/constraint/Constraint.java
+17
-1
ConstraintActionType.java
h2/src/main/org/h2/constraint/ConstraintActionType.java
+17
-1
ConstraintReferential.java
h2/src/main/org/h2/constraint/ConstraintReferential.java
+2
-20
ConstraintUnique.java
h2/src/main/org/h2/constraint/ConstraintUnique.java
+1
-8
MetaTable.java
h2/src/main/org/h2/table/MetaTable.java
+148
-6
TestMetaData.java
h2/src/test/org/h2/test/jdbc/TestMetaData.java
+4
-0
information_schema.sql
h2/src/test/org/h2/test/scripts/information_schema.sql
+52
-2
没有找到文件。
h2/src/main/org/h2/constraint/Constraint.java
浏览文件 @
e47fbaa9
...
...
@@ -39,7 +39,23 @@ public abstract class Constraint extends SchemaObjectBase implements
/**
* The constraint type for referential constraints.
*/
REFERENTIAL
REFERENTIAL
;
/**
* Get standard SQL type name.
*
* @return standard SQL type name
*/
public
String
getSqlName
()
{
if
(
this
==
Constraint
.
Type
.
PRIMARY_KEY
)
{
return
"PRIMARY KEY"
;
}
if
(
this
==
Constraint
.
Type
.
REFERENTIAL
)
{
return
"FOREIGN KEY"
;
}
return
name
();
}
}
/**
...
...
h2/src/main/org/h2/constraint/ConstraintActionType.java
浏览文件 @
e47fbaa9
...
...
@@ -24,5 +24,21 @@ public enum ConstraintActionType {
/**
* The action is to set the value to NULL.
*/
SET_NULL
SET_NULL
;
/**
* Get standard SQL type name.
*
* @return standard SQL type name
*/
public
String
getSqlName
()
{
if
(
this
==
ConstraintActionType
.
SET_DEFAULT
)
{
return
"SET DEFAULT"
;
}
if
(
this
==
SET_NULL
)
{
return
"SET NULL"
;
}
return
name
();
}
}
\ No newline at end of file
h2/src/main/org/h2/constraint/ConstraintReferential.java
浏览文件 @
e47fbaa9
...
...
@@ -54,22 +54,6 @@ public class ConstraintReferential extends Constraint {
return
Constraint
.
Type
.
REFERENTIAL
;
}
private
static
void
appendAction
(
StatementBuilder
buff
,
ConstraintActionType
action
)
{
switch
(
action
)
{
case
CASCADE:
buff
.
append
(
"CASCADE"
);
break
;
case
SET_DEFAULT:
buff
.
append
(
"SET DEFAULT"
);
break
;
case
SET_NULL:
buff
.
append
(
"SET NULL"
);
break
;
default
:
DbException
.
throwInternalError
(
"action="
+
action
);
}
}
/**
* Create the SQL statement of this object so a copy of the table can be
* made.
...
...
@@ -135,12 +119,10 @@ public class ConstraintReferential extends Constraint {
buff
.
append
(
" INDEX "
).
append
(
refIndex
.
getSQL
());
}
if
(
deleteAction
!=
ConstraintActionType
.
RESTRICT
)
{
buff
.
append
(
" ON DELETE "
);
appendAction
(
buff
,
deleteAction
);
buff
.
append
(
" ON DELETE "
).
append
(
deleteAction
.
getSqlName
());
}
if
(
updateAction
!=
ConstraintActionType
.
RESTRICT
)
{
buff
.
append
(
" ON UPDATE "
);
appendAction
(
buff
,
updateAction
);
buff
.
append
(
" ON UPDATE "
).
append
(
updateAction
.
getSqlName
());
}
return
buff
.
append
(
" NOCHECK"
).
toString
();
}
...
...
h2/src/main/org/h2/constraint/ConstraintUnique.java
浏览文件 @
e47fbaa9
...
...
@@ -54,7 +54,7 @@ public class ConstraintUnique extends Constraint {
if
(
comment
!=
null
)
{
buff
.
append
(
" COMMENT "
).
append
(
StringUtils
.
quoteStringSQL
(
comment
));
}
buff
.
append
(
' '
).
append
(
get
Type
Name
()).
append
(
'('
);
buff
.
append
(
' '
).
append
(
get
ConstraintType
().
getSql
Name
()).
append
(
'('
);
for
(
IndexColumn
c
:
columns
)
{
buff
.
appendExceptFirst
(
", "
);
buff
.
append
(
Parser
.
quoteIdentifier
(
c
.
column
.
getName
()));
...
...
@@ -66,13 +66,6 @@ public class ConstraintUnique extends Constraint {
return
buff
.
toString
();
}
private
String
getTypeName
()
{
if
(
primaryKey
)
{
return
"PRIMARY KEY"
;
}
return
"UNIQUE"
;
}
@Override
public
String
getCreateSQLWithoutIndexes
()
{
return
getCreateSQLForCopy
(
table
,
getSQL
(),
false
);
...
...
h2/src/main/org/h2/table/MetaTable.java
浏览文件 @
e47fbaa9
...
...
@@ -110,8 +110,10 @@ public class MetaTable extends Table {
private
static
final
int
SESSION_STATE
=
27
;
private
static
final
int
QUERY_STATISTICS
=
28
;
private
static
final
int
SYNONYMS
=
29
;
private
static
final
int
KEY_COLUMN_USAGE
=
30
;
private
static
final
int
META_TABLE_TYPE_COUNT
=
KEY_COLUMN_USAGE
+
1
;
private
static
final
int
TABLE_CONSTRAINTS
=
30
;
private
static
final
int
KEY_COLUMN_USAGE
=
31
;
private
static
final
int
REFERENTIAL_CONSTRAINTS
=
32
;
private
static
final
int
META_TABLE_TYPE_COUNT
=
REFERENTIAL_CONSTRAINTS
+
1
;
private
final
int
type
;
private
final
int
indexColumn
;
...
...
@@ -558,6 +560,22 @@ public class MetaTable extends Table {
indexColumnName
=
"SYNONYM_NAME"
;
break
;
}
case
TABLE_CONSTRAINTS:
{
setObjectName
(
"TABLE_CONSTRAINTS"
);
cols
=
createColumns
(
"CONSTRAINT_CATALOG"
,
"CONSTRAINT_SCHEMA"
,
"CONSTRAINT_NAME"
,
"CONSTRAINT_TYPE"
,
"TABLE_CATALOG"
,
"TABLE_SCHEMA"
,
"TABLE_NAME"
,
"IS_DEFERRABLE"
,
"INITIALLY_DEFERRED"
);
indexColumnName
=
"TABLE_NAME"
;
break
;
}
case
KEY_COLUMN_USAGE:
{
setObjectName
(
"KEY_COLUMN_USAGE"
);
cols
=
createColumns
(
...
...
@@ -574,6 +592,21 @@ public class MetaTable extends Table {
indexColumnName
=
"TABLE_NAME"
;
break
;
}
case
REFERENTIAL_CONSTRAINTS:
{
setObjectName
(
"REFERENTIAL_CONSTRAINTS"
);
cols
=
createColumns
(
"CONSTRAINT_CATALOG"
,
"CONSTRAINT_SCHEMA"
,
"CONSTRAINT_NAME"
,
"UNIQUE_CONSTRAINT_CATALOG"
,
"UNIQUE_CONSTRAINT_SCHEMA"
,
"UNIQUE_CONSTRAINT_NAME"
,
"MATCH_OPTION"
,
"UPDATE_RULE"
,
"DELETE_RULE"
);
break
;
}
default
:
throw
DbException
.
throwInternalError
(
"type="
+
type
);
}
...
...
@@ -1924,9 +1957,43 @@ public class MetaTable extends Table {
}
break
;
}
case
TABLE_CONSTRAINTS:
{
for
(
SchemaObject
obj
:
database
.
getAllSchemaObjects
(
DbObject
.
CONSTRAINT
))
{
Constraint
constraint
=
(
Constraint
)
obj
;
Constraint
.
Type
constraintType
=
constraint
.
getConstraintType
();
Table
table
=
constraint
.
getTable
();
if
(
hideTable
(
table
,
session
))
{
continue
;
}
String
tableName
=
identifier
(
table
.
getName
());
if
(!
checkIndex
(
session
,
tableName
,
indexFrom
,
indexTo
))
{
continue
;
}
add
(
rows
,
// CONSTRAINT_CATALOG
catalog
,
// CONSTRAINT_SCHEMA
identifier
(
constraint
.
getSchema
().
getName
()),
// CONSTRAINT_NAME
identifier
(
constraint
.
getName
()),
// CONSTRAINT_TYPE
constraintType
.
getSqlName
(),
// TABLE_CATALOG
catalog
,
// TABLE_SCHEMA
identifier
(
table
.
getSchema
().
getName
()),
// TABLE_NAME
tableName
,
// IS_DEFERRABLE
"NO"
,
// INITIALLY_DEFERRED
"NO"
);
}
break
;
}
case
KEY_COLUMN_USAGE:
{
for
(
SchemaObject
obj
:
database
.
getAllSchemaObjects
(
DbObject
.
CONSTRAINT
))
{
for
(
SchemaObject
obj
:
database
.
getAllSchemaObjects
(
DbObject
.
CONSTRAINT
))
{
Constraint
constraint
=
(
Constraint
)
obj
;
Constraint
.
Type
constraintType
=
constraint
.
getConstraintType
();
IndexColumn
[]
indexColumns
=
null
;
...
...
@@ -1947,9 +2014,31 @@ public class MetaTable extends Table {
if
(
indexColumns
==
null
)
{
continue
;
}
ConstraintUnique
referenced
;
if
(
constraintType
==
Constraint
.
Type
.
REFERENTIAL
)
{
referenced
=
lookupUniqueForReferential
((
ConstraintReferential
)
constraint
);
}
else
{
referenced
=
null
;
}
for
(
int
i
=
0
;
i
<
indexColumns
.
length
;
i
++)
{
IndexColumn
indexColumn
=
indexColumns
[
i
];
String
ordinalPosition
=
Integer
.
toString
(
i
+
1
);
String
positionInUniqueConstraint
;
if
(
constraintType
==
Constraint
.
Type
.
REFERENTIAL
)
{
positionInUniqueConstraint
=
ordinalPosition
;
if
(
referenced
!=
null
)
{
Column
c
=
((
ConstraintReferential
)
constraint
).
getRefColumns
()[
i
].
column
;
IndexColumn
[]
refColumns
=
referenced
.
getColumns
();
for
(
int
j
=
0
;
j
<
refColumns
.
length
;
j
++)
{
if
(
refColumns
[
j
].
column
.
equals
(
c
))
{
positionInUniqueConstraint
=
Integer
.
toString
(
j
+
1
);
break
;
}
}
}
}
else
{
positionInUniqueConstraint
=
null
;
}
add
(
rows
,
// CONSTRAINT_CATALOG
catalog
,
...
...
@@ -1968,12 +2057,52 @@ public class MetaTable extends Table {
// ORDINAL_POSITION
ordinalPosition
,
// POSITION_IN_UNIQUE_CONSTRAINT
(
constraintType
==
Constraint
.
Type
.
REFERENTIAL
?
ordinalPosition
:
null
)
positionInUniqueConstraint
);
}
}
break
;
}
case
REFERENTIAL_CONSTRAINTS:
{
for
(
SchemaObject
obj
:
database
.
getAllSchemaObjects
(
DbObject
.
CONSTRAINT
))
{
if
(((
Constraint
)
obj
).
getConstraintType
()
!=
Constraint
.
Type
.
REFERENTIAL
)
{
continue
;
}
ConstraintReferential
constraint
=
(
ConstraintReferential
)
obj
;
Table
table
=
constraint
.
getTable
();
if
(
hideTable
(
table
,
session
))
{
continue
;
}
// Should be referenced unique constraint, but H2 uses indexes instead.
// So try to find matching unique constraint first and there is no such
// constraint use index name to return something.
SchemaObject
unique
=
lookupUniqueForReferential
(
constraint
);
if
(
unique
==
null
)
{
unique
=
constraint
.
getUniqueIndex
();
}
add
(
rows
,
// CONSTRAINT_CATALOG
catalog
,
// CONSTRAINT_SCHEMA
identifier
(
constraint
.
getSchema
().
getName
()),
// CONSTRAINT_NAME
identifier
(
constraint
.
getName
()),
// UNIQUE_CONSTRAINT_CATALOG
catalog
,
// UNIQUE_CONSTRAINT_SCHEMA
identifier
(
unique
.
getSchema
().
getName
()),
// UNIQUE_CONSTRAINT_NAME
unique
.
getName
(),
// MATCH_OPTION
"NONE"
,
// UPDATE_RULE
constraint
.
getUpdateAction
().
getSqlName
(),
// DELETE_RULE
constraint
.
getDeleteAction
().
getSqlName
()
);
}
break
;
}
default
:
DbException
.
throwInternalError
(
"type="
+
type
);
}
...
...
@@ -1995,6 +2124,19 @@ public class MetaTable extends Table {
}
}
private
static
ConstraintUnique
lookupUniqueForReferential
(
ConstraintReferential
referential
)
{
Table
table
=
referential
.
getRefTable
();
for
(
Constraint
c
:
table
.
getConstraints
())
{
if
(
c
.
getConstraintType
()
==
Constraint
.
Type
.
UNIQUE
)
{
ConstraintUnique
unique
=
(
ConstraintUnique
)
c
;
if
(
unique
.
getReferencedColumns
(
table
).
equals
(
referential
.
getReferencedColumns
(
table
)))
{
return
unique
;
}
}
}
return
null
;
}
@Override
public
void
removeRow
(
Session
session
,
Row
row
)
{
throw
DbException
.
getUnsupportedException
(
"META"
);
...
...
h2/src/test/org/h2/test/jdbc/TestMetaData.java
浏览文件 @
e47fbaa9
...
...
@@ -1090,6 +1090,8 @@ public class TestMetaData extends TestBase {
rs
.
next
();
assertEquals
(
"QUERY_STATISTICS"
,
rs
.
getString
(
"TABLE_NAME"
));
rs
.
next
();
assertEquals
(
"REFERENTIAL_CONSTRAINTS"
,
rs
.
getString
(
"TABLE_NAME"
));
rs
.
next
();
assertEquals
(
"RIGHTS"
,
rs
.
getString
(
"TABLE_NAME"
));
rs
.
next
();
assertEquals
(
"ROLES"
,
rs
.
getString
(
"TABLE_NAME"
));
...
...
@@ -1108,6 +1110,8 @@ public class TestMetaData extends TestBase {
rs
.
next
();
assertEquals
(
"TABLES"
,
rs
.
getString
(
"TABLE_NAME"
));
rs
.
next
();
assertEquals
(
"TABLE_CONSTRAINTS"
,
rs
.
getString
(
"TABLE_NAME"
));
rs
.
next
();
assertEquals
(
"TABLE_PRIVILEGES"
,
rs
.
getString
(
"TABLE_NAME"
));
rs
.
next
();
assertEquals
(
"TABLE_TYPES"
,
rs
.
getString
(
"TABLE_NAME"
));
...
...
h2/src/test/org/h2/test/scripts/information_schema.sql
浏览文件 @
e47fbaa9
...
...
@@ -15,9 +15,32 @@ ALTER TABLE T1 ADD CONSTRAINT U_1 UNIQUE(C3, C4);
CREATE
TABLE
T2
(
C1
INT
,
C2
INT
,
C3
INT
,
C4
INT
);
>
ok
ALTER
TABLE
T2
ADD
CONSTRAINT
FK_1
FOREIGN
KEY
(
C3
,
C4
)
REFERENCES
T1
(
C1
,
C3
);
ALTER
TABLE
T2
ADD
CONSTRAINT
FK_1
FOREIGN
KEY
(
C3
,
C4
)
REFERENCES
T1
(
C1
,
C3
)
ON
DELETE
SET
NULL
;
>
ok
ALTER
TABLE
T2
ADD
CONSTRAINT
FK_2
FOREIGN
KEY
(
C3
,
C4
)
REFERENCES
T1
(
C4
,
C3
)
ON
UPDATE
CASCADE
ON
DELETE
SET
DEFAULT
;
>
ok
ALTER
TABLE
T2
ADD
CONSTRAINT
CH_1
CHECK
C4
>
0
;
>
ok
SELECT
*
FROM
INFORMATION_SCHEMA
.
TABLE_CONSTRAINTS
LIMIT
0
;
>
CONSTRAINT_CATALOG
CONSTRAINT_SCHEMA
CONSTRAINT_NAME
CONSTRAINT_TYPE
TABLE_CATALOG
TABLE_SCHEMA
TABLE_NAME
IS_DEFERRABLE
INITIALLY_DEFERRED
>
------------------ ----------------- --------------- --------------- ------------- ------------ ---------- ------------- ------------------
>
rows
:
0
SELECT
CONSTRAINT_NAME
,
CONSTRAINT_TYPE
,
TABLE_NAME
,
IS_DEFERRABLE
,
INITIALLY_DEFERRED
FROM
INFORMATION_SCHEMA
.
TABLE_CONSTRAINTS
WHERE
CONSTRAINT_CATALOG
=
DATABASE
()
AND
CONSTRAINT_SCHEMA
=
SCHEMA
()
AND
TABLE_CATALOG
=
DATABASE
()
AND
TABLE_SCHEMA
=
SCHEMA
()
ORDER
BY
TABLE_NAME
,
CONSTRAINT_NAME
;
>
CONSTRAINT_NAME
CONSTRAINT_TYPE
TABLE_NAME
IS_DEFERRABLE
INITIALLY_DEFERRED
>
--------------- --------------- ---------- ------------- ------------------
>
PK_1
PRIMARY
KEY
T1
NO
NO
>
U_1
UNIQUE
T1
NO
NO
>
CH_1
CHECK
T2
NO
NO
>
FK_1
FOREIGN
KEY
T2
NO
NO
>
FK_2
FOREIGN
KEY
T2
NO
NO
>
rows
(
ordered
):
5
SELECT
*
FROM
INFORMATION_SCHEMA
.
KEY_COLUMN_USAGE
LIMIT
0
;
>
CONSTRAINT_CATALOG
CONSTRAINT_SCHEMA
CONSTRAINT_NAME
TABLE_CATALOG
TABLE_SCHEMA
TABLE_NAME
COLUMN_NAME
ORDINAL_POSITION
POSITION_IN_UNIQUE_CONSTRAINT
>
------------------ ----------------- --------------- ------------- ------------ ---------- ----------- ---------------- -----------------------------
...
...
@@ -34,7 +57,34 @@ SELECT CONSTRAINT_NAME, TABLE_NAME, COLUMN_NAME, ORDINAL_POSITION, POSITION_IN_U
>
U_1
T1
C4
2
null
>
FK_1
T2
C3
1
1
>
FK_1
T2
C4
2
2
>
rows
(
ordered
):
6
>
FK_2
T2
C3
1
2
>
FK_2
T2
C4
2
1
>
rows
(
ordered
):
8
SELECT
*
FROM
INFORMATION_SCHEMA
.
REFERENTIAL_CONSTRAINTS
LIMIT
0
;
>
CONSTRAINT_CATALOG
CONSTRAINT_SCHEMA
CONSTRAINT_NAME
UNIQUE_CONSTRAINT_CATALOG
UNIQUE_CONSTRAINT_SCHEMA
UNIQUE_CONSTRAINT_NAME
MATCH_OPTION
UPDATE_RULE
DELETE_RULE
>
------------------ ----------------- --------------- ------------------------- ------------------------ ---------------------- ------------ ----------- -----------
>
rows
:
0
-- H2 may return name of the index istead of name of the referenced constraint as UNIQUE_CONSTRAINT_NAME
SELECT
CONSTRAINT_NAME
,
SUBSTRING
(
UNIQUE_CONSTRAINT_NAME
,
0
,
11
)
AS
UCN_PART
,
MATCH_OPTION
,
UPDATE_RULE
,
DELETE_RULE
FROM
INFORMATION_SCHEMA
.
REFERENTIAL_CONSTRAINTS
WHERE
CONSTRAINT_CATALOG
=
DATABASE
()
AND
CONSTRAINT_SCHEMA
=
SCHEMA
()
AND
UNIQUE_CONSTRAINT_CATALOG
=
DATABASE
()
AND
UNIQUE_CONSTRAINT_SCHEMA
=
SCHEMA
()
ORDER
BY
CONSTRAINT_NAME
,
UNIQUE_CONSTRAINT_NAME
;
>
CONSTRAINT_NAME
UCN_PART
MATCH_OPTION
UPDATE_RULE
DELETE_RULE
>
--------------- ----------- ------------ ----------- -----------
>
FK_1
FK_1_INDEX_
NONE
RESTRICT
SET
NULL
>
FK_2
U_1
NONE
CASCADE
SET
DEFAULT
>
rows
(
ordered
):
2
SELECT
U1
.
TABLE_NAME
T1
,
U1
.
COLUMN_NAME
C1
,
U2
.
TABLE_NAME
T2
,
U2
.
COLUMN_NAME
C2
FROM
INFORMATION_SCHEMA
.
KEY_COLUMN_USAGE
U1
JOIN
INFORMATION_SCHEMA
.
REFERENTIAL_CONSTRAINTS
RC
ON
U1
.
CONSTRAINT_NAME
=
RC
.
CONSTRAINT_NAME
JOIN
INFORMATION_SCHEMA
.
KEY_COLUMN_USAGE
U2
ON
RC
.
UNIQUE_CONSTRAINT_NAME
=
U2
.
CONSTRAINT_NAME
AND
U1
.
POSITION_IN_UNIQUE_CONSTRAINT
=
U2
.
ORDINAL_POSITION
WHERE
U1
.
CONSTRAINT_NAME
=
'FK_2'
ORDER
BY
U1
.
COLUMN_NAME
;
>
T1
C1
T2
C2
>
-- -- -- --
>
T2
C3
T1
C4
>
T2
C4
T1
C3
>
rows
(
ordered
):
2
DROP
TABLE
T2
;
>
ok
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论