@@ -34,8 +34,13 @@ def mysql_connection(config)
3434 properties [ 'zeroDateTimeBehavior' ] ||= 'convertToNull'
3535 properties [ 'jdbcCompliantTruncation' ] ||= 'false'
3636 properties [ 'useUnicode' ] = 'true' unless properties . key? ( 'useUnicode' ) # otherwise platform default
37- encoding = config . key? ( :encoding ) ? config [ :encoding ] : 'utf8'
38- properties [ 'characterEncoding' ] = encoding if encoding
37+ # NOTE: this is "better" than passing what users are used to set on MRI
38+ # e.g. 'utf8mb4' will fail cause the driver will check for a Java charset
39+ # ... it's smart enough to detect utf8mb4 from server variables :
40+ # "character_set_client" && "character_set_connection" (thus UTF-8)
41+ if encoding = config . key? ( :encoding ) ? config [ :encoding ] : 'utf8'
42+ properties [ 'characterEncoding' ] = convert_mysql_encoding ( encoding ) || encoding
43+ end
3944 if ! ( reconnect = config [ :reconnect ] ) . nil?
4045 properties [ 'autoReconnect' ] ||= reconnect . to_s
4146 # properties['maxReconnects'] ||= '3'
@@ -88,4 +93,53 @@ def mariadb_connection(config)
8893 mysql_connection ( config )
8994 end
9095 alias_method :jdbcmariadb_connection , :mariadb_connection
96+
97+ private
98+
99+ @@mysql_encodings = nil
100+
101+ def convert_mysql_encoding ( encoding ) # from mysql2's ruby_enc_to_mysql
102+ ( @@mysql_encodings ||= {
103+ "big5" => "Big5" ,
104+ "dec8" => nil ,
105+ "cp850" => "CP850" ,
106+ "hp8" => nil ,
107+ "koi8r" => "KOI8-R" ,
108+ "latin1" => "ISO-8859-1" ,
109+ "latin2" => "ISO-8859-2" ,
110+ "swe7" => nil ,
111+ "ascii" => "US-ASCII" ,
112+ #"ujis" => "eucJP-ms",
113+ #"sjis" => "Shift_JIS",
114+ "hebrew" => "ISO-8859-8" ,
115+ #"tis620" => "TIS-620",
116+ #"euckr" => "EUC-KR",
117+ #"koi8u" => "KOI8-R",
118+ #"gb2312" => "GB2312",
119+ "greek" => "ISO-8859-7" ,
120+ "cp1250" => "Windows-1250" ,
121+ #"gbk" => "GBK",
122+ "latin5" => "ISO-8859-9" ,
123+ "armscii8" => nil ,
124+ "utf8" => "UTF-8" ,
125+ "ucs2" => "UTF-16BE" ,
126+ "cp866" => "IBM866" ,
127+ "keybcs2" => nil ,
128+ #"macce" => "macCentEuro",
129+ #"macroman" => "macRoman",
130+ "cp852" => "CP852" ,
131+ "latin7" => "ISO-8859-13" ,
132+ "utf8mb4" => "UTF-8" ,
133+ "cp1251" => "Windows-1251" ,
134+ "utf16" => "UTF-16" ,
135+ "cp1256" => "Windows-1256" ,
136+ "cp1257" => "Windows-1257" ,
137+ "utf32" => "UTF-32" ,
138+ "binary" => "ASCII-8BIT" ,
139+ "geostd8" => nil ,
140+ #"cp932" => "Windows-31J",
141+ #"eucjpms" => "eucJP-ms"
142+ } ) [ encoding ]
143+ end
144+
91145end
0 commit comments