You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by ji...@apache.org on 2022/09/06 05:31:53 UTC
[shardingsphere] branch master updated: Add ShardingSpherePreconditions.checkState with SQLException (#20820)
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 55d178ead06 Add ShardingSpherePreconditions.checkState with SQLException (#20820)
55d178ead06 is described below
commit 55d178ead06fc1fca76810a07296ea87bbd62a5c
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Tue Sep 6 13:31:43 2022 +0800
Add ShardingSpherePreconditions.checkState with SQLException (#20820)
---
.../database/type/dialect/OpenGaussDatabaseType.java | 6 +++---
.../database/type/dialect/PostgreSQLDatabaseType.java | 6 +++---
.../impl/driver/jdbc/type/util/ResultSetUtil.java | 5 ++---
.../merge/result/impl/memory/MemoryMergedResult.java | 6 +++---
.../util/exception/ShardingSpherePreconditions.java | 15 +++++++++++++++
.../driver/jdbc/core/ShardingSphereSavepoint.java | 6 +++---
.../core/connection/ShardingSphereConnection.java | 9 +++------
.../jdbc/core/resultset/ShardingSphereResultSet.java | 19 ++++++++++---------
.../transaction/TransactionBackendHandler.java | 18 +++++++-----------
9 files changed, 49 insertions(+), 41 deletions(-)
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/database/type/dialect/OpenGaussDatabaseType.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/database/type/dialect/OpenGaussDatabaseType.java
index 4355c8ef958..27f59bb64da 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/database/type/dialect/OpenGaussDatabaseType.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/database/type/dialect/OpenGaussDatabaseType.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.infra.database.type.dialect;
import org.apache.shardingsphere.infra.database.metadata.dialect.OpenGaussDataSourceMetaData;
import org.apache.shardingsphere.infra.database.type.SchemaSupportedDatabaseType;
+import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
import org.apache.shardingsphere.sql.parser.sql.common.constant.QuoteCharacter;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.tcl.CommitStatement;
@@ -64,9 +65,8 @@ public final class OpenGaussDatabaseType implements SchemaSupportedDatabaseType
@Override
public void handleRollbackOnly(final boolean rollbackOnly, final SQLStatement statement) throws SQLException {
- if (rollbackOnly && !(statement instanceof CommitStatement) && !(statement instanceof RollbackStatement)) {
- throw new SQLFeatureNotSupportedException("Current transaction is aborted, commands ignored until end of transaction block.");
- }
+ ShardingSpherePreconditions.checkState(!rollbackOnly || statement instanceof CommitStatement || statement instanceof RollbackStatement,
+ new SQLFeatureNotSupportedException("Current transaction is aborted, commands ignored until end of transaction block."));
}
@Override
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/database/type/dialect/PostgreSQLDatabaseType.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/database/type/dialect/PostgreSQLDatabaseType.java
index 5dd9b1f9213..eee522d04c8 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/database/type/dialect/PostgreSQLDatabaseType.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/database/type/dialect/PostgreSQLDatabaseType.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.infra.database.type.dialect;
import org.apache.shardingsphere.infra.database.metadata.dialect.PostgreSQLDataSourceMetaData;
import org.apache.shardingsphere.infra.database.type.SchemaSupportedDatabaseType;
+import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
import org.apache.shardingsphere.sql.parser.sql.common.constant.QuoteCharacter;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.tcl.CommitStatement;
@@ -63,9 +64,8 @@ public final class PostgreSQLDatabaseType implements SchemaSupportedDatabaseType
@Override
public void handleRollbackOnly(final boolean rollbackOnly, final SQLStatement statement) throws SQLException {
- if (rollbackOnly && !(statement instanceof CommitStatement) && !(statement instanceof RollbackStatement)) {
- throw new SQLFeatureNotSupportedException("Current transaction is aborted, commands ignored until end of transaction block.");
- }
+ ShardingSpherePreconditions.checkState(!rollbackOnly || statement instanceof CommitStatement || statement instanceof RollbackStatement,
+ new SQLFeatureNotSupportedException("Current transaction is aborted, commands ignored until end of transaction block."));
}
@Override
diff --git a/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/execute/result/query/impl/driver/jdbc/type/util/ResultSetUtil.java b/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/execute/result/query/impl/driver/jdbc/type/util/ResultSetUtil.java
index bea95d6e605..da89a6768a8 100644
--- a/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/execute/result/query/impl/driver/jdbc/type/util/ResultSetUtil.java
+++ b/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/execute/result/query/impl/driver/jdbc/type/util/ResultSetUtil.java
@@ -23,6 +23,7 @@ import com.google.common.primitives.Shorts;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.infra.executor.exception.UnsupportedDataTypeConversionException;
+import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
import java.math.BigDecimal;
import java.math.RoundingMode;
@@ -54,9 +55,7 @@ public final class ResultSetUtil {
* @throws SQLException SQL exception
*/
public static Object convertValue(final Object value, final Class<?> convertType) throws SQLException {
- if (null == convertType) {
- throw new SQLFeatureNotSupportedException("Type cannot be null");
- }
+ ShardingSpherePreconditions.checkState(null != convertType, new SQLFeatureNotSupportedException("Type can not be null"));
if (null == value) {
return convertNullValue(convertType);
}
diff --git a/shardingsphere-infra/shardingsphere-infra-merge/src/main/java/org/apache/shardingsphere/infra/merge/result/impl/memory/MemoryMergedResult.java b/shardingsphere-infra/shardingsphere-infra-merge/src/main/java/org/apache/shardingsphere/infra/merge/result/impl/memory/MemoryMergedResult.java
index 57c48c3eee0..ec7c405afef 100644
--- a/shardingsphere-infra/shardingsphere-infra-merge/src/main/java/org/apache/shardingsphere/infra/merge/result/impl/memory/MemoryMergedResult.java
+++ b/shardingsphere-infra/shardingsphere-infra-merge/src/main/java/org/apache/shardingsphere/infra/merge/result/impl/memory/MemoryMergedResult.java
@@ -22,6 +22,7 @@ import org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryRe
import org.apache.shardingsphere.infra.merge.result.MergedResult;
import org.apache.shardingsphere.infra.metadata.database.schema.decorator.model.ShardingSphereSchema;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
+import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
import java.io.InputStream;
import java.io.Reader;
@@ -68,9 +69,8 @@ public abstract class MemoryMergedResult<T extends ShardingSphereRule> implement
@Override
public final Object getValue(final int columnIndex, final Class<?> type) throws SQLException {
- if (Blob.class == type || Clob.class == type || Reader.class == type || InputStream.class == type || SQLXML.class == type) {
- throw new SQLFeatureNotSupportedException(String.format("Get value from `%s`", type.getName()));
- }
+ ShardingSpherePreconditions.checkState(Blob.class != type && Clob.class != type && Reader.class != type && InputStream.class != type && SQLXML.class != type,
+ new SQLFeatureNotSupportedException(String.format("Get value from `%s`", type.getName())));
Object result = currentResultSetRow.getCell(columnIndex);
wasNull = null == result;
return result;
diff --git a/shardingsphere-infra/shardingsphere-infra-util/src/main/java/org/apache/shardingsphere/infra/util/exception/ShardingSpherePreconditions.java b/shardingsphere-infra/shardingsphere-infra-util/src/main/java/org/apache/shardingsphere/infra/util/exception/ShardingSpherePreconditions.java
index 8457a6dcdf9..4a004fa96ea 100644
--- a/shardingsphere-infra/shardingsphere-infra-util/src/main/java/org/apache/shardingsphere/infra/util/exception/ShardingSpherePreconditions.java
+++ b/shardingsphere-infra/shardingsphere-infra-util/src/main/java/org/apache/shardingsphere/infra/util/exception/ShardingSpherePreconditions.java
@@ -22,6 +22,8 @@ import lombok.NoArgsConstructor;
import org.apache.shardingsphere.infra.util.exception.external.ShardingSphereExternalException;
import org.apache.shardingsphere.infra.util.exception.internal.ShardingSphereInternalException;
+import java.sql.SQLException;
+
/**
* ShardingSphere preconditions.
*/
@@ -52,4 +54,17 @@ public final class ShardingSpherePreconditions {
throw exceptionIfUnexpected;
}
}
+
+ /**
+ * Ensures the truth of an expression involving the state of the calling instance.
+ *
+ * @param expectedExpression expected expression
+ * @param exceptionIfUnexpected exception thrown if expression is unexpected
+ * @throws SQLException SQL exception
+ */
+ public static void checkState(final boolean expectedExpression, final SQLException exceptionIfUnexpected) throws SQLException {
+ if (!expectedExpression) {
+ throw exceptionIfUnexpected;
+ }
+ }
}
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/ShardingSphereSavepoint.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/ShardingSphereSavepoint.java
index d625edba5d2..0b61080e7af 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/ShardingSphereSavepoint.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/ShardingSphereSavepoint.java
@@ -17,6 +17,8 @@
package org.apache.shardingsphere.driver.jdbc.core;
+import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
+
import java.rmi.server.UID;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
@@ -34,9 +36,7 @@ public final class ShardingSphereSavepoint implements Savepoint {
}
public ShardingSphereSavepoint(final String name) throws SQLException {
- if (null == name || 0 == name.length()) {
- throw new SQLFeatureNotSupportedException("Savepoint name can not be NULL or empty");
- }
+ ShardingSpherePreconditions.checkState(null != name && 0 != name.length(), new SQLFeatureNotSupportedException("Savepoint name can not be NULL or empty"));
savepointName = name;
}
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnection.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnection.java
index 5be5022f233..a2581e94dd8 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnection.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnection.java
@@ -25,6 +25,7 @@ import org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSpherePrepar
import org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSphereStatement;
import org.apache.shardingsphere.driver.jdbc.exception.ConnectionClosedException;
import org.apache.shardingsphere.infra.context.ConnectionContext;
+import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
import org.apache.shardingsphere.mode.manager.ContextManager;
import java.sql.Array;
@@ -209,9 +210,7 @@ public final class ShardingSphereConnection extends AbstractConnectionAdapter {
@Override
public Savepoint setSavepoint() throws SQLException {
checkClose();
- if (!isHoldTransaction()) {
- throw new SQLFeatureNotSupportedException("Savepoint can only be used in transaction blocks.");
- }
+ ShardingSpherePreconditions.checkState(isHoldTransaction(), new SQLFeatureNotSupportedException("Savepoint can only be used in transaction blocks."));
return connectionManager.setSavepoint();
}
@@ -225,9 +224,7 @@ public final class ShardingSphereConnection extends AbstractConnectionAdapter {
}
private void checkClose() throws SQLException {
- if (isClosed()) {
- throw new ConnectionClosedException().toSQLException();
- }
+ ShardingSpherePreconditions.checkState(!isClosed(), new ConnectionClosedException().toSQLException());
}
@SuppressWarnings("MagicConstant")
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/resultset/ShardingSphereResultSet.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/resultset/ShardingSphereResultSet.java
index 94d7315f8aa..638e709652b 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/resultset/ShardingSphereResultSet.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/resultset/ShardingSphereResultSet.java
@@ -21,6 +21,7 @@ import org.apache.shardingsphere.driver.jdbc.adapter.AbstractResultSetAdapter;
import org.apache.shardingsphere.infra.executor.sql.context.ExecutionContext;
import org.apache.shardingsphere.infra.executor.sql.execute.result.query.impl.driver.jdbc.type.util.ResultSetUtil;
import org.apache.shardingsphere.infra.merge.result.MergedResult;
+import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
import java.io.InputStream;
import java.io.Reader;
@@ -369,15 +370,17 @@ public final class ShardingSphereResultSet extends AbstractResultSetAdapter {
public <T> T getObject(final int columnIndex, final Class<T> type) throws SQLException {
if (BigInteger.class.equals(type)) {
return (T) BigInteger.valueOf(getLong(columnIndex));
- } else if (Blob.class.equals(type)) {
+ }
+ if (Blob.class.equals(type)) {
return (T) getBlob(columnIndex);
- } else if (Clob.class.equals(type)) {
+ }
+ if (Clob.class.equals(type)) {
return (T) getClob(columnIndex);
- } else if (LocalDateTime.class.equals(type) || LocalDate.class.equals(type) || LocalTime.class.equals(type) || OffsetDateTime.class.equals(type)) {
+ }
+ if (LocalDateTime.class.equals(type) || LocalDate.class.equals(type) || LocalTime.class.equals(type) || OffsetDateTime.class.equals(type)) {
return (T) ResultSetUtil.convertValue(mergeResultSet.getValue(columnIndex, Timestamp.class), type);
- } else {
- return (T) ResultSetUtil.convertValue(mergeResultSet.getValue(columnIndex, type), type);
}
+ return (T) ResultSetUtil.convertValue(mergeResultSet.getValue(columnIndex, type), type);
}
@Override
@@ -385,11 +388,9 @@ public final class ShardingSphereResultSet extends AbstractResultSetAdapter {
return getObject(getIndexFromColumnLabelAndIndexMap(columnLabel), type);
}
- private Integer getIndexFromColumnLabelAndIndexMap(final String columnLabel) throws SQLFeatureNotSupportedException {
+ private Integer getIndexFromColumnLabelAndIndexMap(final String columnLabel) throws SQLException {
Integer result = columnLabelAndIndexMap.get(columnLabel);
- if (null == result) {
- throw new SQLFeatureNotSupportedException(String.format("can't get index from columnLabel[%s].", columnLabel));
- }
+ ShardingSpherePreconditions.checkState(null != result, new SQLFeatureNotSupportedException(String.format("Can not get index from column label `%s`.", columnLabel)));
return result;
}
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/transaction/TransactionBackendHandler.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/transaction/TransactionBackendHandler.java
index 7c4dbd56bbc..6cfbe02304b 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/transaction/TransactionBackendHandler.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/transaction/TransactionBackendHandler.java
@@ -22,6 +22,7 @@ import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
import org.apache.shardingsphere.infra.database.type.dialect.OpenGaussDatabaseType;
import org.apache.shardingsphere.infra.database.type.dialect.PostgreSQLDatabaseType;
import org.apache.shardingsphere.dialect.exception.transaction.InTransactionException;
+import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
import org.apache.shardingsphere.proxy.backend.communication.TransactionManager;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.JDBCBackendConnection;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.transaction.JDBCBackendTransactionManager;
@@ -147,24 +148,19 @@ public final class TransactionBackendHandler implements ProxyBackendHandler {
}
private void handleSavepoint() throws SQLException {
- if (!connectionSession.getTransactionStatus().isInTransaction() && isPostgreSQLOrOpenGauss()) {
- throw new SQLFeatureNotSupportedException("SAVEPOINT can only be used in transaction blocks");
- }
- backendTransactionManager.setSavepoint(((SavepointStatement) tclStatement).getSavepointName());
+ ShardingSpherePreconditions.checkState(connectionSession.getTransactionStatus().isInTransaction() || !isPostgreSQLOrOpenGauss(),
+ new SQLFeatureNotSupportedException("SAVEPOINT can only be used in transaction blocks"));
}
private void handleRollbackToSavepoint() throws SQLException {
- if (!connectionSession.getTransactionStatus().isInTransaction() && isPostgreSQLOrOpenGauss()) {
- throw new SQLFeatureNotSupportedException("ROLLBACK TO SAVEPOINT can only be used in transaction blocks");
- }
+ ShardingSpherePreconditions.checkState(connectionSession.getTransactionStatus().isInTransaction() || !isPostgreSQLOrOpenGauss(),
+ new SQLFeatureNotSupportedException("ROLLBACK TO SAVEPOINT can only be used in transaction blocks"));
backendTransactionManager.rollbackTo(((RollbackStatement) tclStatement).getSavepointName().get());
}
private void handleReleaseSavepoint() throws SQLException {
- if (!connectionSession.getTransactionStatus().isInTransaction() && isPostgreSQLOrOpenGauss()) {
- throw new SQLFeatureNotSupportedException("RELEASE SAVEPOINT can only be used in transaction blocks");
- }
- backendTransactionManager.releaseSavepoint(((ReleaseSavepointStatement) tclStatement).getSavepointName());
+ ShardingSpherePreconditions.checkState(connectionSession.getTransactionStatus().isInTransaction() || !isPostgreSQLOrOpenGauss(),
+ new SQLFeatureNotSupportedException("RELEASE SAVEPOINT can only be used in transaction blocks"));
}
private boolean isPostgreSQLOrOpenGauss() {