提交 d1ca8df3 authored 作者: Owner's avatar Owner

Code review feedback from grandinj

上级 bf03bc6d
...@@ -5167,9 +5167,8 @@ public class Parser { ...@@ -5167,9 +5167,8 @@ public class Parser {
// 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
// as in CREATE VIEW abc AS WITH my_cte - this auto detects that condition // as in CREATE VIEW abc AS WITH my_cte - this auto detects that condition
if(session.isParsingView()){ if(session.isParsingCreateView()){
isPersistent = true; isPersistent = true;
System.out.println("getParsingViewName="+session.getParsingViewName());
} }
do { do {
...@@ -5177,6 +5176,10 @@ public class Parser { ...@@ -5177,6 +5176,10 @@ public class Parser {
} while (readIf(",")); } while (readIf(","));
Prepared p = null; Prepared p = null;
// reverse the order of constructed CTE views - as the destruction order
// (since later created view may depend on previously created views -
// we preserve that dependency order in the destruction sequence )
// used in setCteCleanups
Collections.reverse(viewsCreated); Collections.reverse(viewsCreated);
if(isToken("SELECT")) { if(isToken("SELECT")) {
...@@ -5224,13 +5227,12 @@ public class Parser { ...@@ -5224,13 +5227,12 @@ public class Parser {
} }
private TableView parseSingleCommonTableExpression(boolean isPersistent) { private TableView parseSingleCommonTableExpression(boolean isPersistent) {
Session targetSession = session;
String cteViewName = readIdentifierWithSchema(); String cteViewName = readIdentifierWithSchema();
Schema schema = getSchema(); Schema schema = getSchema();
Table recursiveTable=null; Table recursiveTable=null;
ArrayList<Column> columns = New.arrayList(); ArrayList<Column> columns = New.arrayList();
String[] cols = null; String[] cols = null;
Database db = targetSession.getDatabase(); Database db = session.getDatabase();
// column names are now optional - they can be inferred from the named // column names are now optional - they can be inferred from the named
// query, if not supplied by user // query, if not supplied by user
...@@ -5245,10 +5247,10 @@ public class Parser { ...@@ -5245,10 +5247,10 @@ public class Parser {
Table oldViewFound = null; Table oldViewFound = null;
if(isPersistent){ if(isPersistent){
oldViewFound = getSchema().findTableOrView(targetSession, cteViewName); oldViewFound = getSchema().findTableOrView(session, cteViewName);
} }
else{ else{
oldViewFound = targetSession.findLocalTempTable(cteViewName); oldViewFound = session.findLocalTempTable(cteViewName);
} }
// this persistent check conflicts with check 10 lines down // this persistent check conflicts with check 10 lines down
if (oldViewFound != null) { if (oldViewFound != null) {
...@@ -5262,11 +5264,11 @@ public class Parser { ...@@ -5262,11 +5264,11 @@ public class Parser {
cteViewName); cteViewName);
} }
if(isPersistent){ if(isPersistent){
oldViewFound.lock(targetSession, true, true); oldViewFound.lock(session, true, true);
targetSession.getDatabase().removeSchemaObject(targetSession, oldViewFound); session.getDatabase().removeSchemaObject(session, oldViewFound);
}else{ }else{
targetSession.removeLocalTempTable(oldViewFound); session.removeLocalTempTable(oldViewFound);
} }
oldViewFound=null; oldViewFound=null;
} }
...@@ -5276,7 +5278,7 @@ public class Parser { ...@@ -5276,7 +5278,7 @@ public class Parser {
// to work (its removed after creation in this method) // to work (its removed after creation in this method)
// only create table data and table if we don't have a working CTE already // only create table data and table if we don't have a working CTE already
if(oldViewFound == null){ if(oldViewFound == null){
recursiveTable = createShadowTableForRecursiveTableExpression(isPersistent, targetSession, cteViewName, recursiveTable = createShadowTableForRecursiveTableExpression(isPersistent, session, cteViewName,
schema, columns, db); schema, columns, db);
} }
List<Column> columnTemplateList; List<Column> columnTemplateList;
...@@ -5286,20 +5288,20 @@ public class Parser { ...@@ -5286,20 +5288,20 @@ public class Parser {
read("("); read("(");
Query withQuery = parseSelect(); Query withQuery = parseSelect();
if(isPersistent){ if(isPersistent){
withQuery.session = targetSession; withQuery.session = session;
} }
read(")"); read(")");
columnTemplateList = createQueryColumnTemplateList(cols, withQuery, querySQLOutput); columnTemplateList = createQueryColumnTemplateList(cols, withQuery, querySQLOutput);
} finally { } finally {
destroyShadowTableForRecursiveExpression(isPersistent, targetSession, recursiveTable); destroyShadowTableForRecursiveExpression(isPersistent, session, recursiveTable);
} }
TableView view = createCTEView(cteViewName, TableView view = createCTEView(cteViewName,
querySQLOutput[0], columnTemplateList, querySQLOutput[0], columnTemplateList,
true/* allowRecursiveQueryDetection */, true/* allowRecursiveQueryDetection */,
true/* add to session */, true/* add to session */,
isPersistent, targetSession); isPersistent, session);
return view; return view;
} }
...@@ -5452,12 +5454,12 @@ public class Parser { ...@@ -5452,12 +5454,12 @@ public class Parser {
read("AS"); read("AS");
try { try {
Query query; Query query;
session.setParsingView(true,viewName); session.setParsingCreateView(true,viewName);
try { try {
query = parseSelect(); query = parseSelect();
query.prepare(); query.prepare();
} finally { } finally {
session.setParsingView(false,viewName); session.setParsingCreateView(false,viewName);
} }
command.setSelect(query); command.setSelect(query);
} catch (DbException e) { } catch (DbException e) {
......
...@@ -77,7 +77,6 @@ public class DropView extends SchemaCommand { ...@@ -77,7 +77,6 @@ public class DropView extends SchemaCommand {
view.lock(session, true, true); view.lock(session, true, true);
session.getDatabase().removeSchemaObject(session, view); session.getDatabase().removeSchemaObject(session, view);
session.getDatabase().unlockMeta(session);
// remove dependent table expressions // remove dependent table expressions
for( Table childTable: copyOfDependencies){ for( Table childTable: copyOfDependencies){
...@@ -88,6 +87,8 @@ public class DropView extends SchemaCommand { ...@@ -88,6 +87,8 @@ public class DropView extends SchemaCommand {
} }
} }
} }
// make sure its all unlocked
session.getDatabase().unlockMeta(session);
} }
return 0; return 0;
} }
......
...@@ -875,7 +875,7 @@ public class Select extends Query { ...@@ -875,7 +875,7 @@ public class Select extends Query {
isQuickAggregateQuery = isEverything(optimizable); isQuickAggregateQuery = isEverything(optimizable);
} }
} }
cost = preparePlan(session.isParsingView()); cost = preparePlan(session.isParsingCreateView());
if (distinct && session.getDatabase().getSettings().optimizeDistinct && if (distinct && session.getDatabase().getSettings().optimizeDistinct &&
!isGroupQuery && filters.size() == 1 && !isGroupQuery && filters.size() == 1 &&
expressions.size() == 1 && condition == null) { expressions.size() == 1 && condition == null) {
...@@ -1087,7 +1087,7 @@ public class Select extends Query { ...@@ -1087,7 +1087,7 @@ public class Select extends Query {
for (TableFilter f : topFilters) { for (TableFilter f : topFilters) {
Table t = f.getTable(); Table t = f.getTable();
TableView tableView = (t instanceof TableView) ? (TableView) t : null; TableView tableView = (t instanceof TableView) ? (TableView) t : null;
if (tableView!=null && tableView.isView() && tableView.isRecursive() && tableView.isTableExpression()) { if (tableView!=null && tableView.isRecursive() && tableView.isTableExpression()) {
if(tableView.isPersistent()){ if(tableView.isPersistent()){
// skip the generation of plan SQL for this already recursive persistent ctes, since using a with // skip the generation of plan SQL for this already recursive persistent ctes, since using a with
......
...@@ -1878,7 +1878,6 @@ public class Database implements DataHandler { ...@@ -1878,7 +1878,6 @@ public class Database implements DataHandler {
int type = obj.getType(); int type = obj.getType();
if (type == DbObject.TABLE_OR_VIEW) { if (type == DbObject.TABLE_OR_VIEW) {
Table table = (Table) obj; Table table = (Table) obj;
//table.setBeingDropped(true);
if (table.isTemporary() && !table.isGlobalTemporary()) { if (table.isTemporary() && !table.isGlobalTemporary()) {
session.removeLocalTempTable(table); session.removeLocalTempTable(table);
return; return;
...@@ -1918,7 +1917,6 @@ public class Database implements DataHandler { ...@@ -1918,7 +1917,6 @@ public class Database implements DataHandler {
} }
removeMeta(session, id); removeMeta(session, id);
return;
} }
} }
......
...@@ -6,7 +6,6 @@ ...@@ -6,7 +6,6 @@
package org.h2.engine; package org.h2.engine;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.EmptyStackException;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
...@@ -229,7 +228,7 @@ public class Session extends SessionWithState { ...@@ -229,7 +228,7 @@ public class Session extends SessionWithState {
return subQueryInfo; return subQueryInfo;
} }
public void setParsingView(boolean parsingView, String viewName) { public void setParsingCreateView(boolean parsingView, String viewName) {
// It can be recursive, thus implemented as counter. // It can be recursive, thus implemented as counter.
this.parsingView += parsingView ? 1 : -1; this.parsingView += parsingView ? 1 : -1;
assert this.parsingView >= 0; assert this.parsingView >= 0;
...@@ -241,16 +240,14 @@ public class Session extends SessionWithState { ...@@ -241,16 +240,14 @@ public class Session extends SessionWithState {
viewNameStack.pop(); viewNameStack.pop();
} }
} }
public String getParsingViewName() { public String getParsingCreateViewName() {
try{ if(viewNameStack.size()==0){
return viewNameStack.peek();
}
catch(EmptyStackException e){
return null; return null;
} }
return viewNameStack.peek();
} }
public boolean isParsingView() { public boolean isParsingCreateView() {
assert parsingView >= 0; assert parsingView >= 0;
return parsingView != 0; return parsingView != 0;
} }
......
...@@ -161,6 +161,7 @@ public abstract class Table extends SchemaObjectBase { ...@@ -161,6 +161,7 @@ public abstract class Table extends SchemaObjectBase {
* @param key the primary key * @param key the primary key
* @return the row * @return the row
*/ */
@SuppressWarnings("unused")
public Row getRow(Session session, long key) { public Row getRow(Session session, long key) {
return null; return null;
} }
......
...@@ -112,11 +112,11 @@ public class TableView extends Table { ...@@ -112,11 +112,11 @@ public class TableView extends Table {
private Query compileViewQuery(Session session, String sql, boolean literalsChecked, String viewName) { private Query compileViewQuery(Session session, String sql, boolean literalsChecked, String viewName) {
Prepared p; Prepared p;
session.setParsingView(true,viewName); session.setParsingCreateView(true,viewName);
try { try {
p = session.prepare(sql, false, literalsChecked); p = session.prepare(sql, false, literalsChecked);
} finally { } finally {
session.setParsingView(false,viewName); session.setParsingCreateView(false,viewName);
} }
if (!(p instanceof Query)) { if (!(p instanceof Query)) {
throw DbException.getSyntaxError(sql, 0); throw DbException.getSyntaxError(sql, 0);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论