Skip to content

Commit

Permalink
Formatting, javadocs
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasmueller committed Jun 8, 2017
1 parent de02c02 commit f974e8b
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 67 deletions.
5 changes: 2 additions & 3 deletions h2/src/docsrc/help/help.csv
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ WITH [ RECURSIVE ] { name [( columnName [,...] )]
AS ( 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.
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.
Expand All @@ -213,8 +213,7 @@ SELECT sum(n) FROM t;
","
WITH t1 AS (
SELECT 1 AS FIRST_COLUMN
),
t2 AS (
), t2 AS (
SELECT FIRST_COLUMN+1 AS FIRST_COLUMN FROM t1
)
SELECT sum(FIRST_COLUMN) FROM t2;
Expand Down
2 changes: 1 addition & 1 deletion h2/src/main/org/h2/res/help.csv
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ WITH [ RECURSIVE ] { name [( columnName [,...] )]
AS ( 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","
ALTER INDEX [ IF EXISTS ] indexName RENAME TO newIndexName
","
Expand Down
2 changes: 1 addition & 1 deletion h2/src/main/org/h2/table/RangeTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public long getRowCount(Session session) {

@Override
public TableType getTableType() {
return TableType.SYSTEM_TABLE;
return TableType.SYSTEM_TABLE;
}

@Override
Expand Down
5 changes: 4 additions & 1 deletion h2/src/main/org/h2/table/TableView.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ private void initColumnsAndTables(Session session) {
// If it can't be compiled, then it's a 'zero column table'
// this avoids problems when creating the view when opening the
// 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)) {
this.isRecursiveQueryDetected = true;
}
Expand Down Expand Up @@ -673,6 +674,8 @@ public boolean equals(Object obj) {

/**
* Was query recursion detected during compiling.
*
* @return true if yes
*/
public boolean isRecursiveQueryDetected() {
return isRecursiveQueryDetected;
Expand Down
124 changes: 63 additions & 61 deletions h2/src/test/org/h2/test/db/TestGeneralCommonTableQueries.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
public class TestGeneralCommonTableQueries extends TestBase {

/**
/**
* Run just this test.
*
* @param a ignored
Expand Down Expand Up @@ -45,22 +45,22 @@ private void testSimple() throws Exception {
final String simple_two_column_query = "with " +
"t1(n) as (select 1 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);
assertTrue(rs.next());
assertEquals(1, rs.getInt(1));
assertTrue(rs.next());
assertEquals(2, rs.getInt(1));
assertFalse(rs.next());

prep = conn.prepareStatement(simple_two_column_query);
rs = prep.executeQuery();
assertTrue(rs.next());
assertEquals(1, rs.getInt(1));
assertTrue(rs.next());
assertEquals(2, rs.getInt(1));
assertFalse(rs.next());

prep = conn.prepareStatement("with " +
"t1(n) as (select 2 as first) " +
",t2(n) as (select 3 as first) " +
Expand All @@ -72,7 +72,7 @@ private void testSimple() throws Exception {
assertTrue(rs.next());
assertEquals(3, rs.getInt(1));
assertFalse(rs.next());

prep = conn.prepareStatement("with " +
"t1(n) as (select 2 as first) " +
",t2(n) as (select 3 as first) " +
Expand All @@ -85,7 +85,7 @@ private void testSimple() throws Exception {
assertTrue(rs.next());
assertEquals(3, rs.getInt(1));
assertFalse(rs.next());

conn.close();
deleteDb("commonTableExpressionQueries");
}
Expand All @@ -95,7 +95,7 @@ private void testImpliedColumnNames() throws Exception {
Connection conn = getConnection("commonTableExpressionQueries");
PreparedStatement prep;
ResultSet rs;

prep = conn.prepareStatement("with " +
"t1 as (select 2 as first_col) " +
",t2 as (select first_col+1 from t1) " +
Expand All @@ -110,98 +110,100 @@ private void testImpliedColumnNames() throws Exception {
assertFalse(rs.next());
assertEquals(rs.getMetaData().getColumnCount(),1);
assertEquals("FIRST_COL",rs.getMetaData().getColumnLabel(1));

conn.close();
deleteDb("commonTableExpressionQueries");
}

private void testChainedQuery() throws Exception {
deleteDb("commonTableExpressionQueries");
Connection conn = getConnection("commonTableExpressionQueries");
PreparedStatement prep;
ResultSet rs;

prep = conn.prepareStatement(" WITH t1 AS ("
+" SELECT 1 AS FIRST_COLUMN"
+"),"
+" t2 AS ("
+" SELECT FIRST_COLUMN+1 AS FIRST_COLUMN FROM t1 "
+") "
+"SELECT sum(FIRST_COLUMN) FROM t2");


prep = conn.prepareStatement(
" WITH t1 AS (" +
" SELECT 1 AS FIRST_COLUMN" +
")," +
" t2 AS (" +
" SELECT FIRST_COLUMN+1 AS FIRST_COLUMN FROM t1 " +
") " +
"SELECT sum(FIRST_COLUMN) FROM t2");

rs = prep.executeQuery();
assertTrue(rs.next());
assertEquals(2, rs.getInt(1));
assertFalse(rs.next());

conn.close();
deleteDb("commonTableExpressionQueries");
}
}

private void testParameterizedQuery() throws Exception {
deleteDb("commonTableExpressionQueries");
Connection conn = getConnection("commonTableExpressionQueries");
PreparedStatement prep;
ResultSet rs;

prep = conn.prepareStatement("WITH t1 AS ("
+" SELECT X, 'T1' FROM SYSTEM_RANGE(?,?)"
+"),"
+"t2 AS ("
+" SELECT X, 'T2' FROM SYSTEM_RANGE(?,?)"
+") "
+"SELECT * FROM t1 UNION ALL SELECT * FROM t2 UNION ALL SELECT X, 'Q' FROM SYSTEM_RANGE(?,?)");
prep.setInt(1, 1);
prep.setInt(2, 2);
prep.setInt(3, 3);
prep.setInt(4, 4);
prep.setInt(5, 5);
prep.setInt(6, 6);

prep = conn.prepareStatement("WITH t1 AS (" +
" SELECT X, 'T1' FROM SYSTEM_RANGE(?,?)" +
")," +
"t2 AS (" +
" SELECT X, 'T2' FROM SYSTEM_RANGE(?,?)" +
") " +
"SELECT * FROM t1 UNION ALL SELECT * FROM t2 " +
"UNION ALL SELECT X, 'Q' FROM SYSTEM_RANGE(?,?)");
prep.setInt(1, 1);
prep.setInt(2, 2);
prep.setInt(3, 3);
prep.setInt(4, 4);
prep.setInt(5, 5);
prep.setInt(6, 6);
rs = prep.executeQuery();

for(int n: new int[]{1,2,3,4,5,6} ){
assertTrue(rs.next());
assertEquals(n, rs.getInt(1));
assertTrue(rs.next());
assertEquals(n, rs.getInt(1));
}
assertFalse(rs.next());

// call it twice
rs = prep.executeQuery();

for(int n: new int[]{1,2,3,4,5,6} ){
assertTrue(rs.next());
assertEquals(n, rs.getInt(1));
assertTrue(rs.next());
assertEquals(n, rs.getInt(1));
}
assertFalse(rs.next());

conn.close();
deleteDb("commonTableExpressionQueries");
}

private void testNumberedParameterizedQuery() throws Exception {
deleteDb("commonTableExpressionQueries");
Connection conn = getConnection("commonTableExpressionQueries");
PreparedStatement prep;
ResultSet rs;

prep = conn.prepareStatement("WITH t1 AS ("
+" SELECT R.X, 'T1' FROM SYSTEM_RANGE(?1,?2) R"
+"),"
+"t2 AS ("
+" 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)");
prep.setInt(1, 1);
prep.setInt(2, 2);
prep.setInt(3, 3);
prep.setInt(4, 4);
prep.setInt(5, 5);
prep.setInt(6, 6);
+" SELECT R.X, 'T1' FROM SYSTEM_RANGE(?1,?2) R"
+"),"
+"t2 AS ("
+" 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)");
prep.setInt(1, 1);
prep.setInt(2, 2);
prep.setInt(3, 3);
prep.setInt(4, 4);
prep.setInt(5, 5);
prep.setInt(6, 6);
rs = prep.executeQuery();
for(int n: new int[]{1,2,3,4,5,6} ){
assertTrue(rs.next());
assertEquals(n, rs.getInt(1));

for (int n : new int[] { 1, 2, 3, 4, 5, 6 }) {
assertTrue(rs.next());
assertEquals(n, rs.getInt(1));
}
assertEquals("X",rs.getMetaData().getColumnLabel(1));
assertEquals("'T1'",rs.getMetaData().getColumnLabel(2));
Expand All @@ -210,5 +212,5 @@ private void testNumberedParameterizedQuery() throws Exception {

conn.close();
deleteDb("commonTableExpressionQueries");
}
}
}
1 change: 1 addition & 0 deletions h2/src/tools/org/h2/build/doc/dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -736,3 +736,4 @@ arbonaut exposing obscure determined turkey buildings indexhints acct
choosing optimise arte preparator katzyn bla jenkins tot artes pgserver npe
suffers closeablem mni significance vise identiy vitalus aka ilike uppercasing reentrant
aff ignite warm upstream producing sfu jit smtm affinity stashed tbl
stumc numbered

0 comments on commit f974e8b

Please sign in to comment.