This is an automated email from the ASF dual-hosted git repository.
jianglongtao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 525c28623ab Check privilege when register or alter storage unit
(#32172)
525c28623ab is described below
commit 525c28623ab84f964adcaecec717d83b043b6e3c
Author: Raigor <[email protected]>
AuthorDate: Thu Jul 18 22:32:36 2024 +0800
Check privilege when register or alter storage unit (#32172)
---
.../core/advice/SQLParseCountAdviceTest.java | 2 +-
...ion.java => StorageUnitsValidateException.java} | 8 +++---
.../DataSourcePoolPropertiesValidator.java | 31 +++++++++++++++++++---
.../DataSourcePoolPropertiesValidatorTest.java | 2 +-
.../database/core/checker/PrivilegeCheckType.java | 2 +-
.../rdl/resource/AlterStorageUnitExecutor.java | 5 ++--
.../rdl/resource/RegisterStorageUnitExecutor.java | 3 ++-
.../DistSQLDataSourcePoolPropertiesValidator.java | 19 ++++++++++---
.../rdl/resource/AlterStorageUnitExecutorTest.java | 5 ++--
.../resource/RegisterStorageUnitExecutorTest.java | 5 ++--
.../transaction/util/AutoCommitUtilsTest.java | 9 +++----
.../engine/src/main/antlr4/imports/Keyword.g4 | 4 +++
.../engine/src/main/antlr4/imports/RDLStatement.g4 | 16 +++++++++--
.../core/kernel/KernelDistSQLStatementVisitor.java | 17 +++++++++---
.../unit/type/AlterStorageUnitStatement.java | 2 ++
.../unit/type/RegisterStorageUnitStatement.java | 2 ++
16 files changed, 100 insertions(+), 32 deletions(-)
diff --git
a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/SQLParseCountAdviceTest.java
b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/SQLParseCountAdviceTest.java
index 661025e3543..da5395622a9 100644
---
a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/SQLParseCountAdviceTest.java
+++
b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/SQLParseCountAdviceTest.java
@@ -102,7 +102,7 @@ class SQLParseCountAdviceTest {
@Test
void assertParseRDL() {
- assertParse(new RegisterStorageUnitStatement(false,
Collections.emptyList()), "RDL=1");
+ assertParse(new RegisterStorageUnitStatement(false,
Collections.emptyList(), Collections.emptySet()), "RDL=1");
}
@Test
diff --git
a/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/kernel/metadata/resource/storageunit/StorageUnitsConnectException.java
b/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/kernel/metadata/resource/storageunit/StorageUnitsValidateException.java
similarity index 82%
rename from
infra/common/src/main/java/org/apache/shardingsphere/infra/exception/kernel/metadata/resource/storageunit/StorageUnitsConnectException.java
rename to
infra/common/src/main/java/org/apache/shardingsphere/infra/exception/kernel/metadata/resource/storageunit/StorageUnitsValidateException.java
index 95cdd19514e..80ccacc50ab 100644
---
a/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/kernel/metadata/resource/storageunit/StorageUnitsConnectException.java
+++
b/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/kernel/metadata/resource/storageunit/StorageUnitsValidateException.java
@@ -24,14 +24,14 @@ import java.util.Map;
import java.util.stream.Collectors;
/**
- * Storage units connect exception.
+ * Storage units validate exception.
*/
-public final class StorageUnitsConnectException extends
ResourceDefinitionException {
+public final class StorageUnitsValidateException extends
ResourceDefinitionException {
private static final long serialVersionUID = 1824912697040264268L;
- public StorageUnitsConnectException(final Map<String, Exception> causes) {
- super(XOpenSQLState.CONNECTION_EXCEPTION, 10, "Storage units can not
connect, error messages are: %s.", causes.entrySet().stream().map(entry ->
String.format(
+ public StorageUnitsValidateException(final Map<String, Exception> causes) {
+ super(XOpenSQLState.CONNECTION_EXCEPTION, 10, "Storage units validate
error, messages are: %s.", causes.entrySet().stream().map(entry ->
String.format(
"Storage unit name: '%s', error message is: %s",
entry.getKey(),
entry.getValue().getMessage())).collect(Collectors.joining(System.lineSeparator())));
}
}
diff --git
a/infra/data-source-pool/core/src/main/java/org/apache/shardingsphere/infra/datasource/pool/props/validator/DataSourcePoolPropertiesValidator.java
b/infra/data-source-pool/core/src/main/java/org/apache/shardingsphere/infra/datasource/pool/props/validator/DataSourcePoolPropertiesValidator.java
index 370fb39ef01..c2ae0a408fc 100644
---
a/infra/data-source-pool/core/src/main/java/org/apache/shardingsphere/infra/datasource/pool/props/validator/DataSourcePoolPropertiesValidator.java
+++
b/infra/data-source-pool/core/src/main/java/org/apache/shardingsphere/infra/datasource/pool/props/validator/DataSourcePoolPropertiesValidator.java
@@ -19,6 +19,11 @@ package
org.apache.shardingsphere.infra.datasource.pool.props.validator;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
+import
org.apache.shardingsphere.infra.database.core.checker.DialectDatabaseEnvironmentChecker;
+import
org.apache.shardingsphere.infra.database.core.checker.PrivilegeCheckType;
+import
org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader;
+import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
+import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeFactory;
import
org.apache.shardingsphere.infra.datasource.pool.creator.DataSourcePoolCreator;
import
org.apache.shardingsphere.infra.datasource.pool.destroyer.DataSourcePoolDestroyer;
import
org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties;
@@ -27,9 +32,11 @@ import
org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
+import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
+import java.util.Optional;
/**
* Data source pool properties validator.
@@ -41,14 +48,15 @@ public final class DataSourcePoolPropertiesValidator {
* Validate data source pool properties map.
*
* @param propsMap data source pool properties map
+ * @param expectedPrivileges excepted privileges
* @return data source name and exception map
*/
- public static Map<String, Exception> validate(final Map<String,
DataSourcePoolProperties> propsMap) {
+ public static Map<String, Exception> validate(final Map<String,
DataSourcePoolProperties> propsMap, final Collection<PrivilegeCheckType>
expectedPrivileges) {
Map<String, Exception> result = new LinkedHashMap<>(propsMap.size(),
1F);
for (Entry<String, DataSourcePoolProperties> entry :
propsMap.entrySet()) {
try {
validateProperties(entry.getKey(), entry.getValue());
- validateConnection(entry.getKey(), entry.getValue());
+ validateConnection(entry.getKey(), entry.getValue(),
expectedPrivileges);
} catch (final InvalidDataSourcePoolPropertiesException ex) {
result.put(entry.getKey(), ex);
}
@@ -64,11 +72,16 @@ public final class DataSourcePoolPropertiesValidator {
}
}
- private static void validateConnection(final String dataSourceName, final
DataSourcePoolProperties props) throws InvalidDataSourcePoolPropertiesException
{
+ private static void validateConnection(final String dataSourceName, final
DataSourcePoolProperties props,
+ final
Collection<PrivilegeCheckType> expectedPrivileges) throws
InvalidDataSourcePoolPropertiesException {
DataSource dataSource = null;
try {
dataSource = DataSourcePoolCreator.create(props);
- checkFailFast(dataSource);
+ if (expectedPrivileges.isEmpty() ||
expectedPrivileges.contains(PrivilegeCheckType.NONE)) {
+ checkFailFast(dataSource);
+ return;
+ }
+ checkPrivileges(dataSource, props, expectedPrivileges);
// CHECKSTYLE:OFF
} catch (final SQLException | RuntimeException ex) {
// CHECKSTYLE:ON
@@ -87,4 +100,14 @@ public final class DataSourcePoolPropertiesValidator {
// CHECKSTYLE:ON
}
}
+
+ private static void checkPrivileges(final DataSource dataSource, final
DataSourcePoolProperties props, final Collection<PrivilegeCheckType>
expectedPrivileges) {
+ DatabaseType databaseType = DatabaseTypeFactory.get((String)
props.getConnectionPropertySynonyms().getStandardProperties().get("url"));
+ Optional<DialectDatabaseEnvironmentChecker> checker =
DatabaseTypedSPILoader.findService(DialectDatabaseEnvironmentChecker.class,
databaseType);
+ if (checker.isPresent()) {
+ for (PrivilegeCheckType each : expectedPrivileges) {
+ checker.get().checkPrivilege(dataSource, each);
+ }
+ }
+ }
}
diff --git
a/infra/data-source-pool/core/src/test/java/org/apache/shardingsphere/infra/datasource/pool/props/validator/DataSourcePoolPropertiesValidatorTest.java
b/infra/data-source-pool/core/src/test/java/org/apache/shardingsphere/infra/datasource/pool/props/validator/DataSourcePoolPropertiesValidatorTest.java
index 548c50901f8..cdbaae15bff 100644
---
a/infra/data-source-pool/core/src/test/java/org/apache/shardingsphere/infra/datasource/pool/props/validator/DataSourcePoolPropertiesValidatorTest.java
+++
b/infra/data-source-pool/core/src/test/java/org/apache/shardingsphere/infra/datasource/pool/props/validator/DataSourcePoolPropertiesValidatorTest.java
@@ -37,6 +37,6 @@ class DataSourcePoolPropertiesValidatorTest {
@Test
void assertValidate() {
assertTrue(DataSourcePoolPropertiesValidator.validate(
- Collections.singletonMap("name", new
DataSourcePoolProperties(HikariDataSource.class.getName(),
Collections.singletonMap("jdbcUrl", "jdbc:mock")))).isEmpty());
+ Collections.singletonMap("name", new
DataSourcePoolProperties(HikariDataSource.class.getName(),
Collections.singletonMap("jdbcUrl", "jdbc:mock"))),
Collections.emptySet()).isEmpty());
}
}
diff --git
a/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/checker/PrivilegeCheckType.java
b/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/checker/PrivilegeCheckType.java
index f670118d7cb..8ce96c4b468 100644
---
a/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/checker/PrivilegeCheckType.java
+++
b/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/checker/PrivilegeCheckType.java
@@ -22,5 +22,5 @@ package org.apache.shardingsphere.infra.database.core.checker;
*/
public enum PrivilegeCheckType {
- PIPELINE, SELECT, XA
+ NONE, PIPELINE, SELECT, XA
}
diff --git
a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/executor/rdl/resource/AlterStorageUnitExecutor.java
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/executor/rdl/resource/AlterStorageUnitExecutor.java
index cc06b9d97dd..e8880c38ff6 100644
---
a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/executor/rdl/resource/AlterStorageUnitExecutor.java
+++
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/executor/rdl/resource/AlterStorageUnitExecutor.java
@@ -27,6 +27,7 @@ import
org.apache.shardingsphere.distsql.segment.HostnameAndPortBasedDataSourceS
import org.apache.shardingsphere.distsql.segment.URLBasedDataSourceSegment;
import
org.apache.shardingsphere.distsql.segment.converter.DataSourceSegmentsConverter;
import
org.apache.shardingsphere.distsql.statement.rdl.resource.unit.type.AlterStorageUnitStatement;
+import
org.apache.shardingsphere.infra.database.core.checker.PrivilegeCheckType;
import
org.apache.shardingsphere.infra.database.core.connector.ConnectionProperties;
import org.apache.shardingsphere.infra.database.core.connector.url.JdbcUrl;
import
org.apache.shardingsphere.infra.database.core.connector.url.StandardJdbcUrlParser;
@@ -35,8 +36,8 @@ import
org.apache.shardingsphere.infra.exception.core.ShardingSpherePrecondition
import
org.apache.shardingsphere.infra.exception.core.external.ShardingSphereExternalException;
import
org.apache.shardingsphere.infra.exception.kernel.metadata.resource.storageunit.AlterStorageUnitConnectionInfoException;
import
org.apache.shardingsphere.infra.exception.kernel.metadata.resource.storageunit.DuplicateStorageUnitException;
-import
org.apache.shardingsphere.infra.exception.kernel.metadata.resource.storageunit.StorageUnitsOperateException;
import
org.apache.shardingsphere.infra.exception.kernel.metadata.resource.storageunit.MissingRequiredStorageUnitsException;
+import
org.apache.shardingsphere.infra.exception.kernel.metadata.resource.storageunit.StorageUnitsOperateException;
import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import
org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
import org.apache.shardingsphere.mode.manager.ContextManager;
@@ -64,7 +65,7 @@ public final class AlterStorageUnitExecutor implements
DistSQLUpdateExecutor<Alt
public void executeUpdate(final AlterStorageUnitStatement sqlStatement,
final ContextManager contextManager) {
checkBefore(sqlStatement);
Map<String, DataSourcePoolProperties> propsMap =
DataSourceSegmentsConverter.convert(database.getProtocolType(),
sqlStatement.getStorageUnits());
- validateHandler.validate(propsMap);
+ validateHandler.validate(propsMap,
sqlStatement.getExpectedPrivileges().stream().map(each ->
PrivilegeCheckType.valueOf(each.toUpperCase())).collect(Collectors.toSet()));
try {
MetaDataContexts originalMetaDataContexts =
contextManager.getMetaDataContexts();
contextManager.getPersistServiceFacade().getMetaDataManagerPersistService().alterStorageUnits(database.getName(),
propsMap);
diff --git
a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/executor/rdl/resource/RegisterStorageUnitExecutor.java
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/executor/rdl/resource/RegisterStorageUnitExecutor.java
index 594b0b3d2c7..9a748961b18 100644
---
a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/executor/rdl/resource/RegisterStorageUnitExecutor.java
+++
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/executor/rdl/resource/RegisterStorageUnitExecutor.java
@@ -25,6 +25,7 @@ import
org.apache.shardingsphere.distsql.handler.validate.DistSQLDataSourcePoolP
import org.apache.shardingsphere.distsql.segment.DataSourceSegment;
import
org.apache.shardingsphere.distsql.segment.converter.DataSourceSegmentsConverter;
import
org.apache.shardingsphere.distsql.statement.rdl.resource.unit.type.RegisterStorageUnitStatement;
+import
org.apache.shardingsphere.infra.database.core.checker.PrivilegeCheckType;
import
org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties;
import
org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import
org.apache.shardingsphere.infra.exception.core.external.ShardingSphereExternalException;
@@ -66,7 +67,7 @@ public final class RegisterStorageUnitExecutor implements
DistSQLUpdateExecutor<
if (propsMap.isEmpty()) {
return;
}
- validateHandler.validate(propsMap);
+ validateHandler.validate(propsMap,
sqlStatement.getExpectedPrivileges().stream().map(each ->
PrivilegeCheckType.valueOf(each.toUpperCase())).collect(Collectors.toSet()));
try {
MetaDataContexts originalMetaDataContexts =
contextManager.getMetaDataContexts();
contextManager.getPersistServiceFacade().getMetaDataManagerPersistService().registerStorageUnits(database.getName(),
propsMap);
diff --git
a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/validate/DistSQLDataSourcePoolPropertiesValidator.java
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/validate/DistSQLDataSourcePoolPropertiesValidator.java
index f9778b3b0d4..5ec0180e80a 100644
---
a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/validate/DistSQLDataSourcePoolPropertiesValidator.java
+++
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/validate/DistSQLDataSourcePoolPropertiesValidator.java
@@ -17,11 +17,14 @@
package org.apache.shardingsphere.distsql.handler.validate;
+import
org.apache.shardingsphere.infra.database.core.checker.PrivilegeCheckType;
import
org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties;
import
org.apache.shardingsphere.infra.datasource.pool.props.validator.DataSourcePoolPropertiesValidator;
import
org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
-import
org.apache.shardingsphere.infra.exception.kernel.metadata.resource.storageunit.StorageUnitsConnectException;
+import
org.apache.shardingsphere.infra.exception.kernel.metadata.resource.storageunit.StorageUnitsValidateException;
+import java.util.Collection;
+import java.util.Collections;
import java.util.Map;
/**
@@ -35,7 +38,17 @@ public final class DistSQLDataSourcePoolPropertiesValidator {
* @param propsMap data source pool properties map
*/
public void validate(final Map<String, DataSourcePoolProperties> propsMap)
{
- Map<String, Exception> exceptions =
DataSourcePoolPropertiesValidator.validate(propsMap);
- ShardingSpherePreconditions.checkMustEmpty(exceptions, () -> new
StorageUnitsConnectException(exceptions));
+ validate(propsMap, Collections.emptySet());
+ }
+
+ /**
+ * Validate data source properties map.
+ *
+ * @param propsMap data source pool properties map
+ * @param expectedPrivileges expected privileges
+ */
+ public void validate(final Map<String, DataSourcePoolProperties> propsMap,
final Collection<PrivilegeCheckType> expectedPrivileges) {
+ Map<String, Exception> exceptions =
DataSourcePoolPropertiesValidator.validate(propsMap, expectedPrivileges);
+ ShardingSpherePreconditions.checkMustEmpty(exceptions, () -> new
StorageUnitsValidateException(exceptions));
}
}
diff --git
a/infra/distsql-handler/src/test/java/org/apache/shardingsphere/distsql/handler/executor/rdl/resource/AlterStorageUnitExecutorTest.java
b/infra/distsql-handler/src/test/java/org/apache/shardingsphere/distsql/handler/executor/rdl/resource/AlterStorageUnitExecutorTest.java
index 1d2a5f9c46c..c9c47ae891d 100644
---
a/infra/distsql-handler/src/test/java/org/apache/shardingsphere/distsql/handler/executor/rdl/resource/AlterStorageUnitExecutorTest.java
+++
b/infra/distsql-handler/src/test/java/org/apache/shardingsphere/distsql/handler/executor/rdl/resource/AlterStorageUnitExecutorTest.java
@@ -103,13 +103,14 @@ class AlterStorageUnitExecutorTest {
}
private AlterStorageUnitStatement createAlterStorageUnitStatement(final
String resourceName) {
- return new AlterStorageUnitStatement(Collections.singleton(new
URLBasedDataSourceSegment(resourceName, "jdbc:mysql://127.0.0.1:3306/ds_0",
"root", "", new Properties())));
+ return new AlterStorageUnitStatement(Collections.singleton(new
URLBasedDataSourceSegment(resourceName, "jdbc:mysql://127.0.0.1:3306/ds_0",
"root", "", new Properties())),
+ Collections.emptySet());
}
private AlterStorageUnitStatement
createAlterStorageUnitStatementWithDuplicateStorageUnitNames() {
return new AlterStorageUnitStatement(Arrays.asList(
new HostnameAndPortBasedDataSourceSegment("ds_0", "127.0.0.1",
"3306", "ds_0", "root", "", new Properties()),
- new URLBasedDataSourceSegment("ds_0",
"jdbc:mysql://127.0.0.1:3306/ds_1", "root", "", new Properties())));
+ new URLBasedDataSourceSegment("ds_0",
"jdbc:mysql://127.0.0.1:3306/ds_1", "root", "", new Properties())),
Collections.emptySet());
}
private ConnectionProperties mockConnectionProperties(final String
catalog) {
diff --git
a/infra/distsql-handler/src/test/java/org/apache/shardingsphere/distsql/handler/executor/rdl/resource/RegisterStorageUnitExecutorTest.java
b/infra/distsql-handler/src/test/java/org/apache/shardingsphere/distsql/handler/executor/rdl/resource/RegisterStorageUnitExecutorTest.java
index fffdbfe5a3f..e06efe08be8 100644
---
a/infra/distsql-handler/src/test/java/org/apache/shardingsphere/distsql/handler/executor/rdl/resource/RegisterStorageUnitExecutorTest.java
+++
b/infra/distsql-handler/src/test/java/org/apache/shardingsphere/distsql/handler/executor/rdl/resource/RegisterStorageUnitExecutorTest.java
@@ -91,12 +91,13 @@ class RegisterStorageUnitExecutorTest {
}
private RegisterStorageUnitStatement createRegisterStorageUnitStatement() {
- return new RegisterStorageUnitStatement(false,
Collections.singleton(new URLBasedDataSourceSegment("ds_0",
"jdbc:mysql://127.0.0.1:3306/test0", "root", "", new Properties())));
+ return new RegisterStorageUnitStatement(false,
Collections.singleton(new URLBasedDataSourceSegment("ds_0",
"jdbc:mysql://127.0.0.1:3306/test0", "root", "", new Properties())),
+ Collections.emptySet());
}
private RegisterStorageUnitStatement
createRegisterStorageUnitStatementWithDuplicateStorageUnitNames() {
return new RegisterStorageUnitStatement(false, Arrays.asList(
new HostnameAndPortBasedDataSourceSegment("ds_0", "127.0.0.1",
"3306", "ds_0", "root", "", new Properties()),
- new URLBasedDataSourceSegment("ds_0",
"jdbc:mysql://127.0.0.1:3306/ds_1", "root", "", new Properties())));
+ new URLBasedDataSourceSegment("ds_0",
"jdbc:mysql://127.0.0.1:3306/ds_1", "root", "", new Properties())),
Collections.emptySet());
}
}
diff --git
a/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/util/AutoCommitUtilsTest.java
b/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/util/AutoCommitUtilsTest.java
index 5a53315e765..249c5ec0efc 100644
---
a/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/util/AutoCommitUtilsTest.java
+++
b/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/util/AutoCommitUtilsTest.java
@@ -19,18 +19,15 @@ package org.apache.shardingsphere.transaction.util;
import
org.apache.shardingsphere.distsql.statement.rdl.resource.unit.type.RegisterStorageUnitStatement;
import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
-import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.TableNameSegment;
import
org.apache.shardingsphere.sql.parser.statement.core.statement.dml.SelectStatement;
-import
org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
import
org.apache.shardingsphere.sql.parser.statement.mysql.ddl.MySQLCreateTableStatement;
import
org.apache.shardingsphere.sql.parser.statement.mysql.dml.MySQLInsertStatement;
import
org.apache.shardingsphere.sql.parser.statement.mysql.dml.MySQLSelectStatement;
import org.junit.jupiter.api.Test;
-import java.util.LinkedList;
-
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
class AutoCommitUtilsTest {
@@ -38,7 +35,7 @@ class AutoCommitUtilsTest {
void assertNeedOpenTransactionForSelectStatement() {
SelectStatement selectStatement = new MySQLSelectStatement();
assertFalse(AutoCommitUtils.needOpenTransaction(selectStatement));
- selectStatement.setFrom(new SimpleTableSegment(new TableNameSegment(0,
5, new IdentifierValue("foo"))));
+ selectStatement.setFrom(mock(SimpleTableSegment.class));
assertTrue(AutoCommitUtils.needOpenTransaction(selectStatement));
}
@@ -50,6 +47,6 @@ class AutoCommitUtilsTest {
@Test
void assertNeedOpenTransactionForOtherStatement() {
- assertFalse(AutoCommitUtils.needOpenTransaction(new
RegisterStorageUnitStatement(false, new LinkedList<>())));
+
assertFalse(AutoCommitUtils.needOpenTransaction(mock(RegisterStorageUnitStatement.class)));
}
}
diff --git a/parser/distsql/engine/src/main/antlr4/imports/Keyword.g4
b/parser/distsql/engine/src/main/antlr4/imports/Keyword.g4
index e2bdb512195..6f096e6417f 100644
--- a/parser/distsql/engine/src/main/antlr4/imports/Keyword.g4
+++ b/parser/distsql/engine/src/main/antlr4/imports/Keyword.g4
@@ -346,3 +346,7 @@ ALGORITHM
FORCE
: F O R C E
;
+
+CHECK_PRIVILEGES
+ : C H E C K UL_ P R I V I L E G E S
+ ;
diff --git a/parser/distsql/engine/src/main/antlr4/imports/RDLStatement.g4
b/parser/distsql/engine/src/main/antlr4/imports/RDLStatement.g4
index 418dd843b7c..5dfb4f7a0bd 100644
--- a/parser/distsql/engine/src/main/antlr4/imports/RDLStatement.g4
+++ b/parser/distsql/engine/src/main/antlr4/imports/RDLStatement.g4
@@ -20,17 +20,21 @@ grammar RDLStatement;
import BaseRule;
registerStorageUnit
- : REGISTER STORAGE UNIT ifNotExists? storageUnitDefinition (COMMA_
storageUnitDefinition)*
+ : REGISTER STORAGE UNIT ifNotExists? storageUnitsDefinition (COMMA_
checkPrivileges)?
;
alterStorageUnit
- : ALTER STORAGE UNIT storageUnitDefinition (COMMA_ storageUnitDefinition)*
+ : ALTER STORAGE UNIT storageUnitsDefinition (COMMA_ checkPrivileges)?
;
unregisterStorageUnit
: UNREGISTER STORAGE UNIT ifExists? storageUnitName (COMMA_
storageUnitName)* ignoreTables?
;
+storageUnitsDefinition
+ : storageUnitDefinition (COMMA_ storageUnitDefinition)*
+ ;
+
storageUnitDefinition
: storageUnitName LP_ (simpleSource | urlSource) COMMA_ USER EQ_ user
(COMMA_ PASSWORD EQ_ password)? (COMMA_ propertiesDefinition)? RP_
;
@@ -80,3 +84,11 @@ ifExists
ifNotExists
: IF NOT EXISTS
;
+
+checkPrivileges
+ : CHECK_PRIVILEGES EQ_ privilegeType (COMMA_ privilegeType)*
+ ;
+
+privilegeType
+ : IDENTIFIER_
+ ;
diff --git
a/parser/distsql/engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/kernel/KernelDistSQLStatementVisitor.java
b/parser/distsql/engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/kernel/KernelDistSQLStatementVisitor.java
index 83dde189cd1..fa7113f6acc 100644
---
a/parser/distsql/engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/kernel/KernelDistSQLStatementVisitor.java
+++
b/parser/distsql/engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/kernel/KernelDistSQLStatementVisitor.java
@@ -22,6 +22,7 @@ import
org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementBa
import
org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.AlgorithmDefinitionContext;
import
org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.AlterComputeNodeContext;
import
org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.AlterStorageUnitContext;
+import
org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.CheckPrivilegesContext;
import
org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ConvertYamlConfigurationContext;
import
org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.DatabaseNameContext;
import
org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.DisableComputeNodeContext;
@@ -58,6 +59,7 @@ import
org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementPa
import
org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ShowStorageUnitsContext;
import
org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ShowTableMetadataContext;
import
org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.StorageUnitDefinitionContext;
+import
org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.StorageUnitsDefinitionContext;
import
org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.UnlabelComputeNodeContext;
import
org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.UnlockClusterContext;
import
org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.UnregisterStorageUnitContext;
@@ -83,8 +85,8 @@ import
org.apache.shardingsphere.distsql.statement.ral.updatable.LabelComputeNod
import
org.apache.shardingsphere.distsql.statement.ral.updatable.LockClusterStatement;
import
org.apache.shardingsphere.distsql.statement.ral.updatable.RefreshDatabaseMetaDataStatement;
import
org.apache.shardingsphere.distsql.statement.ral.updatable.RefreshTableMetaDataStatement;
-import
org.apache.shardingsphere.distsql.statement.ral.updatable.SetDistVariableStatement;
import
org.apache.shardingsphere.distsql.statement.ral.updatable.SetComputeNodeStateStatement;
+import
org.apache.shardingsphere.distsql.statement.ral.updatable.SetDistVariableStatement;
import
org.apache.shardingsphere.distsql.statement.ral.updatable.UnlabelComputeNodeStatement;
import
org.apache.shardingsphere.distsql.statement.ral.updatable.UnlockClusterStatement;
import
org.apache.shardingsphere.distsql.statement.rdl.resource.unit.type.AlterStorageUnitStatement;
@@ -100,6 +102,7 @@ import
org.apache.shardingsphere.sql.parser.statement.core.value.identifier.Iden
import
org.apache.shardingsphere.sql.parser.statement.core.value.literal.impl.StringLiteralValue;
import java.util.Collection;
+import java.util.Collections;
import java.util.Objects;
import java.util.Properties;
import java.util.stream.Collectors;
@@ -111,12 +114,20 @@ public final class KernelDistSQLStatementVisitor extends
KernelDistSQLStatementB
@Override
public ASTNode visitRegisterStorageUnit(final RegisterStorageUnitContext
ctx) {
- return new RegisterStorageUnitStatement(null != ctx.ifNotExists(),
ctx.storageUnitDefinition().stream().map(each -> (DataSourceSegment)
visit(each)).collect(Collectors.toList()));
+ return new RegisterStorageUnitStatement(null != ctx.ifNotExists(),
getStorageUnits(ctx.storageUnitsDefinition()),
getExpectedPrivileges(ctx.checkPrivileges()));
}
@Override
public ASTNode visitAlterStorageUnit(final AlterStorageUnitContext ctx) {
- return new
AlterStorageUnitStatement(ctx.storageUnitDefinition().stream().map(each ->
(DataSourceSegment) visit(each)).collect(Collectors.toList()));
+ return new
AlterStorageUnitStatement(getStorageUnits(ctx.storageUnitsDefinition()),
getExpectedPrivileges(ctx.checkPrivileges()));
+ }
+
+ private Collection<DataSourceSegment> getStorageUnits(final
StorageUnitsDefinitionContext ctx) {
+ return ctx.storageUnitDefinition().stream().map(each ->
(DataSourceSegment) visit(each)).collect(Collectors.toList());
+ }
+
+ private Collection<String> getExpectedPrivileges(final
CheckPrivilegesContext ctx) {
+ return null == ctx ? Collections.emptySet() :
ctx.privilegeType().stream().map(this::getIdentifierValue).collect(Collectors.toSet());
}
@Override
diff --git
a/parser/distsql/statement/src/main/java/org/apache/shardingsphere/distsql/statement/rdl/resource/unit/type/AlterStorageUnitStatement.java
b/parser/distsql/statement/src/main/java/org/apache/shardingsphere/distsql/statement/rdl/resource/unit/type/AlterStorageUnitStatement.java
index 7a26c867c4d..0bf3cb2f506 100644
---
a/parser/distsql/statement/src/main/java/org/apache/shardingsphere/distsql/statement/rdl/resource/unit/type/AlterStorageUnitStatement.java
+++
b/parser/distsql/statement/src/main/java/org/apache/shardingsphere/distsql/statement/rdl/resource/unit/type/AlterStorageUnitStatement.java
@@ -32,4 +32,6 @@ import java.util.Collection;
public final class AlterStorageUnitStatement extends
StorageUnitDefinitionStatement {
private final Collection<DataSourceSegment> storageUnits;
+
+ private final Collection<String> expectedPrivileges;
}
diff --git
a/parser/distsql/statement/src/main/java/org/apache/shardingsphere/distsql/statement/rdl/resource/unit/type/RegisterStorageUnitStatement.java
b/parser/distsql/statement/src/main/java/org/apache/shardingsphere/distsql/statement/rdl/resource/unit/type/RegisterStorageUnitStatement.java
index 84795ad9340..90418b0d600 100644
---
a/parser/distsql/statement/src/main/java/org/apache/shardingsphere/distsql/statement/rdl/resource/unit/type/RegisterStorageUnitStatement.java
+++
b/parser/distsql/statement/src/main/java/org/apache/shardingsphere/distsql/statement/rdl/resource/unit/type/RegisterStorageUnitStatement.java
@@ -34,4 +34,6 @@ public final class RegisterStorageUnitStatement extends
StorageUnitDefinitionSta
private final boolean ifNotExists;
private final Collection<DataSourceSegment> storageUnits;
+
+ private final Collection<String> expectedPrivileges;
}