提交 1057b352 authored 作者: Vakhurin Sergey's avatar Vakhurin Sergey

fix recursive queries: support parameters

上级 9160f508
......@@ -220,6 +220,7 @@ public class Parser {
private boolean recompileAlways;
private ArrayList<Parameter> indexedParameterList;
private int orderInFrom;
private ArrayList<Parameter> suppliedParameterList;
public Parser(Session session) {
this.database = session.getDatabase();
......@@ -311,7 +312,7 @@ public class Parser {
currentPrepared = null;
createView = null;
recompileAlways = false;
indexedParameterList = null;
indexedParameterList = suppliedParameterList;
read();
return parsePrepared();
}
......@@ -4757,7 +4758,7 @@ public class Parser {
}
int id = database.allocateObjectId();
TableView view = new TableView(schema, id, tempViewName, querySQL,
null, cols, session, true);
parameters, cols, session, true);
view.setTableExpression(true);
view.setTemporary(true);
session.addLocalTempTable(view);
......@@ -6099,6 +6100,10 @@ public class Parser {
this.rightsChecked = rightsChecked;
}
public void setSuppliedParameterList(ArrayList<Parameter> suppliedParameterList) {
this.suppliedParameterList = suppliedParameterList;
}
/**
* Parse a SQL code snippet that represents an expression.
*
......
......@@ -9,6 +9,7 @@ import java.util.ArrayList;
import java.util.HashSet;
import java.util.concurrent.TimeUnit;
import org.h2.api.ErrorCode;
import org.h2.command.Parser;
import org.h2.command.Prepared;
import org.h2.command.dml.Query;
import org.h2.command.dml.SelectUnion;
......@@ -184,7 +185,10 @@ public class ViewIndex extends BaseIndex implements SpatialIndex {
return new ViewCursor(this, recResult, first, last);
}
if (query == null) {
query = (Query) createSession.prepare(querySQL, true);
Parser parser = new Parser(createSession);
parser.setRightsChecked(true);
parser.setSuppliedParameterList(originalParameters);
query = (Query) parser.prepare(querySQL);
}
if (!query.isUnion()) {
throw DbException.get(ErrorCode.SYNTAX_ERROR_2,
......
......@@ -123,6 +123,21 @@ public class TestRecursiveQueries extends TestBase {
assertEquals(103, rs.getInt(1));
assertFalse(rs.next());
prep = conn.prepareStatement("with recursive t(n) as " +
"(select ? union all select n+? from t where n<?) " +
"select * from t");
prep.setInt(1, 10);
prep.setInt(2, 2);
prep.setInt(3, 14);
rs = prep.executeQuery();
assertResultSetOrdered(rs, new String[][]{{"10"}, {"12"}, {"14"}});
prep.setInt(1, 100);
prep.setInt(2, 3);
prep.setInt(3, 103);
rs = prep.executeQuery();
assertResultSetOrdered(rs, new String[][]{{"100"}, {"103"}});
conn.close();
deleteDb("recursiveQueries");
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论