You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

211 lines
7.8 KiB

4 years ago
  1. // Copyright ?2004, 2015, Oracle and/or its affiliates. All rights reserved.
  2. //
  3. // MySQL Connector/NET is licensed under the terms of the GPLv2
  4. // <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
  5. // MySQL Connectors. There are special exceptions to the terms and
  6. // conditions of the GPLv2 as it is applied to this software, see the
  7. // FLOSS License Exception
  8. // <http://www.mysql.com/about/legal/licensing/foss-exception.html>.
  9. //
  10. // This program is free software; you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published
  12. // by the Free Software Foundation; version 2 of the License.
  13. //
  14. // This program is distributed in the hope that it will be useful, but
  15. // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16. // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  17. // for more details.
  18. //
  19. // You should have received a copy of the GNU General Public License along
  20. // with this program; if not, write to the Free Software Foundation, Inc.,
  21. // 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  22. #if MYSQL_6_9
  23. using System;
  24. using System.Collections;
  25. using System.Text;
  26. using Externals.MySql.Data.Common;
  27. using System.Collections.Generic;
  28. using System.Data;
  29. namespace Externals.MySql.Data.MySqlClient
  30. {
  31. /// <summary>
  32. /// Summary description for CharSetMap.
  33. /// </summary>
  34. internal class CharSetMap
  35. {
  36. private static Dictionary<string, string> defaultCollations;
  37. private static Dictionary<string, int> maxLengths;
  38. private static Dictionary<string, CharacterSet> mapping;
  39. private static object lockObject;
  40. // we use a static constructor here since we only want to init
  41. // the mapping once
  42. static CharSetMap()
  43. {
  44. lockObject = new Object();
  45. InitializeMapping();
  46. }
  47. public static CharacterSet GetCharacterSet(DBVersion version, string CharSetName)
  48. {
  49. CharacterSet cs = null;
  50. if(mapping.ContainsKey(CharSetName))
  51. cs = (CharacterSet)mapping[CharSetName];
  52. if (cs == null)
  53. throw new MySqlException("Character set '" + CharSetName + "' is not supported by .Net Framework.");
  54. return cs;
  55. }
  56. /// <summary>
  57. /// Returns the text encoding for a given MySQL character set name
  58. /// </summary>
  59. /// <param name="version">Version of the connection requesting the encoding</param>
  60. /// <param name="CharSetName">Name of the character set to get the encoding for</param>
  61. /// <returns>Encoding object for the given character set name</returns>
  62. public static Encoding GetEncoding(DBVersion version, string CharSetName)
  63. {
  64. try
  65. {
  66. CharacterSet cs = GetCharacterSet(version, CharSetName);
  67. return Encoding.GetEncoding(cs.name);
  68. }
  69. catch (NotSupportedException)
  70. {
  71. return Encoding.GetEncoding("utf-8");
  72. }
  73. }
  74. /// <summary>
  75. ///
  76. /// </summary>
  77. private static void InitializeMapping()
  78. {
  79. LoadCharsetMap();
  80. }
  81. private static void LoadCharsetMap()
  82. {
  83. mapping = new Dictionary<string, CharacterSet>();
  84. mapping.Add("latin1", new CharacterSet("windows-1252", 1));
  85. mapping.Add("big5", new CharacterSet("big5", 2));
  86. mapping.Add("dec8", mapping["latin1"]);
  87. mapping.Add("cp850", new CharacterSet("ibm850", 1));
  88. mapping.Add("hp8", mapping["latin1"]);
  89. mapping.Add("koi8r", new CharacterSet("koi8-u", 1));
  90. mapping.Add("latin2", new CharacterSet("latin2", 1));
  91. mapping.Add("swe7", mapping["latin1"]);
  92. mapping.Add("ujis", new CharacterSet("EUC-JP", 3));
  93. mapping.Add("eucjpms", mapping["ujis"]);
  94. mapping.Add("sjis", new CharacterSet("sjis", 2));
  95. mapping.Add("cp932", mapping["sjis"]);
  96. mapping.Add("hebrew", new CharacterSet("hebrew", 1));
  97. mapping.Add("tis620", new CharacterSet("windows-874", 1));
  98. mapping.Add("euckr", new CharacterSet("euc-kr", 2));
  99. mapping.Add("euc_kr", mapping["euckr"]);
  100. mapping.Add("koi8u", new CharacterSet("koi8-u", 1));
  101. mapping.Add("koi8_ru", mapping["koi8u"]);
  102. mapping.Add("gb2312", new CharacterSet("gb2312", 2));
  103. mapping.Add("gbk", mapping["gb2312"]);
  104. mapping.Add("greek", new CharacterSet("greek", 1));
  105. mapping.Add("cp1250", new CharacterSet("windows-1250", 1));
  106. mapping.Add("win1250", mapping["cp1250"]);
  107. mapping.Add("latin5", new CharacterSet("latin5", 1));
  108. mapping.Add("armscii8", mapping["latin1"]);
  109. mapping.Add("utf8", new CharacterSet("utf-8", 3));
  110. mapping.Add("ucs2", new CharacterSet("UTF-16BE", 2));
  111. mapping.Add("cp866", new CharacterSet("cp866", 1));
  112. mapping.Add("keybcs2", mapping["latin1"]);
  113. mapping.Add("macce", new CharacterSet("x-mac-ce", 1));
  114. mapping.Add("macroman", new CharacterSet("x-mac-romanian", 1));
  115. mapping.Add("cp852", new CharacterSet("ibm852", 2));
  116. mapping.Add("latin7", new CharacterSet("iso-8859-7", 1));
  117. mapping.Add("cp1251", new CharacterSet("windows-1251", 1));
  118. mapping.Add("win1251ukr", mapping["cp1251"]);
  119. mapping.Add("cp1251csas", mapping["cp1251"]);
  120. mapping.Add("cp1251cias", mapping["cp1251"]);
  121. mapping.Add("win1251", mapping["cp1251"]);
  122. mapping.Add("cp1256", new CharacterSet("cp1256", 1));
  123. mapping.Add("cp1257", new CharacterSet("windows-1257", 1));
  124. mapping.Add("ascii", new CharacterSet("us-ascii", 1));
  125. mapping.Add("usa7", mapping["ascii"]);
  126. mapping.Add("binary", mapping["ascii"]);
  127. mapping.Add("latin3", new CharacterSet("latin3", 1));
  128. mapping.Add("latin4", new CharacterSet("latin4", 1));
  129. mapping.Add("latin1_de", new CharacterSet("iso-8859-1", 1));
  130. mapping.Add("german1", new CharacterSet("iso-8859-1", 1));
  131. mapping.Add("danish", new CharacterSet("iso-8859-1", 1));
  132. mapping.Add("czech", new CharacterSet("iso-8859-2", 1));
  133. mapping.Add("hungarian", new CharacterSet("iso-8859-2", 1));
  134. mapping.Add("croat", new CharacterSet("iso-8859-2", 1));
  135. mapping.Add("latvian", new CharacterSet("iso-8859-13", 1));
  136. mapping.Add("latvian1", new CharacterSet("iso-8859-13", 1));
  137. mapping.Add("estonia", new CharacterSet("iso-8859-13", 1));
  138. mapping.Add("dos", new CharacterSet("ibm437", 1));
  139. mapping.Add("utf8mb4", new CharacterSet("utf-8", 4));
  140. mapping.Add("utf16", new CharacterSet("utf-16BE", 2));
  141. mapping.Add("utf16le", new CharacterSet("utf-16", 2));
  142. mapping.Add("utf32", new CharacterSet("utf-32BE", 4));
  143. mapping.Add("gb18030", new CharacterSet("gb18030", 4));
  144. }
  145. internal static void InitCollections(MySqlConnection connection)
  146. {
  147. defaultCollations = new Dictionary<string, string>();
  148. maxLengths = new Dictionary<string, int>();
  149. MySqlCommand cmd = new MySqlCommand("SHOW CHARSET", connection);
  150. using (MySqlDataReader reader = cmd.ExecuteReader())
  151. {
  152. while (reader.Read())
  153. {
  154. defaultCollations.Add(reader.GetString(0), reader.GetString(2));
  155. maxLengths.Add(reader.GetString(0), Convert.ToInt32(reader.GetValue(3)));
  156. }
  157. }
  158. }
  159. internal static string GetDefaultCollation(string charset, MySqlConnection connection)
  160. {
  161. lock (lockObject)
  162. {
  163. if (defaultCollations == null)
  164. InitCollections(connection);
  165. }
  166. if (!defaultCollations.ContainsKey(charset))
  167. return null;
  168. return defaultCollations[charset];
  169. }
  170. internal static int GetMaxLength(string charset, MySqlConnection connection)
  171. {
  172. lock (lockObject)
  173. {
  174. if (maxLengths == null)
  175. InitCollections(connection);
  176. }
  177. if (!maxLengths.ContainsKey(charset))
  178. return 1;
  179. return maxLengths[charset];
  180. }
  181. }
  182. internal class CharacterSet
  183. {
  184. public string name;
  185. public int byteCount;
  186. public CharacterSet(string name, int byteCount)
  187. {
  188. this.name = name;
  189. this.byteCount = byteCount;
  190. }
  191. }
  192. }
  193. #endif