This is an automated email from the ASF dual-hosted git repository.
machen 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 1d3b0d15d48 Add UnknownCollationException (#20613)
1d3b0d15d48 is described below
commit 1d3b0d15d485f94fb8cfaf253c8255b3f53349ba
Author: Liang Zhang <[email protected]>
AuthorDate: Sun Aug 28 15:14:40 2022 +0800
Add UnknownCollationException (#20613)
* Add UnknownCollationException
* Add UnknownCollationException
---
.../protocol/mysql/constant/MySQLCharacterSet.java | 5 ++--
.../mysql/constant/MySQLCharacterSetTest.java | 7 ++---
.../connection/UnknownCollationException.java | 34 ++++++++--------------
.../mysql/mapper/MySQLDialectExceptionMapper.java | 4 +++
.../dialect/mysql/vendor/MySQLVendorError.java | 2 ++
.../mapper/MySQLDialectExceptionMapperTest.java | 4 +++
.../mysql/executor/MySQLSetCharsetExecutor.java | 13 +--------
.../authentication/MySQLAuthenticationEngine.java | 6 ++--
8 files changed, 32 insertions(+), 43 deletions(-)
diff --git
a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLCharacterSet.java
b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLCharacterSet.java
index 6bc7e6f1929..1883ca7628f 100644
---
a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLCharacterSet.java
+++
b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLCharacterSet.java
@@ -18,6 +18,7 @@
package org.apache.shardingsphere.db.protocol.mysql.constant;
import lombok.Getter;
+import
org.apache.shardingsphere.dialect.exception.connection.UnknownCollationException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
@@ -334,10 +335,10 @@ public enum MySQLCharacterSet {
public static MySQLCharacterSet findById(final int id) {
MySQLCharacterSet result = CHARACTER_SET_MAP.get(id);
if (null == result) {
- throw new UnsupportedCharsetException(String.format("Character set
corresponding to id %d not found", id));
+ throw new UnknownCollationException(id);
}
if (null == result.getCharset()) {
- throw new UnsupportedCharsetException(String.format("Character set
%s unsupported", result.name().toLowerCase()));
+ throw new UnknownCollationException(id);
}
return result;
}
diff --git
a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLCharacterSetTest.java
b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLCharacterSetTest.java
index f4809c7fe45..abe62ad7944 100644
---
a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLCharacterSetTest.java
+++
b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLCharacterSetTest.java
@@ -17,10 +17,9 @@
package org.apache.shardingsphere.db.protocol.mysql.constant;
+import
org.apache.shardingsphere.dialect.exception.connection.UnknownCollationException;
import org.junit.Test;
-import java.nio.charset.UnsupportedCharsetException;
-
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@@ -32,12 +31,12 @@ public final class MySQLCharacterSetTest {
assertThat(actual, is(MySQLCharacterSet.UTF8MB4_GENERAL_CI));
}
- @Test(expected = UnsupportedCharsetException.class)
+ @Test(expected = UnknownCollationException.class)
public void assertCharacterSetNotFoundById() {
MySQLCharacterSet.findById(-1);
}
- @Test(expected = UnsupportedCharsetException.class)
+ @Test(expected = UnknownCollationException.class)
public void assertFoundUnsupportedCharacterSetById() {
MySQLCharacterSet.findById(63);
}
diff --git
a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLCharacterSetTest.java
b/shardingsphere-dialect-exception/shardingsphere-dialect-exception-core/src/main/java/org/apache/shardingsphere/dialect/exception/connection/UnknownCollationException.java
similarity index 50%
copy from
shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLCharacterSetTest.java
copy to
shardingsphere-dialect-exception/shardingsphere-dialect-exception-core/src/main/java/org/apache/shardingsphere/dialect/exception/connection/UnknownCollationException.java
index f4809c7fe45..165b4deb47f 100644
---
a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLCharacterSetTest.java
+++
b/shardingsphere-dialect-exception/shardingsphere-dialect-exception-core/src/main/java/org/apache/shardingsphere/dialect/exception/connection/UnknownCollationException.java
@@ -15,30 +15,20 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.db.protocol.mysql.constant;
+package org.apache.shardingsphere.dialect.exception.connection;
-import org.junit.Test;
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.dialect.exception.SQLDialectException;
-import java.nio.charset.UnsupportedCharsetException;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertThat;
-
-public final class MySQLCharacterSetTest {
-
- @Test
- public void assertFoundCharacterSetById() {
- MySQLCharacterSet actual = MySQLCharacterSet.findById(45);
- assertThat(actual, is(MySQLCharacterSet.UTF8MB4_GENERAL_CI));
- }
+/**
+ * Unknown collation exception.
+ */
+@RequiredArgsConstructor
+@Getter
+public final class UnknownCollationException extends SQLDialectException {
- @Test(expected = UnsupportedCharsetException.class)
- public void assertCharacterSetNotFoundById() {
- MySQLCharacterSet.findById(-1);
- }
+ private static final long serialVersionUID = 6920150607711135228L;
- @Test(expected = UnsupportedCharsetException.class)
- public void assertFoundUnsupportedCharacterSetById() {
- MySQLCharacterSet.findById(63);
- }
+ private final int collationId;
}
diff --git
a/shardingsphere-dialect-exception/shardingsphere-mysql-dialect-exception/src/main/java/org/apache/shardingsphere/dialect/mysql/mapper/MySQLDialectExceptionMapper.java
b/shardingsphere-dialect-exception/shardingsphere-mysql-dialect-exception/src/main/java/org/apache/shardingsphere/dialect/mysql/mapper/MySQLDialectExceptionMapper.java
index 8866adf7ffb..c6fdfba72dd 100644
---
a/shardingsphere-dialect-exception/shardingsphere-mysql-dialect-exception/src/main/java/org/apache/shardingsphere/dialect/mysql/mapper/MySQLDialectExceptionMapper.java
+++
b/shardingsphere-dialect-exception/shardingsphere-mysql-dialect-exception/src/main/java/org/apache/shardingsphere/dialect/mysql/mapper/MySQLDialectExceptionMapper.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.dialect.mysql.mapper;
import org.apache.shardingsphere.dialect.exception.SQLDialectException;
import
org.apache.shardingsphere.dialect.exception.connection.TooManyConnectionsException;
+import
org.apache.shardingsphere.dialect.exception.connection.UnknownCollationException;
import
org.apache.shardingsphere.dialect.exception.connection.UnsupportedPreparedStatementException;
import
org.apache.shardingsphere.dialect.exception.data.InsertColumnsAndValuesMismatchedException;
import
org.apache.shardingsphere.dialect.exception.syntax.database.DatabaseCreateExistsException;
@@ -74,6 +75,9 @@ public final class MySQLDialectExceptionMapper implements
SQLDialectExceptionMap
if (sqlDialectException instanceof
UnsupportedPreparedStatementException) {
return toSQLException(MySQLVendorError.ER_UNSUPPORTED_PS);
}
+ if (sqlDialectException instanceof UnknownCollationException) {
+ return toSQLException(MySQLVendorError.ER_UNKNOWN_COLLATION,
((UnknownCollationException) sqlDialectException).getCollationId());
+ }
return new UnknownSQLException(sqlDialectException).toSQLException();
}
diff --git
a/shardingsphere-dialect-exception/shardingsphere-mysql-dialect-exception/src/main/java/org/apache/shardingsphere/dialect/mysql/vendor/MySQLVendorError.java
b/shardingsphere-dialect-exception/shardingsphere-mysql-dialect-exception/src/main/java/org/apache/shardingsphere/dialect/mysql/vendor/MySQLVendorError.java
index 636b66bdc39..ea237e2ed7e 100644
---
a/shardingsphere-dialect-exception/shardingsphere-mysql-dialect-exception/src/main/java/org/apache/shardingsphere/dialect/mysql/vendor/MySQLVendorError.java
+++
b/shardingsphere-dialect-exception/shardingsphere-mysql-dialect-exception/src/main/java/org/apache/shardingsphere/dialect/mysql/vendor/MySQLVendorError.java
@@ -58,6 +58,8 @@ public enum MySQLVendorError implements VendorError {
ER_WRONG_VALUE_COUNT_ON_ROW(XOpenSQLState.MISMATCH_INSERT_VALUES_AND_COLUMNS,
1136, "Column count doesn't match value count at row %d"),
+ ER_UNKNOWN_COLLATION(XOpenSQLState.GENERAL_ERROR, 1273, "Unknown
collation: '%s'"),
+
ER_ERROR_ON_MODIFYING_GTID_EXECUTED_TABLE(XOpenSQLState.GENERAL_ERROR,
3176,
"Please do not modify the %s table with an XA transaction. This is
an internal system table used to store GTIDs for committed transactions. "
+ "Although modifying it can lead to an inconsistent GTID
state, if necessary you can modify it with a non-XA transaction.");
diff --git
a/shardingsphere-dialect-exception/shardingsphere-mysql-dialect-exception/src/test/java/org/apache/shardingsphere/dialect/mysql/mapper/MySQLDialectExceptionMapperTest.java
b/shardingsphere-dialect-exception/shardingsphere-mysql-dialect-exception/src/test/java/org/apache/shardingsphere/dialect/mysql/mapper/MySQLDialectExceptionMapperTest.java
index 63c73dab036..599deb8155e 100644
---
a/shardingsphere-dialect-exception/shardingsphere-mysql-dialect-exception/src/test/java/org/apache/shardingsphere/dialect/mysql/mapper/MySQLDialectExceptionMapperTest.java
+++
b/shardingsphere-dialect-exception/shardingsphere-mysql-dialect-exception/src/test/java/org/apache/shardingsphere/dialect/mysql/mapper/MySQLDialectExceptionMapperTest.java
@@ -19,6 +19,8 @@ package org.apache.shardingsphere.dialect.mysql.mapper;
import org.apache.shardingsphere.dialect.exception.SQLDialectException;
import
org.apache.shardingsphere.dialect.exception.connection.TooManyConnectionsException;
+import
org.apache.shardingsphere.dialect.exception.connection.UnknownCollationException;
+import
org.apache.shardingsphere.dialect.exception.connection.UnsupportedPreparedStatementException;
import
org.apache.shardingsphere.dialect.exception.data.InsertColumnsAndValuesMismatchedException;
import
org.apache.shardingsphere.dialect.exception.syntax.database.DatabaseCreateExistsException;
import
org.apache.shardingsphere.dialect.exception.syntax.database.DatabaseDropNotExistsException;
@@ -50,6 +52,8 @@ public final class MySQLDialectExceptionMapperTest {
{InsertColumnsAndValuesMismatchedException.class,
MySQLVendorError.ER_WRONG_VALUE_COUNT_ON_ROW},
{TableModifyInTransactionException.class,
MySQLVendorError.ER_ERROR_ON_MODIFYING_GTID_EXECUTED_TABLE},
{TooManyConnectionsException.class,
MySQLVendorError.ER_CON_COUNT_ERROR},
+ {UnsupportedPreparedStatementException.class,
MySQLVendorError.ER_UNSUPPORTED_PS},
+ {UnknownCollationException.class,
MySQLVendorError.ER_UNKNOWN_COLLATION},
});
}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/admin/mysql/executor/MySQLSetCharsetExecutor.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/admin/mysql/executor/MySQLSetCharsetExecutor.java
index 4618899e712..6a190f1fbab 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/admin/mysql/executor/MySQLSetCharsetExecutor.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/admin/mysql/executor/MySQLSetCharsetExecutor.java
@@ -19,17 +19,13 @@ package
org.apache.shardingsphere.proxy.backend.handler.admin.mysql.executor;
import org.apache.shardingsphere.db.protocol.CommonConstants;
import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLServerInfo;
-import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
import
org.apache.shardingsphere.proxy.backend.handler.admin.mysql.MySQLSessionVariableHandler;
-import
org.apache.shardingsphere.sql.parser.sql.common.segment.dal.VariableAssignSegment;
-import
org.apache.shardingsphere.sql.parser.sql.common.statement.dal.SetStatement;
+import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.charset.UnsupportedCharsetException;
-import java.util.Arrays;
import java.util.Collection;
-import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import java.util.TreeSet;
@@ -41,8 +37,6 @@ public final class MySQLSetCharsetExecutor implements
MySQLSessionVariableHandle
private static final Set<String> TYPE_ALIASES = new
TreeSet<>(String.CASE_INSENSITIVE_ORDER);
- private static final Set<String> CHARSET_VARIABLE_NAMES = new
HashSet<>(Arrays.asList("charset", "character_set_client"));
-
static {
TYPE_ALIASES.add("character_set_client");
}
@@ -53,11 +47,6 @@ public final class MySQLSetCharsetExecutor implements
MySQLSessionVariableHandle
connectionSession.getAttributeMap().attr(CommonConstants.CHARSET_ATTRIBUTE_KEY).set(parseCharset(value));
}
- private String getCharacterSetValue(final SetStatement setStatement) {
- return setStatement.getVariableAssigns().stream().filter(each ->
CHARSET_VARIABLE_NAMES.contains(each.getVariable().getVariable().toLowerCase(Locale.ROOT)))
-
.map(VariableAssignSegment::getAssignValue).findFirst().orElse("");
- }
-
private String formatValue(final String value) {
return value.startsWith("'") && value.endsWith("'") ||
value.startsWith("\"") && value.endsWith("\"") ? value.substring(1,
value.length() - 1) : value.trim();
}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/MySQLAuthenticationEngine.java
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/MySQLAuthenticationEngine.java
index d87b45d5a5e..524786322d6 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/MySQLAuthenticationEngine.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/MySQLAuthenticationEngine.java
@@ -91,9 +91,9 @@ public final class MySQLAuthenticationEngine implements
AuthenticationEngine {
MySQLHandshakeResponse41Packet packet = new
MySQLHandshakeResponse41Packet((MySQLPacketPayload) payload);
authResponse = packet.getAuthResponse();
sequenceId = packet.getSequenceId();
- MySQLCharacterSet mySQLCharacterSet =
MySQLCharacterSet.findById(packet.getCharacterSet());
-
context.channel().attr(CommonConstants.CHARSET_ATTRIBUTE_KEY).set(mySQLCharacterSet.getCharset());
-
context.channel().attr(MySQLConstants.MYSQL_CHARACTER_SET_ATTRIBUTE_KEY).set(mySQLCharacterSet);
+ MySQLCharacterSet characterSet =
MySQLCharacterSet.findById(packet.getCharacterSet());
+
context.channel().attr(CommonConstants.CHARSET_ATTRIBUTE_KEY).set(characterSet.getCharset());
+
context.channel().attr(MySQLConstants.MYSQL_CHARACTER_SET_ATTRIBUTE_KEY).set(characterSet);
if (!Strings.isNullOrEmpty(packet.getDatabase()) &&
!ProxyContext.getInstance().databaseExists(packet.getDatabase())) {
context.writeAndFlush(new MySQLErrPacket(++sequenceId,
MySQLVendorError.ER_BAD_DB_ERROR, packet.getDatabase()));
return AuthenticationResultBuilder.continued();