This is an automated email from the ASF dual-hosted git repository.
menghaoran 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 e06883fc926 Reuse SQLExceptionTransformEngine in jdbc module (#20695)
e06883fc926 is described below
commit e06883fc9264f93dc913a3512a88551be035ffc3
Author: Liang Zhang <[email protected]>
AuthorDate: Thu Sep 1 07:14:41 2022 +0800
Reuse SQLExceptionTransformEngine in jdbc module (#20695)
---
.../dialect/SQLExceptionTransformEngine.java | 7 ++-
.../mapper/SQLDialectExceptionMapperFactory.java | 10 ++--
.../SQLDialectExceptionMapperFactoryTest.java | 4 +-
.../shardingsphere-jdbc-core/pom.xml | 12 +++++
.../statement/ShardingSpherePreparedStatement.java | 25 ++++++----
.../core/statement/ShardingSphereStatement.java | 55 +++++++++++++++-------
6 files changed, 81 insertions(+), 32 deletions(-)
diff --git
a/shardingsphere-dialect-exception/shardingsphere-dialect-exception-core/src/main/java/org/apache/shardingsphere/dialect/SQLExceptionTransformEngine.java
b/shardingsphere-dialect-exception/shardingsphere-dialect-exception-core/src/main/java/org/apache/shardingsphere/dialect/SQLExceptionTransformEngine.java
index 823b5e5b3bf..7145cf72a2e 100644
---
a/shardingsphere-dialect-exception/shardingsphere-dialect-exception-core/src/main/java/org/apache/shardingsphere/dialect/SQLExceptionTransformEngine.java
+++
b/shardingsphere-dialect-exception/shardingsphere-dialect-exception-core/src/main/java/org/apache/shardingsphere/dialect/SQLExceptionTransformEngine.java
@@ -20,11 +20,13 @@ package org.apache.shardingsphere.dialect;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.dialect.exception.SQLDialectException;
+import org.apache.shardingsphere.dialect.mapper.SQLDialectExceptionMapper;
import
org.apache.shardingsphere.dialect.mapper.SQLDialectExceptionMapperFactory;
import
org.apache.shardingsphere.infra.util.exception.sql.ShardingSphereSQLException;
import org.apache.shardingsphere.infra.util.exception.sql.UnknownSQLException;
import java.sql.SQLException;
+import java.util.Optional;
/**
* SQL Exception transform engine.
@@ -47,7 +49,10 @@ public final class SQLExceptionTransformEngine {
return ((ShardingSphereSQLException) cause).toSQLException();
}
if (cause instanceof SQLDialectException) {
- return
SQLDialectExceptionMapperFactory.getInstance(databaseType).convert((SQLDialectException)
cause);
+ Optional<SQLDialectExceptionMapper> dialectExceptionMapper =
SQLDialectExceptionMapperFactory.findInstance(databaseType);
+ if (dialectExceptionMapper.isPresent()) {
+ return
dialectExceptionMapper.get().convert((SQLDialectException) cause);
+ }
}
return new UnknownSQLException(cause).toSQLException();
}
diff --git
a/shardingsphere-dialect-exception/shardingsphere-dialect-exception-core/src/main/java/org/apache/shardingsphere/dialect/mapper/SQLDialectExceptionMapperFactory.java
b/shardingsphere-dialect-exception/shardingsphere-dialect-exception-core/src/main/java/org/apache/shardingsphere/dialect/mapper/SQLDialectExceptionMapperFactory.java
index 5103dacb701..485b286f5c4 100644
---
a/shardingsphere-dialect-exception/shardingsphere-dialect-exception-core/src/main/java/org/apache/shardingsphere/dialect/mapper/SQLDialectExceptionMapperFactory.java
+++
b/shardingsphere-dialect-exception/shardingsphere-dialect-exception-core/src/main/java/org/apache/shardingsphere/dialect/mapper/SQLDialectExceptionMapperFactory.java
@@ -22,6 +22,8 @@ import lombok.NoArgsConstructor;
import org.apache.shardingsphere.infra.util.spi.ShardingSphereServiceLoader;
import org.apache.shardingsphere.infra.util.spi.type.typed.TypedSPIRegistry;
+import java.util.Optional;
+
/**
* SQL dialect exception mapper factory.
*/
@@ -33,12 +35,12 @@ public final class SQLDialectExceptionMapperFactory {
}
/**
- * Get instance of SQL dialect exception mapper.
+ * Find instance of SQL dialect exception mapper.
*
* @param databaseType database type
- * @return got instance
+ * @return found instance
*/
- public static SQLDialectExceptionMapper getInstance(final String
databaseType) {
- return
TypedSPIRegistry.getRegisteredService(SQLDialectExceptionMapper.class,
databaseType);
+ public static Optional<SQLDialectExceptionMapper> findInstance(final
String databaseType) {
+ return
TypedSPIRegistry.findRegisteredService(SQLDialectExceptionMapper.class,
databaseType);
}
}
diff --git
a/shardingsphere-dialect-exception/shardingsphere-dialect-exception-core/src/test/java/org/apache/shardingsphere/dialect/mapper/SQLDialectExceptionMapperFactoryTest.java
b/shardingsphere-dialect-exception/shardingsphere-dialect-exception-core/src/test/java/org/apache/shardingsphere/dialect/mapper/SQLDialectExceptionMapperFactoryTest.java
index 7af91f27f62..15c25f787e4 100644
---
a/shardingsphere-dialect-exception/shardingsphere-dialect-exception-core/src/test/java/org/apache/shardingsphere/dialect/mapper/SQLDialectExceptionMapperFactoryTest.java
+++
b/shardingsphere-dialect-exception/shardingsphere-dialect-exception-core/src/test/java/org/apache/shardingsphere/dialect/mapper/SQLDialectExceptionMapperFactoryTest.java
@@ -22,11 +22,13 @@ import org.junit.Test;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
public final class SQLDialectExceptionMapperFactoryTest {
@Test
public void assertGetInstance() {
- assertThat(SQLDialectExceptionMapperFactory.getInstance("FIXTURE"),
instanceOf(FixtureSQLDialectExceptionMapper.class));
+
assertTrue(SQLDialectExceptionMapperFactory.findInstance("FIXTURE").isPresent());
+
assertThat(SQLDialectExceptionMapperFactory.findInstance("FIXTURE").get(),
instanceOf(FixtureSQLDialectExceptionMapper.class));
}
}
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/pom.xml
b/shardingsphere-jdbc/shardingsphere-jdbc-core/pom.xml
index 6f62069e643..a64da39f2ec 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/pom.xml
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/pom.xml
@@ -63,6 +63,18 @@
<artifactId>shardingsphere-sql-parser-opengauss</artifactId>
<version>${project.version}</version>
</dependency>
+
+ <dependency>
+ <groupId>org.apache.shardingsphere</groupId>
+ <artifactId>shardingsphere-mysql-dialect-exception</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.shardingsphere</groupId>
+
<artifactId>shardingsphere-postgresql-dialect-exception</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-authority-core</artifactId>
diff --git
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java
index 9bfd3a30ecc..87aa9496f96 100644
---
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java
+++
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java
@@ -20,6 +20,7 @@ package org.apache.shardingsphere.driver.jdbc.core.statement;
import com.google.common.base.Strings;
import lombok.AccessLevel;
import lombok.Getter;
+import org.apache.shardingsphere.dialect.SQLExceptionTransformEngine;
import org.apache.shardingsphere.driver.executor.DriverExecutor;
import org.apache.shardingsphere.driver.executor.batch.BatchExecutionUnit;
import
org.apache.shardingsphere.driver.executor.batch.BatchPreparedStatementExecutor;
@@ -235,9 +236,11 @@ public final class ShardingSpherePreparedStatement extends
AbstractPreparedState
Map<String, Integer> columnLabelAndIndexMap = null !=
this.columnLabelAndIndexMap ? this.columnLabelAndIndexMap
: (this.columnLabelAndIndexMap =
ShardingSphereResultSetUtil.createColumnLabelAndIndexMap(sqlStatementContext,
resultSets.get(0).getMetaData()));
result = new ShardingSphereResultSet(resultSets, mergedResult,
this, executionContext, columnLabelAndIndexMap);
- } catch (SQLException ex) {
+ // CHECKSTYLE:OFF
+ } catch (final Exception ex) {
+ // CHECKSTYLE:ON
handleExceptionInTransaction(connection, metaDataContexts);
- throw ex;
+ throw SQLExceptionTransformEngine.toSQLException(ex,
metaDataContexts.getMetaData().getDatabase(connection.getDatabaseName()).getResource().getDatabaseType().getType());
} finally {
clearBatch();
}
@@ -335,9 +338,11 @@ public final class ShardingSpherePreparedStatement extends
AbstractPreparedState
cacheStatements(executionGroupContext.getInputGroups());
return
executor.getRegularExecutor().executeUpdate(executionGroupContext,
executionContext.getQueryContext(),
executionContext.getRouteContext().getRouteUnits(),
createExecuteUpdateCallback());
- } catch (SQLException ex) {
+ // CHECKSTYLE:OFF
+ } catch (final Exception ex) {
+ // CHECKSTYLE:ON
handleExceptionInTransaction(connection, metaDataContexts);
- throw ex;
+ throw SQLExceptionTransformEngine.toSQLException(ex,
metaDataContexts.getMetaData().getDatabase(connection.getDatabaseName()).getResource().getDatabaseType().getType());
} finally {
clearBatch();
}
@@ -398,9 +403,11 @@ public final class ShardingSpherePreparedStatement extends
AbstractPreparedState
cacheStatements(executionGroupContext.getInputGroups());
return executor.getRegularExecutor().execute(executionGroupContext,
executionContext.getQueryContext(),
executionContext.getRouteContext().getRouteUnits(), createExecuteCallback());
- } catch (SQLException ex) {
+ // CHECKSTYLE:OFF
+ } catch (final Exception ex) {
+ // CHECKSTYLE:ON
handleExceptionInTransaction(connection, metaDataContexts);
- throw ex;
+ throw SQLExceptionTransformEngine.toSQLException(ex,
metaDataContexts.getMetaData().getDatabase(connection.getDatabaseName()).getResource().getDatabaseType().getType());
} finally {
clearBatch();
}
@@ -584,9 +591,11 @@ public final class ShardingSpherePreparedStatement extends
AbstractPreparedState
// TODO add raw SQL executor
initBatchPreparedStatementExecutor();
return
batchPreparedStatementExecutor.executeBatch(executionContext.getSqlStatementContext());
- } catch (SQLException ex) {
+ // CHECKSTYLE:OFF
+ } catch (final Exception ex) {
+ // CHECKSTYLE:ON
handleExceptionInTransaction(connection, metaDataContexts);
- throw ex;
+ throw SQLExceptionTransformEngine.toSQLException(ex,
metaDataContexts.getMetaData().getDatabase(connection.getDatabaseName()).getResource().getDatabaseType().getType());
} finally {
clearBatch();
}
diff --git
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java
index 216bfb3062a..da35c83bda4 100644
---
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java
+++
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java
@@ -20,6 +20,7 @@ package org.apache.shardingsphere.driver.jdbc.core.statement;
import com.google.common.base.Strings;
import lombok.AccessLevel;
import lombok.Getter;
+import org.apache.shardingsphere.dialect.SQLExceptionTransformEngine;
import org.apache.shardingsphere.driver.executor.DriverExecutor;
import org.apache.shardingsphere.driver.executor.callback.ExecuteCallback;
import
org.apache.shardingsphere.driver.executor.callback.ExecuteUpdateCallback;
@@ -172,9 +173,11 @@ public final class ShardingSphereStatement extends
AbstractStatementAdapter {
List<QueryResult> queryResults = executeQuery0();
MergedResult mergedResult = mergeQuery(queryResults);
result = new ShardingSphereResultSet(getResultSets(),
mergedResult, this, executionContext);
- } catch (SQLException ex) {
+ // CHECKSTYLE:OFF
+ } catch (final Exception ex) {
+ // CHECKSTYLE:ON
handleExceptionInTransaction(connection, metaDataContexts);
- throw ex;
+ throw SQLExceptionTransformEngine.toSQLException(ex,
metaDataContexts.getMetaData().getDatabase(connection.getDatabaseName()).getResource().getDatabaseType().getType());
} finally {
currentResultSet = null;
}
@@ -249,9 +252,11 @@ public final class ShardingSphereStatement extends
AbstractStatementAdapter {
cacheStatements(executionGroupContext.getInputGroups());
return executeUpdate(executionGroupContext, (actualSQL, statement)
-> statement.executeUpdate(actualSQL),
executionContext.getSqlStatementContext(),
executionContext.getRouteContext().getRouteUnits());
- } catch (SQLException ex) {
+ // CHECKSTYLE:OFF
+ } catch (final Exception ex) {
+ // CHECKSTYLE:ON
handleExceptionInTransaction(connection, metaDataContexts);
- throw ex;
+ throw SQLExceptionTransformEngine.toSQLException(ex,
metaDataContexts.getMetaData().getDatabase(connection.getDatabaseName()).getResource().getDatabaseType().getType());
} finally {
currentResultSet = null;
}
@@ -278,9 +283,11 @@ public final class ShardingSphereStatement extends
AbstractStatementAdapter {
cacheStatements(executionGroupContext.getInputGroups());
return executeUpdate(executionGroupContext, (actualSQL, statement)
-> statement.executeUpdate(actualSQL, autoGeneratedKeys),
executionContext.getSqlStatementContext(),
executionContext.getRouteContext().getRouteUnits());
- } catch (SQLException ex) {
+ // CHECKSTYLE:OFF
+ } catch (final Exception ex) {
+ // CHECKSTYLE:ON
handleExceptionInTransaction(connection, metaDataContexts);
- throw ex;
+ throw SQLExceptionTransformEngine.toSQLException(ex,
metaDataContexts.getMetaData().getDatabase(connection.getDatabaseName()).getResource().getDatabaseType().getType());
} finally {
currentResultSet = null;
}
@@ -305,9 +312,11 @@ public final class ShardingSphereStatement extends
AbstractStatementAdapter {
cacheStatements(executionGroups.getInputGroups());
return executeUpdate(executionGroups, (actualSQL, statement) ->
statement.executeUpdate(actualSQL, columnIndexes),
executionContext.getSqlStatementContext(),
executionContext.getRouteContext().getRouteUnits());
- } catch (SQLException ex) {
+ // CHECKSTYLE:OFF
+ } catch (final Exception ex) {
+ // CHECKSTYLE:ON
handleExceptionInTransaction(connection, metaDataContexts);
- throw ex;
+ throw SQLExceptionTransformEngine.toSQLException(ex,
metaDataContexts.getMetaData().getDatabase(connection.getDatabaseName()).getResource().getDatabaseType().getType());
} finally {
currentResultSet = null;
}
@@ -332,9 +341,11 @@ public final class ShardingSphereStatement extends
AbstractStatementAdapter {
cacheStatements(executionGroupContext.getInputGroups());
return executeUpdate(executionGroupContext, (actualSQL, statement)
-> statement.executeUpdate(actualSQL, columnNames),
executionContext.getSqlStatementContext(),
executionContext.getRouteContext().getRouteUnits());
- } catch (SQLException ex) {
+ // CHECKSTYLE:OFF
+ } catch (final Exception ex) {
+ // CHECKSTYLE:ON
handleExceptionInTransaction(connection, metaDataContexts);
- throw ex;
+ throw SQLExceptionTransformEngine.toSQLException(ex,
metaDataContexts.getMetaData().getDatabase(connection.getDatabaseName()).getResource().getDatabaseType().getType());
} finally {
currentResultSet = null;
}
@@ -372,9 +383,11 @@ public final class ShardingSphereStatement extends
AbstractStatementAdapter {
public boolean execute(final String sql) throws SQLException {
try {
return execute0(sql, (actualSQL, statement) ->
statement.execute(actualSQL));
- } catch (SQLException ex) {
+ // CHECKSTYLE:OFF
+ } catch (final SQLException ex) {
+ // CHECKSTYLE:ON
handleExceptionInTransaction(connection, metaDataContexts);
- throw ex;
+ throw SQLExceptionTransformEngine.toSQLException(ex,
metaDataContexts.getMetaData().getDatabase(connection.getDatabaseName()).getResource().getDatabaseType().getType());
}
}
@@ -385,9 +398,11 @@ public final class ShardingSphereStatement extends
AbstractStatementAdapter {
returnGeneratedKeys = true;
}
return execute0(sql, (actualSQL, statement) ->
statement.execute(actualSQL, autoGeneratedKeys));
- } catch (SQLException ex) {
+ // CHECKSTYLE:OFF
+ } catch (final SQLException ex) {
+ // CHECKSTYLE:ON
handleExceptionInTransaction(connection, metaDataContexts);
- throw ex;
+ throw SQLExceptionTransformEngine.toSQLException(ex,
metaDataContexts.getMetaData().getDatabase(connection.getDatabaseName()).getResource().getDatabaseType().getType());
}
}
@@ -396,9 +411,11 @@ public final class ShardingSphereStatement extends
AbstractStatementAdapter {
try {
returnGeneratedKeys = true;
return execute0(sql, (actualSQL, statement) ->
statement.execute(actualSQL, columnIndexes));
- } catch (SQLException ex) {
+ // CHECKSTYLE:OFF
+ } catch (final SQLException ex) {
+ // CHECKSTYLE:ON
handleExceptionInTransaction(connection, metaDataContexts);
- throw ex;
+ throw SQLExceptionTransformEngine.toSQLException(ex,
metaDataContexts.getMetaData().getDatabase(connection.getDatabaseName()).getResource().getDatabaseType().getType());
}
}
@@ -407,9 +424,11 @@ public final class ShardingSphereStatement extends
AbstractStatementAdapter {
try {
returnGeneratedKeys = true;
return execute0(sql, (actualSQL, statement) ->
statement.execute(actualSQL, columnNames));
- } catch (SQLException ex) {
+ // CHECKSTYLE:OFF
+ } catch (final SQLException ex) {
+ // CHECKSTYLE:ON
handleExceptionInTransaction(connection, metaDataContexts);
- throw ex;
+ throw SQLExceptionTransformEngine.toSQLException(ex,
metaDataContexts.getMetaData().getDatabase(connection.getDatabaseName()).getResource().getDatabaseType().getType());
}
}