Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/rbmysql/charset.rb
Views: 11765
# coding: ascii-8bit1# Copyright (C) 2008-2012 TOMITA Masahiro2# mailto:[email protected]34#5class RbMysql6# @!attribute [r] number7# @private8# @!attribute [r] name9# @return [String] charset name10# @!attribute [r] csname11# @return [String] collation name12class Charset13# @private14# @param [Integer] number15# @param [String] name16# @param [String] csname17def initialize(number, name, csname)18@number, @name, @csname = number, name, csname19@unsafe = false20end2122attr_reader :number, :name, :csname2324# @private25attr_accessor :unsafe2627# [[charset_number, charset_name, collation_name, default], ...]28# @private29CHARSETS = [30[ 1, "big5", "big5_chinese_ci", true ],31[ 2, "latin2", "latin2_czech_cs", false],32[ 3, "dec8", "dec8_swedish_ci", true ],33[ 4, "cp850", "cp850_general_ci", true ],34[ 5, "latin1", "latin1_german1_ci", false],35[ 6, "hp8", "hp8_english_ci", true ],36[ 7, "koi8r", "koi8r_general_ci", true ],37[ 8, "latin1", "latin1_swedish_ci", true ],38[ 9, "latin2", "latin2_general_ci", true ],39[ 10, "swe7", "swe7_swedish_ci", true ],40[ 11, "ascii", "ascii_general_ci", true ],41[ 12, "ujis", "ujis_japanese_ci", true ],42[ 13, "sjis", "sjis_japanese_ci", true ],43[ 14, "cp1251", "cp1251_bulgarian_ci", false],44[ 15, "latin1", "latin1_danish_ci", false],45[ 16, "hebrew", "hebrew_general_ci", true ],46[ 17, "filename", "filename", true ],47[ 18, "tis620", "tis620_thai_ci", true ],48[ 19, "euckr", "euckr_korean_ci", true ],49[ 20, "latin7", "latin7_estonian_cs", false],50[ 21, "latin2", "latin2_hungarian_ci", false],51[ 22, "koi8u", "koi8u_general_ci", true ],52[ 23, "cp1251", "cp1251_ukrainian_ci", false],53[ 24, "gb2312", "gb2312_chinese_ci", true ],54[ 25, "greek", "greek_general_ci", true ],55[ 26, "cp1250", "cp1250_general_ci", true ],56[ 27, "latin2", "latin2_croatian_ci", false],57[ 28, "gbk", "gbk_chinese_ci", true ],58[ 29, "cp1257", "cp1257_lithuanian_ci", false],59[ 30, "latin5", "latin5_turkish_ci", true ],60[ 31, "latin1", "latin1_german2_ci", false],61[ 32, "armscii8", "armscii8_general_ci", true ],62[ 33, "utf8", "utf8_general_ci", true ],63[ 34, "cp1250", "cp1250_czech_cs", false],64[ 35, "ucs2", "ucs2_general_ci", true ],65[ 36, "cp866", "cp866_general_ci", true ],66[ 37, "keybcs2", "keybcs2_general_ci", true ],67[ 38, "macce", "macce_general_ci", true ],68[ 39, "macroman", "macroman_general_ci", true ],69[ 40, "cp852", "cp852_general_ci", true ],70[ 41, "latin7", "latin7_general_ci", true ],71[ 42, "latin7", "latin7_general_cs", false],72[ 43, "macce", "macce_bin", false],73[ 44, "cp1250", "cp1250_croatian_ci", false],74[ 45, "utf8mb4", "utf8mb4_general_ci", true ],75[ 46, "utf8mb4", "utf8mb4_bin", false],76[ 47, "latin1", "latin1_bin", false],77[ 48, "latin1", "latin1_general_ci", false],78[ 49, "latin1", "latin1_general_cs", false],79[ 50, "cp1251", "cp1251_bin", false],80[ 51, "cp1251", "cp1251_general_ci", true ],81[ 52, "cp1251", "cp1251_general_cs", false],82[ 53, "macroman", "macroman_bin", false],83[ 54, "utf16", "utf16_general_ci", true ],84[ 55, "utf16", "utf16_bin", false],85[ 56, "utf16le", "utf16le_general_ci", true ],86[ 57, "cp1256", "cp1256_general_ci", true ],87[ 58, "cp1257", "cp1257_bin", false],88[ 59, "cp1257", "cp1257_general_ci", true ],89[ 60, "utf32", "utf32_general_ci", true ],90[ 61, "utf32", "utf32_bin", false],91[ 62, "utf16le", "utf16le_bin", false],92[ 63, "binary", "binary", true ],93[ 64, "armscii8", "armscii8_bin", false],94[ 65, "ascii", "ascii_bin", false],95[ 66, "cp1250", "cp1250_bin", false],96[ 67, "cp1256", "cp1256_bin", false],97[ 68, "cp866", "cp866_bin", false],98[ 69, "dec8", "dec8_bin", false],99[ 70, "greek", "greek_bin", false],100[ 71, "hebrew", "hebrew_bin", false],101[ 72, "hp8", "hp8_bin", false],102[ 73, "keybcs2", "keybcs2_bin", false],103[ 74, "koi8r", "koi8r_bin", false],104[ 75, "koi8u", "koi8u_bin", false],105[ 77, "latin2", "latin2_bin", false],106[ 78, "latin5", "latin5_bin", false],107[ 79, "latin7", "latin7_bin", false],108[ 80, "cp850", "cp850_bin", false],109[ 81, "cp852", "cp852_bin", false],110[ 82, "swe7", "swe7_bin", false],111[ 83, "utf8", "utf8_bin", false],112[ 84, "big5", "big5_bin", false],113[ 85, "euckr", "euckr_bin", false],114[ 86, "gb2312", "gb2312_bin", false],115[ 87, "gbk", "gbk_bin", false],116[ 88, "sjis", "sjis_bin", false],117[ 89, "tis620", "tis620_bin", false],118[ 90, "ucs2", "ucs2_bin", false],119[ 91, "ujis", "ujis_bin", false],120[ 92, "geostd8", "geostd8_general_ci", true ],121[ 93, "geostd8", "geostd8_bin", false],122[ 94, "latin1", "latin1_spanish_ci", false],123[ 95, "cp932", "cp932_japanese_ci", true ],124[ 96, "cp932", "cp932_bin", false],125[ 97, "eucjpms", "eucjpms_japanese_ci", true ],126[ 98, "eucjpms", "eucjpms_bin", false],127[ 99, "cp1250", "cp1250_polish_ci", false],128[101, "utf16", "utf16_unicode_ci", false],129[102, "utf16", "utf16_icelandic_ci", false],130[103, "utf16", "utf16_latvian_ci", false],131[104, "utf16", "utf16_romanian_ci", false],132[105, "utf16", "utf16_slovenian_ci", false],133[106, "utf16", "utf16_polish_ci", false],134[107, "utf16", "utf16_estonian_ci", false],135[108, "utf16", "utf16_spanish_ci", false],136[109, "utf16", "utf16_swedish_ci", false],137[110, "utf16", "utf16_turkish_ci", false],138[111, "utf16", "utf16_czech_ci", false],139[112, "utf16", "utf16_danish_ci", false],140[113, "utf16", "utf16_lithuanian_ci", false],141[114, "utf16", "utf16_slovak_ci", false],142[115, "utf16", "utf16_spanish2_ci", false],143[116, "utf16", "utf16_roman_ci", false],144[117, "utf16", "utf16_persian_ci", false],145[118, "utf16", "utf16_esperanto_ci", false],146[119, "utf16", "utf16_hungarian_ci", false],147[120, "utf16", "utf16_sinhala_ci", false],148[121, "utf16", "utf16_german2_ci", false],149[122, "utf16", "utf16_croatian_ci", false],150[123, "utf16", "utf16_unicode_520_ci", false],151[124, "utf16", "utf16_vietnamese_ci", false],152[128, "ucs2", "ucs2_unicode_ci", false],153[129, "ucs2", "ucs2_icelandic_ci", false],154[130, "ucs2", "ucs2_latvian_ci", false],155[131, "ucs2", "ucs2_romanian_ci", false],156[132, "ucs2", "ucs2_slovenian_ci", false],157[133, "ucs2", "ucs2_polish_ci", false],158[134, "ucs2", "ucs2_estonian_ci", false],159[135, "ucs2", "ucs2_spanish_ci", false],160[136, "ucs2", "ucs2_swedish_ci", false],161[137, "ucs2", "ucs2_turkish_ci", false],162[138, "ucs2", "ucs2_czech_ci", false],163[139, "ucs2", "ucs2_danish_ci", false],164[140, "ucs2", "ucs2_lithuanian_ci", false],165[141, "ucs2", "ucs2_slovak_ci", false],166[142, "ucs2", "ucs2_spanish2_ci", false],167[143, "ucs2", "ucs2_roman_ci", false],168[144, "ucs2", "ucs2_persian_ci", false],169[145, "ucs2", "ucs2_esperanto_ci", false],170[146, "ucs2", "ucs2_hungarian_ci", false],171[147, "ucs2", "ucs2_sinhala_ci", false],172[148, "ucs2", "ucs2_german2_ci", false],173[149, "ucs2", "ucs2_croatian_ci", false],174[150, "ucs2", "ucs2_unicode_520_ci", false],175[151, "ucs2", "ucs2_vietnamese_ci", false],176[159, "ucs2", "ucs2_general_mysql500_ci", false],177[160, "utf32", "utf32_unicode_ci", false],178[161, "utf32", "utf32_icelandic_ci", false],179[162, "utf32", "utf32_latvian_ci", false],180[163, "utf32", "utf32_romanian_ci", false],181[164, "utf32", "utf32_slovenian_ci", false],182[165, "utf32", "utf32_polish_ci", false],183[166, "utf32", "utf32_estonian_ci", false],184[167, "utf32", "utf32_spanish_ci", false],185[168, "utf32", "utf32_swedish_ci", false],186[169, "utf32", "utf32_turkish_ci", false],187[170, "utf32", "utf32_czech_ci", false],188[171, "utf32", "utf32_danish_ci", false],189[172, "utf32", "utf32_lithuanian_ci", false],190[173, "utf32", "utf32_slovak_ci", false],191[174, "utf32", "utf32_spanish2_ci", false],192[175, "utf32", "utf32_roman_ci", false],193[176, "utf32", "utf32_persian_ci", false],194[177, "utf32", "utf32_esperanto_ci", false],195[178, "utf32", "utf32_hungarian_ci", false],196[179, "utf32", "utf32_sinhala_ci", false],197[180, "utf32", "utf32_german2_ci", false],198[181, "utf32", "utf32_croatian_ci", false],199[182, "utf32", "utf32_unicode_520_ci", false],200[183, "utf32", "utf32_vietnamese_ci", false],201[192, "utf8", "utf8_unicode_ci", false],202[193, "utf8", "utf8_icelandic_ci", false],203[194, "utf8", "utf8_latvian_ci", false],204[195, "utf8", "utf8_romanian_ci", false],205[196, "utf8", "utf8_slovenian_ci", false],206[197, "utf8", "utf8_polish_ci", false],207[198, "utf8", "utf8_estonian_ci", false],208[199, "utf8", "utf8_spanish_ci", false],209[200, "utf8", "utf8_swedish_ci", false],210[201, "utf8", "utf8_turkish_ci", false],211[202, "utf8", "utf8_czech_ci", false],212[203, "utf8", "utf8_danish_ci", false],213[204, "utf8", "utf8_lithuanian_ci", false],214[205, "utf8", "utf8_slovak_ci", false],215[206, "utf8", "utf8_spanish2_ci", false],216[207, "utf8", "utf8_roman_ci", false],217[208, "utf8", "utf8_persian_ci", false],218[209, "utf8", "utf8_esperanto_ci", false],219[210, "utf8", "utf8_hungarian_ci", false],220[211, "utf8", "utf8_sinhala_ci", false],221[212, "utf8", "utf8_german2_ci", false],222[213, "utf8", "utf8_croatian_ci", false],223[214, "utf8", "utf8_unicode_520_ci", false],224[215, "utf8", "utf8_vietnamese_ci", false],225[223, "utf8", "utf8_general_mysql500_ci", false],226[224, "utf8mb4", "utf8mb4_unicode_ci", false],227[225, "utf8mb4", "utf8mb4_icelandic_ci", false],228[226, "utf8mb4", "utf8mb4_latvian_ci", false],229[227, "utf8mb4", "utf8mb4_romanian_ci", false],230[228, "utf8mb4", "utf8mb4_slovenian_ci", false],231[229, "utf8mb4", "utf8mb4_polish_ci", false],232[230, "utf8mb4", "utf8mb4_estonian_ci", false],233[231, "utf8mb4", "utf8mb4_spanish_ci", false],234[232, "utf8mb4", "utf8mb4_swedish_ci", false],235[233, "utf8mb4", "utf8mb4_turkish_ci", false],236[234, "utf8mb4", "utf8mb4_czech_ci", false],237[235, "utf8mb4", "utf8mb4_danish_ci", false],238[236, "utf8mb4", "utf8mb4_lithuanian_ci", false],239[237, "utf8mb4", "utf8mb4_slovak_ci", false],240[238, "utf8mb4", "utf8mb4_spanish2_ci", false],241[239, "utf8mb4", "utf8mb4_roman_ci", false],242[240, "utf8mb4", "utf8mb4_persian_ci", false],243[241, "utf8mb4", "utf8mb4_esperanto_ci", false],244[242, "utf8mb4", "utf8mb4_hungarian_ci", false],245[243, "utf8mb4", "utf8mb4_sinhala_ci", false],246[244, "utf8mb4", "utf8mb4_german2_ci", false],247[245, "utf8mb4", "utf8mb4_croatian_ci", false],248[246, "utf8mb4", "utf8mb4_unicode_520_ci", false],249[247, "utf8mb4", "utf8mb4_vietnamese_ci", false],250[248, "gb18030", "gb18030_chinese_ci", true ],251[249, "gb18030", "gb18030_bin", false],252[250, "gb18030", "gb18030_unicode_520_ci", false],253[254, "utf8", "utf8_general_cs", false],254]255256# @private257UNSAFE_CHARSET = [258"big5", "sjis", "filename", "gbk", "ucs2", "cp932",259]260261# @private262NUMBER_TO_CHARSET = {}263# @private264COLLATION_TO_CHARSET = {}265# @private266CHARSET_DEFAULT = {}267CHARSETS.each do |number, csname, clname, default|268cs = Charset.new number, csname, clname269cs.unsafe = true if UNSAFE_CHARSET.include? csname270NUMBER_TO_CHARSET[number] = cs271COLLATION_TO_CHARSET[clname] = cs272CHARSET_DEFAULT[csname] = cs if default273end274275# @private276BINARY_CHARSET_NUMBER = CHARSET_DEFAULT['binary'].number277278# @private279# @param [Integer] n280# @return [RbMysql::Charset]281def self.by_number(n)282raise ClientError, "unknown charset number: #{n}" unless NUMBER_TO_CHARSET.key? n283NUMBER_TO_CHARSET[n]284end285286# @private287# @param [String] str288# @return [RbMysql::Charset]289def self.by_name(str)290ret = COLLATION_TO_CHARSET[str] || CHARSET_DEFAULT[str]291raise ClientError, "unknown charset: #{str}" unless ret292ret293end294295if defined? Encoding296297# @private298# MySQL Charset -> Ruby's Encoding299CHARSET_ENCODING = {300"armscii8" => nil,301"ascii" => Encoding::US_ASCII,302"big5" => Encoding::Big5,303"binary" => Encoding::ASCII_8BIT,304"cp1250" => Encoding::Windows_1250,305"cp1251" => Encoding::Windows_1251,306"cp1256" => Encoding::Windows_1256,307"cp1257" => Encoding::Windows_1257,308"cp850" => Encoding::CP850,309"cp852" => Encoding::CP852,310"cp866" => Encoding::IBM866,311"cp932" => Encoding::Windows_31J,312"dec8" => nil,313"eucjpms" => Encoding::EucJP_ms,314"euckr" => Encoding::EUC_KR,315"gb2312" => Encoding::EUC_CN,316"gbk" => Encoding::GBK,317"geostd8" => nil,318"greek" => Encoding::ISO_8859_7,319"hebrew" => Encoding::ISO_8859_8,320"hp8" => nil,321"keybcs2" => nil,322"koi8r" => Encoding::KOI8_R,323"koi8u" => Encoding::KOI8_U,324"latin1" => Encoding::ISO_8859_1,325"latin2" => Encoding::ISO_8859_2,326"latin5" => Encoding::ISO_8859_9,327"latin7" => Encoding::ISO_8859_13,328"macce" => Encoding::MacCentEuro,329"macroman" => Encoding::MacRoman,330"sjis" => Encoding::SHIFT_JIS,331"swe7" => nil,332"tis620" => Encoding::TIS_620,333"ucs2" => Encoding::UTF_16BE,334"ujis" => Encoding::EucJP_ms,335"utf8" => Encoding::UTF_8,336"utf8mb4" => Encoding::UTF_8,337}338339# @private340# @param [String] value341# @return [String]342def self.to_binary(value)343value.force_encoding Encoding::ASCII_8BIT344end345346# @private347# convert raw to encoding and convert to Encoding.default_internal348# @param [String] raw349# @param [Encoding] encoding350# @return [String] result351def self.convert_encoding(raw, encoding)352raw.force_encoding(encoding).encode353end354355# @private356# return corresponding Ruby encoding357# @return [Encoding] encoding358def encoding359enc = CHARSET_ENCODING[@name.downcase]360raise RbMysql::ClientError, "unsupported charset: #{@name}" unless enc361enc362end363364# @private365# convert encoding to corresponding to MySQL charset366# @param [String] value367# @return [String]368def convert(value)369if value.is_a? String and value.encoding != Encoding::ASCII_8BIT370value = value.encode encoding371end372value373end374375else376# for Ruby 1.8377378def self.to_binary(value)379value380end381382def self.convert_encoding(raw, encoding)383raw384end385386def encoding387nil388end389390def convert(value)391value392end393394end395end396end397398