提交 efaa313e authored 作者: Noel Grandin's avatar Noel Grandin

simplify and cleanup

Move the list of things to be cleaned up down to Prepared.
Store the list as a list of TableView rather than Runnable, we don't
need that much flexibility.
上级 d0d44836
......@@ -7,8 +7,6 @@ package org.h2.command;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.h2.api.ErrorCode;
import org.h2.engine.Constants;
import org.h2.engine.Database;
......@@ -47,8 +45,6 @@ public abstract class Command implements CommandInterface {
private boolean canReuse;
private List<Runnable> cleanupCallbacks;
Command(Parser parser, String sql) {
this.session = parser.getSession();
this.sql = sql;
......@@ -172,7 +168,6 @@ public abstract class Command implements CommandInterface {
trace.info("slow query: {0} ms", timeMillis);
}
}
session.commandCleanup(this);
}
/**
......@@ -375,14 +370,6 @@ public abstract class Command implements CommandInterface {
}
}
public void setCleanupCallbacks(List<Runnable> cleanupCallbacks) {
this.cleanupCallbacks = cleanupCallbacks;
}
public List<Runnable> getCleanupCallbacks() {
return cleanupCallbacks;
}
public void setCanReuse(boolean canReuse) {
this.canReuse = canReuse;
}
......
......@@ -12,6 +12,7 @@ import org.h2.command.dml.Query;
import org.h2.expression.Parameter;
import org.h2.expression.ParameterInterface;
import org.h2.result.ResultInterface;
import org.h2.table.TableView;
import org.h2.value.Value;
import org.h2.value.ValueNull;
......@@ -116,6 +117,27 @@ public class CommandContainer extends Command {
return result;
}
@Override
public void stop() {
super.stop();
// Clean up after the command was run in the session.
// Must restart query (and dependency construction) to reuse.
if (prepared.getCteCleanups() != null) {
for (TableView view : prepared.getCteCleanups()) {
// check if view was previously deleted as their name is set to
// null
if (view.getName() != null) {
session.removeLocalTempTable(view);
}
}
}
}
@Override
public boolean canReuse() {
return super.canReuse() && prepared.getCteCleanups() == null;
}
@Override
public boolean isReadOnly() {
if (!readOnlyKnown) {
......
......@@ -4914,25 +4914,6 @@ public class Parser {
return command;
}
private class WithCleanup implements Runnable {
TableView view;
Session session;
public WithCleanup(TableView view, Session session){
this.view = view;
this.session = session;
}
@Override
public void run() {
// check if view was previously deleted as their name is set to null
if(view.getName()!=null) {
session.removeLocalTempTable(view);
}
}
}
private Prepared parseWith() {
List<TableView> viewsCreated = new ArrayList<TableView>();
readIf("RECURSIVE");
......@@ -4978,18 +4959,10 @@ public class Parser {
WITH_STATEMENT_SUPPORTS_LIMITED_STATEMENTS);
}
List<Runnable> cleanupCallbacks = new ArrayList<Runnable>();
// clean up temp views starting with last to first (in case of dependencies)
// clean up temp views starting with last to first (in case of
// dependencies)
Collections.reverse(viewsCreated);
for (final TableView view : viewsCreated){
if(view==null){
continue;
}
cleanupCallbacks.add(new WithCleanup(view,session));
}
p.setCleanupCallbacks(cleanupCallbacks);
p.setCteCleanups(viewsCreated);
return p;
}
......
......@@ -7,7 +7,6 @@ package org.h2.command;
import java.util.ArrayList;
import java.util.List;
import org.h2.api.DatabaseEventListener;
import org.h2.api.ErrorCode;
import org.h2.engine.Database;
......@@ -17,6 +16,7 @@ import org.h2.expression.Parameter;
import org.h2.message.DbException;
import org.h2.message.Trace;
import org.h2.result.ResultInterface;
import org.h2.table.TableView;
import org.h2.util.StatementBuilder;
import org.h2.value.Value;
......@@ -57,7 +57,11 @@ public abstract class Prepared {
private int objectId;
private int currentRowNumber;
private int rowScanCount;
private List<Runnable> cleanupCallbacks;
/**
* Common table expressions (CTE) in queries require us to create temporary views,
* which need to be cleaned up once a command is done executing.
*/
private List<TableView> cteCleanups;
/**
* Create a new object.
......@@ -176,9 +180,6 @@ public abstract class Prepared {
*/
public void setCommand(Command command) {
this.command = command;
if(command!=null && cleanupCallbacks!=null){
command.setCleanupCallbacks(cleanupCallbacks);
}
}
/**
......@@ -440,13 +441,17 @@ public abstract class Prepared {
return false;
}
public List<Runnable> getCleanupCallbacks() {
return cleanupCallbacks;
/**
* Get the temporary views created for CTE's.
*/
public List<TableView> getCteCleanups() {
return cteCleanups;
}
public void setCleanupCallbacks(List<Runnable> cleanupCallbacks) {
this.cleanupCallbacks = cleanupCallbacks;
/**
* Set the temporary views created for CTE's.
*/
public void setCteCleanups(List<TableView> cteCleanups) {
this.cteCleanups = cteCleanups;
}
}
......@@ -1707,22 +1707,6 @@ public class Session extends SessionWithState {
tablesToAnalyze.add(table);
}
/**
* Clean up after the command was run in the session
*
* @param command the command to cleanup for
*/
public void commandCleanup(Command command) {
if (command.getCleanupCallbacks()!=null){
for(Runnable eachCleanup : command.getCleanupCallbacks()){
eachCleanup.run();
// clean up done - must restart query (and dependency construction) to reuse
command.setCanReuse(false);
}
command.getCleanupCallbacks().clear();
}
}
/**
* Represents a savepoint (a position in a transaction to where one can roll
* back to).
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论