Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -32,12 +31,12 @@ public void assertFoundCharacterSetById() {
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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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.dialect.exception.connection;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.dialect.exception.SQLDialectException;

/**
* Unknown collation exception.
*/
@RequiredArgsConstructor
@Getter
public final class UnknownCollationException extends SQLDialectException {

private static final long serialVersionUID = 6920150607711135228L;

private final int collationId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

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;
Expand Down Expand Up @@ -74,6 +75,9 @@ public SQLException convert(final SQLDialectException sqlDialectException) {
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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

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;
Expand Down Expand Up @@ -50,6 +52,8 @@ private Collection<Object[]> getConvertParameters() {
{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},
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,13 @@

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;
Expand All @@ -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");
}
Expand All @@ -53,11 +47,6 @@ public void handle(final ConnectionSession connectionSession, final String varia
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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ private AuthenticationResult authPhaseFastPath(final ChannelHandlerContext conte
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();
Expand Down