提交 67e7b4bd authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Add INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS from the SQL standard

上级 b55500e7
......@@ -112,7 +112,8 @@ public class MetaTable extends Table {
private static final int SYNONYMS = 29;
private static final int TABLE_CONSTRAINTS = 30;
private static final int KEY_COLUMN_USAGE = 31;
private static final int META_TABLE_TYPE_COUNT = KEY_COLUMN_USAGE + 1;
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;
......@@ -591,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);
}
......@@ -2025,6 +2041,41 @@ public class MetaTable extends Table {
}
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;
}
Index index = 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(index.getSchema().getName()),
// UNIQUE_CONSTRAINT_NAME
// Should be name of referenced unique constraint, but H2 uses indexes instead
index.getName(),
// MATCH_OPTION
"NONE",
// UPDATE_RULE
constraint.getUpdateAction().getSqlName(),
// DELETE_RULE
constraint.getDeleteAction().getSqlName()
);
}
break;
}
default:
DbException.throwInternalError("type="+type);
}
......
......@@ -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"));
......
......@@ -15,7 +15,7 @@ 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 CH_1 CHECK C4 > 0;
......@@ -55,6 +55,20 @@ SELECT CONSTRAINT_NAME, TABLE_NAME, COLUMN_NAME, ORDINAL_POSITION, POSITION_IN_U
> FK_1 T2 C4 2 2
> rows (ordered): 6
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 returns 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
> rows (ordered): 1
DROP TABLE T2;
> ok
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论