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 aed1a6772ef Refactor PrepareJobWithoutUserException (#21012)
aed1a6772ef is described below

commit aed1a6772ef03ce80f9da343918dec287fe24e4c
Author: Liang Zhang <[email protected]>
AuthorDate: Fri Sep 16 14:04:05 2022 +0800

    Refactor PrepareJobWithoutUserException (#21012)
---
 .../user-manual/error-code/sql-error-code.cn.md    |  1 +
 .../user-manual/error-code/sql-error-code.en.md    |  1 +
 .../job/PrepareJobWithoutUserException.java        | 33 ++++++++++++++++++++++
 .../datasource/OpenGaussDataSourceChecker.java     |  6 ++--
 .../datasource/PostgreSQLDataSourceChecker.java    |  6 ++--
 5 files changed, 41 insertions(+), 6 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 5e364d00a45..b485a76384b 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
@@ -99,6 +99,7 @@ SQL 错误码以标准的 SQL State,Vendor Code 和详细错误信息提供,
 | HY000     | 18084       | Target table \`%s\` is not empty |
 | 01007     | 18085       | Source data source lacks %s privilege(s) |
 | HY000     | 18086       | Source data source required \`%s = %s\`, now is 
\`%s\` |
+| HY000     | 18087       | User \`%s\` does exist |
 | HY000     | 18090       | Importer job write data failed |
 | HY000     | 18091       | Can not poll event because of binlog sync channel 
already closed |
 | HY000     | 18092       | Task \`%s\` execute failed |
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 39cc672679e..2472ef0f813 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
@@ -99,6 +99,7 @@ SQL error codes provide by standard `SQL State`, `Vendor 
Code` and `Reason`, whi
 | HY000     | 18084       | Target table \`%s\` is not empty |
 | 01007     | 18085       | Source data source lacks %s privilege(s) |
 | HY000     | 18086       | Source data source required \`%s = %s\`, now is 
\`%s\` |
+| HY000     | 18087       | User \`%s\` does exist |
 | HY000     | 18090       | Importer job write data failed |
 | HY000     | 18091       | Can not poll event because of binlog sync channel 
already closed |
 | HY000     | 18092       | Task \`%s\` execute failed |
diff --git 
a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/exception/job/PrepareJobWithoutUserException.java
 
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/exception/job/PrepareJobWithoutUserException.java
new file mode 100644
index 00000000000..d80861dc9ae
--- /dev/null
+++ 
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/exception/job/PrepareJobWithoutUserException.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.data.pipeline.core.exception.job;
+
+import 
org.apache.shardingsphere.data.pipeline.core.exception.PipelineSQLException;
+import 
org.apache.shardingsphere.infra.util.exception.external.sql.sqlstate.XOpenSQLState;
+
+/**
+ * Prepare job without user exception.
+ */
+public final class PrepareJobWithoutUserException extends PipelineSQLException 
{
+    
+    private static final long serialVersionUID = 7250019436391155770L;
+    
+    public PrepareJobWithoutUserException(final String username) {
+        super(XOpenSQLState.PRIVILEGE_NOT_GRANTED, 87, "User `%s` does exist", 
username);
+    }
+}
diff --git 
a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-opengauss/src/main/java/org/apache/shardingsphere/data/pipeline/opengauss/check/datasource/OpenGaussDataSourceChecker.java
 
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-opengauss/src/main/java/org/apache/shardingsphere/data/pipeline/opengauss/check/datasource/OpenGaussDataSourceChecker.java
index 1e141512a28..18a4d77280d 100644
--- 
a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-opengauss/src/main/java/org/apache/shardingsphere/data/pipeline/opengauss/check/datasource/OpenGaussDataSourceChecker.java
+++ 
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-opengauss/src/main/java/org/apache/shardingsphere/data/pipeline/opengauss/check/datasource/OpenGaussDataSourceChecker.java
@@ -22,6 +22,7 @@ import org.apache.commons.lang3.StringUtils;
 import 
org.apache.shardingsphere.data.pipeline.core.check.datasource.AbstractDataSourceChecker;
 import 
org.apache.shardingsphere.data.pipeline.core.exception.job.PipelineJobPrepareFailedException;
 import 
org.apache.shardingsphere.data.pipeline.core.exception.job.PrepareJobWithoutEnoughPrivilegeException;
+import 
org.apache.shardingsphere.data.pipeline.core.exception.job.PrepareJobWithoutUserException;
 import 
org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
 
 import javax.sql.DataSource;
@@ -53,9 +54,8 @@ public final class OpenGaussDataSourceChecker extends 
AbstractDataSourceChecker
             DatabaseMetaData metaData = connection.getMetaData();
             preparedStatement.setString(1, metaData.getUserName());
             try (ResultSet resultSet = preparedStatement.executeQuery()) {
-                if (!resultSet.next()) {
-                    throw new 
PipelineJobPrepareFailedException(String.format("No role exists, rolname: %s.", 
metaData.getUserName()));
-                }
+                String username = metaData.getUserName();
+                ShardingSpherePreconditions.checkState(resultSet.next(), () -> 
new PrepareJobWithoutUserException(username));
                 String isSuperRole = resultSet.getString("rolsuper");
                 String isReplicationRole = 
resultSet.getString("rolreplication");
                 String isSystemAdminRole = 
resultSet.getString("rolsystemadmin");
diff --git 
a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/check/datasource/PostgreSQLDataSourceChecker.java
 
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/check/datasource/PostgreSQLDataSourceChecker.java
index c16589ce4f7..cfea7e8eba8 100644
--- 
a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/check/datasource/PostgreSQLDataSourceChecker.java
+++ 
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/check/datasource/PostgreSQLDataSourceChecker.java
@@ -22,6 +22,7 @@ import org.apache.commons.lang3.StringUtils;
 import 
org.apache.shardingsphere.data.pipeline.core.check.datasource.AbstractDataSourceChecker;
 import 
org.apache.shardingsphere.data.pipeline.core.exception.job.PipelineJobPrepareFailedException;
 import 
org.apache.shardingsphere.data.pipeline.core.exception.job.PrepareJobWithoutEnoughPrivilegeException;
+import 
org.apache.shardingsphere.data.pipeline.core.exception.job.PrepareJobWithoutUserException;
 import 
org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
 
 import javax.sql.DataSource;
@@ -53,9 +54,8 @@ public class PostgreSQLDataSourceChecker extends 
AbstractDataSourceChecker {
             DatabaseMetaData metaData = connection.getMetaData();
             preparedStatement.setString(1, metaData.getUserName());
             try (ResultSet resultSet = preparedStatement.executeQuery()) {
-                if (!resultSet.next()) {
-                    throw new 
PipelineJobPrepareFailedException(String.format("No role exists, rolname: %s.", 
metaData.getUserName()));
-                }
+                String username = metaData.getUserName();
+                ShardingSpherePreconditions.checkState(resultSet.next(), () -> 
new PrepareJobWithoutUserException(username));
                 String isSuperRole = resultSet.getString("rolsuper");
                 String isReplicationRole = 
resultSet.getString("rolreplication");
                 log.info("checkPrivilege: isSuperRole: {}, isReplicationRole: 
{}", isSuperRole, isReplicationRole);

Reply via email to