Skip to content

Commit d680767

Browse files
committed
Fix for Bug#116120 (Bug#37079448), Inappropriate charset selected for connection when jdk.charsets not included.
Change-Id: I3c0777759bc63ff88fbce2b196d2d541f4660e37
1 parent eb84d9a commit d680767

File tree

3 files changed

+28
-27
lines changed

3 files changed

+28
-27
lines changed

CHANGES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
Version 9.4.0
55

6+
- Fix for Bug#116120 (Bug#37079448), Inappropriate charset selected for connection when jdk.charsets not included.
7+
68
- Fix for Bug#98620 (Bug#31503893), Using DatabaseMetaData.getColumns() gives collation mix error.
79

810
- Fix for Bug#118389 (Bug#38044940), OCI ephemeral keys not working after change in OCI CLI.

src/main/core-api/java/com/mysql/cj/CharsetMapping.java

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ public class CharsetMapping {
109109
new MysqlCharset(MYSQL_CHARSET_NAME_big5, 2, 0, new String[] { "Big5" }),
110110
new MysqlCharset(MYSQL_CHARSET_NAME_gbk, 2, 0, new String[] { "GBK" }),
111111

112-
new MysqlCharset(MYSQL_CHARSET_NAME_sjis, 2, 0, new String[] { "SHIFT_JIS", "Cp943", "WINDOWS-31J" }), // SJIS is alias for SHIFT_JIS, Cp943 is rather a cp932 but we map it to sjis for years
112+
// SJIS is alias for SHIFT_JIS, Cp943 is rather a cp932 but we map it to sjis for years
113+
new MysqlCharset(MYSQL_CHARSET_NAME_sjis, 2, 0, new String[] { "SHIFT_JIS", "Cp943", "WINDOWS-31J" }),
113114
new MysqlCharset(MYSQL_CHARSET_NAME_cp932, 2, 1, new String[] { "WINDOWS-31J" }),
114115

115116
new MysqlCharset(MYSQL_CHARSET_NAME_gb2312, 2, 0, new String[] { "GB2312" }),
@@ -135,7 +136,9 @@ public class CharsetMapping {
135136
new MysqlCharset(MYSQL_CHARSET_NAME_cp850, 1, 0, new String[] { "Cp850", "Cp437" }),
136137

137138
new MysqlCharset(MYSQL_CHARSET_NAME_cp852, 1, 0, new String[] { "Cp852" }),
138-
new MysqlCharset(MYSQL_CHARSET_NAME_keybcs2, 1, 0, new String[] { "Cp852" }), // Kamenicky encoding usually known as Cp895 but there is no official cp895 specification; close to Cp852, see http://ftp.muni.cz/pub/localization/charsets/cs-encodings-faq
139+
// Kamenicky encoding usually known as Cp895 but there is no official cp895 specification; close to Cp852, see
140+
// http://ftp.muni.cz/pub/localization/charsets/cs-encodings-faq
141+
new MysqlCharset(MYSQL_CHARSET_NAME_keybcs2, 1, 0, new String[] { "Cp852" }),
139142

140143
new MysqlCharset(MYSQL_CHARSET_NAME_cp866, 1, 0, new String[] { "Cp866" }),
141144

@@ -541,13 +544,10 @@ public class CharsetMapping {
541544
protected static String getStaticMysqlCharsetForJavaEncoding(String javaEncoding, ServerVersion version) {
542545
List<MysqlCharset> mysqlCharsets = CharsetMapping.JAVA_ENCODING_UC_TO_MYSQL_CHARSET.get(javaEncoding.toUpperCase(Locale.ENGLISH));
543546
if (mysqlCharsets != null) {
544-
if (version == null) {
545-
return mysqlCharsets.get(0).charsetName; // Take the first one we get
546-
}
547547
MysqlCharset currentChoice = null;
548548
for (MysqlCharset charset : mysqlCharsets) {
549-
if (charset.isOkayForVersion(version) && (currentChoice == null || currentChoice.minimumVersion.compareTo(charset.minimumVersion) < 0
550-
|| currentChoice.priority < charset.priority && currentChoice.minimumVersion.compareTo(charset.minimumVersion) == 0)) {
549+
if ((version == null || charset.isOkayForVersion(version)) && (currentChoice == null || currentChoice.priority < charset.priority
550+
|| currentChoice.priority == charset.priority && currentChoice.minimumVersion.compareTo(charset.minimumVersion) < 0)) {
551551
currentChoice = charset;
552552
}
553553
}
@@ -681,7 +681,6 @@ class MysqlCharset {
681681
public final int priority;
682682
public final List<String> javaEncodingsUc = new ArrayList<>();
683683
public final List<String> aliases = new ArrayList<>();
684-
685684
public final ServerVersion minimumVersion;
686685

687686
/**
@@ -720,7 +719,6 @@ private void addEncodingMapping(String encoding) {
720719
public MysqlCharset(String charsetName, int mblen, int priority, String[] javaEncodings, ServerVersion minimumVersion) {
721720
this.charsetName = charsetName;
722721
this.mblen = mblen;
723-
this.priority = priority;
724722

725723
for (int i = 0; i < javaEncodings.length; i++) {
726724
String encoding = javaEncodings[i];
@@ -729,15 +727,19 @@ public MysqlCharset(String charsetName, int mblen, int priority, String[] javaEn
729727
addEncodingMapping(cs.name());
730728
cs.aliases().forEach(this::addEncodingMapping);
731729
} catch (Exception e) {
732-
// if there is no support of this charset in JVM it's still possible to use our converter for 1-byte charsets
730+
// If there is no support of this charset in JVM it's still possible to use our converter for 1-byte charsets.
733731
if (mblen == 1) {
734732
addEncodingMapping(encoding);
735733
}
736734
}
737735
}
738736

739737
if (this.javaEncodingsUc.size() == 0) {
738+
// Drop priority to minimum and map to default charset.
739+
this.priority = -1;
740740
addEncodingMapping(mblen > 1 ? "UTF-8" : "Cp1252");
741+
} else {
742+
this.priority = priority;
741743
}
742744

743745
this.minimumVersion = minimumVersion;
@@ -747,12 +749,12 @@ public MysqlCharset(String charsetName, int mblen, int priority, String[] javaEn
747749
public String toString() {
748750
StringBuilder asString = new StringBuilder();
749751
asString.append("[");
750-
asString.append("charsetName=");
751-
asString.append(this.charsetName);
752-
asString.append(",mblen=");
753-
asString.append(this.mblen);
754-
// asString.append(",javaEncoding=");
755-
// asString.append(this.javaEncodings.toString());
752+
asString.append("charsetName=").append(this.charsetName);
753+
asString.append(",mblen=").append(this.mblen);
754+
asString.append(",pirority=").append(this.priority);
755+
asString.append(",javaEncoding=").append(this.javaEncodingsUc);
756+
asString.append(",aliases=").append(this.aliases);
757+
asString.append(",minimumVersion=").append(this.minimumVersion);
756758
asString.append("]");
757759
return asString.toString();
758760
}
@@ -800,14 +802,11 @@ public Collation(int index, String[] collationNames, int priority, String charse
800802
public String toString() {
801803
StringBuilder asString = new StringBuilder();
802804
asString.append("[");
803-
asString.append("index=");
804-
asString.append(this.index);
805-
asString.append(",collationNames=");
806-
asString.append(Arrays.toString(this.collationNames));
807-
asString.append(",charsetName=");
808-
asString.append(this.mysqlCharset.charsetName);
809-
asString.append(",javaCharsetName=");
810-
asString.append(this.mysqlCharset.getMatchingJavaEncoding(null));
805+
asString.append("index=").append(this.index);
806+
asString.append(",collationNames=").append(Arrays.toString(this.collationNames));
807+
asString.append(",priority=").append(this.priority);
808+
asString.append(",charsetName=").append(this.mysqlCharset.charsetName);
809+
asString.append(",javaCharsetName=").append(this.mysqlCharset.getMatchingJavaEncoding(null));
811810
asString.append("]");
812811
return asString.toString();
813812
}

src/main/core-impl/java/com/mysql/cj/NativeCharsetSettings.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -581,9 +581,9 @@ private void buildCollationMapping() {
581581
while ((r = rs.getRows().next()) != null) {
582582
String collationName = r.getValue(0, svf);
583583
String charsetName = r.getValue(1, svf);
584-
int collationIndex = ((Number) r.getValue(2, ivf)).intValue();
585-
int maxlen = ((Number) r.getValue(3, ivf)).intValue();
586-
boolean isDefault = ((Number) r.getValue(4, ivf)).intValue() > 0;
584+
int collationIndex = r.getValue(2, ivf).intValue();
585+
int maxlen = r.getValue(3, ivf).intValue();
586+
boolean isDefault = r.getValue(4, ivf).intValue() > 0;
587587

588588
if (collationIndex >= MAP_SIZE //
589589
|| collationIndex != getStaticCollationIndexForCollationName(collationName)

0 commit comments

Comments
 (0)