提交 f974e8b5 authored 作者: thomasmueller's avatar thomasmueller

Formatting, javadocs

上级 de02c023
...@@ -197,7 +197,7 @@ WITH [ RECURSIVE ] { name [( columnName [,...] )] ...@@ -197,7 +197,7 @@ WITH [ RECURSIVE ] { name [( columnName [,...] )]
AS ( select ) [,...] } AS ( select ) [,...] }
select select
"," ","
Can be used to create a recursive or non-recursive query (common table expression). Can be used to create a recursive or non-recursive query (common table expression).
For recursive queries the first select has to be a UNION. For recursive queries the first select has to be a UNION.
One or more common table entries can be referred to by name. One or more common table entries can be referred to by name.
Column name declarations are now optional - the column names will be inferred from the named select queries. Column name declarations are now optional - the column names will be inferred from the named select queries.
...@@ -213,8 +213,7 @@ SELECT sum(n) FROM t; ...@@ -213,8 +213,7 @@ SELECT sum(n) FROM t;
"," ","
WITH t1 AS ( WITH t1 AS (
SELECT 1 AS FIRST_COLUMN SELECT 1 AS FIRST_COLUMN
), ), t2 AS (
t2 AS (
SELECT FIRST_COLUMN+1 AS FIRST_COLUMN FROM t1 SELECT FIRST_COLUMN+1 AS FIRST_COLUMN FROM t1
) )
SELECT sum(FIRST_COLUMN) FROM t2; SELECT sum(FIRST_COLUMN) FROM t2;
......
...@@ -71,7 +71,7 @@ WITH [ RECURSIVE ] { name [( columnName [,...] )] ...@@ -71,7 +71,7 @@ WITH [ RECURSIVE ] { name [( columnName [,...] )]
AS ( select ) [,...] } AS ( select ) [,...] }
select select
"," ","
Can be used to create a recursive query." Can be used to create a recursive or non-recursive query (common table expression)."
"Commands (DDL)","ALTER INDEX RENAME"," "Commands (DDL)","ALTER INDEX RENAME","
ALTER INDEX [ IF EXISTS ] indexName RENAME TO newIndexName ALTER INDEX [ IF EXISTS ] indexName RENAME TO newIndexName
"," ","
......
...@@ -144,7 +144,7 @@ public class RangeTable extends Table { ...@@ -144,7 +144,7 @@ public class RangeTable extends Table {
@Override @Override
public TableType getTableType() { public TableType getTableType() {
return TableType.SYSTEM_TABLE; return TableType.SYSTEM_TABLE;
} }
@Override @Override
......
...@@ -207,7 +207,8 @@ public class TableView extends Table { ...@@ -207,7 +207,8 @@ public class TableView extends Table {
// If it can't be compiled, then it's a 'zero column table' // If it can't be compiled, then it's a 'zero column table'
// this avoids problems when creating the view when opening the // this avoids problems when creating the view when opening the
// database. // database.
// If it can not be compiled - it could also be a recursive common table expression query. // If it can not be compiled - it could also be a recursive common
// table expression query.
if (isRecursiveQueryExceptionDetected(createException)) { if (isRecursiveQueryExceptionDetected(createException)) {
this.isRecursiveQueryDetected = true; this.isRecursiveQueryDetected = true;
} }
...@@ -673,6 +674,8 @@ public class TableView extends Table { ...@@ -673,6 +674,8 @@ public class TableView extends Table {
/** /**
* Was query recursion detected during compiling. * Was query recursion detected during compiling.
*
* @return true if yes
*/ */
public boolean isRecursiveQueryDetected() { public boolean isRecursiveQueryDetected() {
return isRecursiveQueryDetected; return isRecursiveQueryDetected;
......
...@@ -16,7 +16,7 @@ import org.h2.test.TestBase; ...@@ -16,7 +16,7 @@ import org.h2.test.TestBase;
*/ */
public class TestGeneralCommonTableQueries extends TestBase { public class TestGeneralCommonTableQueries extends TestBase {
/** /**
* Run just this test. * Run just this test.
* *
* @param a ignored * @param a ignored
...@@ -45,14 +45,14 @@ public class TestGeneralCommonTableQueries extends TestBase { ...@@ -45,14 +45,14 @@ public class TestGeneralCommonTableQueries extends TestBase {
final String simple_two_column_query = "with " + final String simple_two_column_query = "with " +
"t1(n) as (select 1 as first) " + "t1(n) as (select 1 as first) " +
",t2(n) as (select 2 as first) " + ",t2(n) as (select 2 as first) " +
"select * from t1 union all select * from t2"; "select * from t1 union all select * from t2";
rs = stat.executeQuery(simple_two_column_query); rs = stat.executeQuery(simple_two_column_query);
assertTrue(rs.next()); assertTrue(rs.next());
assertEquals(1, rs.getInt(1)); assertEquals(1, rs.getInt(1));
assertTrue(rs.next()); assertTrue(rs.next());
assertEquals(2, rs.getInt(1)); assertEquals(2, rs.getInt(1));
assertFalse(rs.next()); assertFalse(rs.next());
prep = conn.prepareStatement(simple_two_column_query); prep = conn.prepareStatement(simple_two_column_query);
rs = prep.executeQuery(); rs = prep.executeQuery();
assertTrue(rs.next()); assertTrue(rs.next());
...@@ -60,7 +60,7 @@ public class TestGeneralCommonTableQueries extends TestBase { ...@@ -60,7 +60,7 @@ public class TestGeneralCommonTableQueries extends TestBase {
assertTrue(rs.next()); assertTrue(rs.next());
assertEquals(2, rs.getInt(1)); assertEquals(2, rs.getInt(1));
assertFalse(rs.next()); assertFalse(rs.next());
prep = conn.prepareStatement("with " + prep = conn.prepareStatement("with " +
"t1(n) as (select 2 as first) " + "t1(n) as (select 2 as first) " +
",t2(n) as (select 3 as first) " + ",t2(n) as (select 3 as first) " +
...@@ -72,7 +72,7 @@ public class TestGeneralCommonTableQueries extends TestBase { ...@@ -72,7 +72,7 @@ public class TestGeneralCommonTableQueries extends TestBase {
assertTrue(rs.next()); assertTrue(rs.next());
assertEquals(3, rs.getInt(1)); assertEquals(3, rs.getInt(1));
assertFalse(rs.next()); assertFalse(rs.next());
prep = conn.prepareStatement("with " + prep = conn.prepareStatement("with " +
"t1(n) as (select 2 as first) " + "t1(n) as (select 2 as first) " +
",t2(n) as (select 3 as first) " + ",t2(n) as (select 3 as first) " +
...@@ -85,7 +85,7 @@ public class TestGeneralCommonTableQueries extends TestBase { ...@@ -85,7 +85,7 @@ public class TestGeneralCommonTableQueries extends TestBase {
assertTrue(rs.next()); assertTrue(rs.next());
assertEquals(3, rs.getInt(1)); assertEquals(3, rs.getInt(1));
assertFalse(rs.next()); assertFalse(rs.next());
conn.close(); conn.close();
deleteDb("commonTableExpressionQueries"); deleteDb("commonTableExpressionQueries");
} }
...@@ -95,7 +95,7 @@ public class TestGeneralCommonTableQueries extends TestBase { ...@@ -95,7 +95,7 @@ public class TestGeneralCommonTableQueries extends TestBase {
Connection conn = getConnection("commonTableExpressionQueries"); Connection conn = getConnection("commonTableExpressionQueries");
PreparedStatement prep; PreparedStatement prep;
ResultSet rs; ResultSet rs;
prep = conn.prepareStatement("with " + prep = conn.prepareStatement("with " +
"t1 as (select 2 as first_col) " + "t1 as (select 2 as first_col) " +
",t2 as (select first_col+1 from t1) " + ",t2 as (select first_col+1 from t1) " +
...@@ -110,98 +110,100 @@ public class TestGeneralCommonTableQueries extends TestBase { ...@@ -110,98 +110,100 @@ public class TestGeneralCommonTableQueries extends TestBase {
assertFalse(rs.next()); assertFalse(rs.next());
assertEquals(rs.getMetaData().getColumnCount(),1); assertEquals(rs.getMetaData().getColumnCount(),1);
assertEquals("FIRST_COL",rs.getMetaData().getColumnLabel(1)); assertEquals("FIRST_COL",rs.getMetaData().getColumnLabel(1));
conn.close(); conn.close();
deleteDb("commonTableExpressionQueries"); deleteDb("commonTableExpressionQueries");
} }
private void testChainedQuery() throws Exception { private void testChainedQuery() throws Exception {
deleteDb("commonTableExpressionQueries"); deleteDb("commonTableExpressionQueries");
Connection conn = getConnection("commonTableExpressionQueries"); Connection conn = getConnection("commonTableExpressionQueries");
PreparedStatement prep; PreparedStatement prep;
ResultSet rs; ResultSet rs;
prep = conn.prepareStatement(" WITH t1 AS (" prep = conn.prepareStatement(
+" SELECT 1 AS FIRST_COLUMN" " WITH t1 AS (" +
+")," " SELECT 1 AS FIRST_COLUMN" +
+" t2 AS (" ")," +
+" SELECT FIRST_COLUMN+1 AS FIRST_COLUMN FROM t1 " " t2 AS (" +
+") " " SELECT FIRST_COLUMN+1 AS FIRST_COLUMN FROM t1 " +
+"SELECT sum(FIRST_COLUMN) FROM t2"); ") " +
"SELECT sum(FIRST_COLUMN) FROM t2");
rs = prep.executeQuery(); rs = prep.executeQuery();
assertTrue(rs.next()); assertTrue(rs.next());
assertEquals(2, rs.getInt(1)); assertEquals(2, rs.getInt(1));
assertFalse(rs.next()); assertFalse(rs.next());
conn.close(); conn.close();
deleteDb("commonTableExpressionQueries"); deleteDb("commonTableExpressionQueries");
} }
private void testParameterizedQuery() throws Exception { private void testParameterizedQuery() throws Exception {
deleteDb("commonTableExpressionQueries"); deleteDb("commonTableExpressionQueries");
Connection conn = getConnection("commonTableExpressionQueries"); Connection conn = getConnection("commonTableExpressionQueries");
PreparedStatement prep; PreparedStatement prep;
ResultSet rs; ResultSet rs;
prep = conn.prepareStatement("WITH t1 AS (" prep = conn.prepareStatement("WITH t1 AS (" +
+" SELECT X, 'T1' FROM SYSTEM_RANGE(?,?)" " SELECT X, 'T1' FROM SYSTEM_RANGE(?,?)" +
+")," ")," +
+"t2 AS (" "t2 AS (" +
+" SELECT X, 'T2' FROM SYSTEM_RANGE(?,?)" " SELECT X, 'T2' FROM SYSTEM_RANGE(?,?)" +
+") " ") " +
+"SELECT * FROM t1 UNION ALL SELECT * FROM t2 UNION ALL SELECT X, 'Q' FROM SYSTEM_RANGE(?,?)"); "SELECT * FROM t1 UNION ALL SELECT * FROM t2 " +
prep.setInt(1, 1); "UNION ALL SELECT X, 'Q' FROM SYSTEM_RANGE(?,?)");
prep.setInt(2, 2); prep.setInt(1, 1);
prep.setInt(3, 3); prep.setInt(2, 2);
prep.setInt(4, 4); prep.setInt(3, 3);
prep.setInt(5, 5); prep.setInt(4, 4);
prep.setInt(6, 6); prep.setInt(5, 5);
prep.setInt(6, 6);
rs = prep.executeQuery(); rs = prep.executeQuery();
for(int n: new int[]{1,2,3,4,5,6} ){ for(int n: new int[]{1,2,3,4,5,6} ){
assertTrue(rs.next()); assertTrue(rs.next());
assertEquals(n, rs.getInt(1)); assertEquals(n, rs.getInt(1));
} }
assertFalse(rs.next()); assertFalse(rs.next());
// call it twice // call it twice
rs = prep.executeQuery(); rs = prep.executeQuery();
for(int n: new int[]{1,2,3,4,5,6} ){ for(int n: new int[]{1,2,3,4,5,6} ){
assertTrue(rs.next()); assertTrue(rs.next());
assertEquals(n, rs.getInt(1)); assertEquals(n, rs.getInt(1));
} }
assertFalse(rs.next()); assertFalse(rs.next());
conn.close(); conn.close();
deleteDb("commonTableExpressionQueries"); deleteDb("commonTableExpressionQueries");
} }
private void testNumberedParameterizedQuery() throws Exception { private void testNumberedParameterizedQuery() throws Exception {
deleteDb("commonTableExpressionQueries"); deleteDb("commonTableExpressionQueries");
Connection conn = getConnection("commonTableExpressionQueries"); Connection conn = getConnection("commonTableExpressionQueries");
PreparedStatement prep; PreparedStatement prep;
ResultSet rs; ResultSet rs;
prep = conn.prepareStatement("WITH t1 AS (" prep = conn.prepareStatement("WITH t1 AS ("
+" SELECT R.X, 'T1' FROM SYSTEM_RANGE(?1,?2) R" +" SELECT R.X, 'T1' FROM SYSTEM_RANGE(?1,?2) R"
+")," +"),"
+"t2 AS (" +"t2 AS ("
+" SELECT R.X, 'T2' FROM SYSTEM_RANGE(?3,?4) R" +" SELECT R.X, 'T2' FROM SYSTEM_RANGE(?3,?4) R"
+") " +") "
+"SELECT * FROM t1 UNION ALL SELECT * FROM t2 UNION ALL SELECT X, 'Q' FROM SYSTEM_RANGE(?5,?6)"); +"SELECT * FROM t1 UNION ALL SELECT * FROM t2 UNION ALL SELECT X, 'Q' FROM SYSTEM_RANGE(?5,?6)");
prep.setInt(1, 1); prep.setInt(1, 1);
prep.setInt(2, 2); prep.setInt(2, 2);
prep.setInt(3, 3); prep.setInt(3, 3);
prep.setInt(4, 4); prep.setInt(4, 4);
prep.setInt(5, 5); prep.setInt(5, 5);
prep.setInt(6, 6); prep.setInt(6, 6);
rs = prep.executeQuery(); rs = prep.executeQuery();
for(int n: new int[]{1,2,3,4,5,6} ){ for (int n : new int[] { 1, 2, 3, 4, 5, 6 }) {
assertTrue(rs.next()); assertTrue(rs.next());
assertEquals(n, rs.getInt(1)); assertEquals(n, rs.getInt(1));
} }
assertEquals("X",rs.getMetaData().getColumnLabel(1)); assertEquals("X",rs.getMetaData().getColumnLabel(1));
assertEquals("'T1'",rs.getMetaData().getColumnLabel(2)); assertEquals("'T1'",rs.getMetaData().getColumnLabel(2));
...@@ -210,5 +212,5 @@ public class TestGeneralCommonTableQueries extends TestBase { ...@@ -210,5 +212,5 @@ public class TestGeneralCommonTableQueries extends TestBase {
conn.close(); conn.close();
deleteDb("commonTableExpressionQueries"); deleteDb("commonTableExpressionQueries");
} }
} }
...@@ -736,3 +736,4 @@ arbonaut exposing obscure determined turkey buildings indexhints acct ...@@ -736,3 +736,4 @@ arbonaut exposing obscure determined turkey buildings indexhints acct
choosing optimise arte preparator katzyn bla jenkins tot artes pgserver npe choosing optimise arte preparator katzyn bla jenkins tot artes pgserver npe
suffers closeablem mni significance vise identiy vitalus aka ilike uppercasing reentrant suffers closeablem mni significance vise identiy vitalus aka ilike uppercasing reentrant
aff ignite warm upstream producing sfu jit smtm affinity stashed tbl aff ignite warm upstream producing sfu jit smtm affinity stashed tbl
stumc numbered
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论