You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2022/07/01 11:16:30 UTC

[shardingsphere] branch master updated: add audit algorithm (#18765)

This is an automated email from the ASF dual-hosted git repository.

duanzhengqiang 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 cb90a5b33cd add audit algorithm (#18765)
cb90a5b33cd is described below

commit cb90a5b33cd15e9a7ad6328a7c89b0e6141e878b
Author: natehuang <na...@tencent.com>
AuthorDate: Fri Jul 1 19:15:52 2022 +0800

    add audit algorithm (#18765)
    
    * add audit algorithm
    
    * update algorithm name
    
    * add comment
---
 ...MLShardingConditionsShardingAuditAlgorithm.java | 72 ++++++++++++++++++++++
 ...rdingsphere.sharding.spi.ShardingAuditAlgorithm | 18 ++++++
 2 files changed, 90 insertions(+)

diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/audit/DMLShardingConditionsShardingAuditAlgorithm.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/audit/DMLShardingConditionsShardingAuditAlgorithm.java
new file mode 100644
index 00000000000..640b7380bd4
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/audit/DMLShardingConditionsShardingAuditAlgorithm.java
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.sharding.algorithm.audit;
+
+import lombok.Getter;
+import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
+import org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementContext;
+import org.apache.shardingsphere.infra.check.SQLCheckResult;
+import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.metadata.user.Grantee;
+import org.apache.shardingsphere.sharding.route.engine.condition.engine.ShardingConditionEngine;
+import org.apache.shardingsphere.sharding.route.engine.condition.engine.impl.InsertClauseShardingConditionEngine;
+import org.apache.shardingsphere.sharding.route.engine.condition.engine.impl.WhereClauseShardingConditionEngine;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
+import org.apache.shardingsphere.sharding.spi.ShardingAuditAlgorithm;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.DMLStatement;
+
+import java.util.List;
+import java.util.Properties;
+
+/**
+ * DML sharding conditions sharding audit algorithm.
+ */
+public final class DMLShardingConditionsShardingAuditAlgorithm implements ShardingAuditAlgorithm {
+
+    @Getter
+    private Properties props;
+
+    @Override
+    public void init(final Properties props) {
+        this.props = props;
+    }
+
+    @SuppressWarnings({"rawtypes", "unchecked"})
+    @Override
+    public SQLCheckResult check(final SQLStatementContext<?> sqlStatementContext, final List<Object> parameters, final Grantee grantee, final ShardingSphereDatabase database) {
+        if (sqlStatementContext.getSqlStatement() instanceof DMLStatement) {
+            ShardingRule rule = ((List<ShardingRule>) database.getRuleMetaData().findRules(ShardingRule.class)).get(0);
+            if (rule.isAllBroadcastTables(sqlStatementContext.getTablesContext().getTableNames())
+                    || sqlStatementContext.getTablesContext().getTableNames().stream().noneMatch(rule::isShardingTable)) {
+                return new SQLCheckResult(true, "");
+            }
+            ShardingConditionEngine shardingConditionEngine = sqlStatementContext instanceof InsertStatementContext
+                    ? new InsertClauseShardingConditionEngine(rule, database)
+                    : new WhereClauseShardingConditionEngine(rule, database);
+            if (shardingConditionEngine.createShardingConditions(sqlStatementContext, parameters).isEmpty()) {
+                return new SQLCheckResult(false, "Not allow DML operation without sharding conditions");
+            }
+        }
+        return new SQLCheckResult(true, "");
+    }
+
+    @Override
+    public String getType() {
+        return "DML_SHARDING_CONDITIONS";
+    }
+}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/resources/META-INF/services/org.apache.shardingsphere.sharding.spi.ShardingAuditAlgorithm b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/resources/META-INF/services/org.apache.shardingsphere.sharding.spi.ShardingAuditAlgorithm
new file mode 100644
index 00000000000..70f2852eb5e
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/resources/META-INF/services/org.apache.shardingsphere.sharding.spi.ShardingAuditAlgorithm
@@ -0,0 +1,18 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+ 
+org.apache.shardingsphere.sharding.algorithm.audit.DMLShardingConditionsShardingAuditAlgorithm