You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by ch...@apache.org on 2023/05/22 04:43:00 UTC

[shardingsphere] branch master updated: Fix sonar issue of sql federation (#25834)

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

chengzhang 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 55cb18a25a8 Fix sonar issue of sql federation (#25834)
55cb18a25a8 is described below

commit 55cb18a25a8703b3d87fff582140038c1f7eeeda
Author: Zhengqiang Duan <du...@apache.org>
AuthorDate: Mon May 22 12:42:46 2023 +0800

    Fix sonar issue of sql federation (#25834)
    
    * Fix sonar issue of sql federation
    
    * Fix sonar issue of sql federation
    
    * optimize checkstyle
    
    * optimize checkstyle
---
 .../user-manual/error-code/sql-error-code.cn.md    |  1 +
 .../user-manual/error-code/sql-error-code.en.md    |  1 +
 .../AdvancedSQLFederationExecutorTest.java         | 93 ----------------------
 .../SQLFederationDriverRegisterException.java      | 33 ++++++++
 .../original/OriginalSQLFederationExecutor.java    |  6 +-
 5 files changed, 39 insertions(+), 95 deletions(-)

diff --git a/docs/document/content/user-manual/error-code/sql-error-code.cn.md b/docs/document/content/user-manual/error-code/sql-error-code.cn.md
index 2a7281f403c..dc7a9f70a2e 100644
--- a/docs/document/content/user-manual/error-code/sql-error-code.cn.md
+++ b/docs/document/content/user-manual/error-code/sql-error-code.cn.md
@@ -52,6 +52,7 @@ SQL 错误码以标准的 SQL State,Vendor Code 和详细错误信息提供,
 | SQL State | Vendor Code | 错误信息                                                                                                                                                                                                                        |
 |-----------|-------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
 | 08000     | 13000       | Can not register driver, reason is: %s                                                                                                                                                                                      |
+| 08000     | 13001       | Can not register SQL federation driver, reason is: %s                                                                                                                                                                       |
 | 01000     | 13010       | Circuit break open, the request has been ignored.                                                                                                                                                                           |
 | 01000     | 13011       | The cluster status is read-only.                                                                                                                                                                                            |
 | 01000     | 13012       | The cluster status is unavailable.                                                                                                                                                                                          |
diff --git a/docs/document/content/user-manual/error-code/sql-error-code.en.md b/docs/document/content/user-manual/error-code/sql-error-code.en.md
index 89f3b990383..a047ef463e7 100644
--- a/docs/document/content/user-manual/error-code/sql-error-code.en.md
+++ b/docs/document/content/user-manual/error-code/sql-error-code.en.md
@@ -52,6 +52,7 @@ SQL error codes provide by standard `SQL State`, `Vendor Code` and `Reason`, whi
 | SQL State | Vendor Code | Reason                                                                                                                                                                                                                      |
 |-----------|-------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
 | 08000     | 13000       | Can not register driver, reason is: %s                                                                                                                                                                                      |
+| 08000     | 13001       | Can not register SQL federation driver, reason is: %s                                                                                                                                                                       |
 | 01000     | 13010       | Circuit break open, the request has been ignored.                                                                                                                                                                           |
 | 01000     | 13011       | The cluster status is read-only.                                                                                                                                                                                            |
 | 01000     | 13012       | The cluster status is unavailable.                                                                                                                                                                                          |
diff --git a/kernel/sql-federation/executor/advanced/src/test/java/org/apache/shardingsphere/sqlfederation/advanced/AdvancedSQLFederationExecutorTest.java b/kernel/sql-federation/executor/advanced/src/test/java/org/apache/shardingsphere/sqlfederation/advanced/AdvancedSQLFederationExecutorTest.java
deleted file mode 100644
index ded974c29a3..00000000000
--- a/kernel/sql-federation/executor/advanced/src/test/java/org/apache/shardingsphere/sqlfederation/advanced/AdvancedSQLFederationExecutorTest.java
+++ /dev/null
@@ -1,93 +0,0 @@
-/*
- * 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.sqlfederation.advanced;
-
-import org.apache.shardingsphere.infra.database.type.dialect.H2DatabaseType;
-import org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutor;
-import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
-import org.apache.shardingsphere.infra.metadata.data.ShardingSphereData;
-import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
-import org.apache.shardingsphere.infra.metadata.database.resource.ShardingSphereResourceMetaData;
-import org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
-import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereColumn;
-import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
-import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
-import org.apache.shardingsphere.parser.config.SQLParserRuleConfiguration;
-import org.apache.shardingsphere.parser.rule.SQLParserRule;
-import org.apache.shardingsphere.sql.parser.api.CacheOption;
-import org.apache.shardingsphere.sqlfederation.spi.SQLFederationExecutor;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-
-import java.sql.Types;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
-
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-class AdvancedSQLFederationExecutorTest {
-    
-    @BeforeEach
-    void init() {
-        Map<String, ShardingSphereTable> tables = new HashMap<>(2, 1F);
-        tables.put("t_order_federate", createOrderTableMetaData());
-        tables.put("t_user_info", createUserInfoTableMetaData());
-        String schemaName = "federate_jdbc";
-        String databaseName = "database_name";
-        Map<String, ShardingSphereSchema> schemas = Collections.singletonMap(schemaName, new ShardingSphereSchema(tables, Collections.emptyMap()));
-        ShardingSphereDatabase database = new ShardingSphereDatabase(databaseName, new H2DatabaseType(), mockResourceMetaData(), null, schemas);
-        ShardingSphereRuleMetaData globalRuleMetaData = createGlobalRuleMetaData();
-        ShardingSphereMetaData metaData = mock(ShardingSphereMetaData.class);
-        when(metaData.getDatabase(databaseName)).thenReturn(database);
-        when(metaData.getGlobalRuleMetaData()).thenReturn(globalRuleMetaData);
-        SQLFederationExecutor sqlFederationExecutor = new AdvancedSQLFederationExecutor();
-        sqlFederationExecutor.init(databaseName, schemaName, metaData, new ShardingSphereData(), mock(JDBCExecutor.class));
-    }
-    
-    private ShardingSphereRuleMetaData createGlobalRuleMetaData() {
-        CacheOption cacheOption = new CacheOption(128, 1024L);
-        return new ShardingSphereRuleMetaData(Collections.singleton(new SQLParserRule(new SQLParserRuleConfiguration(false, cacheOption, cacheOption))));
-    }
-    
-    private ShardingSphereResourceMetaData mockResourceMetaData() {
-        ShardingSphereResourceMetaData result = mock(ShardingSphereResourceMetaData.class);
-        when(result.getStorageTypes()).thenReturn(Collections.singletonMap("ds_0", new H2DatabaseType()));
-        return result;
-    }
-    
-    private ShardingSphereTable createOrderTableMetaData() {
-        ShardingSphereColumn orderIdColumn = new ShardingSphereColumn("order_id", Types.VARCHAR, true, false, false, true, false);
-        ShardingSphereColumn userIdColumn = new ShardingSphereColumn("user_id", Types.VARCHAR, false, false, false, true, false);
-        ShardingSphereColumn statusColumn = new ShardingSphereColumn("status", Types.VARCHAR, false, false, false, true, false);
-        return new ShardingSphereTable("t_order_federate", Arrays.asList(orderIdColumn, userIdColumn, statusColumn), Collections.emptyList(), Collections.emptyList());
-    }
-    
-    private ShardingSphereTable createUserInfoTableMetaData() {
-        ShardingSphereColumn userIdColumn = new ShardingSphereColumn("user_id", Types.VARCHAR, true, false, false, true, false);
-        ShardingSphereColumn informationColumn = new ShardingSphereColumn("information", Types.VARCHAR, false, false, false, true, false);
-        return new ShardingSphereTable("t_user_info", Arrays.asList(userIdColumn, informationColumn), Collections.emptyList(), Collections.emptyList());
-    }
-    
-    @Test
-    void assertSelectWhereSingleField() {
-        // TODO add executor.executeQuery()
-    }
-}
diff --git a/kernel/sql-federation/executor/core/src/main/java/org/apache/shardingsphere/sqlfederation/exception/SQLFederationDriverRegisterException.java b/kernel/sql-federation/executor/core/src/main/java/org/apache/shardingsphere/sqlfederation/exception/SQLFederationDriverRegisterException.java
new file mode 100644
index 00000000000..633aa5a5c6e
--- /dev/null
+++ b/kernel/sql-federation/executor/core/src/main/java/org/apache/shardingsphere/sqlfederation/exception/SQLFederationDriverRegisterException.java
@@ -0,0 +1,33 @@
+/*
+ * 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.sqlfederation.exception;
+
+import org.apache.shardingsphere.infra.exception.ConnectionSQLException;
+import org.apache.shardingsphere.infra.util.exception.external.sql.sqlstate.XOpenSQLState;
+
+/**
+ * SQL federation driver register exception.
+ */
+public final class SQLFederationDriverRegisterException extends ConnectionSQLException {
+    
+    private static final long serialVersionUID = -2853184636838700216L;
+    
+    public SQLFederationDriverRegisterException(final String message) {
+        super(XOpenSQLState.CONNECTION_EXCEPTION, 1, "Can not register SQL federation driver, reason is: %s", message);
+    }
+}
diff --git a/kernel/sql-federation/executor/original/src/main/java/org/apache/shardingsphere/sqlfederation/original/OriginalSQLFederationExecutor.java b/kernel/sql-federation/executor/original/src/main/java/org/apache/shardingsphere/sqlfederation/original/OriginalSQLFederationExecutor.java
index 8b98a2542f1..75099b9bd92 100644
--- a/kernel/sql-federation/executor/original/src/main/java/org/apache/shardingsphere/sqlfederation/original/OriginalSQLFederationExecutor.java
+++ b/kernel/sql-federation/executor/original/src/main/java/org/apache/shardingsphere/sqlfederation/original/OriginalSQLFederationExecutor.java
@@ -31,6 +31,7 @@ import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
 import org.apache.shardingsphere.infra.metadata.data.ShardingSphereData;
 import org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
 import org.apache.shardingsphere.sql.parser.sql.common.util.SQLUtils;
+import org.apache.shardingsphere.sqlfederation.exception.SQLFederationDriverRegisterException;
 import org.apache.shardingsphere.sqlfederation.executor.FilterableTableScanExecutor;
 import org.apache.shardingsphere.sqlfederation.executor.TableScanExecutorContext;
 import org.apache.shardingsphere.sqlfederation.optimizer.context.OptimizerContext;
@@ -80,7 +81,7 @@ public final class OriginalSQLFederationExecutor implements SQLFederationExecuto
         try {
             Class.forName(DRIVER_NAME);
         } catch (final ClassNotFoundException ex) {
-            throw new RuntimeException(ex);
+            throw new SQLFederationDriverRegisterException(ex.getMessage());
         }
     }
     
@@ -105,7 +106,8 @@ public final class OriginalSQLFederationExecutor implements SQLFederationExecuto
     }
     
     private Connection createConnection(final DriverExecutionPrepareEngine<JDBCExecutionUnit, Connection> prepareEngine,
-                                        final JDBCExecutorCallback<? extends ExecuteResult> callback, final SQLFederationExecutorContext federationContext) throws SQLException {
+                                        final JDBCExecutorCallback<? extends ExecuteResult> callback,
+                                        final SQLFederationExecutorContext federationContext) throws SQLException {
         Connection result = DriverManager.getConnection(CONNECTION_URL, optimizerContext.getParserContext(databaseName).getDialectProps());
         addSchema(result.unwrap(CalciteConnection.class), prepareEngine, callback, federationContext);
         return result;