0001: /*
0002: Copyright (C) 2002-2006 MySQL AB
0003:
0004: This program is free software; you can redistribute it and/or modify
0005: it under the terms of version 2 of the GNU General Public License as
0006: published by the Free Software Foundation.
0007:
0008: There are special exceptions to the terms and conditions of the GPL
0009: as it is applied to this software. View the full text of the
0010: exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
0011: software distribution.
0012:
0013: This program is distributed in the hope that it will be useful,
0014: but WITHOUT ANY WARRANTY; without even the implied warranty of
0015: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0016: GNU General Public License for more details.
0017:
0018: You should have received a copy of the GNU General Public License
0019: along with this program; if not, write to the Free Software
0020: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0021:
0022:
0023:
0024: */
0025: package com.mysql.jdbc;
0026:
0027: import java.sql.SQLException;
0028: import java.util.ArrayList;
0029: import java.util.Collections;
0030: import java.util.HashMap;
0031: import java.util.Iterator;
0032: import java.util.List;
0033: import java.util.Locale;
0034: import java.util.Map;
0035: import java.util.Properties;
0036: import java.util.Set;
0037: import java.util.TreeMap;
0038:
0039: /**
0040: * Mapping between MySQL charset names and Java charset names. I've investigated
0041: * placing these in a .properties file, but unfortunately under most appservers
0042: * this complicates configuration because the security policy needs to be
0043: * changed by the user to allow the driver to read them :(
0044: *
0045: * @author Mark Matthews
0046: */
0047: public class CharsetMapping {
0048: private static final Properties CHARSET_CONFIG = new Properties();
0049:
0050: /**
0051: * Map of MySQL-4.1 charset indexes to Java encoding names
0052: */
0053: public static final String[] INDEX_TO_CHARSET;
0054:
0055: /**
0056: * Map of MySQL-4.1 collation index to collation names
0057: */
0058: public static final String[] INDEX_TO_COLLATION;
0059:
0060: /** Mapping of Java charset names to MySQL charset names */
0061: private static final Map JAVA_TO_MYSQL_CHARSET_MAP;
0062:
0063: private static final Map JAVA_UC_TO_MYSQL_CHARSET_MAP;
0064:
0065: private static final Map ERROR_MESSAGE_FILE_TO_MYSQL_CHARSET_MAP;
0066:
0067: /** Map/List of multibyte character sets (using MySQL names) */
0068: private static final Map MULTIBYTE_CHARSETS;
0069:
0070: private static final Map MYSQL_TO_JAVA_CHARSET_MAP;
0071:
0072: private static final Map MYSQL_ENCODING_NAME_TO_CHARSET_INDEX_MAP;
0073:
0074: private static final String NOT_USED = "ISO8859_1"; // punting for not-used character sets
0075:
0076: public static final Map STATIC_CHARSET_TO_NUM_BYTES_MAP;
0077:
0078: static {
0079: HashMap tempNumBytesMap = new HashMap();
0080:
0081: tempNumBytesMap.put("big5", Constants.integerValueOf(2));
0082: tempNumBytesMap.put("dec8", Constants.integerValueOf(1));
0083: tempNumBytesMap.put("cp850", Constants.integerValueOf(1));
0084: tempNumBytesMap.put("hp8", Constants.integerValueOf(1));
0085: tempNumBytesMap.put("koi8r", Constants.integerValueOf(1));
0086: tempNumBytesMap.put("latin1", Constants.integerValueOf(1));
0087: tempNumBytesMap.put("latin2", Constants.integerValueOf(1));
0088: tempNumBytesMap.put("swe7", Constants.integerValueOf(1));
0089: tempNumBytesMap.put("ascii", Constants.integerValueOf(1));
0090: tempNumBytesMap.put("ujis", Constants.integerValueOf(3));
0091: tempNumBytesMap.put("sjis", Constants.integerValueOf(2));
0092: tempNumBytesMap.put("hebrew", Constants.integerValueOf(1));
0093: tempNumBytesMap.put("tis620", Constants.integerValueOf(1));
0094: tempNumBytesMap.put("euckr", Constants.integerValueOf(2));
0095: tempNumBytesMap.put("koi8u", Constants.integerValueOf(1));
0096: tempNumBytesMap.put("gb2312", Constants.integerValueOf(2));
0097: tempNumBytesMap.put("greek", Constants.integerValueOf(1));
0098: tempNumBytesMap.put("cp1250", Constants.integerValueOf(1));
0099: tempNumBytesMap.put("gbk", Constants.integerValueOf(2));
0100: tempNumBytesMap.put("latin5", Constants.integerValueOf(1));
0101: tempNumBytesMap.put("armscii8", Constants.integerValueOf(1));
0102: tempNumBytesMap.put("utf8", Constants.integerValueOf(3));
0103: tempNumBytesMap.put("ucs2", Constants.integerValueOf(2));
0104: tempNumBytesMap.put("cp866", Constants.integerValueOf(1));
0105: tempNumBytesMap.put("keybcs2", Constants.integerValueOf(1));
0106: tempNumBytesMap.put("macce", Constants.integerValueOf(1));
0107: tempNumBytesMap.put("macroman", Constants.integerValueOf(1));
0108: tempNumBytesMap.put("cp852", Constants.integerValueOf(1));
0109: tempNumBytesMap.put("latin7", Constants.integerValueOf(1));
0110: tempNumBytesMap.put("cp1251", Constants.integerValueOf(1));
0111: tempNumBytesMap.put("cp1256", Constants.integerValueOf(1));
0112: tempNumBytesMap.put("cp1257", Constants.integerValueOf(1));
0113: tempNumBytesMap.put("binary", Constants.integerValueOf(1));
0114: tempNumBytesMap.put("geostd8", Constants.integerValueOf(1));
0115: tempNumBytesMap.put("cp932", Constants.integerValueOf(2));
0116: tempNumBytesMap.put("eucjpms", Constants.integerValueOf(3));
0117:
0118: STATIC_CHARSET_TO_NUM_BYTES_MAP = Collections
0119: .unmodifiableMap(tempNumBytesMap);
0120:
0121: CHARSET_CONFIG.setProperty("javaToMysqlMappings",
0122: //
0123: // Note: This used to be stored in Charsets.properties,
0124: // but turned out to be problematic when dealing with
0125: // Tomcat classloaders when the security manager was
0126: // enabled
0127: //
0128: // Java Encoding MySQL Name (and version, '*'
0129: // denotes preferred value)
0130: //
0131: "US-ASCII = usa7," + "US-ASCII = >4.1.0 ascii,"
0132: + "Big5 = big5," + "GBK = gbk,"
0133: + "SJIS = sjis," + "EUC_CN = gb2312,"
0134: + "EUC_JP = ujis,"
0135: + "EUC_JP_Solaris = >5.0.3 eucjpms,"
0136: + "EUC_KR = euc_kr,"
0137: + "EUC_KR = >4.1.0 euckr,"
0138: + "ISO8859_1 = *latin1,"
0139: + "ISO8859_1 = latin1_de,"
0140: + "ISO8859_1 = german1,"
0141: + "ISO8859_1 = danish,"
0142: + "ISO8859_2 = latin2,"
0143: + "ISO8859_2 = czech,"
0144: + "ISO8859_2 = hungarian,"
0145: + "ISO8859_2 = croat,"
0146: + "ISO8859_7 = greek,"
0147: + "ISO8859_7 = latin7,"
0148: + "ISO8859_8 = hebrew,"
0149: + "ISO8859_9 = latin5,"
0150: + "ISO8859_13 = latvian,"
0151: + "ISO8859_13 = latvian1,"
0152: + "ISO8859_13 = estonia,"
0153: + "Cp437 = *>4.1.0 cp850,"
0154: + "Cp437 = dos," + "Cp850 = cp850,"
0155: + "Cp852 = cp852," + "Cp866 = cp866,"
0156: + "KOI8_R = koi8_ru,"
0157: + "KOI8_R = >4.1.0 koi8r,"
0158: + "TIS620 = tis620," + "Cp1250 = cp1250,"
0159: + "Cp1250 = win1250,"
0160: + "Cp1251 = *>4.1.0 cp1251,"
0161: + "Cp1251 = win1251,"
0162: + "Cp1251 = cp1251cias,"
0163: + "Cp1251 = cp1251csas,"
0164: + "Cp1256 = cp1256,"
0165: + "Cp1251 = win1251ukr,"
0166: + "Cp1252 = latin1,"
0167: + "Cp1257 = cp1257,"
0168: + "MacRoman = macroman,"
0169: + "MacCentralEurope = macce,"
0170: + "UTF-8 = utf8," + "UnicodeBig = ucs2,"
0171: + "US-ASCII = binary,"
0172: + "Cp943 = sjis," + "MS932 = sjis,"
0173: + "MS932 = >4.1.11 cp932,"
0174: + "WINDOWS-31J = sjis,"
0175: + "WINDOWS-31J = >4.1.11 cp932,"
0176: + "CP932 = sjis,"
0177: + "CP932 = *>4.1.11 cp932,"
0178: + "SHIFT_JIS = sjis," + "ASCII = ascii,"
0179: + "LATIN5 = latin5," + "LATIN7 = latin7,"
0180: + "HEBREW = hebrew," + "GREEK = greek,"
0181: + "EUCKR = euckr," + "GB2312 = gb2312,"
0182: + "LATIN2 = latin2");
0183:
0184: HashMap javaToMysqlMap = new HashMap();
0185:
0186: populateMapWithKeyValuePairs("javaToMysqlMappings",
0187: javaToMysqlMap, true, false);
0188: JAVA_TO_MYSQL_CHARSET_MAP = Collections
0189: .unmodifiableMap(javaToMysqlMap);
0190:
0191: HashMap mysqlToJavaMap = new HashMap();
0192:
0193: Set keySet = JAVA_TO_MYSQL_CHARSET_MAP.keySet();
0194:
0195: Iterator javaCharsets = keySet.iterator();
0196:
0197: while (javaCharsets.hasNext()) {
0198: Object javaEncodingName = javaCharsets.next();
0199: List mysqlEncodingList = (List) JAVA_TO_MYSQL_CHARSET_MAP
0200: .get(javaEncodingName);
0201:
0202: Iterator mysqlEncodings = mysqlEncodingList.iterator();
0203:
0204: String mysqlEncodingName = null;
0205:
0206: while (mysqlEncodings.hasNext()) {
0207: VersionedStringProperty mysqlProp = (VersionedStringProperty) mysqlEncodings
0208: .next();
0209: mysqlEncodingName = mysqlProp.toString();
0210:
0211: mysqlToJavaMap.put(mysqlEncodingName, javaEncodingName);
0212: mysqlToJavaMap.put(mysqlEncodingName
0213: .toUpperCase(Locale.ENGLISH), javaEncodingName);
0214: }
0215: }
0216:
0217: // we don't want CP932 to map to CP932
0218: mysqlToJavaMap.put("cp932", "Windows-31J");
0219: mysqlToJavaMap.put("CP932", "Windows-31J");
0220:
0221: MYSQL_TO_JAVA_CHARSET_MAP = Collections
0222: .unmodifiableMap(mysqlToJavaMap);
0223:
0224: TreeMap ucMap = new TreeMap(String.CASE_INSENSITIVE_ORDER);
0225:
0226: Iterator javaNamesKeys = JAVA_TO_MYSQL_CHARSET_MAP.keySet()
0227: .iterator();
0228:
0229: while (javaNamesKeys.hasNext()) {
0230: String key = (String) javaNamesKeys.next();
0231:
0232: ucMap.put(key.toUpperCase(Locale.ENGLISH),
0233: JAVA_TO_MYSQL_CHARSET_MAP.get(key));
0234: }
0235:
0236: JAVA_UC_TO_MYSQL_CHARSET_MAP = Collections
0237: .unmodifiableMap(ucMap);
0238:
0239: //
0240: // Character sets that we can't convert
0241: // ourselves.
0242: //
0243: HashMap tempMapMulti = new HashMap();
0244:
0245: CHARSET_CONFIG.setProperty("multibyteCharsets",
0246: //
0247: // Note: This used to be stored in Charsets.properties,
0248: // but turned out to be problematic when dealing with
0249: // Tomcat classloaders when the security manager was
0250: // enabled
0251: //
0252: // Java Name MySQL Name (not currently used)
0253: //
0254:
0255: "Big5 = big5," + "GBK = gbk," + "SJIS = sjis,"
0256: + "EUC_CN = gb2312," + "EUC_JP = ujis,"
0257: + "EUC_JP_Solaris = eucjpms,"
0258: + "EUC_KR = euc_kr,"
0259: + "EUC_KR = >4.1.0 euckr,"
0260: + "Cp943 = sjis," + "Cp943 = cp943,"
0261: + "WINDOWS-31J = sjis,"
0262: + "WINDOWS-31J = cp932," + "CP932 = cp932,"
0263: + "MS932 = sjis," + "MS932 = cp932,"
0264: + "SHIFT_JIS = sjis," + "EUCKR = euckr,"
0265: + "GB2312 = gb2312," + "UTF-8 = utf8,"
0266: + "utf8 = utf8,"
0267: + "UnicodeBig = ucs2");
0268:
0269: populateMapWithKeyValuePairs("multibyteCharsets", tempMapMulti,
0270: false, true);
0271:
0272: MULTIBYTE_CHARSETS = Collections.unmodifiableMap(tempMapMulti);
0273:
0274: INDEX_TO_CHARSET = new String[211];
0275:
0276: try {
0277: INDEX_TO_CHARSET[1] = getJavaEncodingForMysqlEncoding(
0278: "big5", null);
0279: INDEX_TO_CHARSET[2] = getJavaEncodingForMysqlEncoding(
0280: "czech", null);
0281: INDEX_TO_CHARSET[3] = "ISO8859_1"; // punting for "dec8"
0282: INDEX_TO_CHARSET[4] = "ISO8859_1"; // punting for "dos"
0283: INDEX_TO_CHARSET[5] = getJavaEncodingForMysqlEncoding(
0284: "german1", null);
0285: INDEX_TO_CHARSET[6] = "ISO8859_1"; // punting for "hp8"
0286: INDEX_TO_CHARSET[7] = getJavaEncodingForMysqlEncoding(
0287: "koi8_ru", null);
0288: INDEX_TO_CHARSET[8] = getJavaEncodingForMysqlEncoding(
0289: "latin1", null);
0290: INDEX_TO_CHARSET[9] = getJavaEncodingForMysqlEncoding(
0291: "latin2", null);
0292: INDEX_TO_CHARSET[10] = "ISO8859_1"; // punting for "swe7"
0293: INDEX_TO_CHARSET[11] = getJavaEncodingForMysqlEncoding(
0294: "usa7", null);
0295: INDEX_TO_CHARSET[12] = getJavaEncodingForMysqlEncoding(
0296: "ujis", null);
0297: INDEX_TO_CHARSET[13] = getJavaEncodingForMysqlEncoding(
0298: "sjis", null);
0299: INDEX_TO_CHARSET[14] = getJavaEncodingForMysqlEncoding(
0300: "cp1251", null);
0301: INDEX_TO_CHARSET[15] = getJavaEncodingForMysqlEncoding(
0302: "danish", null);
0303: INDEX_TO_CHARSET[16] = getJavaEncodingForMysqlEncoding(
0304: "hebrew", null);
0305:
0306: INDEX_TO_CHARSET[17] = NOT_USED; // not used in the server
0307:
0308: INDEX_TO_CHARSET[18] = getJavaEncodingForMysqlEncoding(
0309: "tis620", null);
0310: INDEX_TO_CHARSET[19] = getJavaEncodingForMysqlEncoding(
0311: "euc_kr", null);
0312: INDEX_TO_CHARSET[20] = getJavaEncodingForMysqlEncoding(
0313: "estonia", null);
0314: INDEX_TO_CHARSET[21] = getJavaEncodingForMysqlEncoding(
0315: "hungarian", null);
0316: INDEX_TO_CHARSET[22] = "KOI8_R"; //punting for "koi8_ukr"
0317: INDEX_TO_CHARSET[23] = getJavaEncodingForMysqlEncoding(
0318: "win1251ukr", null);
0319: INDEX_TO_CHARSET[24] = getJavaEncodingForMysqlEncoding(
0320: "gb2312", null);
0321: INDEX_TO_CHARSET[25] = getJavaEncodingForMysqlEncoding(
0322: "greek", null);
0323: INDEX_TO_CHARSET[26] = getJavaEncodingForMysqlEncoding(
0324: "win1250", null);
0325: INDEX_TO_CHARSET[27] = getJavaEncodingForMysqlEncoding(
0326: "croat", null);
0327: INDEX_TO_CHARSET[28] = getJavaEncodingForMysqlEncoding(
0328: "gbk", null);
0329: INDEX_TO_CHARSET[29] = getJavaEncodingForMysqlEncoding(
0330: "cp1257", null);
0331: INDEX_TO_CHARSET[30] = getJavaEncodingForMysqlEncoding(
0332: "latin5", null);
0333: INDEX_TO_CHARSET[31] = getJavaEncodingForMysqlEncoding(
0334: "latin1_de", null);
0335: INDEX_TO_CHARSET[32] = "ISO8859_1"; // punting "armscii8"
0336: INDEX_TO_CHARSET[33] = getJavaEncodingForMysqlEncoding(
0337: "utf8", null);
0338: INDEX_TO_CHARSET[34] = "Cp1250"; // punting "win1250ch"
0339: INDEX_TO_CHARSET[35] = getJavaEncodingForMysqlEncoding(
0340: "ucs2", null);
0341: INDEX_TO_CHARSET[36] = getJavaEncodingForMysqlEncoding(
0342: "cp866", null);
0343: INDEX_TO_CHARSET[37] = "Cp895"; // punting "keybcs2"
0344: INDEX_TO_CHARSET[38] = getJavaEncodingForMysqlEncoding(
0345: "macce", null);
0346: INDEX_TO_CHARSET[39] = getJavaEncodingForMysqlEncoding(
0347: "macroman", null);
0348: INDEX_TO_CHARSET[40] = "latin2"; // punting "pclatin2"
0349: INDEX_TO_CHARSET[41] = getJavaEncodingForMysqlEncoding(
0350: "latvian", null);
0351: INDEX_TO_CHARSET[42] = getJavaEncodingForMysqlEncoding(
0352: "latvian1", null);
0353: INDEX_TO_CHARSET[43] = getJavaEncodingForMysqlEncoding(
0354: "macce", null);
0355: INDEX_TO_CHARSET[44] = getJavaEncodingForMysqlEncoding(
0356: "macce", null);
0357: INDEX_TO_CHARSET[45] = getJavaEncodingForMysqlEncoding(
0358: "macce", null);
0359: INDEX_TO_CHARSET[46] = getJavaEncodingForMysqlEncoding(
0360: "macce", null);
0361: INDEX_TO_CHARSET[47] = getJavaEncodingForMysqlEncoding(
0362: "latin1", null);
0363: INDEX_TO_CHARSET[48] = getJavaEncodingForMysqlEncoding(
0364: "latin1", null);
0365: INDEX_TO_CHARSET[49] = getJavaEncodingForMysqlEncoding(
0366: "latin1", null);
0367: INDEX_TO_CHARSET[50] = getJavaEncodingForMysqlEncoding(
0368: "cp1251", null);
0369: INDEX_TO_CHARSET[51] = getJavaEncodingForMysqlEncoding(
0370: "cp1251", null);
0371: INDEX_TO_CHARSET[52] = getJavaEncodingForMysqlEncoding(
0372: "cp1251", null);
0373: INDEX_TO_CHARSET[53] = getJavaEncodingForMysqlEncoding(
0374: "macroman", null);
0375: INDEX_TO_CHARSET[54] = getJavaEncodingForMysqlEncoding(
0376: "macroman", null);
0377: INDEX_TO_CHARSET[55] = getJavaEncodingForMysqlEncoding(
0378: "macroman", null);
0379: INDEX_TO_CHARSET[56] = getJavaEncodingForMysqlEncoding(
0380: "macroman", null);
0381: INDEX_TO_CHARSET[57] = getJavaEncodingForMysqlEncoding(
0382: "cp1256", null);
0383:
0384: INDEX_TO_CHARSET[58] = NOT_USED; // not used
0385: INDEX_TO_CHARSET[59] = NOT_USED; // not used
0386: INDEX_TO_CHARSET[60] = NOT_USED; // not used
0387: INDEX_TO_CHARSET[61] = NOT_USED; // not used
0388: INDEX_TO_CHARSET[62] = NOT_USED; // not used
0389:
0390: INDEX_TO_CHARSET[63] = getJavaEncodingForMysqlEncoding(
0391: "binary", null);
0392: INDEX_TO_CHARSET[64] = "ISO8859_2"; // punting "armscii"
0393: INDEX_TO_CHARSET[65] = getJavaEncodingForMysqlEncoding(
0394: "ascii", null);
0395: INDEX_TO_CHARSET[66] = getJavaEncodingForMysqlEncoding(
0396: "cp1250", null);
0397: INDEX_TO_CHARSET[67] = getJavaEncodingForMysqlEncoding(
0398: "cp1256", null);
0399: INDEX_TO_CHARSET[68] = getJavaEncodingForMysqlEncoding(
0400: "cp866", null);
0401: INDEX_TO_CHARSET[69] = "US-ASCII"; // punting for "dec8"
0402: INDEX_TO_CHARSET[70] = getJavaEncodingForMysqlEncoding(
0403: "greek", null);
0404: INDEX_TO_CHARSET[71] = getJavaEncodingForMysqlEncoding(
0405: "hebrew", null);
0406: INDEX_TO_CHARSET[72] = "US-ASCII"; // punting for "hp8"
0407: INDEX_TO_CHARSET[73] = "Cp895"; // punting for "keybcs2"
0408: INDEX_TO_CHARSET[74] = getJavaEncodingForMysqlEncoding(
0409: "koi8r", null);
0410: INDEX_TO_CHARSET[75] = "KOI8_r"; // punting for koi8ukr"
0411:
0412: INDEX_TO_CHARSET[76] = NOT_USED; // not used
0413:
0414: INDEX_TO_CHARSET[77] = getJavaEncodingForMysqlEncoding(
0415: "latin2", null);
0416: INDEX_TO_CHARSET[78] = getJavaEncodingForMysqlEncoding(
0417: "latin5", null);
0418: INDEX_TO_CHARSET[79] = getJavaEncodingForMysqlEncoding(
0419: "latin7", null);
0420: INDEX_TO_CHARSET[80] = getJavaEncodingForMysqlEncoding(
0421: "cp850", null);
0422: INDEX_TO_CHARSET[81] = getJavaEncodingForMysqlEncoding(
0423: "cp852", null);
0424: INDEX_TO_CHARSET[82] = "ISO8859_1"; // punting for "swe7"
0425: INDEX_TO_CHARSET[83] = getJavaEncodingForMysqlEncoding(
0426: "utf8", null);
0427: INDEX_TO_CHARSET[84] = getJavaEncodingForMysqlEncoding(
0428: "big5", null);
0429: INDEX_TO_CHARSET[85] = getJavaEncodingForMysqlEncoding(
0430: "euckr", null);
0431: INDEX_TO_CHARSET[86] = getJavaEncodingForMysqlEncoding(
0432: "gb2312", null);
0433: INDEX_TO_CHARSET[87] = getJavaEncodingForMysqlEncoding(
0434: "gbk", null);
0435: INDEX_TO_CHARSET[88] = getJavaEncodingForMysqlEncoding(
0436: "sjis", null);
0437: INDEX_TO_CHARSET[89] = getJavaEncodingForMysqlEncoding(
0438: "tis620", null);
0439: INDEX_TO_CHARSET[90] = getJavaEncodingForMysqlEncoding(
0440: "ucs2", null);
0441: INDEX_TO_CHARSET[91] = getJavaEncodingForMysqlEncoding(
0442: "ujis", null);
0443: INDEX_TO_CHARSET[92] = "US-ASCII"; //punting for "geostd8"
0444: INDEX_TO_CHARSET[93] = "US-ASCII"; // punting for "geostd8"
0445: INDEX_TO_CHARSET[94] = getJavaEncodingForMysqlEncoding(
0446: "latin1", null);
0447: INDEX_TO_CHARSET[95] = getJavaEncodingForMysqlEncoding(
0448: "cp932", null);
0449: INDEX_TO_CHARSET[96] = getJavaEncodingForMysqlEncoding(
0450: "cp932", null);
0451: INDEX_TO_CHARSET[97] = getJavaEncodingForMysqlEncoding(
0452: "eucjpms", null);
0453: INDEX_TO_CHARSET[98] = getJavaEncodingForMysqlEncoding(
0454: "eucjpms", null);
0455:
0456: for (int i = 99; i < 128; i++) {
0457: INDEX_TO_CHARSET[i] = NOT_USED; // not used
0458: }
0459:
0460: INDEX_TO_CHARSET[128] = getJavaEncodingForMysqlEncoding(
0461: "ucs2", null);
0462: INDEX_TO_CHARSET[129] = getJavaEncodingForMysqlEncoding(
0463: "ucs2", null);
0464: INDEX_TO_CHARSET[130] = getJavaEncodingForMysqlEncoding(
0465: "ucs2", null);
0466: INDEX_TO_CHARSET[131] = getJavaEncodingForMysqlEncoding(
0467: "ucs2", null);
0468: INDEX_TO_CHARSET[132] = getJavaEncodingForMysqlEncoding(
0469: "ucs2", null);
0470: INDEX_TO_CHARSET[133] = getJavaEncodingForMysqlEncoding(
0471: "ucs2", null);
0472: INDEX_TO_CHARSET[134] = getJavaEncodingForMysqlEncoding(
0473: "ucs2", null);
0474: INDEX_TO_CHARSET[135] = getJavaEncodingForMysqlEncoding(
0475: "ucs2", null);
0476: INDEX_TO_CHARSET[136] = getJavaEncodingForMysqlEncoding(
0477: "ucs2", null);
0478: INDEX_TO_CHARSET[137] = getJavaEncodingForMysqlEncoding(
0479: "ucs2", null);
0480: INDEX_TO_CHARSET[138] = getJavaEncodingForMysqlEncoding(
0481: "ucs2", null);
0482: INDEX_TO_CHARSET[139] = getJavaEncodingForMysqlEncoding(
0483: "ucs2", null);
0484: INDEX_TO_CHARSET[140] = getJavaEncodingForMysqlEncoding(
0485: "ucs2", null);
0486: INDEX_TO_CHARSET[141] = getJavaEncodingForMysqlEncoding(
0487: "ucs2", null);
0488: INDEX_TO_CHARSET[142] = getJavaEncodingForMysqlEncoding(
0489: "ucs2", null);
0490: INDEX_TO_CHARSET[143] = getJavaEncodingForMysqlEncoding(
0491: "ucs2", null);
0492: INDEX_TO_CHARSET[144] = getJavaEncodingForMysqlEncoding(
0493: "ucs2", null);
0494: INDEX_TO_CHARSET[145] = getJavaEncodingForMysqlEncoding(
0495: "ucs2", null);
0496: INDEX_TO_CHARSET[146] = getJavaEncodingForMysqlEncoding(
0497: "ucs2", null);
0498:
0499: for (int i = 147; i < 192; i++) {
0500: INDEX_TO_CHARSET[i] = NOT_USED; // not used
0501: }
0502:
0503: INDEX_TO_CHARSET[192] = getJavaEncodingForMysqlEncoding(
0504: "utf8", null);
0505: INDEX_TO_CHARSET[193] = getJavaEncodingForMysqlEncoding(
0506: "utf8", null);
0507: INDEX_TO_CHARSET[194] = getJavaEncodingForMysqlEncoding(
0508: "utf8", null);
0509: INDEX_TO_CHARSET[195] = getJavaEncodingForMysqlEncoding(
0510: "utf8", null);
0511: INDEX_TO_CHARSET[196] = getJavaEncodingForMysqlEncoding(
0512: "utf8", null);
0513: INDEX_TO_CHARSET[197] = getJavaEncodingForMysqlEncoding(
0514: "utf8", null);
0515: INDEX_TO_CHARSET[198] = getJavaEncodingForMysqlEncoding(
0516: "utf8", null);
0517: INDEX_TO_CHARSET[199] = getJavaEncodingForMysqlEncoding(
0518: "utf8", null);
0519: INDEX_TO_CHARSET[200] = getJavaEncodingForMysqlEncoding(
0520: "utf8", null);
0521: INDEX_TO_CHARSET[201] = getJavaEncodingForMysqlEncoding(
0522: "utf8", null);
0523: INDEX_TO_CHARSET[202] = getJavaEncodingForMysqlEncoding(
0524: "utf8", null);
0525: INDEX_TO_CHARSET[203] = getJavaEncodingForMysqlEncoding(
0526: "utf8", null);
0527: INDEX_TO_CHARSET[204] = getJavaEncodingForMysqlEncoding(
0528: "utf8", null);
0529: INDEX_TO_CHARSET[205] = getJavaEncodingForMysqlEncoding(
0530: "utf8", null);
0531: INDEX_TO_CHARSET[206] = getJavaEncodingForMysqlEncoding(
0532: "utf8", null);
0533: INDEX_TO_CHARSET[207] = getJavaEncodingForMysqlEncoding(
0534: "utf8", null);
0535: INDEX_TO_CHARSET[208] = getJavaEncodingForMysqlEncoding(
0536: "utf8", null);
0537: INDEX_TO_CHARSET[209] = getJavaEncodingForMysqlEncoding(
0538: "utf8", null);
0539: INDEX_TO_CHARSET[210] = getJavaEncodingForMysqlEncoding(
0540: "utf8", null);
0541:
0542: // Sanity check
0543:
0544: for (int i = 1; i < INDEX_TO_CHARSET.length; i++) {
0545: if (INDEX_TO_CHARSET[i] == null) {
0546: throw new RuntimeException(
0547: "Assertion failure: No mapping from charset index "
0548: + i + " to a Java character set");
0549: }
0550: }
0551: } catch (SQLException sqlEx) {
0552: // ignore, it won't happen in this case
0553: }
0554:
0555: INDEX_TO_COLLATION = new String[211];
0556:
0557: INDEX_TO_COLLATION[1] = "big5_chinese_ci";
0558: INDEX_TO_COLLATION[2] = "latin2_czech_cs";
0559: INDEX_TO_COLLATION[3] = "dec8_swedish_ci";
0560: INDEX_TO_COLLATION[4] = "cp850_general_ci";
0561: INDEX_TO_COLLATION[5] = "latin1_german1_ci";
0562: INDEX_TO_COLLATION[6] = "hp8_english_ci";
0563: INDEX_TO_COLLATION[7] = "koi8r_general_ci";
0564: INDEX_TO_COLLATION[8] = "latin1_swedish_ci";
0565: INDEX_TO_COLLATION[9] = "latin2_general_ci";
0566: INDEX_TO_COLLATION[10] = "swe7_swedish_ci";
0567: INDEX_TO_COLLATION[11] = "ascii_general_ci";
0568: INDEX_TO_COLLATION[12] = "ujis_japanese_ci";
0569: INDEX_TO_COLLATION[13] = "sjis_japanese_ci";
0570: INDEX_TO_COLLATION[14] = "cp1251_bulgarian_ci";
0571: INDEX_TO_COLLATION[15] = "latin1_danish_ci";
0572: INDEX_TO_COLLATION[16] = "hebrew_general_ci";
0573: INDEX_TO_COLLATION[18] = "tis620_thai_ci";
0574: INDEX_TO_COLLATION[19] = "euckr_korean_ci";
0575: INDEX_TO_COLLATION[20] = "latin7_estonian_cs";
0576: INDEX_TO_COLLATION[21] = "latin2_hungarian_ci";
0577: INDEX_TO_COLLATION[22] = "koi8u_general_ci";
0578: INDEX_TO_COLLATION[23] = "cp1251_ukrainian_ci";
0579: INDEX_TO_COLLATION[24] = "gb2312_chinese_ci";
0580: INDEX_TO_COLLATION[25] = "greek_general_ci";
0581: INDEX_TO_COLLATION[26] = "cp1250_general_ci";
0582: INDEX_TO_COLLATION[27] = "latin2_croatian_ci";
0583: INDEX_TO_COLLATION[28] = "gbk_chinese_ci";
0584: INDEX_TO_COLLATION[29] = "cp1257_lithuanian_ci";
0585: INDEX_TO_COLLATION[30] = "latin5_turkish_ci";
0586: INDEX_TO_COLLATION[31] = "latin1_german2_ci";
0587: INDEX_TO_COLLATION[32] = "armscii8_general_ci";
0588: INDEX_TO_COLLATION[33] = "utf8_general_ci";
0589: INDEX_TO_COLLATION[34] = "cp1250_czech_cs";
0590: INDEX_TO_COLLATION[35] = "ucs2_general_ci";
0591: INDEX_TO_COLLATION[36] = "cp866_general_ci";
0592: INDEX_TO_COLLATION[37] = "keybcs2_general_ci";
0593: INDEX_TO_COLLATION[38] = "macce_general_ci";
0594: INDEX_TO_COLLATION[39] = "macroman_general_ci";
0595: INDEX_TO_COLLATION[40] = "cp852_general_ci";
0596: INDEX_TO_COLLATION[41] = "latin7_general_ci";
0597: INDEX_TO_COLLATION[42] = "latin7_general_cs";
0598: INDEX_TO_COLLATION[43] = "macce_bin";
0599: INDEX_TO_COLLATION[44] = "cp1250_croatian_ci";
0600: INDEX_TO_COLLATION[47] = "latin1_bin";
0601: INDEX_TO_COLLATION[48] = "latin1_general_ci";
0602: INDEX_TO_COLLATION[49] = "latin1_general_cs";
0603: INDEX_TO_COLLATION[50] = "cp1251_bin";
0604: INDEX_TO_COLLATION[51] = "cp1251_general_ci";
0605: INDEX_TO_COLLATION[52] = "cp1251_general_cs";
0606: INDEX_TO_COLLATION[53] = "macroman_bin";
0607: INDEX_TO_COLLATION[57] = "cp1256_general_ci";
0608: INDEX_TO_COLLATION[58] = "cp1257_bin";
0609: INDEX_TO_COLLATION[59] = "cp1257_general_ci";
0610: INDEX_TO_COLLATION[63] = "binary";
0611: INDEX_TO_COLLATION[64] = "armscii8_bin";
0612: INDEX_TO_COLLATION[65] = "ascii_bin";
0613: INDEX_TO_COLLATION[66] = "cp1250_bin";
0614: INDEX_TO_COLLATION[67] = "cp1256_bin";
0615: INDEX_TO_COLLATION[68] = "cp866_bin";
0616: INDEX_TO_COLLATION[69] = "dec8_bin";
0617: INDEX_TO_COLLATION[70] = "greek_bin";
0618: INDEX_TO_COLLATION[71] = "hebrew_bin";
0619: INDEX_TO_COLLATION[72] = "hp8_bin";
0620: INDEX_TO_COLLATION[73] = "keybcs2_bin";
0621: INDEX_TO_COLLATION[74] = "koi8r_bin";
0622: INDEX_TO_COLLATION[75] = "koi8u_bin";
0623: INDEX_TO_COLLATION[77] = "latin2_bin";
0624: INDEX_TO_COLLATION[78] = "latin5_bin";
0625: INDEX_TO_COLLATION[79] = "latin7_bin";
0626: INDEX_TO_COLLATION[80] = "cp850_bin";
0627: INDEX_TO_COLLATION[81] = "cp852_bin";
0628: INDEX_TO_COLLATION[82] = "swe7_bin";
0629: INDEX_TO_COLLATION[83] = "utf8_bin";
0630: INDEX_TO_COLLATION[84] = "big5_bin";
0631: INDEX_TO_COLLATION[85] = "euckr_bin";
0632: INDEX_TO_COLLATION[86] = "gb2312_bin";
0633: INDEX_TO_COLLATION[87] = "gbk_bin";
0634: INDEX_TO_COLLATION[88] = "sjis_bin";
0635: INDEX_TO_COLLATION[89] = "tis620_bin";
0636: INDEX_TO_COLLATION[90] = "ucs2_bin";
0637: INDEX_TO_COLLATION[91] = "ujis_bin";
0638: INDEX_TO_COLLATION[92] = "geostd8_general_ci";
0639: INDEX_TO_COLLATION[93] = "geostd8_bin";
0640: INDEX_TO_COLLATION[94] = "latin1_spanish_ci";
0641: INDEX_TO_COLLATION[95] = "cp932_japanese_ci";
0642: INDEX_TO_COLLATION[96] = "cp932_bin";
0643: INDEX_TO_COLLATION[97] = "eucjpms_japanese_ci";
0644: INDEX_TO_COLLATION[98] = "eucjpms_bin";
0645: INDEX_TO_COLLATION[99] = "cp1250_polish_ci";
0646: INDEX_TO_COLLATION[128] = "ucs2_unicode_ci";
0647: INDEX_TO_COLLATION[129] = "ucs2_icelandic_ci";
0648: INDEX_TO_COLLATION[130] = "ucs2_latvian_ci";
0649: INDEX_TO_COLLATION[131] = "ucs2_romanian_ci";
0650: INDEX_TO_COLLATION[132] = "ucs2_slovenian_ci";
0651: INDEX_TO_COLLATION[133] = "ucs2_polish_ci";
0652: INDEX_TO_COLLATION[134] = "ucs2_estonian_ci";
0653: INDEX_TO_COLLATION[135] = "ucs2_spanish_ci";
0654: INDEX_TO_COLLATION[136] = "ucs2_swedish_ci";
0655: INDEX_TO_COLLATION[137] = "ucs2_turkish_ci";
0656: INDEX_TO_COLLATION[138] = "ucs2_czech_ci";
0657: INDEX_TO_COLLATION[139] = "ucs2_danish_ci";
0658: INDEX_TO_COLLATION[140] = "ucs2_lithuanian_ci ";
0659: INDEX_TO_COLLATION[141] = "ucs2_slovak_ci";
0660: INDEX_TO_COLLATION[142] = "ucs2_spanish2_ci";
0661: INDEX_TO_COLLATION[143] = "ucs2_roman_ci";
0662: INDEX_TO_COLLATION[144] = "ucs2_persian_ci";
0663: INDEX_TO_COLLATION[145] = "ucs2_esperanto_ci";
0664: INDEX_TO_COLLATION[146] = "ucs2_hungarian_ci";
0665: INDEX_TO_COLLATION[192] = "utf8_unicode_ci";
0666: INDEX_TO_COLLATION[193] = "utf8_icelandic_ci";
0667: INDEX_TO_COLLATION[194] = "utf8_latvian_ci";
0668: INDEX_TO_COLLATION[195] = "utf8_romanian_ci";
0669: INDEX_TO_COLLATION[196] = "utf8_slovenian_ci";
0670: INDEX_TO_COLLATION[197] = "utf8_polish_ci";
0671: INDEX_TO_COLLATION[198] = "utf8_estonian_ci";
0672: INDEX_TO_COLLATION[199] = "utf8_spanish_ci";
0673: INDEX_TO_COLLATION[200] = "utf8_swedish_ci";
0674: INDEX_TO_COLLATION[201] = "utf8_turkish_ci";
0675: INDEX_TO_COLLATION[202] = "utf8_czech_ci";
0676: INDEX_TO_COLLATION[203] = "utf8_danish_ci";
0677: INDEX_TO_COLLATION[204] = "utf8_lithuanian_ci ";
0678: INDEX_TO_COLLATION[205] = "utf8_slovak_ci";
0679: INDEX_TO_COLLATION[206] = "utf8_spanish2_ci";
0680: INDEX_TO_COLLATION[207] = "utf8_roman_ci";
0681: INDEX_TO_COLLATION[208] = "utf8_persian_ci";
0682: INDEX_TO_COLLATION[209] = "utf8_esperanto_ci";
0683: INDEX_TO_COLLATION[210] = "utf8_hungarian_ci";
0684:
0685: Map indexMap = new TreeMap(String.CASE_INSENSITIVE_ORDER);
0686:
0687: for (int i = 0; i < INDEX_TO_CHARSET.length; i++) {
0688: String mysqlEncodingName = INDEX_TO_CHARSET[i];
0689:
0690: if (mysqlEncodingName != null) {
0691: indexMap.put(INDEX_TO_CHARSET[i], Constants
0692: .integerValueOf(i));
0693: }
0694: }
0695:
0696: MYSQL_ENCODING_NAME_TO_CHARSET_INDEX_MAP = Collections
0697: .unmodifiableMap(indexMap);
0698:
0699: Map tempMap = new HashMap();
0700:
0701: tempMap.put("czech", "latin2");
0702: tempMap.put("danish", "latin1");
0703: tempMap.put("dutch", "latin1");
0704: tempMap.put("english", "latin1");
0705: tempMap.put("estonian", "latin7");
0706: tempMap.put("french", "latin1");
0707: tempMap.put("german", "latin1");
0708: tempMap.put("greek", "greek");
0709: tempMap.put("hungarian", "latin2");
0710: tempMap.put("italian", "latin1");
0711: tempMap.put("japanese", "ujis");
0712: tempMap.put("japanese-sjis", "sjis");
0713: tempMap.put("korean", "euckr");
0714: tempMap.put("norwegian", "latin1");
0715: tempMap.put("norwegian-ny", "latin1");
0716: tempMap.put("polish", "latin2");
0717: tempMap.put("portuguese", "latin1");
0718: tempMap.put("romanian", "latin2");
0719: tempMap.put("russian", "koi8r");
0720: tempMap.put("serbian", "cp1250");
0721: tempMap.put("slovak", "latin2");
0722: tempMap.put("spanish", "latin1");
0723: tempMap.put("swedish", "latin1");
0724: tempMap.put("ukrainian", "koi8u");
0725:
0726: ERROR_MESSAGE_FILE_TO_MYSQL_CHARSET_MAP = Collections
0727: .unmodifiableMap(tempMap);
0728: }
0729:
0730: public final static String getJavaEncodingForMysqlEncoding(
0731: String mysqlEncoding, Connection conn) throws SQLException {
0732:
0733: if (conn != null && conn.versionMeetsMinimum(4, 1, 0)
0734: && "latin1".equalsIgnoreCase(mysqlEncoding)) {
0735: return "Cp1252";
0736: }
0737:
0738: return (String) MYSQL_TO_JAVA_CHARSET_MAP.get(mysqlEncoding);
0739: }
0740:
0741: public final static String getMysqlEncodingForJavaEncoding(
0742: String javaEncodingUC, Connection conn) throws SQLException {
0743: List mysqlEncodings = (List) CharsetMapping.JAVA_UC_TO_MYSQL_CHARSET_MAP
0744: .get(javaEncodingUC);
0745: ;
0746:
0747: if (mysqlEncodings != null) {
0748: Iterator iter = mysqlEncodings.iterator();
0749:
0750: VersionedStringProperty versionedProp = null;
0751:
0752: while (iter.hasNext()) {
0753: VersionedStringProperty propToCheck = (VersionedStringProperty) iter
0754: .next();
0755:
0756: if (conn == null) {
0757: // Take the first one we get
0758:
0759: return propToCheck.toString();
0760: }
0761:
0762: if (versionedProp != null
0763: && !versionedProp.preferredValue) {
0764: if (versionedProp.majorVersion == propToCheck.majorVersion
0765: && versionedProp.minorVersion == propToCheck.minorVersion
0766: && versionedProp.subminorVersion == propToCheck.subminorVersion) {
0767: return versionedProp.toString();
0768: }
0769: }
0770:
0771: if (propToCheck.isOkayForVersion(conn)) {
0772: if (propToCheck.preferredValue) {
0773: return propToCheck.toString();
0774: }
0775:
0776: versionedProp = propToCheck;
0777: } else {
0778: break;
0779: }
0780: }
0781:
0782: if (versionedProp != null) {
0783: return versionedProp.toString();
0784: }
0785: }
0786:
0787: return null;
0788: }
0789:
0790: final static int getNumberOfCharsetsConfigured() {
0791: return MYSQL_TO_JAVA_CHARSET_MAP.size() / 2; // because we UC every
0792: // key
0793: }
0794:
0795: /**
0796: * Returns the character encoding for error messages returned from the
0797: * server. Doesn't return useful values other than Cp1252 until the driver
0798: * has gone through initialization phase and determined server configuration,
0799: * as not enough information is available to make an intelligent decision
0800: * until then.
0801: *
0802: * @param conn the connection to the MySQL server
0803: * @return the Java encoding name that error messages use
0804: * @throws SQLException if determination of the character encoding fails
0805: */
0806: final static String getCharacterEncodingForErrorMessages(
0807: ConnectionImpl conn) throws SQLException {
0808: String errorMessageFile = conn.getServerVariable("language");
0809:
0810: if (errorMessageFile == null || errorMessageFile.length() == 0) {
0811: // punt
0812: return "Cp1252";
0813: }
0814:
0815: int endWithoutSlash = errorMessageFile.length();
0816:
0817: if (errorMessageFile.endsWith("/")
0818: || errorMessageFile.endsWith("\\")) {
0819: endWithoutSlash--;
0820: }
0821:
0822: int lastSlashIndex = errorMessageFile.lastIndexOf('/',
0823: endWithoutSlash - 1);
0824:
0825: if (lastSlashIndex == -1) {
0826: lastSlashIndex = errorMessageFile.lastIndexOf('\\',
0827: endWithoutSlash - 1);
0828: }
0829:
0830: if (lastSlashIndex == -1) {
0831: lastSlashIndex = 0;
0832: }
0833:
0834: if (lastSlashIndex == endWithoutSlash
0835: || endWithoutSlash < lastSlashIndex) {
0836: // punt
0837: return "Cp1252";
0838: }
0839:
0840: errorMessageFile = errorMessageFile.substring(
0841: lastSlashIndex + 1, endWithoutSlash);
0842:
0843: String errorMessageEncodingMysql = (String) ERROR_MESSAGE_FILE_TO_MYSQL_CHARSET_MAP
0844: .get(errorMessageFile);
0845:
0846: if (errorMessageEncodingMysql == null) {
0847: // punt
0848: return "Cp1252";
0849: }
0850:
0851: String javaEncoding = getJavaEncodingForMysqlEncoding(
0852: errorMessageEncodingMysql, conn);
0853:
0854: if (javaEncoding == null) {
0855: // punt
0856: return "Cp1252";
0857: }
0858:
0859: return javaEncoding;
0860: }
0861:
0862: final static boolean isAliasForSjis(String encoding) {
0863: return ("SJIS".equalsIgnoreCase(encoding)
0864: || "WINDOWS-31J".equalsIgnoreCase(encoding)
0865: || "MS932".equalsIgnoreCase(encoding)
0866: || "SHIFT_JIS".equalsIgnoreCase(encoding) || "CP943"
0867: .equalsIgnoreCase(encoding));
0868:
0869: }
0870:
0871: final static boolean isMultibyteCharset(String javaEncodingName) {
0872: String javaEncodingNameUC = javaEncodingName
0873: .toUpperCase(Locale.ENGLISH);
0874:
0875: return MULTIBYTE_CHARSETS.containsKey(javaEncodingNameUC);
0876: }
0877:
0878: private static void populateMapWithKeyValuePairs(String configKey,
0879: Map mapToPopulate, boolean addVersionedProperties,
0880: boolean addUppercaseKeys) {
0881: String javaToMysqlConfig = CHARSET_CONFIG
0882: .getProperty(configKey);
0883:
0884: if (javaToMysqlConfig != null) {
0885: List mappings = StringUtils.split(javaToMysqlConfig, ",",
0886: true);
0887:
0888: if (mappings != null) {
0889: Iterator mappingsIter = mappings.iterator();
0890:
0891: while (mappingsIter.hasNext()) {
0892: String aMapping = (String) mappingsIter.next();
0893:
0894: List parsedPair = StringUtils.split(aMapping, "=",
0895: true);
0896:
0897: if (parsedPair.size() == 2) {
0898: String key = parsedPair.get(0).toString();
0899: String value = parsedPair.get(1).toString();
0900:
0901: if (addVersionedProperties) {
0902: List versionedProperties = (List) mapToPopulate
0903: .get(key);
0904:
0905: if (versionedProperties == null) {
0906: versionedProperties = new ArrayList();
0907: mapToPopulate.put(key,
0908: versionedProperties);
0909: }
0910:
0911: VersionedStringProperty verProp = new VersionedStringProperty(
0912: value);
0913: versionedProperties.add(verProp);
0914:
0915: if (addUppercaseKeys) {
0916: String keyUc = key
0917: .toUpperCase(Locale.ENGLISH);
0918:
0919: versionedProperties = (List) mapToPopulate
0920: .get(keyUc);
0921:
0922: if (versionedProperties == null) {
0923: versionedProperties = new ArrayList();
0924: mapToPopulate.put(keyUc,
0925: versionedProperties);
0926: }
0927:
0928: versionedProperties.add(verProp);
0929: }
0930: } else {
0931: mapToPopulate.put(key, value);
0932:
0933: if (addUppercaseKeys) {
0934: mapToPopulate.put(key
0935: .toUpperCase(Locale.ENGLISH),
0936: value);
0937: }
0938: }
0939: } else {
0940: throw new RuntimeException(
0941: "Syntax error in Charsets.properties "
0942: + "resource for token \""
0943: + aMapping + "\".");
0944: }
0945: }
0946: } else {
0947: throw new RuntimeException(
0948: "Missing/corrupt entry for \"" + configKey
0949: + "\" in Charsets.properties.");
0950: }
0951: } else {
0952: throw new RuntimeException(
0953: "Could not find configuration value " + "\""
0954: + configKey
0955: + "\" in Charsets.properties resource");
0956: }
0957: }
0958:
0959: public static int getCharsetIndexForMysqlEncodingName(String name) {
0960: if (name == null) {
0961: return 0;
0962: }
0963:
0964: Integer asInt = (Integer) MYSQL_ENCODING_NAME_TO_CHARSET_INDEX_MAP
0965: .get(name);
0966:
0967: if (asInt == null) {
0968: return 0;
0969: }
0970:
0971: return asInt.intValue();
0972: }
0973: }
0974:
0975: class VersionedStringProperty {
0976: int majorVersion, minorVersion, subminorVersion;
0977:
0978: boolean preferredValue = false;
0979:
0980: String propertyInfo;
0981:
0982: VersionedStringProperty(String property) {
0983: property = property.trim();
0984:
0985: if (property.startsWith("*")) {
0986: property = property.substring(1);
0987: preferredValue = true;
0988: }
0989:
0990: if (property.startsWith(">")) {
0991: property = property.substring(1);
0992:
0993: int charPos = 0;
0994:
0995: for (charPos = 0; charPos < property.length(); charPos++) {
0996: char c = property.charAt(charPos);
0997:
0998: if (!Character.isWhitespace(c) && !Character.isDigit(c)
0999: && c != '.') {
1000: break;
1001: }
1002: }
1003:
1004: String versionInfo = property.substring(0, charPos);
1005: List versionParts = StringUtils.split(versionInfo, ".",
1006: true);
1007:
1008: majorVersion = Integer.parseInt(versionParts.get(0)
1009: .toString());
1010:
1011: if (versionParts.size() > 1) {
1012: minorVersion = Integer.parseInt(versionParts.get(1)
1013: .toString());
1014: } else {
1015: minorVersion = 0;
1016: }
1017:
1018: if (versionParts.size() > 2) {
1019: subminorVersion = Integer.parseInt(versionParts.get(2)
1020: .toString());
1021: } else {
1022: subminorVersion = 0;
1023: }
1024:
1025: propertyInfo = property.substring(charPos);
1026: } else {
1027: majorVersion = minorVersion = subminorVersion = 0;
1028: propertyInfo = property;
1029: }
1030: }
1031:
1032: VersionedStringProperty(String property, int major, int minor,
1033: int subminor) {
1034: propertyInfo = property;
1035: majorVersion = major;
1036: minorVersion = minor;
1037: subminorVersion = subminor;
1038: }
1039:
1040: boolean isOkayForVersion(Connection conn) throws SQLException {
1041: return conn.versionMeetsMinimum(majorVersion, minorVersion,
1042: subminorVersion);
1043: }
1044:
1045: public String toString() {
1046: return propertyInfo;
1047: }
1048: }
|