提交 23d020cc authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Fix CommandList with DDL commands

上级 f5c3bd9d
...@@ -17,13 +17,19 @@ import org.h2.result.ResultInterface; ...@@ -17,13 +17,19 @@ import org.h2.result.ResultInterface;
*/ */
class CommandList extends Command { class CommandList extends Command {
private final ArrayList<CommandContainer> commands; private CommandContainer command;
private final ArrayList<Prepared> commands;
private final ArrayList<Parameter> parameters; private final ArrayList<Parameter> parameters;
private String remaining;
private Command remainingCommand;
CommandList(Session session, String sql, ArrayList<CommandContainer> commands, ArrayList<Parameter> parameters) { CommandList(Session session, String sql, CommandContainer command, ArrayList<Prepared> commands,
ArrayList<Parameter> parameters, String remaining) {
super(session, sql); super(session, sql);
this.command = command;
this.commands = commands; this.commands = commands;
this.parameters = parameters; this.parameters = parameters;
this.remaining = remaining;
} }
@Override @Override
...@@ -32,38 +38,58 @@ class CommandList extends Command { ...@@ -32,38 +38,58 @@ class CommandList extends Command {
} }
private void executeRemaining() { private void executeRemaining() {
for (int i = 1, l = commands.size(); i < l; i++) { for (Prepared prepared : commands) {
CommandContainer command = commands.get(i); prepared.prepare();
if (command.isQuery()) { if (prepared.isQuery()) {
command.query(0); prepared.query(0);
} else { } else {
command.update(); prepared.update();
}
}
if (remaining != null) {
remainingCommand = session.prepareLocal(remaining);
remaining = null;
if (remainingCommand.isQuery()) {
remainingCommand.query(0);
} else {
remainingCommand.update();
} }
} }
} }
@Override @Override
public int update() { public int update() {
int updateCount = commands.get(0).executeUpdate(false).getUpdateCount(); int updateCount = command.executeUpdate(false).getUpdateCount();
executeRemaining(); executeRemaining();
return updateCount; return updateCount;
} }
@Override @Override
public void prepareJoinBatch() { public void prepareJoinBatch() {
commands.get(0).prepareJoinBatch(); command.prepareJoinBatch();
} }
@Override @Override
public ResultInterface query(int maxrows) { public ResultInterface query(int maxrows) {
ResultInterface result = commands.get(0).query(maxrows); ResultInterface result = command.query(maxrows);
executeRemaining(); executeRemaining();
return result; return result;
} }
@Override
public void stop() {
command.stop();
for (Prepared prepared : commands) {
CommandContainer.clearCTE(session, prepared);
}
if (remainingCommand != null) {
remainingCommand.stop();
}
}
@Override @Override
public boolean isQuery() { public boolean isQuery() {
return commands.get(0).isQuery(); return command.isQuery();
} }
@Override @Override
...@@ -78,12 +104,12 @@ class CommandList extends Command { ...@@ -78,12 +104,12 @@ class CommandList extends Command {
@Override @Override
public ResultInterface queryMeta() { public ResultInterface queryMeta() {
return commands.get(0).queryMeta(); return command.queryMeta();
} }
@Override @Override
public int getCommandType() { public int getCommandType() {
return commands.get(0).getCommandType(); return command.getCommandType();
} }
} }
...@@ -686,24 +686,44 @@ public class Parser { ...@@ -686,24 +686,44 @@ public class Parser {
} }
} }
private CommandList prepareCommandList(CommandContainer c, String sql, String remaining) { private CommandList prepareCommandList(CommandContainer command, String sql, String remaining) {
ArrayList<CommandContainer> list = Utils.newSmallArrayList(); try {
list.add(c); ArrayList<Prepared> list = Utils.newSmallArrayList();
do { boolean stop = false;
suppliedParameters = parameters; do {
suppliedParameterList = indexedParameterList; if (stop) {
try { return new CommandList(session, sql, command, list, parameters, remaining);
c = prepareSingleCommand(remaining);
} catch (Throwable t) {
for (CommandContainer cc : list) {
cc.clearCTE();
} }
throw t; suppliedParameters = parameters;
} suppliedParameterList = indexedParameterList;
list.add(c); Prepared p;
} while (currentTokenType == SEMICOLON try {
&& !StringUtils.isWhitespaceOrEmpty(remaining = originalSQL.substring(parseIndex))); p = parse(remaining);
return new CommandList(session, sql, list, parameters); } catch (DbException ex) {
// This command may depend on results of previous commands.
if (ex.getErrorCode() == ErrorCode.CANNOT_MIX_INDEXED_AND_UNINDEXED_PARAMS) {
throw ex;
}
return new CommandList(session, sql, command, list, parameters, remaining);
}
if (p instanceof DefineCommand) {
// Next commands may depend on results of this command.
stop = true;
}
list.add(p);
if (currentTokenType == END) {
break;
}
if (currentTokenType != SEMICOLON) {
addExpected(SEMICOLON);
throw getSyntaxError();
}
} while (!StringUtils.isWhitespaceOrEmpty(remaining = originalSQL.substring(parseIndex)));
return new CommandList(session, sql, command, list, parameters, null);
} catch (Throwable t) {
command.clearCTE();
throw t;
}
} }
private CommandContainer prepareSingleCommand(String sql) { private CommandContainer prepareSingleCommand(String sql) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论