提交 aab911e5 authored 作者: Thomas Mueller's avatar Thomas Mueller

Column constraints are also visible in views (patch from Nicolas Fortin for H2GIS).

上级 4307ec5b
......@@ -18,7 +18,8 @@ Change Log
<h1>Change Log</h1>
<h2>Next Version (unreleased)</h2>
<ul><li>Granting a additional right to a role that already had a right for that table was not working.
<ul><li>Column constraints are also visible in views (patch from Nicolas Fortin for H2GIS).
</li><li>Granting a additional right to a role that already had a right for that table was not working.
</li><li>Spatial index: a few bugs have been fixed (using spatial constraints in views,
transferring geometry objects over TCP/IP).
</li><li>Issue 551: the datatype documentation was incorrect (found by Bernd Eckenfels).
......
......@@ -16,7 +16,9 @@ import org.h2.engine.Constants;
import org.h2.engine.DbObject;
import org.h2.engine.Session;
import org.h2.engine.User;
import org.h2.expression.Alias;
import org.h2.expression.Expression;
import org.h2.expression.ExpressionColumn;
import org.h2.expression.ExpressionVisitor;
import org.h2.expression.Parameter;
import org.h2.index.Index;
......@@ -39,6 +41,8 @@ import org.h2.value.Value;
/**
* A view is a virtual table that is defined by a query.
* @author Thomas Mueller
* @author Nicolas Fortin, Atelier SIG, IRSTV FR CNRS 24888
*/
public class TableView extends Table {
......@@ -168,6 +172,23 @@ public class TableView extends Table {
int displaySize = expr.getDisplaySize();
Column col = new Column(name, type, precision, scale, displaySize);
col.setTable(this, i);
// Fetch check constraint from view column source
ExpressionColumn fromColumn = null;
if (expr instanceof ExpressionColumn) {
fromColumn = (ExpressionColumn) expr;
} else if (expr instanceof Alias) {
Expression aliasExpr = expr.getNonAliasExpression();
if (aliasExpr instanceof ExpressionColumn) {
fromColumn = (ExpressionColumn) aliasExpr;
}
}
if (fromColumn != null) {
Expression checkExpression = fromColumn.getColumn()
.getCheckConstraint(session, name);
if (checkExpression != null) {
col.addCheckConstraint(session, checkExpression);
}
}
list.add(col);
}
cols = new Column[list.size()];
......
......@@ -43,6 +43,7 @@ public class TestView extends TestBase {
testManyViews();
testReferenceView();
testViewAlterAndCommandCache();
testViewConstraintFromColumnExpression();
deleteDb("view");
}
......@@ -236,4 +237,45 @@ public class TestView extends TestBase {
deleteDb("view");
}
/**
* Make sure that the table constraint is still available when create a view of other table.
*/
private void testViewConstraintFromColumnExpression() throws SQLException {
deleteDb("view");
Connection conn = getConnection("view");
Statement stat = conn.createStatement();
stat.execute("create table t0(id1 int primary key CHECK ((ID1 % 2) = 0))");
stat.execute("create table t1(id2 int primary key CHECK ((ID2 % 1) = 0))");
stat.execute("insert into t0 values(0)");
stat.execute("insert into t1 values(1)");
stat.execute("create view v1 as select * from t0,t1");
// Check with ColumnExpression
ResultSet rs = stat.executeQuery("select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = 'V1'");
assertTrue(rs.next());
assertEquals("ID1", rs.getString("COLUMN_NAME"));
assertEquals("((ID1 % 2) = 0)", rs.getString("CHECK_CONSTRAINT"));
assertTrue(rs.next());
assertEquals("ID2", rs.getString("COLUMN_NAME"));
assertEquals("((ID2 % 1) = 0)", rs.getString("CHECK_CONSTRAINT"));
// Check with AliasExpression
stat.execute("create view v2 as select ID1 key1,ID2 key2 from t0,t1");
rs = stat.executeQuery("select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = 'V2'");
assertTrue(rs.next());
assertEquals("KEY1", rs.getString("COLUMN_NAME"));
assertEquals("((KEY1 % 2) = 0)", rs.getString("CHECK_CONSTRAINT"));
assertTrue(rs.next());
assertEquals("KEY2", rs.getString("COLUMN_NAME"));
assertEquals("((KEY2 % 1) = 0)", rs.getString("CHECK_CONSTRAINT"));
// Check hide of constraint if column is an Operation
stat.execute("create view v3 as select ID1 + 1 ID1, ID2 + 1 ID2 from t0,t1");
rs = stat.executeQuery("select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = 'V3'");
assertTrue(rs.next());
assertEquals("ID1", rs.getString("COLUMN_NAME"));
assertEquals("", rs.getString("CHECK_CONSTRAINT"));
assertTrue(rs.next());
assertEquals("ID2", rs.getString("COLUMN_NAME"));
assertEquals("", rs.getString("CHECK_CONSTRAINT"));
conn.close();
deleteDb("view");
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论