mysql-pr 2.9.11 → 3.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,124 +1,124 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Copyright (C) 2008-2012 TOMITA Masahiro
2
4
  # mailto:tommy@tmtm.org
3
5
 
4
- #
5
6
  class MysqlPR
7
+ # MySQL character set and collation handling
6
8
  # @!attribute [r] number
7
- # @private
9
+ # @return [Integer] charset number
8
10
  # @!attribute [r] name
9
11
  # @return [String] charset name
10
12
  # @!attribute [r] csname
11
13
  # @return [String] collation name
12
14
  class Charset
13
- # @private
14
15
  # @param [Integer] number
15
16
  # @param [String] name
16
17
  # @param [String] csname
17
18
  def initialize(number, name, csname)
18
- @number, @name, @csname = number, name, csname
19
+ @number = number
20
+ @name = name
21
+ @csname = csname
19
22
  @unsafe = false
20
23
  end
21
24
 
22
25
  attr_reader :number, :name, :csname
23
-
24
- # @private
25
26
  attr_accessor :unsafe
26
27
 
27
28
  # [[charset_number, charset_name, collation_name, default], ...]
28
- # @private
29
29
  CHARSETS = [
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
- [ 57, "cp1256", "cp1256_general_ci", true ],
84
- [ 58, "cp1257", "cp1257_bin", false],
85
- [ 59, "cp1257", "cp1257_general_ci", true ],
86
- [ 63, "binary", "binary", true ],
87
- [ 64, "armscii8", "armscii8_bin", false],
88
- [ 65, "ascii", "ascii_bin", false],
89
- [ 66, "cp1250", "cp1250_bin", false],
90
- [ 67, "cp1256", "cp1256_bin", false],
91
- [ 68, "cp866", "cp866_bin", false],
92
- [ 69, "dec8", "dec8_bin", false],
93
- [ 70, "greek", "greek_bin", false],
94
- [ 71, "hebrew", "hebrew_bin", false],
95
- [ 72, "hp8", "hp8_bin", false],
96
- [ 73, "keybcs2", "keybcs2_bin", false],
97
- [ 74, "koi8r", "koi8r_bin", false],
98
- [ 75, "koi8u", "koi8u_bin", false],
99
- [ 77, "latin2", "latin2_bin", false],
100
- [ 78, "latin5", "latin5_bin", false],
101
- [ 79, "latin7", "latin7_bin", false],
102
- [ 80, "cp850", "cp850_bin", false],
103
- [ 81, "cp852", "cp852_bin", false],
104
- [ 82, "swe7", "swe7_bin", false],
105
- [ 83, "utf8", "utf8_bin", false],
106
- [ 84, "big5", "big5_bin", false],
107
- [ 85, "euckr", "euckr_bin", false],
108
- [ 86, "gb2312", "gb2312_bin", false],
109
- [ 87, "gbk", "gbk_bin", false],
110
- [ 88, "sjis", "sjis_bin", false],
111
- [ 89, "tis620", "tis620_bin", false],
112
- [ 90, "ucs2", "ucs2_bin", false],
113
- [ 91, "ujis", "ujis_bin", false],
114
- [ 92, "geostd8", "geostd8_general_ci", true ],
115
- [ 93, "geostd8", "geostd8_bin", false],
116
- [ 94, "latin1", "latin1_spanish_ci", false],
117
- [ 95, "cp932", "cp932_japanese_ci", true ],
118
- [ 96, "cp932", "cp932_bin", false],
119
- [ 97, "eucjpms", "eucjpms_japanese_ci", true ],
120
- [ 98, "eucjpms", "eucjpms_bin", false],
121
- [ 99, "cp1250", "cp1250_polish_ci", false],
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
+ [57, "cp1256", "cp1256_general_ci", true],
84
+ [58, "cp1257", "cp1257_bin", false],
85
+ [59, "cp1257", "cp1257_general_ci", true],
86
+ [63, "binary", "binary", true],
87
+ [64, "armscii8", "armscii8_bin", false],
88
+ [65, "ascii", "ascii_bin", false],
89
+ [66, "cp1250", "cp1250_bin", false],
90
+ [67, "cp1256", "cp1256_bin", false],
91
+ [68, "cp866", "cp866_bin", false],
92
+ [69, "dec8", "dec8_bin", false],
93
+ [70, "greek", "greek_bin", false],
94
+ [71, "hebrew", "hebrew_bin", false],
95
+ [72, "hp8", "hp8_bin", false],
96
+ [73, "keybcs2", "keybcs2_bin", false],
97
+ [74, "koi8r", "koi8r_bin", false],
98
+ [75, "koi8u", "koi8u_bin", false],
99
+ [77, "latin2", "latin2_bin", false],
100
+ [78, "latin5", "latin5_bin", false],
101
+ [79, "latin7", "latin7_bin", false],
102
+ [80, "cp850", "cp850_bin", false],
103
+ [81, "cp852", "cp852_bin", false],
104
+ [82, "swe7", "swe7_bin", false],
105
+ [83, "utf8", "utf8_bin", false],
106
+ [84, "big5", "big5_bin", false],
107
+ [85, "euckr", "euckr_bin", false],
108
+ [86, "gb2312", "gb2312_bin", false],
109
+ [87, "gbk", "gbk_bin", false],
110
+ [88, "sjis", "sjis_bin", false],
111
+ [89, "tis620", "tis620_bin", false],
112
+ [90, "ucs2", "ucs2_bin", false],
113
+ [91, "ujis", "ujis_bin", false],
114
+ [92, "geostd8", "geostd8_general_ci", true],
115
+ [93, "geostd8", "geostd8_bin", false],
116
+ [94, "latin1", "latin1_spanish_ci", false],
117
+ [95, "cp932", "cp932_japanese_ci", true],
118
+ [96, "cp932", "cp932_bin", false],
119
+ [97, "eucjpms", "eucjpms_japanese_ci", true],
120
+ [98, "eucjpms", "eucjpms_bin", false],
121
+ [99, "cp1250", "cp1250_polish_ci", false],
122
122
  [128, "ucs2", "ucs2_unicode_ci", false],
123
123
  [129, "ucs2", "ucs2_icelandic_ci", false],
124
124
  [130, "ucs2", "ucs2_latvian_ci", false],
@@ -179,146 +179,115 @@ class MysqlPR
179
179
  [242, "utf8mb4", "utf8mb4_hungarian_ci", false],
180
180
  [243, "utf8mb4", "utf8mb4_sinhala_ci", false],
181
181
  [254, "utf8", "utf8_general_cs", false],
182
- ]
182
+ [255, "utf8mb4", "utf8mb4_0900_ai_ci", false]
183
+ ].freeze
184
+
185
+ UNSAFE_CHARSET = %w[big5 sjis filename gbk ucs2 cp932].freeze
183
186
 
184
- # @private
185
- UNSAFE_CHARSET = [
186
- "big5", "sjis", "filename", "gbk", "ucs2", "cp932",
187
- ]
187
+ NUMBER_TO_CHARSET = {} # rubocop:disable Style/MutableConstant
188
+ COLLATION_TO_CHARSET = {} # rubocop:disable Style/MutableConstant
189
+ CHARSET_DEFAULT = {} # rubocop:disable Style/MutableConstant
188
190
 
189
- # @private
190
- NUMBER_TO_CHARSET = {}
191
- # @private
192
- COLLATION_TO_CHARSET = {}
193
- # @private
194
- CHARSET_DEFAULT = {}
195
191
  CHARSETS.each do |number, csname, clname, default|
196
- cs = Charset.new number, csname, clname
197
- cs.unsafe = true if UNSAFE_CHARSET.include? csname
192
+ cs = Charset.new(number, csname, clname)
193
+ cs.unsafe = true if UNSAFE_CHARSET.include?(csname)
198
194
  NUMBER_TO_CHARSET[number] = cs
199
195
  COLLATION_TO_CHARSET[clname] = cs
200
196
  CHARSET_DEFAULT[csname] = cs if default
201
197
  end
202
198
 
203
- # @private
204
- BINARY_CHARSET_NUMBER = CHARSET_DEFAULT['binary'].number
199
+ NUMBER_TO_CHARSET.freeze
200
+ COLLATION_TO_CHARSET.freeze
201
+ CHARSET_DEFAULT.freeze
202
+
203
+ BINARY_CHARSET_NUMBER = CHARSET_DEFAULT["binary"].number
204
+
205
+ # MySQL Charset -> Ruby Encoding mapping
206
+ CHARSET_ENCODING = {
207
+ "armscii8" => nil,
208
+ "ascii" => Encoding::US_ASCII,
209
+ "big5" => Encoding::Big5,
210
+ "binary" => Encoding::ASCII_8BIT,
211
+ "cp1250" => Encoding::Windows_1250,
212
+ "cp1251" => Encoding::Windows_1251,
213
+ "cp1256" => Encoding::Windows_1256,
214
+ "cp1257" => Encoding::Windows_1257,
215
+ "cp850" => Encoding::CP850,
216
+ "cp852" => Encoding::CP852,
217
+ "cp866" => Encoding::IBM866,
218
+ "cp932" => Encoding::Windows_31J,
219
+ "dec8" => nil,
220
+ "eucjpms" => Encoding::EucJP_ms,
221
+ "euckr" => Encoding::EUC_KR,
222
+ "gb2312" => Encoding::EUC_CN,
223
+ "gbk" => Encoding::GBK,
224
+ "geostd8" => nil,
225
+ "greek" => Encoding::ISO_8859_7,
226
+ "hebrew" => Encoding::ISO_8859_8,
227
+ "hp8" => nil,
228
+ "keybcs2" => nil,
229
+ "koi8r" => Encoding::KOI8_R,
230
+ "koi8u" => Encoding::KOI8_U,
231
+ "latin1" => Encoding::ISO_8859_1,
232
+ "latin2" => Encoding::ISO_8859_2,
233
+ "latin5" => Encoding::ISO_8859_9,
234
+ "latin7" => Encoding::ISO_8859_13,
235
+ "macce" => Encoding::MacCentEuro,
236
+ "macroman" => Encoding::MacRoman,
237
+ "sjis" => Encoding::SHIFT_JIS,
238
+ "swe7" => nil,
239
+ "tis620" => Encoding::TIS_620,
240
+ "ucs2" => Encoding::UTF_16BE,
241
+ "ujis" => Encoding::EucJP_ms,
242
+ "utf8" => Encoding::UTF_8,
243
+ "utf8mb4" => Encoding::UTF_8
244
+ }.freeze
205
245
 
206
- # @private
207
- # @param [Integer] n
208
- # @return [Mysql::Charset]
246
+ # @param [Integer] n charset number
247
+ # @return [MysqlPR::Charset]
209
248
  def self.by_number(n)
210
- raise ClientError, "unknown charset number: #{n}" unless NUMBER_TO_CHARSET.key? n
249
+ raise ClientError, "unknown charset number: #{n}" unless NUMBER_TO_CHARSET.key?(n)
250
+
211
251
  NUMBER_TO_CHARSET[n]
212
252
  end
213
253
 
214
- # @private
215
- # @param [String] str
216
- # @return [Mysql::Charset]
254
+ # @param [String] str charset or collation name
255
+ # @return [MysqlPR::Charset]
217
256
  def self.by_name(str)
218
257
  ret = COLLATION_TO_CHARSET[str] || CHARSET_DEFAULT[str]
219
258
  raise ClientError, "unknown charset: #{str}" unless ret
259
+
220
260
  ret
221
261
  end
222
262
 
223
- if defined? Encoding
224
-
225
- # @private
226
- # MySQL Charset -> Ruby's Encodeing
227
- CHARSET_ENCODING = {
228
- "armscii8" => nil,
229
- "ascii" => Encoding::US_ASCII,
230
- "big5" => Encoding::Big5,
231
- "binary" => Encoding::ASCII_8BIT,
232
- "cp1250" => Encoding::Windows_1250,
233
- "cp1251" => Encoding::Windows_1251,
234
- "cp1256" => Encoding::Windows_1256,
235
- "cp1257" => Encoding::Windows_1257,
236
- "cp850" => Encoding::CP850,
237
- "cp852" => Encoding::CP852,
238
- "cp866" => Encoding::IBM866,
239
- "cp932" => Encoding::Windows_31J,
240
- "dec8" => nil,
241
- "eucjpms" => Encoding::EucJP_ms,
242
- "euckr" => Encoding::EUC_KR,
243
- "gb2312" => Encoding::EUC_CN,
244
- "gbk" => Encoding::GBK,
245
- "geostd8" => nil,
246
- "greek" => Encoding::ISO_8859_7,
247
- "hebrew" => Encoding::ISO_8859_8,
248
- "hp8" => nil,
249
- "keybcs2" => nil,
250
- "koi8r" => Encoding::KOI8_R,
251
- "koi8u" => Encoding::KOI8_U,
252
- "latin1" => Encoding::ISO_8859_1,
253
- "latin2" => Encoding::ISO_8859_2,
254
- "latin5" => Encoding::ISO_8859_9,
255
- "latin7" => Encoding::ISO_8859_13,
256
- "macce" => Encoding::MacCentEuro,
257
- "macroman" => Encoding::MacRoman,
258
- "sjis" => Encoding::SHIFT_JIS,
259
- "swe7" => nil,
260
- "tis620" => Encoding::TIS_620,
261
- "ucs2" => Encoding::UTF_16BE,
262
- "ujis" => Encoding::EucJP_ms,
263
- "utf8" => Encoding::UTF_8,
264
- "utf8mb4" => Encoding::UTF_8,
265
- }
266
-
267
- # @private
268
- # @param [String] value
269
- # @return [String]
270
- def self.to_binary(value)
271
- value.force_encoding Encoding::ASCII_8BIT
272
- end
273
-
274
- # @private
275
- # convert raw to encoding and convert to Encoding.default_internal
276
- # @param [String] raw
277
- # @param [Encoding] encoding
278
- # @return [String] result
279
- def self.convert_encoding(raw, encoding)
280
- raw.force_encoding(encoding).encode
281
- end
282
-
283
- # @private
284
- # retrun corresponding Ruby encoding
285
- # @return [Encoding] encoding
286
- def encoding
287
- enc = CHARSET_ENCODING[@name.downcase]
288
- raise Mysql::ClientError, "unsupported charset: #{@name}" unless enc
289
- enc
290
- end
291
-
292
- # @private
293
- # convert encoding to corrensponding to MySQL charset
294
- # @param [String] value
295
- # @return [String]
296
- def convert(value)
297
- if value.is_a? String and value.encoding != Encoding::ASCII_8BIT
298
- value = value.encode encoding
299
- end
300
- value
301
- end
302
-
303
- else
304
- # for Ruby 1.8
305
-
306
- def self.to_binary(value)
307
- value
308
- end
263
+ # @param [String] value
264
+ # @return [String] value with binary encoding
265
+ def self.to_binary(value)
266
+ value.dup.force_encoding(Encoding::ASCII_8BIT)
267
+ end
309
268
 
310
- def self.convert_encoding(raw, encoding)
311
- raw
312
- end
269
+ # Convert raw bytes to encoding and then to Encoding.default_internal
270
+ # @param [String] raw
271
+ # @param [Encoding] encoding
272
+ # @return [String]
273
+ def self.convert_encoding(raw, encoding)
274
+ raw.force_encoding(encoding).encode
275
+ end
313
276
 
314
- def encoding
315
- nil
316
- end
277
+ # @return [Encoding] corresponding Ruby encoding
278
+ def encoding
279
+ enc = CHARSET_ENCODING[@name.downcase]
280
+ raise MysqlPR::ClientError, "unsupported charset: #{@name}" unless enc
317
281
 
318
- def convert(value)
319
- value
320
- end
282
+ enc
283
+ end
321
284
 
285
+ # Convert value encoding to match MySQL charset
286
+ # @param [String] value
287
+ # @return [String]
288
+ def convert(value)
289
+ value = value.encode(encoding) if value.is_a?(String) && value.encoding != Encoding::ASCII_8BIT
290
+ value
322
291
  end
323
292
  end
324
293
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Copyright (C) 2003-2008 TOMITA Masahiro
2
4
  # mailto:tommy@tmtm.org
3
5
 
@@ -52,6 +54,8 @@ class MysqlPR
52
54
  CLIENT_SECURE_CONNECTION = 1 << 15 # New 4.1 authentication
53
55
  CLIENT_MULTI_STATEMENTS = 1 << 16 # Enable/disable multi-stmt support
54
56
  CLIENT_MULTI_RESULTS = 1 << 17 # Enable/disable multi-results
57
+ CLIENT_PS_MULTI_RESULTS = 1 << 18 # Multi-results in PS-protocol
58
+ CLIENT_PLUGIN_AUTH = 1 << 19 # Client supports plugin authentication
55
59
 
56
60
  # Connection Option
57
61
  OPT_CONNECT_TIMEOUT = 0
@@ -149,11 +153,11 @@ class MysqlPR
149
153
  AUTO_INCREMENT_FLAG = 512
150
154
  TIMESTAMP_FLAG = 1024
151
155
  SET_FLAG = 2048
152
- NUM_FLAG = 32768
153
- PART_KEY_FLAG = 16384
154
- GROUP_FLAG = 32768
155
- UNIQUE_FLAG = 65536
156
- BINCMP_FLAG = 131072
156
+ NUM_FLAG = 32_768
157
+ PART_KEY_FLAG = 16_384
158
+ GROUP_FLAG = 32_768
159
+ UNIQUE_FLAG = 65_536
160
+ BINCMP_FLAG = 131_072
157
161
  end
158
162
 
159
163
  class Stmt
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Copyright (C) 2003-2010 TOMITA Masahiro
2
4
  # mailto:tommy@tmtm.org
3
5
 
@@ -6,14 +8,15 @@ class MysqlPR
6
8
  ERRNO = 0
7
9
 
8
10
  def self.define_error_class(prefix_re)
9
- self.constants.each do |errname|
11
+ constants.each do |errname|
10
12
  errname = errname.to_s
11
13
  next unless errname =~ prefix_re
12
- errno = self.const_get errname
13
- excname = errname.sub(prefix_re,'').gsub(/(\A.|_.)([A-Z]+)/){$1+$2.downcase}.gsub(/_/,'')
14
+
15
+ errno = const_get errname
16
+ excname = errname.sub(prefix_re, "").gsub(/(\A.|_.)([A-Z]+)/) { ::Regexp.last_match(1) + ::Regexp.last_match(2).downcase }.gsub(/_/, "")
14
17
  klass = Class.new self
15
- klass.const_set 'ERRNO', errno
16
- self.const_set excname, klass
18
+ klass.const_set "ERRNO", errno
19
+ const_set excname, klass
17
20
  self::ERROR_MAP[errno] = klass
18
21
  MysqlPR::Error.const_set errname, errno
19
22
  end
@@ -21,7 +24,7 @@ class MysqlPR
21
24
 
22
25
  attr_reader :sqlstate, :error
23
26
 
24
- def initialize(message, sqlstate='HY000')
27
+ def initialize(message, sqlstate = "HY000")
25
28
  @sqlstate = sqlstate
26
29
  @error = message
27
30
  super message
@@ -34,7 +37,7 @@ class MysqlPR
34
37
 
35
38
  # server side error
36
39
  class ServerError < Error
37
- ERROR_MAP = {}
40
+ ERROR_MAP = {} # rubocop:disable Style/MutableConstant
38
41
 
39
42
  ER_HASHCHK = 1000
40
43
  ER_NISAMCHK = 1001
@@ -745,11 +748,11 @@ class MysqlPR
745
748
  end
746
749
 
747
750
  ServerError.define_error_class(/\AER_/)
748
- ServerError::ERROR_MAP.values.each{|v| MysqlPR.const_set v.name.split(/::/).last, v} # for compatibility
751
+ ServerError::ERROR_MAP.each_value { |v| MysqlPR.const_set v.name.split(/::/).last, v } # for compatibility
749
752
 
750
753
  # client side error
751
754
  class ClientError < Error
752
- ERROR_MAP = {}
755
+ ERROR_MAP = {} # rubocop:disable Style/MutableConstant
753
756
 
754
757
  CR_UNKNOWN_ERROR = 2000
755
758
  CR_SOCKET_CREATE_ERROR = 2001
@@ -818,5 +821,4 @@ class MysqlPR
818
821
  # protocol error
819
822
  class ProtocolError < ClientError
820
823
  end
821
-
822
824
  end
@@ -1,18 +1,22 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class MysqlPR
4
+ # Binary packet parsing for MySQL protocol
2
5
  class Packet
3
6
  # convert Numeric to LengthCodedBinary
4
7
  def self.lcb(num)
5
- return "\xfb" if num.nil?
8
+ return "\xfb".b if num.nil?
6
9
  return [num].pack("C") if num < 251
7
- return [252, num].pack("Cv") if num < 65536
8
- return [253, num&0xffff, num>>16].pack("CvC") if num < 16777216
9
- return [254, num&0xffffffff, num>>32].pack("CVV")
10
+ return [252, num].pack("Cv") if num < 65_536
11
+ return [253, num & 0xffff, num >> 16].pack("CvC") if num < 16_777_216
12
+
13
+ [254, num & 0xffffffff, num >> 32].pack("CVV")
10
14
  end
11
15
 
12
16
  # convert String to LengthCodedString
13
17
  def self.lcs(str)
14
18
  str = Charset.to_binary str
15
- lcb(str.length)+str
19
+ lcb(str.length) + str
16
20
  end
17
21
 
18
22
  def initialize(data)
@@ -21,25 +25,29 @@ class MysqlPR
21
25
 
22
26
  def lcb
23
27
  return nil if @data.empty?
28
+
24
29
  case v = utiny
25
30
  when 0xfb
26
- return nil
31
+ nil
27
32
  when 0xfc
28
- return ushort
33
+ ushort
29
34
  when 0xfd
30
- c, v = utiny, ushort
31
- return (v << 8)+c
35
+ c = utiny
36
+ v = ushort
37
+ (v << 8) + c
32
38
  when 0xfe
33
- v1, v2 = ulong, ulong
34
- return (v2 << 32)+v1
39
+ v1 = ulong
40
+ v2 = ulong
41
+ (v2 << 32) + v1
35
42
  else
36
- return v
43
+ v
37
44
  end
38
45
  end
39
46
 
40
47
  def lcs
41
- len = self.lcb
48
+ len = lcb
42
49
  return nil unless len
50
+
43
51
  @data.slice!(0, len)
44
52
  end
45
53
 
@@ -48,30 +56,29 @@ class MysqlPR
48
56
  end
49
57
 
50
58
  def string
51
- str = @data.unpack('Z*').first
52
- @data.slice!(0, str.length+1)
59
+ str = @data.unpack1("Z*")
60
+ @data.slice!(0, str.length + 1)
53
61
  str
54
62
  end
55
63
 
56
64
  def utiny
57
- @data.slice!(0, 1).unpack('C').first
65
+ @data.slice!(0, 1).unpack1("C")
58
66
  end
59
67
 
60
68
  def ushort
61
- @data.slice!(0, 2).unpack('v').first
69
+ @data.slice!(0, 2).unpack1("v")
62
70
  end
63
71
 
64
72
  def ulong
65
- @data.slice!(0, 4).unpack('V').first
73
+ @data.slice!(0, 4).unpack1("V")
66
74
  end
67
75
 
68
76
  def eof?
69
- @data[0] == ?\xfe && @data.length == 5
77
+ @data.getbyte(0) == 0xfe && @data.length == 5
70
78
  end
71
79
 
72
80
  def to_s
73
81
  @data
74
82
  end
75
-
76
83
  end
77
84
  end