提交 26bcb6df authored 作者: Owner's avatar Owner

Issue#479 Made column names optional

上级 b28ee4f9
...@@ -18,6 +18,7 @@ import java.util.Comparator; ...@@ -18,6 +18,7 @@ import java.util.Comparator;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
import org.h2.api.ErrorCode; import org.h2.api.ErrorCode;
import org.h2.api.Trigger; import org.h2.api.Trigger;
...@@ -4906,7 +4907,10 @@ public class Parser { ...@@ -4906,7 +4907,10 @@ public class Parser {
querySQL = StringUtils.cache(withQuery.getPlanSQL()); querySQL = StringUtils.cache(withQuery.getPlanSQL());
ArrayList<Expression> withExpressions = withQuery.getExpressions(); ArrayList<Expression> withExpressions = withQuery.getExpressions();
for (int i = 0; i < withExpressions.size(); ++i) { for (int i = 0; i < withExpressions.size(); ++i) {
String columnName = cols !=null ? cols[i] : withExpressions.get(i).getColumnName(); System.out.println("columnName="+withExpressions.get(i).getColumnName());
System.out.println("alias="+withExpressions.get(i).getAlias());
System.out.println("nonAliasExpression="+withExpressions.get(i).getNonAliasExpression());
String columnName = cols != null ? cols[i] : withExpressions.get(i).getColumnName();
columnTemplateList.add(new Column(columnName, withExpressions.get(i).getType())); columnTemplateList.add(new Column(columnName, withExpressions.get(i).getType()));
} }
} finally { } finally {
...@@ -4915,7 +4919,7 @@ public class Parser { ...@@ -4915,7 +4919,7 @@ public class Parser {
int id = database.allocateObjectId(); int id = database.allocateObjectId();
boolean isRecursive = RecursiveQuery.isRecursive(tempViewName,querySQL); boolean isRecursive = RecursiveQuery.isRecursive(tempViewName,querySQL);
System.out.println("tempViewName=>"+tempViewName+"<"); System.out.println("tempViewName=>"+tempViewName+"<");
System.out.println("columnTemplateList="+columnTemplateList); System.out.println("columnTemplateList="+columnTemplateList.stream().map(Column::toStringWithType).collect(Collectors.toList()));
System.out.println("isRecursive="+isRecursive); System.out.println("isRecursive="+isRecursive);
System.out.println("querySQL="+querySQL); System.out.println("querySQL="+querySQL);
TableView view = new TableView(schema, id, tempViewName, querySQL, TableView view = new TableView(schema, id, tempViewName, querySQL,
......
...@@ -7,21 +7,15 @@ public class RecursiveQuery { ...@@ -7,21 +7,15 @@ public class RecursiveQuery {
// A query is recursive if it references it's own name in its definition // A query is recursive if it references it's own name in its definition
public static boolean isRecursive(String tempViewName, String querySQL) { public static boolean isRecursive(String tempViewName, String querySQL) {
// ?i is case insensitive
// ?m is multi-line search
// ?d is Unix line endings
String pattern = "(?i)(?m)(?d).*\\b("+tempViewName+")\\b";
System.out.println("pattern="+pattern);
boolean stringContains = querySQL.contains(tempViewName);
System.out.println("stringContains="+stringContains);
boolean foundAny = RecursiveQuery.foundAny(tempViewName,querySQL); boolean foundAny = RecursiveQuery.foundAny(tempViewName,querySQL);
System.out.println("foundAny="+foundAny); System.out.println("foundAny="+foundAny);
boolean patternMatch = Pattern.matches(pattern,querySQL); return foundAny;
System.out.println("patternMatch="+patternMatch);
return patternMatch||stringContains|| foundAny;
} }
private static boolean foundAny(String tempViewName, String querySQL){ private static boolean foundAny(String tempViewName, String querySQL){
// ?i is case insensitive
// ?m is multi-line search
// ?d is Unix line endings
Pattern p = Pattern.compile("(?i)(?m)(?d)\\b("+tempViewName+")\\b"); Pattern p = Pattern.compile("(?i)(?m)(?d)\\b("+tempViewName+")\\b");
Matcher m = p.matcher(querySQL); Matcher m = p.matcher(querySQL);
while (m.find()) { while (m.find()) {
......
...@@ -738,6 +738,9 @@ public class Column { ...@@ -738,6 +738,9 @@ public class Column {
public String toString() { public String toString() {
return name; return name;
} }
public String toStringWithType() {
return DataType.getTypeClassName(type)+":"+name;
}
/** /**
* Check whether the new column is of the same type and not more restricted * Check whether the new column is of the same type and not more restricted
......
...@@ -32,6 +32,11 @@ public class TestGeneralCommonTableQueries extends TestBase { ...@@ -32,6 +32,11 @@ public class TestGeneralCommonTableQueries extends TestBase {
",t3(n) as (select 4 as first) " + ",t3(n) as (select 4 as first) " +
"select * from t1 union all select * from t2 union all select * from t3 where n<>?"; "select * from t1 union all select * from t2 union all select * from t3 where n<>?";
private static final String PARAMETERIZED_THREE_COMMON_QUERY_IMPLIED_COLUMN_NAMES = "with " +
"t1 as (select 2 as first_col) " +
",t2 as (select first_col+1 from t1) " +
",t3 as (select 4 as first_col) " +
"select * from t1 union all select * from t2 union all select * from t3 where first_col<>?";
/** /**
* Run just this test. * Run just this test.
* *
...@@ -45,6 +50,7 @@ public class TestGeneralCommonTableQueries extends TestBase { ...@@ -45,6 +50,7 @@ public class TestGeneralCommonTableQueries extends TestBase {
@Override @Override
public void test() throws Exception { public void test() throws Exception {
testSimple(); testSimple();
testImpliedColumnNames();
} }
private void testSimple() throws Exception { private void testSimple() throws Exception {
...@@ -92,4 +98,24 @@ public class TestGeneralCommonTableQueries extends TestBase { ...@@ -92,4 +98,24 @@ public class TestGeneralCommonTableQueries extends TestBase {
deleteDb("commonTableExpressionQueries"); deleteDb("commonTableExpressionQueries");
} }
private void testImpliedColumnNames() throws Exception {
deleteDb("commonTableExpressionQueries");
Connection conn = getConnection("commonTableExpressionQueries");
PreparedStatement prep;
ResultSet rs;
prep = conn.prepareStatement(PARAMETERIZED_THREE_COMMON_QUERY_IMPLIED_COLUMN_NAMES);
prep.setInt(1, 4); // omit 4 line (last)
rs = prep.executeQuery();
assertTrue(rs.next());
assertEquals(2, rs.getInt(1));
assertTrue(rs.next());
assertEquals(3, rs.getInt("FIRST_COL"));
assertFalse(rs.next());
assertEquals("rsMeta0: columns=1",rs.getMetaData().toString());
assertEquals("FIRST_COL",rs.getMetaData().getColumnLabel(1));
conn.close();
deleteDb("commonTableExpressionQueries");
}
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论