Unverified 提交 e47fbaa9 authored 作者: Noel Grandin's avatar Noel Grandin 提交者: GitHub

Merge pull request #916 from katzyn/information_schema

Implement TABLE_CONSTRAINTS and REFERENTIAL_CONSTRAINTS from the SQL standard
......@@ -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();
}
}
/**
......
......@@ -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
......@@ -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();
}
......
......@@ -54,7 +54,7 @@ public class ConstraintUnique extends Constraint {
if (comment != null) {
buff.append(" COMMENT ").append(StringUtils.quoteStringSQL(comment));
}
buff.append(' ').append(getTypeName()).append('(');
buff.append(' ').append(getConstraintType().getSqlName()).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);
......
......@@ -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");
......
......@@ -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"));
......
......@@ -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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论