提交 e78ce27d authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Clear CTE views on parsing or preparation error

上级 e063498f
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
package org.h2.command; package org.h2.command;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import org.h2.api.DatabaseEventListener; import org.h2.api.DatabaseEventListener;
import org.h2.command.dml.Explain; import org.h2.command.dml.Explain;
import org.h2.command.dml.Query; import org.h2.command.dml.Query;
...@@ -27,6 +28,35 @@ public class CommandContainer extends Command { ...@@ -27,6 +28,35 @@ public class CommandContainer extends Command {
private boolean readOnlyKnown; private boolean readOnlyKnown;
private boolean readOnly; private boolean readOnly;
/**
* Clears CTE views for a specified statement.
*
* @param session the session
* @param prepared prepared statement
*/
static void clearCTE(Session session, Prepared prepared) {
List<TableView> cteCleanups = prepared.getCteCleanups();
if (cteCleanups != null) {
clearCTE(session, cteCleanups);
}
}
/**
* Clears CTE views.
*
* @param session the session
* @param views list of view
*/
static void clearCTE(Session session, List<TableView> views) {
for (TableView view : views) {
// check if view was previously deleted as their name is set to
// null
if (view.getName() != null) {
session.removeLocalTempTable(view);
}
}
}
CommandContainer(Session session, String sql, Prepared prepared) { CommandContainer(Session session, String sql, Prepared prepared) {
super(session, sql); super(session, sql);
prepared.setCommand(this); prepared.setCommand(this);
...@@ -123,15 +153,7 @@ public class CommandContainer extends Command { ...@@ -123,15 +153,7 @@ public class CommandContainer extends Command {
super.stop(); super.stop();
// Clean up after the command was run in the session. // Clean up after the command was run in the session.
// Must restart query (and dependency construction) to reuse. // Must restart query (and dependency construction) to reuse.
if (prepared.getCteCleanups() != null) { clearCTE(session, prepared);
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 @Override
......
...@@ -672,7 +672,12 @@ public class Parser { ...@@ -672,7 +672,12 @@ public class Parser {
if (!hasMore && currentTokenType != END) { if (!hasMore && currentTokenType != END) {
throw getSyntaxError(); throw getSyntaxError();
} }
p.prepare(); try {
p.prepare();
} catch (Throwable t) {
CommandContainer.clearCTE(session, p);
throw t;
}
Command c = new CommandContainer(session, sql, p); Command c = new CommandContainer(session, sql, p);
if (hasMore) { if (hasMore) {
String remaining = originalSQL.substring(parseIndex); String remaining = originalSQL.substring(parseIndex);
...@@ -6128,6 +6133,15 @@ public class Parser { ...@@ -6128,6 +6133,15 @@ public class Parser {
private Prepared parseWith() { private Prepared parseWith() {
List<TableView> viewsCreated = new ArrayList<>(); List<TableView> viewsCreated = new ArrayList<>();
try {
return parseWith1(viewsCreated);
} catch (Throwable t) {
CommandContainer.clearCTE(session, viewsCreated);
throw t;
}
}
private Prepared parseWith1(List<TableView> viewsCreated) {
readIf("RECURSIVE"); readIf("RECURSIVE");
// This WITH statement is not a temporary view - it is part of a persistent view // This WITH statement is not a temporary view - it is part of a persistent view
......
...@@ -146,5 +146,11 @@ WITH CTE_TEST AS (TABLE TEST) ((TABLE CTE_TEST)); ...@@ -146,5 +146,11 @@ WITH CTE_TEST AS (TABLE TEST) ((TABLE CTE_TEST));
> 1 2 > 1 2
> rows: 1 > rows: 1
WITH CTE_TEST AS (TABLE TEST) ((SELECT A, B FROM CTE_TEST2));
> exception TABLE_OR_VIEW_NOT_FOUND_1
WITH CTE_TEST AS (TABLE TEST) ((SELECT A, B, C FROM CTE_TEST));
> exception COLUMN_NOT_FOUND_1
DROP TABLE TEST; DROP TABLE TEST;
> ok > ok
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论