in src/main/core-impl/java/com/mysql/cj/NativeCharsetSettings.java [511:645]
private void buildCollationMapping() {
Map<Integer, String> customCollationIndexToCollationName = null;
Map<String, Integer> customCollationNameToCollationIndex = null;
Map<Integer, String> customCollationIndexToCharsetName = null;
Map<String, Integer> customCharsetNameToMblen = null;
Map<String, String> customCharsetNameToJavaEncoding = new HashMap<>();
Map<String, String> customJavaEncodingUcToCharsetName = new HashMap<>();
Map<String, Integer> customCharsetNameToCollationIndex = new HashMap<>();
Set<String> customMultibyteEncodings = new HashSet<>();
String databaseURL = this.session.getHostInfo().getDatabaseUrl();
if (this.cacheServerConfiguration.getValue()) {
synchronized (customCollationIndexToCharsetNameByUrl) {
customCollationIndexToCollationName = customCollationIndexToCollationNameByUrl.get(databaseURL);
customCollationNameToCollationIndex = customCollationNameToCollationIndexByUrl.get(databaseURL);
customCollationIndexToCharsetName = customCollationIndexToCharsetNameByUrl.get(databaseURL);
customCharsetNameToMblen = customCharsetNameToMblenByUrl.get(databaseURL);
customCharsetNameToJavaEncoding = customCharsetNameToJavaEncodingByUrl.get(databaseURL);
customJavaEncodingUcToCharsetName = customJavaEncodingUcToCharsetNameByUrl.get(databaseURL);
customCharsetNameToCollationIndex = customCharsetNameToCollationIndexByUrl.get(databaseURL);
customMultibyteEncodings = customMultibyteEncodingsByUrl.get(databaseURL);
}
}
if (customCollationIndexToCharsetName == null && this.session.getPropertySet().getBooleanProperty(PropertyKey.detectCustomCollations).getValue()) {
TelemetrySpan span = this.session.getTelemetryHandler().startSpan(TelemetrySpanName.LOAD_COLLATIONS);
try (TelemetryScope scope = span.makeCurrent()) {
span.setAttribute(TelemetryAttribute.DB_NAME, () -> this.session.getHostInfo().getDatabase());
span.setAttribute(TelemetryAttribute.DB_OPERATION, TelemetryAttribute.OPERATION_SELECT);
span.setAttribute(TelemetryAttribute.DB_STATEMENT, TelemetryAttribute.OPERATION_SELECT + TelemetryAttribute.STATEMENT_SUFFIX);
span.setAttribute(TelemetryAttribute.DB_SYSTEM, TelemetryAttribute.DB_SYSTEM_DEFAULT);
span.setAttribute(TelemetryAttribute.DB_USER, () -> this.session.getHostInfo().getUser());
span.setAttribute(TelemetryAttribute.THREAD_ID, () -> Thread.currentThread().getId());
span.setAttribute(TelemetryAttribute.THREAD_NAME, () -> Thread.currentThread().getName());
customCollationIndexToCollationName = new HashMap<>();
customCollationNameToCollationIndex = new HashMap<>();
customCollationIndexToCharsetName = new HashMap<>();
customCharsetNameToMblen = new HashMap<>();
customCharsetNameToJavaEncoding = new HashMap<>();
customJavaEncodingUcToCharsetName = new HashMap<>();
customCharsetNameToCollationIndex = new HashMap<>();
customMultibyteEncodings = new HashSet<>();
String customCharsetMapping = this.session.getPropertySet().getStringProperty(PropertyKey.customCharsetMapping).getValue();
if (customCharsetMapping != null) {
String[] pairs = customCharsetMapping.split(",");
for (String pair : pairs) {
int keyEnd = pair.indexOf(":");
if (keyEnd > 0 && keyEnd + 1 < pair.length()) {
String charset = pair.substring(0, keyEnd);
String encoding = pair.substring(keyEnd + 1);
customCharsetNameToJavaEncoding.put(charset, encoding);
customJavaEncodingUcToCharsetName.put(encoding.toUpperCase(Locale.ENGLISH), charset);
}
}
}
ValueFactory<Integer> ivf = new IntegerValueFactory(this.session.getPropertySet());
try {
NativePacketPayload resultPacket = this.session.getProtocol().sendCommand(getCommandBuilder().buildComQuery(null, this.session,
"SELECT c.COLLATION_NAME, c.CHARACTER_SET_NAME, c.ID, cs.MAXLEN, c.IS_DEFAULT='Yes' from INFORMATION_SCHEMA.COLLATIONS AS c LEFT JOIN"
+ " INFORMATION_SCHEMA.CHARACTER_SETS AS cs ON cs.CHARACTER_SET_NAME=c.CHARACTER_SET_NAME"),
false, 0);
Resultset rs = this.session.getProtocol().readAllResults(-1, false, resultPacket, false, null,
new ResultsetFactory(Type.FORWARD_ONLY, null));
ValueFactory<String> svf = new StringValueFactory(this.session.getPropertySet());
Row r;
while ((r = rs.getRows().next()) != null) {
String collationName = r.getValue(0, svf);
String charsetName = r.getValue(1, svf);
int collationIndex = ((Number) r.getValue(2, ivf)).intValue();
int maxlen = ((Number) r.getValue(3, ivf)).intValue();
boolean isDefault = ((Number) r.getValue(4, ivf)).intValue() > 0;
if (collationIndex >= MAP_SIZE //
|| collationIndex != getStaticCollationIndexForCollationName(collationName)
|| !charsetName.equals(getStaticMysqlCharsetNameForCollationIndex(collationIndex))) {
customCollationIndexToCollationName.put(collationIndex, collationName);
customCollationNameToCollationIndex.put(collationName, collationIndex);
customCollationIndexToCharsetName.put(collationIndex, charsetName);
if (isDefault || !customCharsetNameToCollationIndex.containsKey(charsetName)
&& CharsetMapping.getStaticCollationIndexForMysqlCharsetName(charsetName) == 0) {
customCharsetNameToCollationIndex.put(charsetName, collationIndex);
}
}
// if no static map for charsetName adding to custom map
if (getStaticMysqlCharsetByName(charsetName) == null) {
customCharsetNameToMblen.put(charsetName, maxlen);
if (maxlen > 1) {
String enc = customCharsetNameToJavaEncoding.get(charsetName);
if (enc != null) {
customMultibyteEncodings.add(enc.toUpperCase(Locale.ENGLISH));
}
}
}
}
} catch (IOException e) {
throw ExceptionFactory.createException(e.getMessage(), e, this.session.getExceptionInterceptor());
}
if (this.cacheServerConfiguration.getValue()) {
synchronized (customCollationIndexToCharsetNameByUrl) {
customCollationIndexToCollationNameByUrl.put(databaseURL, Collections.unmodifiableMap(customCollationIndexToCollationName));
customCollationNameToCollationIndexByUrl.put(databaseURL, Collections.unmodifiableMap(customCollationNameToCollationIndex));
customCollationIndexToCharsetNameByUrl.put(databaseURL, Collections.unmodifiableMap(customCollationIndexToCharsetName));
customCharsetNameToMblenByUrl.put(databaseURL, Collections.unmodifiableMap(customCharsetNameToMblen));
customCharsetNameToJavaEncodingByUrl.put(databaseURL, Collections.unmodifiableMap(customCharsetNameToJavaEncoding));
customJavaEncodingUcToCharsetNameByUrl.put(databaseURL, Collections.unmodifiableMap(customJavaEncodingUcToCharsetName));
customCharsetNameToCollationIndexByUrl.put(databaseURL, Collections.unmodifiableMap(customCharsetNameToCollationIndex));
customMultibyteEncodingsByUrl.put(databaseURL, Collections.unmodifiableSet(customMultibyteEncodings));
}
}
} catch (Throwable t) {
span.setError(t);
throw t;
} finally {
span.end();
}
}
if (customCollationIndexToCharsetName != null) {
this.collationIndexToCollationName = customCollationIndexToCollationName;
this.collationNameToCollationIndex = customCollationNameToCollationIndex;
this.collationIndexToCharsetName = customCollationIndexToCharsetName;
this.charsetNameToMblen = customCharsetNameToMblen;
this.charsetNameToJavaEncoding = customCharsetNameToJavaEncoding;
this.javaEncodingUcToCharsetName = customJavaEncodingUcToCharsetName;
this.charsetNameToCollationIndex = customCharsetNameToCollationIndex;
this.multibyteEncodings = customMultibyteEncodings;
}
}