ruby-mysql 2.9.7 → 2.9.8
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.
Potentially problematic release.
This version of ruby-mysql might be problematic. Click here for more details.
- data/lib/mysql.rb +239 -273
- data/lib/mysql/charset.rb +42 -8
- data/spec/mysql_spec.rb +1 -1
- metadata +3 -3
data/lib/mysql.rb
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
# mailto:tommy@tmtm.org
|
3
3
|
|
4
4
|
# MySQL connection class.
|
5
|
-
#
|
5
|
+
# @example
|
6
6
|
# my = Mysql.connect('hostname', 'user', 'password', 'dbname')
|
7
7
|
# res = my.query 'select col1,col2 from tbl where id=123'
|
8
8
|
# res.each do |c1, c2|
|
@@ -20,17 +20,21 @@ class Mysql
|
|
20
20
|
require "mysql/packet.rb"
|
21
21
|
end
|
22
22
|
|
23
|
-
VERSION =
|
23
|
+
VERSION = 20908 # Version number of this library
|
24
24
|
MYSQL_UNIX_PORT = "/tmp/mysql.sock" # UNIX domain socket filename
|
25
25
|
MYSQL_TCP_PORT = 3306 # TCP socket port number
|
26
26
|
|
27
|
-
|
28
|
-
attr_reader :
|
27
|
+
# @return [Mysql::Charset] character set of MySQL connection
|
28
|
+
attr_reader :charset
|
29
|
+
# @private
|
30
|
+
attr_reader :protocol
|
29
31
|
|
32
|
+
# @return [Boolean] if true, {#query} return {Mysql::Result}.
|
30
33
|
attr_accessor :query_with_result
|
31
34
|
|
32
35
|
class << self
|
33
36
|
# Make Mysql object without connecting.
|
37
|
+
# @return [Mysql]
|
34
38
|
def init
|
35
39
|
my = self.allocate
|
36
40
|
my.instance_eval{initialize}
|
@@ -38,7 +42,8 @@ class Mysql
|
|
38
42
|
end
|
39
43
|
|
40
44
|
# Make Mysql object and connect to mysqld.
|
41
|
-
#
|
45
|
+
# @param args same as arguments for {#connect}.
|
46
|
+
# @return [Mysql]
|
42
47
|
def new(*args)
|
43
48
|
my = self.init
|
44
49
|
my.connect(*args)
|
@@ -48,8 +53,8 @@ class Mysql
|
|
48
53
|
alias connect new
|
49
54
|
|
50
55
|
# Escape special character in string.
|
51
|
-
#
|
52
|
-
#
|
56
|
+
# @param [String] str
|
57
|
+
# @return [String]
|
53
58
|
def escape_string(str)
|
54
59
|
str.gsub(/[\0\n\r\\\'\"\x1a]/) do |s|
|
55
60
|
case s
|
@@ -63,22 +68,20 @@ class Mysql
|
|
63
68
|
end
|
64
69
|
alias quote escape_string
|
65
70
|
|
66
|
-
#
|
67
|
-
# This value is dummy.
|
71
|
+
# @return [String] client version. This value is dummy for MySQL/Ruby compatibility.
|
68
72
|
def client_info
|
69
73
|
"5.0.0"
|
70
74
|
end
|
71
75
|
alias get_client_info client_info
|
72
76
|
|
73
|
-
#
|
74
|
-
# This value is dummy. If you want to get version of this library, use Mysql::VERSION.
|
77
|
+
# @return [Integer] client version. This value is dummy for MySQL/Ruby compatibility.
|
75
78
|
def client_version
|
76
79
|
50000
|
77
80
|
end
|
78
81
|
alias get_client_version client_version
|
79
82
|
end
|
80
83
|
|
81
|
-
def initialize
|
84
|
+
def initialize
|
82
85
|
@fields = nil
|
83
86
|
@protocol = nil
|
84
87
|
@charset = nil
|
@@ -95,16 +98,14 @@ class Mysql
|
|
95
98
|
end
|
96
99
|
|
97
100
|
# Connect to mysqld.
|
98
|
-
#
|
99
|
-
#
|
100
|
-
#
|
101
|
-
#
|
102
|
-
#
|
103
|
-
#
|
104
|
-
#
|
105
|
-
#
|
106
|
-
# === Return
|
107
|
-
# self
|
101
|
+
# @param [String / nil] host hostname mysqld running
|
102
|
+
# @param [String / nil] user username to connect to mysqld
|
103
|
+
# @param [String / nil] passwd password to connect to mysqld
|
104
|
+
# @param [String / nil] db initial database name
|
105
|
+
# @param [Integer / nil] port port number (used if host is not 'localhost' or nil)
|
106
|
+
# @param [String / nil] socket socket file name (used if host is 'localhost' or nil)
|
107
|
+
# @param [Integer / nil] flag connection flag. Mysql::CLIENT_* ORed
|
108
|
+
# @return self
|
108
109
|
def connect(host=nil, user=nil, passwd=nil, db=nil, port=nil, socket=nil, flag=0)
|
109
110
|
if flag & CLIENT_COMPRESS != 0
|
110
111
|
warn 'unsupported flag: CLIENT_COMPRESS'
|
@@ -120,6 +121,7 @@ class Mysql
|
|
120
121
|
alias real_connect connect
|
121
122
|
|
122
123
|
# Disconnect from mysql.
|
124
|
+
# @return [Mysql] self
|
123
125
|
def close
|
124
126
|
if @protocol
|
125
127
|
@protocol.quit_command
|
@@ -129,6 +131,7 @@ class Mysql
|
|
129
131
|
end
|
130
132
|
|
131
133
|
# Disconnect from mysql without QUIT packet.
|
134
|
+
# @return [Mysql] self
|
132
135
|
def close!
|
133
136
|
if @protocol
|
134
137
|
@protocol.close
|
@@ -142,11 +145,9 @@ class Mysql
|
|
142
145
|
# Available options:
|
143
146
|
# Mysql::INIT_COMMAND, Mysql::OPT_CONNECT_TIMEOUT, Mysql::OPT_READ_TIMEOUT,
|
144
147
|
# Mysql::OPT_WRITE_TIMEOUT, Mysql::SET_CHARSET_NAME
|
145
|
-
#
|
146
|
-
#
|
147
|
-
#
|
148
|
-
# === Return
|
149
|
-
# self
|
148
|
+
# @param [Integer] opt option
|
149
|
+
# @param [Integer] value option value that is depend on opt
|
150
|
+
# @return [Mysql] self
|
150
151
|
def options(opt, value=nil)
|
151
152
|
case opt
|
152
153
|
when Mysql::INIT_COMMAND
|
@@ -183,9 +184,11 @@ class Mysql
|
|
183
184
|
end
|
184
185
|
|
185
186
|
# Escape special character in MySQL.
|
186
|
-
#
|
187
|
+
#
|
187
188
|
# In Ruby 1.8, this is not safe for multibyte charset such as 'SJIS'.
|
188
189
|
# You should use place-holder in prepared-statement.
|
190
|
+
# @param [String] str
|
191
|
+
# return [String]
|
189
192
|
def escape_string(str)
|
190
193
|
if not defined? Encoding and @charset.unsafe
|
191
194
|
raise ClientError, 'Mysql#escape_string is called for unsafe multibyte charset'
|
@@ -194,25 +197,20 @@ class Mysql
|
|
194
197
|
end
|
195
198
|
alias quote escape_string
|
196
199
|
|
197
|
-
#
|
198
|
-
# [String] client version
|
200
|
+
# @return [String] client version
|
199
201
|
def client_info
|
200
202
|
self.class.client_info
|
201
203
|
end
|
202
204
|
alias get_client_info client_info
|
203
205
|
|
204
|
-
#
|
205
|
-
# [Integer] client version
|
206
|
+
# @return [Integer] client version
|
206
207
|
def client_version
|
207
208
|
self.class.client_version
|
208
209
|
end
|
209
210
|
alias get_client_version client_version
|
210
211
|
|
211
212
|
# Set charset of MySQL connection.
|
212
|
-
#
|
213
|
-
# cs :: [String / Mysql::Charset]
|
214
|
-
# === Return
|
215
|
-
# cs
|
213
|
+
# @param [String / Mysql::Charset] cs
|
216
214
|
def charset=(cs)
|
217
215
|
charset = cs.is_a?(Charset) ? cs : Charset.by_name(cs)
|
218
216
|
if @protocol
|
@@ -223,122 +221,101 @@ class Mysql
|
|
223
221
|
cs
|
224
222
|
end
|
225
223
|
|
226
|
-
#
|
227
|
-
# [String] charset name
|
224
|
+
# @return [String] charset name
|
228
225
|
def character_set_name
|
229
226
|
@charset.name
|
230
227
|
end
|
231
228
|
|
232
|
-
#
|
233
|
-
# [Integer] last error number
|
229
|
+
# @return [Integer] last error number
|
234
230
|
def errno
|
235
231
|
@last_error ? @last_error.errno : 0
|
236
232
|
end
|
237
233
|
|
238
|
-
#
|
239
|
-
# [String] last error message
|
234
|
+
# @return [String] last error message
|
240
235
|
def error
|
241
236
|
@last_error && @last_error.error
|
242
237
|
end
|
243
238
|
|
244
|
-
#
|
245
|
-
# [String] sqlstate for last error
|
239
|
+
# @return [String] sqlstate for last error
|
246
240
|
def sqlstate
|
247
241
|
@last_error ? @last_error.sqlstate : "00000"
|
248
242
|
end
|
249
243
|
|
250
|
-
#
|
251
|
-
# [Integer] number of columns for last query
|
244
|
+
# @return [Integer] number of columns for last query
|
252
245
|
def field_count
|
253
246
|
@fields.size
|
254
247
|
end
|
255
248
|
|
256
|
-
#
|
257
|
-
# [String] connection type
|
249
|
+
# @return [String] connection type
|
258
250
|
def host_info
|
259
251
|
@host_info
|
260
252
|
end
|
261
253
|
alias get_host_info host_info
|
262
254
|
|
263
|
-
#
|
264
|
-
# [Integer] protocol version
|
255
|
+
# @return [Integer] protocol version
|
265
256
|
def proto_info
|
266
257
|
Mysql::Protocol::VERSION
|
267
258
|
end
|
268
259
|
alias get_proto_info proto_info
|
269
260
|
|
270
|
-
#
|
271
|
-
# [String] server version
|
261
|
+
# @return [String] server version
|
272
262
|
def server_info
|
273
263
|
check_connection
|
274
264
|
@protocol.server_info
|
275
265
|
end
|
276
266
|
alias get_server_info server_info
|
277
267
|
|
278
|
-
#
|
279
|
-
# [Integer] server version
|
268
|
+
# @return [Integer] server version
|
280
269
|
def server_version
|
281
270
|
check_connection
|
282
271
|
@protocol.server_version
|
283
272
|
end
|
284
273
|
alias get_server_version server_version
|
285
274
|
|
286
|
-
#
|
287
|
-
# [String] information for last query
|
275
|
+
# @return [String] information for last query
|
288
276
|
def info
|
289
277
|
@protocol && @protocol.message
|
290
278
|
end
|
291
279
|
|
292
|
-
#
|
293
|
-
# [Integer] number of affected records by insert/update/delete.
|
280
|
+
# @return [Integer] number of affected records by insert/update/delete.
|
294
281
|
def affected_rows
|
295
282
|
@protocol ? @protocol.affected_rows : 0
|
296
283
|
end
|
297
284
|
|
298
|
-
#
|
299
|
-
# [Integer] latest auto_increment value
|
285
|
+
# @return [Integer] latest auto_increment value
|
300
286
|
def insert_id
|
301
287
|
@protocol ? @protocol.insert_id : 0
|
302
288
|
end
|
303
289
|
|
304
|
-
#
|
305
|
-
# [Integer] number of warnings for previous query
|
290
|
+
# @return [Integer] number of warnings for previous query
|
306
291
|
def warning_count
|
307
292
|
@protocol ? @protocol.warning_count : 0
|
308
293
|
end
|
309
294
|
|
310
295
|
# Kill query.
|
311
|
-
#
|
312
|
-
#
|
313
|
-
# === Return
|
314
|
-
# self
|
296
|
+
# @param [Integer] pid thread id
|
297
|
+
# @return [Mysql] self
|
315
298
|
def kill(pid)
|
316
299
|
check_connection
|
317
300
|
@protocol.kill_command pid
|
318
301
|
self
|
319
302
|
end
|
320
303
|
|
321
|
-
#
|
322
|
-
#
|
323
|
-
#
|
324
|
-
# === Return
|
325
|
-
# [Array of String] database list
|
304
|
+
# database list.
|
305
|
+
# @param [String] db database name that may contain wild card.
|
306
|
+
# @return [Array<String>] database list
|
326
307
|
def list_dbs(db=nil)
|
327
308
|
db &&= db.gsub(/[\\\']/){"\\#{$&}"}
|
328
309
|
query(db ? "show databases like '#{db}'" : "show databases").map(&:first)
|
329
310
|
end
|
330
311
|
|
331
312
|
# Execute query string.
|
332
|
-
#
|
333
|
-
#
|
334
|
-
#
|
335
|
-
#
|
336
|
-
# Mysql
|
337
|
-
#
|
338
|
-
# self :: If block is specified.
|
339
|
-
# === Block parameter
|
340
|
-
# [Mysql::Result]
|
341
|
-
# === Example
|
313
|
+
# @param [String] str Query.
|
314
|
+
# @yield [Mysql::Result] evaluated per query.
|
315
|
+
# @return [Mysql::Result] If {#query_with_result} is true and result set exist.
|
316
|
+
# @return [nil] If {#query_with_result} is true and the query does not return result set.
|
317
|
+
# @return [Mysql] If {#query_with_result} is false or block is specified
|
318
|
+
# @example
|
342
319
|
# my.query("select 1,NULL,'abc'").fetch # => [1, nil, "abc"]
|
343
320
|
def query(str, &block)
|
344
321
|
check_connection
|
@@ -370,8 +347,7 @@ class Mysql
|
|
370
347
|
alias real_query query
|
371
348
|
|
372
349
|
# Get all data for last query if query_with_result is false.
|
373
|
-
#
|
374
|
-
# [Mysql::Result]
|
350
|
+
# @return [Mysql::Result]
|
375
351
|
def store_result
|
376
352
|
check_connection
|
377
353
|
raise ClientError, 'invalid usage' unless @result_exist
|
@@ -380,39 +356,35 @@ class Mysql
|
|
380
356
|
res
|
381
357
|
end
|
382
358
|
|
383
|
-
#
|
384
|
-
# === Return
|
385
|
-
# [Integer] Thread ID
|
359
|
+
# @return [Integer] Thread ID
|
386
360
|
def thread_id
|
387
361
|
check_connection
|
388
362
|
@protocol.thread_id
|
389
363
|
end
|
390
364
|
|
391
|
-
# Use result of query. The result data is retrieved when you use Mysql::Result#
|
365
|
+
# Use result of query. The result data is retrieved when you use Mysql::Result#fetch.
|
366
|
+
# @return [Mysql::Result]
|
392
367
|
def use_result
|
393
368
|
store_result
|
394
369
|
end
|
395
370
|
|
396
371
|
# Set server option.
|
397
|
-
#
|
398
|
-
#
|
399
|
-
# === Return
|
400
|
-
# self
|
372
|
+
# @param [Integer] opt {Mysql::OPTION_MULTI_STATEMENTS_ON} or {Mysql::OPTION_MULTI_STATEMENTS_OFF}
|
373
|
+
# @return [Mysql] self
|
401
374
|
def set_server_option(opt)
|
402
375
|
check_connection
|
403
376
|
@protocol.set_option_command opt
|
404
377
|
self
|
405
378
|
end
|
406
379
|
|
407
|
-
# true if multiple queries are specified and unexecuted queries exists.
|
380
|
+
# @return [Boolean] true if multiple queries are specified and unexecuted queries exists.
|
408
381
|
def more_results
|
409
382
|
@protocol.server_status & SERVER_MORE_RESULTS_EXISTS != 0
|
410
383
|
end
|
411
384
|
alias more_results? more_results
|
412
385
|
|
413
386
|
# execute next query if multiple queries are specified.
|
414
|
-
#
|
415
|
-
# true if next query exists.
|
387
|
+
# @return [Boolean] true if next query exists.
|
416
388
|
def next_result
|
417
389
|
return false unless more_results
|
418
390
|
check_connection
|
@@ -426,30 +398,26 @@ class Mysql
|
|
426
398
|
end
|
427
399
|
|
428
400
|
# Parse prepared-statement.
|
429
|
-
#
|
430
|
-
#
|
431
|
-
# === Return
|
432
|
-
# Mysql::Statement :: Prepared-statement object
|
401
|
+
# @param [String] str query string
|
402
|
+
# @return [Mysql::Stmt] Prepared-statement object
|
433
403
|
def prepare(str)
|
434
404
|
st = Stmt.new @protocol, @charset
|
435
405
|
st.prepare str
|
436
406
|
st
|
437
407
|
end
|
438
408
|
|
409
|
+
# @private
|
439
410
|
# Make empty prepared-statement object.
|
440
|
-
#
|
441
|
-
# Mysql::Stmt :: If block is not specified.
|
411
|
+
# @return [Mysql::Stmt] If block is not specified.
|
442
412
|
def stmt_init
|
443
413
|
Stmt.new @protocol, @charset
|
444
414
|
end
|
445
415
|
|
446
416
|
# Returns Mysql::Result object that is empty.
|
447
417
|
# Use fetch_fields to get list of fields.
|
448
|
-
#
|
449
|
-
#
|
450
|
-
#
|
451
|
-
# === Return
|
452
|
-
# [Mysql::Result]
|
418
|
+
# @param [String] table table name.
|
419
|
+
# @param [String] field field name that may contain wild card.
|
420
|
+
# @return [Mysql::Result]
|
453
421
|
def list_fields(table, field=nil)
|
454
422
|
check_connection
|
455
423
|
begin
|
@@ -462,9 +430,7 @@ class Mysql
|
|
462
430
|
end
|
463
431
|
end
|
464
432
|
|
465
|
-
#
|
466
|
-
# === Return
|
467
|
-
# [Mysql::Result]
|
433
|
+
# @return [Mysql::Result] containing process list
|
468
434
|
def list_processes
|
469
435
|
check_connection
|
470
436
|
@fields = @protocol.process_info_command
|
@@ -472,22 +438,16 @@ class Mysql
|
|
472
438
|
store_result
|
473
439
|
end
|
474
440
|
|
475
|
-
#
|
476
|
-
#
|
477
|
-
#
|
478
|
-
# multi-byte charset such as cp932.
|
479
|
-
# === Argument
|
480
|
-
# table :: [String] database name that may contain wild card.
|
481
|
-
# === Return
|
482
|
-
# [Array of String]
|
441
|
+
# @note for Ruby 1.8: This is not multi-byte safe. Don't use for multi-byte charset such as cp932.
|
442
|
+
# @param [String] table database name that may contain wild card.
|
443
|
+
# @return [Array<String>] list of table name.
|
483
444
|
def list_tables(table=nil)
|
484
445
|
q = table ? "show tables like '#{quote table}'" : "show tables"
|
485
446
|
query(q).map(&:first)
|
486
447
|
end
|
487
448
|
|
488
449
|
# Check whether the connection is available.
|
489
|
-
#
|
490
|
-
# self
|
450
|
+
# @return [Mysql] self
|
491
451
|
def ping
|
492
452
|
check_connection
|
493
453
|
@protocol.ping_command
|
@@ -495,10 +455,8 @@ class Mysql
|
|
495
455
|
end
|
496
456
|
|
497
457
|
# Flush tables or caches.
|
498
|
-
#
|
499
|
-
#
|
500
|
-
# === Return
|
501
|
-
# self
|
458
|
+
# @param [Integer] op operation. Use Mysql::REFRESH_* value.
|
459
|
+
# @return [Mysql] self
|
502
460
|
def refresh(op)
|
503
461
|
check_connection
|
504
462
|
@protocol.refresh_command op
|
@@ -506,56 +464,48 @@ class Mysql
|
|
506
464
|
end
|
507
465
|
|
508
466
|
# Reload grant tables.
|
509
|
-
#
|
510
|
-
# self
|
467
|
+
# @return [Mysql] self
|
511
468
|
def reload
|
512
469
|
refresh Mysql::REFRESH_GRANT
|
513
470
|
end
|
514
471
|
|
515
472
|
# Select default database
|
516
|
-
#
|
517
|
-
# self
|
473
|
+
# @return [Mysql] self
|
518
474
|
def select_db(db)
|
519
475
|
query "use #{db}"
|
520
476
|
self
|
521
477
|
end
|
522
478
|
|
523
479
|
# shutdown server.
|
524
|
-
#
|
525
|
-
# self
|
480
|
+
# @return [Mysql] self
|
526
481
|
def shutdown(level=0)
|
527
482
|
check_connection
|
528
483
|
@protocol.shutdown_command level
|
529
484
|
self
|
530
485
|
end
|
531
486
|
|
532
|
-
#
|
533
|
-
# [String] statistics message
|
487
|
+
# @return [String] statistics message
|
534
488
|
def stat
|
535
489
|
@protocol ? @protocol.statistics_command : 'MySQL server has gone away'
|
536
490
|
end
|
537
491
|
|
538
492
|
# Commit transaction
|
539
|
-
#
|
540
|
-
# self
|
493
|
+
# @return [Mysql] self
|
541
494
|
def commit
|
542
495
|
query 'commit'
|
543
496
|
self
|
544
497
|
end
|
545
498
|
|
546
499
|
# Rollback transaction
|
547
|
-
#
|
548
|
-
# self
|
500
|
+
# @return [Mysql] self
|
549
501
|
def rollback
|
550
502
|
query 'rollback'
|
551
503
|
self
|
552
504
|
end
|
553
505
|
|
554
506
|
# Set autocommit mode
|
555
|
-
#
|
556
|
-
#
|
557
|
-
# === Return
|
558
|
-
# self
|
507
|
+
# @param [Boolean] flag
|
508
|
+
# @return [Mysql] self
|
559
509
|
def autocommit(flag)
|
560
510
|
query "set autocommit=#{flag ? 1 : 0}"
|
561
511
|
self
|
@@ -567,24 +517,37 @@ class Mysql
|
|
567
517
|
raise ClientError::ServerGoneError, 'The MySQL server has gone away' unless @protocol
|
568
518
|
end
|
569
519
|
|
520
|
+
# @!visibility public
|
570
521
|
# Field class
|
571
522
|
class Field
|
572
|
-
|
573
|
-
attr_reader :
|
574
|
-
|
575
|
-
attr_reader :
|
576
|
-
|
577
|
-
attr_reader :
|
578
|
-
|
579
|
-
attr_reader :
|
580
|
-
|
581
|
-
attr_reader :
|
582
|
-
|
523
|
+
# @return [String] database name
|
524
|
+
attr_reader :db
|
525
|
+
# @return [String] table name
|
526
|
+
attr_reader :table
|
527
|
+
# @return [String] original table name
|
528
|
+
attr_reader :org_table
|
529
|
+
# @return [String] field name
|
530
|
+
attr_reader :name
|
531
|
+
# @return [String] original field name
|
532
|
+
attr_reader :org_name
|
533
|
+
# @return [Integer] charset id number
|
534
|
+
attr_reader :charsetnr
|
535
|
+
# @return [Integer] field length
|
536
|
+
attr_reader :length
|
537
|
+
# @return [Integer] field type
|
538
|
+
attr_reader :type
|
539
|
+
# @return [Integer] flag
|
540
|
+
attr_reader :flags
|
541
|
+
# @return [Integer] number of decimals
|
542
|
+
attr_reader :decimals
|
543
|
+
# @return [String] defualt value
|
544
|
+
attr_reader :default
|
583
545
|
alias :def :default
|
584
|
-
attr_accessor :result # :nodoc:
|
585
546
|
|
586
|
-
#
|
587
|
-
|
547
|
+
# @private
|
548
|
+
attr_accessor :result
|
549
|
+
|
550
|
+
# @attr [Protocol::FieldPacket] packet
|
588
551
|
def initialize(packet)
|
589
552
|
@db, @table, @org_table, @name, @org_name, @charsetnr, @length, @type, @flags, @decimals, @default =
|
590
553
|
packet.db, packet.table, packet.org_table, packet.name, packet.org_name, packet.charsetnr, packet.length, packet.type, packet.flags, packet.decimals, packet.default
|
@@ -592,6 +555,7 @@ class Mysql
|
|
592
555
|
@max_length = nil
|
593
556
|
end
|
594
557
|
|
558
|
+
# @return [Hash] field information
|
595
559
|
def hash
|
596
560
|
{
|
597
561
|
"name" => @name,
|
@@ -605,26 +569,27 @@ class Mysql
|
|
605
569
|
}
|
606
570
|
end
|
607
571
|
|
572
|
+
# @private
|
608
573
|
def inspect
|
609
574
|
"#<Mysql::Field:#{@name}>"
|
610
575
|
end
|
611
576
|
|
612
|
-
#
|
577
|
+
# @return [Boolean] true if numeric field.
|
613
578
|
def is_num?
|
614
579
|
@flags & NUM_FLAG != 0
|
615
580
|
end
|
616
581
|
|
617
|
-
#
|
582
|
+
# @return [Boolean] true if not null field.
|
618
583
|
def is_not_null?
|
619
584
|
@flags & NOT_NULL_FLAG != 0
|
620
585
|
end
|
621
586
|
|
622
|
-
#
|
587
|
+
# @return [Boolean] true if primary key field.
|
623
588
|
def is_pri_key?
|
624
589
|
@flags & PRI_KEY_FLAG != 0
|
625
590
|
end
|
626
591
|
|
627
|
-
# maximum width of the field for the result set
|
592
|
+
# @return [Integer] maximum width of the field for the result set
|
628
593
|
def max_length
|
629
594
|
return @max_length if @max_length
|
630
595
|
@max_length = 0
|
@@ -642,14 +607,15 @@ class Mysql
|
|
642
607
|
|
643
608
|
end
|
644
609
|
|
610
|
+
# @!visibility public
|
645
611
|
# Result set
|
646
612
|
class ResultBase
|
647
613
|
include Enumerable
|
648
614
|
|
615
|
+
# @return [Array<Mysql::Field>] field list
|
649
616
|
attr_reader :fields
|
650
617
|
|
651
|
-
#
|
652
|
-
# fields :: [Array of Mysql::Field]
|
618
|
+
# @param [Array of Mysql::Field] fields
|
653
619
|
def initialize(fields)
|
654
620
|
@fields = fields
|
655
621
|
@field_index = 0 # index of field
|
@@ -660,19 +626,17 @@ class Mysql
|
|
660
626
|
end
|
661
627
|
|
662
628
|
# ignore
|
629
|
+
# @return [void]
|
663
630
|
def free
|
664
631
|
end
|
665
632
|
|
666
|
-
#
|
667
|
-
# [Integer] number of record
|
633
|
+
# @return [Integer] number of record
|
668
634
|
def size
|
669
635
|
@records.size
|
670
636
|
end
|
671
637
|
alias num_rows size
|
672
638
|
|
673
|
-
#
|
674
|
-
# === Return
|
675
|
-
# [Array] record data
|
639
|
+
# @return [Array] current record data
|
676
640
|
def fetch
|
677
641
|
@fetched_record = nil
|
678
642
|
return nil if @index >= @records.size
|
@@ -685,10 +649,8 @@ class Mysql
|
|
685
649
|
|
686
650
|
# Return data of current record as Hash.
|
687
651
|
# The hash key is field name.
|
688
|
-
#
|
689
|
-
#
|
690
|
-
# === Return
|
691
|
-
# [Array of Hash] record data
|
652
|
+
# @param [Boolean] with_table if true, hash key is "table_name.field_name".
|
653
|
+
# @return [Hash] current record data
|
692
654
|
def fetch_hash(with_table=nil)
|
693
655
|
row = fetch
|
694
656
|
return nil unless row
|
@@ -704,10 +666,8 @@ class Mysql
|
|
704
666
|
end
|
705
667
|
|
706
668
|
# Iterate block with record.
|
707
|
-
#
|
708
|
-
# [
|
709
|
-
# === Return
|
710
|
-
# self. If block is not specified, this returns Enumerator.
|
669
|
+
# @yield [Array] record data
|
670
|
+
# @return [self] self. If block is not specified, this returns Enumerator.
|
711
671
|
def each(&block)
|
712
672
|
return enum_for(:each) unless block
|
713
673
|
while rec = fetch
|
@@ -717,12 +677,9 @@ class Mysql
|
|
717
677
|
end
|
718
678
|
|
719
679
|
# Iterate block with record as Hash.
|
720
|
-
#
|
721
|
-
#
|
722
|
-
#
|
723
|
-
# [Array of Hash] record data
|
724
|
-
# === Return
|
725
|
-
# self. If block is not specified, this returns Enumerator.
|
680
|
+
# @param [Boolean] with_table if true, hash key is "table_name.field_name".
|
681
|
+
# @yield [Hash] record data
|
682
|
+
# @return [self] self. If block is not specified, this returns Enumerator.
|
726
683
|
def each_hash(with_table=nil, &block)
|
727
684
|
return enum_for(:each_hash, with_table) unless block
|
728
685
|
while rec = fetch_hash(with_table)
|
@@ -732,27 +689,21 @@ class Mysql
|
|
732
689
|
end
|
733
690
|
|
734
691
|
# Set record position
|
735
|
-
#
|
736
|
-
#
|
737
|
-
# === Return
|
738
|
-
# self
|
692
|
+
# @param [Integer] n record index
|
693
|
+
# @return [self] self
|
739
694
|
def data_seek(n)
|
740
695
|
@index = n
|
741
696
|
self
|
742
697
|
end
|
743
698
|
|
744
|
-
#
|
745
|
-
# === Return
|
746
|
-
# [Integer] record position
|
699
|
+
# @return [Integer] current record position
|
747
700
|
def row_tell
|
748
701
|
@index
|
749
702
|
end
|
750
703
|
|
751
704
|
# Set current position of record
|
752
|
-
#
|
753
|
-
#
|
754
|
-
# === Return
|
755
|
-
# [Integer] previous position
|
705
|
+
# @param [Integer] n record index
|
706
|
+
# @return [Integer] previous position
|
756
707
|
def row_seek(n)
|
757
708
|
ret = @index
|
758
709
|
@index = n
|
@@ -760,8 +711,12 @@ class Mysql
|
|
760
711
|
end
|
761
712
|
end
|
762
713
|
|
714
|
+
# @!visibility public
|
763
715
|
# Result set for simple query
|
764
716
|
class Result < ResultBase
|
717
|
+
# @private
|
718
|
+
# @param [Array<Mysql::Field>] fields
|
719
|
+
# @param [Mysql::Protocol] protocol
|
765
720
|
def initialize(fields, protocol=nil)
|
766
721
|
super fields
|
767
722
|
return unless protocol
|
@@ -769,6 +724,7 @@ class Mysql
|
|
769
724
|
fields.each{|f| f.result = self} # for calculating max_field
|
770
725
|
end
|
771
726
|
|
727
|
+
# @private
|
772
728
|
# calculate max_length of all fields
|
773
729
|
def calculate_field_max_length
|
774
730
|
max_length = Array.new(@fields.size, 0)
|
@@ -783,9 +739,7 @@ class Mysql
|
|
783
739
|
end
|
784
740
|
end
|
785
741
|
|
786
|
-
#
|
787
|
-
# === Return
|
788
|
-
# [Mysql::Field] field object
|
742
|
+
# @return [Mysql::Field] current field
|
789
743
|
def fetch_field
|
790
744
|
return nil if @field_index >= @fields.length
|
791
745
|
ret = @fields[@field_index]
|
@@ -793,77 +747,90 @@ class Mysql
|
|
793
747
|
ret
|
794
748
|
end
|
795
749
|
|
796
|
-
#
|
797
|
-
# === Return
|
798
|
-
# [Integer] field position
|
750
|
+
# @return [Integer] current field position
|
799
751
|
def field_tell
|
800
752
|
@field_index
|
801
753
|
end
|
802
754
|
|
803
755
|
# Set field position
|
804
|
-
#
|
805
|
-
#
|
806
|
-
# === Return
|
807
|
-
# [Integer] previous position
|
756
|
+
# @param [Integer] n field index
|
757
|
+
# @return [Integer] previous position
|
808
758
|
def field_seek(n)
|
809
759
|
ret = @field_index
|
810
760
|
@field_index = n
|
811
761
|
ret
|
812
762
|
end
|
813
763
|
|
814
|
-
# Return field
|
815
|
-
#
|
816
|
-
#
|
817
|
-
# === Return
|
818
|
-
# [Mysql::Field] field
|
764
|
+
# Return specified field
|
765
|
+
# @param [Integer] n field index
|
766
|
+
# @return [Mysql::Field] field
|
819
767
|
def fetch_field_direct(n)
|
820
768
|
raise ClientError, "invalid argument: #{n}" if n < 0 or n >= @fields.length
|
821
769
|
@fields[n]
|
822
770
|
end
|
823
771
|
|
824
|
-
#
|
825
|
-
# === Return
|
826
|
-
# [Array of Mysql::Field] all fields
|
772
|
+
# @return [Array<Mysql::Field>] all fields
|
827
773
|
def fetch_fields
|
828
774
|
@fields
|
829
775
|
end
|
830
776
|
|
831
|
-
#
|
832
|
-
# === Return
|
833
|
-
# [Array of Integer] length of each fields
|
777
|
+
# @return [Array<Integer>] length of each fields
|
834
778
|
def fetch_lengths
|
835
779
|
return nil unless @fetched_record
|
836
780
|
@fetched_record.map{|c|c.nil? ? 0 : c.length}
|
837
781
|
end
|
838
782
|
|
839
|
-
#
|
840
|
-
# [Integer] number of fields
|
783
|
+
# @return [Integer] number of fields
|
841
784
|
def num_fields
|
842
785
|
@fields.size
|
843
786
|
end
|
844
787
|
end
|
845
788
|
|
789
|
+
# @!visibility private
|
846
790
|
# Result set for prepared statement
|
847
791
|
class StatementResult < ResultBase
|
792
|
+
# @private
|
793
|
+
# @param [Array<Mysql::Field>] fields
|
794
|
+
# @param [Mysql::Protocol] protocol
|
795
|
+
# @param [Mysql::Charset] charset
|
848
796
|
def initialize(fields, protocol, charset)
|
849
797
|
super fields
|
850
798
|
@records = protocol.stmt_retr_all_records @fields, charset
|
851
799
|
end
|
852
800
|
end
|
853
801
|
|
802
|
+
# @!visibility public
|
854
803
|
# Prepared statement
|
804
|
+
# @!attribute [r] affected_rows
|
805
|
+
# @return [Integer]
|
806
|
+
# @!attribute [r] insert_id
|
807
|
+
# @return [Integer]
|
808
|
+
# @!attribute [r] server_status
|
809
|
+
# @return [Integer]
|
810
|
+
# @!attribute [r] warning_count
|
811
|
+
# @return [Integer]
|
812
|
+
# @!attribute [r] param_count
|
813
|
+
# @return [Integer]
|
814
|
+
# @!attribute [r] fields
|
815
|
+
# @return [Array<Mysql::Field>]
|
816
|
+
# @!attribute [r] sqlstate
|
817
|
+
# @return [String]
|
855
818
|
class Stmt
|
856
819
|
include Enumerable
|
857
820
|
|
858
821
|
attr_reader :affected_rows, :insert_id, :server_status, :warning_count
|
859
822
|
attr_reader :param_count, :fields, :sqlstate
|
860
823
|
|
824
|
+
# @private
|
861
825
|
def self.finalizer(protocol, statement_id)
|
862
826
|
proc do
|
863
827
|
protocol.gc_stmt statement_id
|
864
828
|
end
|
865
829
|
end
|
866
830
|
|
831
|
+
# @private
|
832
|
+
# @param [Mysql::Protocol] protocol
|
833
|
+
# @param [Mysql::Charset] charset
|
867
834
|
def initialize(protocol, charset)
|
868
835
|
@protocol = protocol
|
869
836
|
@charset = charset
|
@@ -874,11 +841,10 @@ class Mysql
|
|
874
841
|
@bind_result = nil
|
875
842
|
end
|
876
843
|
|
877
|
-
#
|
878
|
-
#
|
879
|
-
#
|
880
|
-
#
|
881
|
-
# self
|
844
|
+
# @private
|
845
|
+
# parse prepared-statement and return {Mysql::Stmt} object
|
846
|
+
# @param [String] str query string
|
847
|
+
# @return self
|
882
848
|
def prepare(str)
|
883
849
|
close
|
884
850
|
begin
|
@@ -894,10 +860,8 @@ class Mysql
|
|
894
860
|
end
|
895
861
|
|
896
862
|
# Execute prepared statement.
|
897
|
-
#
|
898
|
-
#
|
899
|
-
# === Return
|
900
|
-
# self
|
863
|
+
# @param [Object] values values passed to query
|
864
|
+
# @return [Mysql::Stmt] self
|
901
865
|
def execute(*values)
|
902
866
|
raise ClientError, "not prepared" unless @param_count
|
903
867
|
raise ClientError, "parameter count mismatch" if values.length != @param_count
|
@@ -921,15 +885,14 @@ class Mysql
|
|
921
885
|
end
|
922
886
|
|
923
887
|
# Close prepared statement
|
888
|
+
# @return [void]
|
924
889
|
def close
|
925
890
|
ObjectSpace.undefine_finalizer(self)
|
926
891
|
@protocol.stmt_close_command @statement_id if @statement_id
|
927
892
|
@statement_id = nil
|
928
893
|
end
|
929
894
|
|
930
|
-
#
|
931
|
-
# === Return
|
932
|
-
# [Array] record data
|
895
|
+
# @return [Array] current record data
|
933
896
|
def fetch
|
934
897
|
row = @result.fetch
|
935
898
|
return row unless @bind_result
|
@@ -975,19 +938,15 @@ class Mysql
|
|
975
938
|
|
976
939
|
# Return data of current record as Hash.
|
977
940
|
# The hash key is field name.
|
978
|
-
#
|
979
|
-
#
|
980
|
-
# === Return
|
981
|
-
# [Array of Hash] record data
|
941
|
+
# @param [Boolean] with_table if true, hash key is "table_name.field_name".
|
942
|
+
# @return [Hash] record data
|
982
943
|
def fetch_hash(with_table=nil)
|
983
944
|
@result.fetch_hash with_table
|
984
945
|
end
|
985
946
|
|
986
947
|
# Set retrieve type of value
|
987
|
-
#
|
988
|
-
# [
|
989
|
-
# === Return
|
990
|
-
# self
|
948
|
+
# @param [Numeric / Fixnum / Integer / Float / String / Mysql::Time / nil] args value type
|
949
|
+
# @return [Mysql::Stmt] self
|
991
950
|
def bind_result(*args)
|
992
951
|
if @fields.length != args.length
|
993
952
|
raise ClientError, "bind_result: result value count(#{@fields.length}) != number of argument(#{args.length})"
|
@@ -1000,10 +959,9 @@ class Mysql
|
|
1000
959
|
end
|
1001
960
|
|
1002
961
|
# Iterate block with record.
|
1003
|
-
#
|
1004
|
-
# [
|
1005
|
-
#
|
1006
|
-
# self. If block is not specified, this returns Enumerator.
|
962
|
+
# @yield [Array] record data
|
963
|
+
# @return [Mysql::Stmt] self
|
964
|
+
# @return [Enumerator] If block is not specified
|
1007
965
|
def each(&block)
|
1008
966
|
return enum_for(:each) unless block
|
1009
967
|
while rec = fetch
|
@@ -1013,12 +971,10 @@ class Mysql
|
|
1013
971
|
end
|
1014
972
|
|
1015
973
|
# Iterate block with record as Hash.
|
1016
|
-
#
|
1017
|
-
#
|
1018
|
-
#
|
1019
|
-
# [
|
1020
|
-
# === Return
|
1021
|
-
# self. If block is not specified, this returns Enumerator.
|
974
|
+
# @param [Boolean] with_table if true, hash key is "table_name.field_name".
|
975
|
+
# @yield [Hash] record data
|
976
|
+
# @return [Mysql::Stmt] self
|
977
|
+
# @return [Enumerator] If block is not specified
|
1022
978
|
def each_hash(with_table=nil, &block)
|
1023
979
|
return enum_for(:each_hash, with_table) unless block
|
1024
980
|
while rec = fetch_hash(with_table)
|
@@ -1027,67 +983,76 @@ class Mysql
|
|
1027
983
|
self
|
1028
984
|
end
|
1029
985
|
|
1030
|
-
#
|
1031
|
-
# [Integer] number of record
|
986
|
+
# @return [Integer] number of record
|
1032
987
|
def size
|
1033
988
|
@result.size
|
1034
989
|
end
|
1035
990
|
alias num_rows size
|
1036
991
|
|
1037
992
|
# Set record position
|
1038
|
-
#
|
1039
|
-
#
|
1040
|
-
# === Return
|
1041
|
-
# self
|
993
|
+
# @param [Integer] n record index
|
994
|
+
# @return [void]
|
1042
995
|
def data_seek(n)
|
1043
996
|
@result.data_seek(n)
|
1044
997
|
end
|
1045
998
|
|
1046
|
-
#
|
1047
|
-
# === Return
|
1048
|
-
# [Integer] record position
|
999
|
+
# @return [Integer] current record position
|
1049
1000
|
def row_tell
|
1050
1001
|
@result.row_tell
|
1051
1002
|
end
|
1052
1003
|
|
1053
1004
|
# Set current position of record
|
1054
|
-
#
|
1055
|
-
#
|
1056
|
-
# === Return
|
1057
|
-
# [Integer] previous position
|
1005
|
+
# @param [Integer] n record index
|
1006
|
+
# @return [Integer] previous position
|
1058
1007
|
def row_seek(n)
|
1059
1008
|
@result.row_seek(n)
|
1060
1009
|
end
|
1061
1010
|
|
1062
|
-
#
|
1063
|
-
# [Integer] number of columns for last query
|
1011
|
+
# @return [Integer] number of columns for last query
|
1064
1012
|
def field_count
|
1065
1013
|
@fields.length
|
1066
1014
|
end
|
1067
1015
|
|
1068
1016
|
# ignore
|
1017
|
+
# @return [void]
|
1069
1018
|
def free_result
|
1070
1019
|
end
|
1071
1020
|
|
1072
1021
|
# Returns Mysql::Result object that is empty.
|
1073
1022
|
# Use fetch_fields to get list of fields.
|
1074
|
-
#
|
1075
|
-
# [Mysql::Result]
|
1023
|
+
# @return [Mysql::Result]
|
1076
1024
|
def result_metadata
|
1077
1025
|
return nil if @fields.empty?
|
1078
1026
|
Result.new @fields
|
1079
1027
|
end
|
1080
1028
|
end
|
1081
1029
|
|
1030
|
+
# @!visibility public
|
1031
|
+
# @!attribute [rw] year
|
1032
|
+
# @return [Integer]
|
1033
|
+
# @!attribute [rw] month
|
1034
|
+
# @return [Integer]
|
1035
|
+
# @!attribute [rw] day
|
1036
|
+
# @return [Integer]
|
1037
|
+
# @!attribute [rw] hour
|
1038
|
+
# @return [Integer]
|
1039
|
+
# @!attribute [rw] minute
|
1040
|
+
# @return [Integer]
|
1041
|
+
# @!attribute [rw] second
|
1042
|
+
# @return [Integer]
|
1043
|
+
# @!attribute [rw] neg
|
1044
|
+
# @return [Boolean] negative flag
|
1045
|
+
# @!attribute [rw] second_part
|
1046
|
+
# @return [Integer]
|
1082
1047
|
class Time
|
1083
|
-
#
|
1084
|
-
#
|
1085
|
-
#
|
1086
|
-
#
|
1087
|
-
#
|
1088
|
-
#
|
1089
|
-
#
|
1090
|
-
#
|
1048
|
+
# @param [Integer] year
|
1049
|
+
# @param [Integer] month
|
1050
|
+
# @param [Integer] day
|
1051
|
+
# @param [Integer] hour
|
1052
|
+
# @param [Integer] minute
|
1053
|
+
# @param [Integer] second
|
1054
|
+
# @param [Boolean] neg negative flag
|
1055
|
+
# @param [Integer] second_part
|
1091
1056
|
def initialize(year=0, month=0, day=0, hour=0, minute=0, second=0, neg=false, second_part=0)
|
1092
1057
|
@year, @month, @day, @hour, @minute, @second, @neg, @second_part =
|
1093
1058
|
year.to_i, month.to_i, day.to_i, hour.to_i, minute.to_i, second.to_i, neg, second_part.to_i
|
@@ -1097,19 +1062,20 @@ class Mysql
|
|
1097
1062
|
alias min minute
|
1098
1063
|
alias sec second
|
1099
1064
|
|
1100
|
-
|
1065
|
+
# @private
|
1066
|
+
def ==(other)
|
1101
1067
|
other.is_a?(Mysql::Time) &&
|
1102
1068
|
@year == other.year && @month == other.month && @day == other.day &&
|
1103
1069
|
@hour == other.hour && @minute == other.minute && @second == other.second &&
|
1104
1070
|
@neg == neg && @second_part == other.second_part
|
1105
1071
|
end
|
1106
1072
|
|
1107
|
-
|
1073
|
+
# @private
|
1074
|
+
def eql?(other)
|
1108
1075
|
self == other
|
1109
1076
|
end
|
1110
1077
|
|
1111
|
-
#
|
1112
|
-
# [String] "yyyy-mm-dd HH:MM:SS"
|
1078
|
+
# @return [String] "yyyy-mm-dd HH:MM:SS"
|
1113
1079
|
def to_s
|
1114
1080
|
if year == 0 and mon == 0 and day == 0
|
1115
1081
|
h = neg ? hour * -1 : hour
|
@@ -1119,13 +1085,13 @@ class Mysql
|
|
1119
1085
|
end
|
1120
1086
|
end
|
1121
1087
|
|
1122
|
-
#
|
1123
|
-
# [Integer] yyyymmddHHMMSS
|
1088
|
+
# @return [Integer] yyyymmddHHMMSS
|
1124
1089
|
def to_i
|
1125
1090
|
sprintf("%04d%02d%02d%02d%02d%02d", year, mon, day, hour, min, sec).to_i
|
1126
1091
|
end
|
1127
1092
|
|
1128
|
-
|
1093
|
+
# @private
|
1094
|
+
def inspect
|
1129
1095
|
sprintf "#<#{self.class.name}:%04d-%02d-%02d %02d:%02d:%02d>", year, mon, day, hour, min, sec
|
1130
1096
|
end
|
1131
1097
|
|
data/lib/mysql/charset.rb
CHANGED
@@ -1,16 +1,31 @@
|
|
1
|
-
# Copyright (C) 2008-
|
1
|
+
# Copyright (C) 2008-2012 TOMITA Masahiro
|
2
2
|
# mailto:tommy@tmtm.org
|
3
3
|
|
4
|
+
#
|
4
5
|
class Mysql
|
6
|
+
# @!attribute [r] number
|
7
|
+
# @private
|
8
|
+
# @!attribute [r] name
|
9
|
+
# @return [String] charset name
|
10
|
+
# @!attribute [r] csname
|
11
|
+
# @return [String] collation name
|
5
12
|
class Charset
|
13
|
+
# @private
|
14
|
+
# @param [Integer] number
|
15
|
+
# @param [String] name
|
16
|
+
# @param [String] csname
|
6
17
|
def initialize(number, name, csname)
|
7
18
|
@number, @name, @csname = number, name, csname
|
8
19
|
@unsafe = false
|
9
20
|
end
|
21
|
+
|
10
22
|
attr_reader :number, :name, :csname
|
23
|
+
|
24
|
+
# @private
|
11
25
|
attr_accessor :unsafe
|
12
26
|
|
13
27
|
# [[charset_number, charset_name, collation_name, default], ...]
|
28
|
+
# @private
|
14
29
|
CHARSETS = [
|
15
30
|
[ 1, "big5", "big5_chinese_ci", true ],
|
16
31
|
[ 2, "latin2", "latin2_czech_cs", false],
|
@@ -56,6 +71,8 @@ class Mysql
|
|
56
71
|
[ 42, "latin7", "latin7_general_cs", false],
|
57
72
|
[ 43, "macce", "macce_bin", false],
|
58
73
|
[ 44, "cp1250", "cp1250_croatian_ci", false],
|
74
|
+
[ 45, "utf8mb4", "utf8mb4_general_ci", true ],
|
75
|
+
[ 46, "utf8mb4", "utf8mb4_bin", false],
|
59
76
|
[ 47, "latin1", "latin1_bin", false],
|
60
77
|
[ 48, "latin1", "latin1_general_ci", false],
|
61
78
|
[ 49, "latin1", "latin1_general_cs", false],
|
@@ -164,12 +181,16 @@ class Mysql
|
|
164
181
|
[254, "utf8", "utf8_general_cs", false],
|
165
182
|
]
|
166
183
|
|
184
|
+
# @private
|
167
185
|
UNSAFE_CHARSET = [
|
168
186
|
"big5", "sjis", "filename", "gbk", "ucs2", "cp932",
|
169
187
|
]
|
170
188
|
|
189
|
+
# @private
|
171
190
|
NUMBER_TO_CHARSET = {}
|
191
|
+
# @private
|
172
192
|
COLLATION_TO_CHARSET = {}
|
193
|
+
# @private
|
173
194
|
CHARSET_DEFAULT = {}
|
174
195
|
CHARSETS.each do |number, csname, clname, default|
|
175
196
|
cs = Charset.new number, csname, clname
|
@@ -179,13 +200,20 @@ class Mysql
|
|
179
200
|
CHARSET_DEFAULT[csname] = cs if default
|
180
201
|
end
|
181
202
|
|
203
|
+
# @private
|
182
204
|
BINARY_CHARSET_NUMBER = CHARSET_DEFAULT['binary'].number
|
183
205
|
|
206
|
+
# @private
|
207
|
+
# @param [Integer] n
|
208
|
+
# @return [Mysql::Charset]
|
184
209
|
def self.by_number(n)
|
185
210
|
raise ClientError, "unknown charset number: #{n}" unless NUMBER_TO_CHARSET.key? n
|
186
211
|
NUMBER_TO_CHARSET[n]
|
187
212
|
end
|
188
213
|
|
214
|
+
# @private
|
215
|
+
# @param [String] str
|
216
|
+
# @return [Mysql::Charset]
|
189
217
|
def self.by_name(str)
|
190
218
|
ret = COLLATION_TO_CHARSET[str] || CHARSET_DEFAULT[str]
|
191
219
|
raise ClientError, "unknown charset: #{str}" unless ret
|
@@ -194,6 +222,7 @@ class Mysql
|
|
194
222
|
|
195
223
|
if defined? Encoding
|
196
224
|
|
225
|
+
# @private
|
197
226
|
# MySQL Charset -> Ruby's Encodeing
|
198
227
|
CHARSET_ENCODING = {
|
199
228
|
"armscii8" => nil,
|
@@ -235,30 +264,35 @@ class Mysql
|
|
235
264
|
"utf8mb4" => Encoding::UTF_8,
|
236
265
|
}
|
237
266
|
|
267
|
+
# @private
|
268
|
+
# @param [String] value
|
269
|
+
# @return [String]
|
238
270
|
def self.to_binary(value)
|
239
271
|
value.force_encoding Encoding::ASCII_8BIT
|
240
272
|
end
|
241
273
|
|
274
|
+
# @private
|
242
275
|
# convert raw to encoding and convert to Encoding.default_internal
|
243
|
-
#
|
244
|
-
#
|
245
|
-
#
|
246
|
-
# === Return
|
247
|
-
# result [String]
|
276
|
+
# @param [String] raw
|
277
|
+
# @param [Encoding] encoding
|
278
|
+
# @return [String] result
|
248
279
|
def self.convert_encoding(raw, encoding)
|
249
280
|
raw.force_encoding(encoding).encode
|
250
281
|
end
|
251
282
|
|
283
|
+
# @private
|
252
284
|
# retrun corresponding Ruby encoding
|
253
|
-
#
|
254
|
-
# encoding [Encoding]
|
285
|
+
# @return [Encoding] encoding
|
255
286
|
def encoding
|
256
287
|
enc = CHARSET_ENCODING[@name.downcase]
|
257
288
|
raise Mysql::ClientError, "unsupported charset: #{@name}" unless enc
|
258
289
|
enc
|
259
290
|
end
|
260
291
|
|
292
|
+
# @private
|
261
293
|
# convert encoding to corrensponding to MySQL charset
|
294
|
+
# @param [String] value
|
295
|
+
# @return [String]
|
262
296
|
def convert(value)
|
263
297
|
if value.is_a? String and value.encoding != Encoding::ASCII_8BIT
|
264
298
|
value = value.encode encoding
|
data/spec/mysql_spec.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-mysql
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.9.
|
4
|
+
version: 2.9.8
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-05-08 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: This is MySQL connector. pure Ruby version
|
15
15
|
email: tommy@tmtm.org
|
@@ -48,7 +48,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
48
48
|
version: '0'
|
49
49
|
requirements: []
|
50
50
|
rubyforge_project:
|
51
|
-
rubygems_version: 1.8.
|
51
|
+
rubygems_version: 1.8.23
|
52
52
|
signing_key:
|
53
53
|
specification_version: 3
|
54
54
|
summary: MySQL connector
|