提交 3e77d2fb authored 作者: Noel Grandin's avatar Noel Grandin

couple of cleanups to the CTE merge

上级 5a428e25
......@@ -21,6 +21,8 @@ Change Log
<h2>Next Version (unreleased)</h2>
<ul>
<li>Issue#479 Allow non-recursive CTEs (WITH statements), patch from stumc
</li>
<li>Fix startup issue when using "CHECK" as a column name.
</li>
<li>Issue #423: ANALYZE performed multiple times on one table during execution of the same statement.
......
......@@ -18,7 +18,6 @@ import java.util.Comparator;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import org.h2.api.ErrorCode;
import org.h2.api.Trigger;
import org.h2.command.ddl.AlterIndexRename;
......@@ -4859,9 +4858,9 @@ public class Parser {
private Query parseWith() {
List<TableView> viewsCreated = new ArrayList<TableView>();
readIf("RECURSIVE");
do{
do {
viewsCreated.add(parseSingleCommonTableExpression());
} while(readIf(","));
} while (readIf(","));
Query q = parseSelectUnion();
q.setPrepareAlways(true);
......@@ -4877,10 +4876,11 @@ public class Parser {
// column names are now optional - they can be inferred from the named
// query if not supplied
if(readIf("(")){
if (readIf("(")) {
cols = parseColumnList();
for (String c : cols) {
// we dont really know the type of the column, so string will have to do
// we dont really know the type of the column, so string will
// have to do
columns.add(new Column(c, Value.STRING));
}
}
......@@ -4898,7 +4898,8 @@ public class Parser {
session.removeLocalTempTable(old);
}
// this table is created as a work around because recursive
// table expressions need to reference something that look like themselves
// table expressions need to reference something that look like
// themselves
// to work (its removed after creation in this method)
CreateTableData data = new CreateTableData();
data.id = database.allocateObjectId();
......@@ -4922,26 +4923,26 @@ public class Parser {
querySQL = StringUtils.cache(withQuery.getPlanSQL());
ArrayList<Expression> withExpressions = withQuery.getExpressions();
for (int i = 0; i < withExpressions.size(); ++i) {
String columnName = cols != null ? cols[i] : withExpressions.get(i).getColumnName();
columnTemplateList.add(new Column(columnName, withExpressions.get(i).getType()));
String columnName = cols != null ? cols[i]
: withExpressions.get(i).getColumnName();
columnTemplateList.add(new Column(columnName,
withExpressions.get(i).getType()));
}
} finally {
session.removeLocalTempTable(recursiveTable);
}
int id = database.allocateObjectId();
boolean isRecursive = true;
TableView view = null;
do{
view = new TableView(schema, id, tempViewName, querySQL,
// No easy way to determine if this is a recursive query up front, so we just compile
// it twice - once without the flag set, and if we didn't see a recursive term,
// then we just compile it again.
TableView view = new TableView(schema, id, tempViewName, querySQL,
parameters, columnTemplateList.toArray(new Column[0]), session,
isRecursive);
if(view.isRecursiveQueryDetected()!=isRecursive){
isRecursive = view.isRecursiveQueryDetected();
view = null;
continue;
true/* recursive */);
if (!view.isRecursiveQueryDetected()) {
view = new TableView(schema, id, tempViewName, querySQL, parameters,
columnTemplateList.toArray(new Column[0]), session,
false/* recursive */);
}
} while(view==null);
view.setTableExpression(true);
view.setTemporary(true);
session.addLocalTempTable(view);
......
......@@ -205,12 +205,11 @@ public class TableView extends Table {
} catch (DbException e) {
e.addSQL(getCreateSQL());
createException = e;
// 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
// database
// if it can not be compiled - it could also be a recursive common table expression query
if(isRecursiveQueryExceptionDetected(createException)){
// database.
// If it can not be compiled - it could also be a recursive common table expression query.
if (isRecursiveQueryExceptionDetected(createException)) {
this.isRecursiveQueryDetected = true;
}
tables = New.arrayList();
......@@ -674,25 +673,23 @@ public class TableView extends Table {
}
/**
* If query recursion is detected (for recursion detection)
* @return is Recursive Query Flag Set
* Was query recursion detected during compiling.
*/
public boolean isRecursiveQueryDetected(){
public boolean isRecursiveQueryDetected() {
return isRecursiveQueryDetected;
}
/**
* If query an exception indicates query recursion
* @return is Recursive Query Exception Detected
* Does exception indicate query recursion?
*/
private boolean isRecursiveQueryExceptionDetected(DbException exception){
if (exception==null){
private boolean isRecursiveQueryExceptionDetected(DbException exception) {
if (exception == null) {
return false;
}
if (exception.getErrorCode()!=ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1){
if (exception.getErrorCode() != ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1) {
return false;
}
if (! exception.getMessage().contains("\""+this.getName()+"\"")){
if (!exception.getMessage().contains("\"" + this.getName() + "\"")) {
return false;
}
return true;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论