提交 48942c18 authored 作者: S.Vladykin's avatar S.Vladykin

create or replace view fixed

上级 b3eb9567
...@@ -31,7 +31,7 @@ public class AlterView extends DefineCommand { ...@@ -31,7 +31,7 @@ public class AlterView extends DefineCommand {
public int update() { public int update() {
session.commit(true); session.commit(true);
session.getUser().checkRight(view, Right.ALL); session.getUser().checkRight(view, Right.ALL);
DbException e = view.recompile(session, false); DbException e = view.recompile(session, false, true);
if (e != null) { if (e != null) {
throw e; throw e;
} }
......
...@@ -811,7 +811,7 @@ public class Database implements DataHandler { ...@@ -811,7 +811,7 @@ public class Database implements DataHandler {
if (obj instanceof TableView) { if (obj instanceof TableView) {
TableView view = (TableView) obj; TableView view = (TableView) obj;
if (view.isInvalid()) { if (view.isInvalid()) {
view.recompile(session, true); view.recompile(session, true, false);
if (!view.isInvalid()) { if (!view.isInvalid()) {
recompileSuccessful = true; recompileSuccessful = true;
} }
...@@ -819,17 +819,7 @@ public class Database implements DataHandler { ...@@ -819,17 +819,7 @@ public class Database implements DataHandler {
} }
} }
} while (recompileSuccessful); } while (recompileSuccessful);
// when opening a database, views are initialized before indexes, TableView.clearIndexCaches(session.getDatabase());
// so they may not have the optimal plan yet
// this is not a problem, it is just nice to see the newest plan
for (Table obj : getAllTablesAndViews(false)) {
if (obj instanceof TableView) {
TableView view = (TableView) obj;
if (!view.isInvalid()) {
view.recompile(systemSession, true);
}
}
}
} }
private void initMetaTables() { private void initMetaTables() {
......
...@@ -13,6 +13,7 @@ import org.h2.api.ErrorCode; ...@@ -13,6 +13,7 @@ import org.h2.api.ErrorCode;
import org.h2.command.Prepared; import org.h2.command.Prepared;
import org.h2.command.dml.Query; import org.h2.command.dml.Query;
import org.h2.engine.Constants; import org.h2.engine.Constants;
import org.h2.engine.Database;
import org.h2.engine.DbObject; import org.h2.engine.DbObject;
import org.h2.engine.Session; import org.h2.engine.Session;
import org.h2.engine.User; import org.h2.engine.User;
...@@ -80,10 +81,10 @@ public class TableView extends Table { ...@@ -80,10 +81,10 @@ public class TableView extends Table {
String[] oldColumnNames = this.columnNames; String[] oldColumnNames = this.columnNames;
boolean oldRecursive = this.recursive; boolean oldRecursive = this.recursive;
init(querySQL, null, columnNames, session, recursive); init(querySQL, null, columnNames, session, recursive);
DbException e = recompile(session, force); DbException e = recompile(session, force, true);
if (e != null) { if (e != null) {
init(oldQuerySQL, null, oldColumnNames, session, oldRecursive); init(oldQuerySQL, null, oldColumnNames, session, oldRecursive);
recompile(session, true); recompile(session, true, false);
throw e; throw e;
} }
} }
...@@ -116,10 +117,11 @@ public class TableView extends Table { ...@@ -116,10 +117,11 @@ public class TableView extends Table {
* *
* @param session the session * @param session the session
* @param force if exceptions should be ignored * @param force if exceptions should be ignored
* @param clearIndexCache if we need to clear view index cache
* @return the exception if re-compiling this or any dependent view failed * @return the exception if re-compiling this or any dependent view failed
* (only when force is disabled) * (only when force is disabled)
*/ */
public synchronized DbException recompile(Session session, boolean force) { public synchronized DbException recompile(Session session, boolean force, boolean clearIndexCache) {
try { try {
compileViewQuery(session, querySQL); compileViewQuery(session, querySQL);
} catch (DbException e) { } catch (DbException e) {
...@@ -134,12 +136,15 @@ public class TableView extends Table { ...@@ -134,12 +136,15 @@ public class TableView extends Table {
initColumnsAndTables(session); initColumnsAndTables(session);
if (views != null) { if (views != null) {
for (TableView v : views) { for (TableView v : views) {
DbException e = v.recompile(session, force); DbException e = v.recompile(session, force, false);
if (e != null && !force) { if (e != null && !force) {
return e; return e;
} }
} }
} }
if (clearIndexCache) {
clearIndexCaches(database);
}
return force ? null : createException; return force ? null : createException;
} }
...@@ -399,10 +404,14 @@ public class TableView extends Table { ...@@ -399,10 +404,14 @@ public class TableView extends Table {
database.removeMeta(session, getId()); database.removeMeta(session, getId());
querySQL = null; querySQL = null;
index = null; index = null;
clearIndexCaches(database);
invalidate();
}
public static void clearIndexCaches(Database database) {
for (Session s : database.getSessions(true)) { for (Session s : database.getSessions(true)) {
s.clearViewIndexCache(); s.clearViewIndexCache();
} }
invalidate();
} }
@Override @Override
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论