提交 273167ab authored 作者: Thomas Mueller's avatar Thomas Mueller

New system property h2.analyzeAuto.

上级 6bb3ea33
...@@ -9,6 +9,7 @@ package org.h2.command.ddl; ...@@ -9,6 +9,7 @@ package org.h2.command.ddl;
import org.h2.command.Prepared; import org.h2.command.Prepared;
import org.h2.constant.SysProperties; import org.h2.constant.SysProperties;
import org.h2.engine.Database; import org.h2.engine.Database;
import org.h2.engine.Right;
import org.h2.engine.Session; import org.h2.engine.Session;
import org.h2.result.ResultInterface; import org.h2.result.ResultInterface;
import org.h2.table.Column; import org.h2.table.Column;
...@@ -54,17 +55,22 @@ public class Analyze extends DefineCommand { ...@@ -54,17 +55,22 @@ public class Analyze extends DefineCommand {
if (!(table instanceof RegularTable) || table.isHidden() || session == null) { if (!(table instanceof RegularTable) || table.isHidden() || session == null) {
return; return;
} }
if (session.getDatabase().isSysTableLocked()) { if (!manual) {
return; if (session.getDatabase().isSysTableLocked()) {
return;
}
if (table.hasSelectTrigger()) {
return;
}
} }
if (table.isTemporary() && !table.isGlobalTemporary() if (table.isTemporary() && !table.isGlobalTemporary()
&& session.findLocalTempTable(table.getName()) == null) { && session.findLocalTempTable(table.getName()) == null) {
return; return;
} }
if (!manual && table.hasSelectTrigger()) { if (table.isLockedExclusively() && !table.isLockedExclusivelyBy(session)) {
return; return;
} }
if (table.isLockedExclusively() && !table.isLockedExclusivelyBy(session)) { if (!session.getUser().hasRight(table, Right.SELECT)) {
return; return;
} }
Database db = session.getDatabase(); Database db = session.getDatabase();
......
...@@ -91,19 +91,32 @@ public class User extends RightOwner { ...@@ -91,19 +91,32 @@ public class User extends RightOwner {
* @throws SQLException if this user does not have the required rights * @throws SQLException if this user does not have the required rights
*/ */
public void checkRight(Table table, int rightMask) { public void checkRight(Table table, int rightMask) {
if (!hasRight(table, rightMask)) {
throw DbException.get(ErrorCode.NOT_ENOUGH_RIGHTS_FOR_1, table.getSQL());
}
}
/**
* See if this user has the given rights for this database object.
*
* @param table the database object
* @param rightMask the rights required
* @return true if the user has the rights
*/
public boolean hasRight(Table table, int rightMask) {
if (rightMask != Right.SELECT && !systemUser) { if (rightMask != Right.SELECT && !systemUser) {
table.checkWritingAllowed(); table.checkWritingAllowed();
} }
if (admin) { if (admin) {
return; return true;
} }
Role publicRole = database.getPublicRole(); Role publicRole = database.getPublicRole();
if (publicRole.isRightGrantedRecursive(table, rightMask)) { if (publicRole.isRightGrantedRecursive(table, rightMask)) {
return; return true;
} }
if (table instanceof MetaTable || table instanceof RangeTable) { if (table instanceof MetaTable || table instanceof RangeTable) {
// everybody has access to the metadata information // everybody has access to the metadata information
return; return true;
} }
String tableType = table.getTableType(); String tableType = table.getTableType();
if (Table.VIEW.equals(tableType)) { if (Table.VIEW.equals(tableType)) {
...@@ -111,19 +124,20 @@ public class User extends RightOwner { ...@@ -111,19 +124,20 @@ public class User extends RightOwner {
if (v.getOwner() == this) { if (v.getOwner() == this) {
// the owner of a view has access: // the owner of a view has access:
// SELECT * FROM (SELECT * FROM ...) // SELECT * FROM (SELECT * FROM ...)
return; return true;
} }
} else if (tableType == null) { } else if (tableType == null) {
// function table // function table
return; return true;
} }
if (!isRightGrantedRecursive(table, rightMask)) { if (table.isTemporary() && !table.isGlobalTemporary()) {
if (table.isTemporary() && !table.isGlobalTemporary()) { // the owner has all rights on local temporary tables
// the owner has all rights on local temporary tables return true;
return; }
} if (isRightGrantedRecursive(table, rightMask)) {
throw DbException.get(ErrorCode.NOT_ENOUGH_RIGHTS_FOR_1, table.getSQL()); return true;
} }
return false;
} }
/** /**
......
...@@ -22,6 +22,7 @@ import java.sql.Types; ...@@ -22,6 +22,7 @@ import java.sql.Types;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedList; import java.util.LinkedList;
import org.h2.constant.SysProperties;
import org.h2.jdbc.JdbcConnection; import org.h2.jdbc.JdbcConnection;
import org.h2.message.TraceSystem; import org.h2.message.TraceSystem;
import org.h2.store.FileLock; import org.h2.store.FileLock;
...@@ -1080,6 +1081,10 @@ public abstract class TestBase { ...@@ -1080,6 +1081,10 @@ public abstract class TestBase {
* @throws AssertionError if the databases don't match * @throws AssertionError if the databases don't match
*/ */
protected void assertEqualDatabases(Statement stat1, Statement stat2) throws SQLException { protected void assertEqualDatabases(Statement stat1, Statement stat2) throws SQLException {
if (SysProperties.ANALYZE_AUTO > 0) {
stat1.execute("analyze");
stat2.execute("analyze");
}
ResultSet rs1 = stat1.executeQuery("SCRIPT NOPASSWORDS"); ResultSet rs1 = stat1.executeQuery("SCRIPT NOPASSWORDS");
ResultSet rs2 = stat2.executeQuery("SCRIPT NOPASSWORDS"); ResultSet rs2 = stat2.executeQuery("SCRIPT NOPASSWORDS");
ArrayList<String> list1 = new ArrayList<String>(); ArrayList<String> list1 = new ArrayList<String>();
......
...@@ -16,6 +16,7 @@ import java.util.ArrayList; ...@@ -16,6 +16,7 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Random; import java.util.Random;
import java.util.TreeSet; import java.util.TreeSet;
import org.h2.constant.SysProperties;
import org.h2.test.TestBase; import org.h2.test.TestBase;
import org.h2.tools.SimpleResultSet; import org.h2.tools.SimpleResultSet;
import org.h2.util.New; import org.h2.util.New;
...@@ -36,6 +37,7 @@ public class TestOptimizations extends TestBase { ...@@ -36,6 +37,7 @@ public class TestOptimizations extends TestBase {
} }
public void test() throws Exception { public void test() throws Exception {
testAutoAnalyze();
testInAndBetween(); testInAndBetween();
testNestedIn(); testNestedIn();
testNestedInSelectAndLike(); testNestedInSelectAndLike();
...@@ -59,6 +61,24 @@ public class TestOptimizations extends TestBase { ...@@ -59,6 +61,24 @@ public class TestOptimizations extends TestBase {
deleteDb("optimizations"); deleteDb("optimizations");
} }
private void testAutoAnalyze() throws SQLException {
int auto = SysProperties.ANALYZE_AUTO;
if (auto == 0) {
return;
}
deleteDb("optimizations");
Connection conn = getConnection("optimizations");
Statement stat = conn.createStatement();
stat.execute("create table test(id int)");
stat.execute("create user onlyInsert password ''");
stat.execute("grant insert on test to onlyInsert");
Connection conn2 = getConnection("optimizations", "onlyInsert", getPassword(""));
Statement stat2 = conn2.createStatement();
stat2.execute("insert into test select x from system_range(1, " + (auto + 10) + ")");
conn.close();
conn2.close();
}
private void testInAndBetween() throws SQLException { private void testInAndBetween() throws SQLException {
deleteDb("optimizations"); deleteDb("optimizations");
Connection conn = getConnection("optimizations"); Connection conn = getConnection("optimizations");
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论