outback 0.0.14 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG +11 -0
- data/LICENSE +21 -0
- data/README.md +29 -3
- data/lib/outback/archive.rb +6 -17
- data/lib/outback/backup.rb +38 -20
- data/lib/outback/cli.rb +6 -2
- data/lib/outback/configuration.rb +15 -10
- data/lib/outback/directory_source.rb +8 -7
- data/lib/outback/directory_target.rb +18 -11
- data/lib/outback/encryption_processor.rb +34 -0
- data/lib/outback/errors.rb +7 -0
- data/lib/outback/logging.rb +7 -0
- data/lib/outback/mysql_source.rb +9 -9
- data/lib/outback/processor.rb +17 -0
- data/lib/outback/s3_target.rb +18 -9
- data/lib/outback/sftp_target.rb +70 -0
- data/lib/outback/source.rb +9 -2
- data/lib/outback/source_archive.rb +17 -0
- data/lib/outback/support/attr_setter.rb +1 -1
- data/lib/outback/support/configurable.rb +5 -3
- data/lib/outback/target.rb +52 -14
- data/lib/outback/target_archive.rb +30 -0
- data/lib/outback/version.rb +3 -0
- data/lib/outback.rb +16 -11
- data/lib/vendor/enumerable_ext.rb +9 -0
- data/lib/{outback/vendor → vendor}/metaclass.rb +1 -1
- data/lib/vendor/methodphitamine.rb +28 -0
- data/lib/vendor/mysql/charset.rb +325 -0
- data/lib/vendor/mysql/constants.rb +165 -0
- data/lib/vendor/mysql/error.rb +989 -0
- data/lib/vendor/mysql/packet.rb +78 -0
- data/lib/vendor/mysql/protocol.rb +770 -0
- data/lib/vendor/mysql.rb +1093 -0
- data/lib/vendor/numeric_ext.rb +49 -0
- data/lib/vendor/string_ext.rb +19 -0
- metadata +84 -43
- data/MIT-LICENSE +0 -20
- data/VERSION +0 -1
- data/lib/outback/configuration_error.rb +0 -4
- data/lib/outback/directory_archive.rb +0 -8
- data/lib/outback/local_archive.rb +0 -6
- data/lib/outback/s3_archive.rb +0 -18
- data/lib/outback/temp_archive.rb +0 -5
- data/lib/outback/vendor/methodphitamine.rb +0 -25
- data/lib/outback/vendor/mysql.rb +0 -1214
data/lib/outback/source.rb
CHANGED
@@ -1,10 +1,17 @@
|
|
1
1
|
module Outback
|
2
2
|
class Source
|
3
3
|
include Configurable
|
4
|
+
include Logging
|
5
|
+
|
6
|
+
attr_reader :backup_name
|
7
|
+
|
8
|
+
def initialize(backup_name)
|
9
|
+
@backup_name = backup_name
|
10
|
+
end
|
4
11
|
|
5
12
|
def create_archive(backup_name, timestamp, tmpdir)
|
6
|
-
|
13
|
+
raise NotImplementedError
|
7
14
|
end
|
8
15
|
|
9
16
|
end
|
10
|
-
end
|
17
|
+
end
|
@@ -3,11 +3,13 @@ module Outback
|
|
3
3
|
|
4
4
|
def self.included(base)
|
5
5
|
base.extend ClassMethods
|
6
|
-
base.class_attribute :attributes
|
7
|
-
base.attributes = []
|
8
6
|
end
|
9
7
|
|
10
8
|
module ClassMethods
|
9
|
+
def attributes
|
10
|
+
@attributes ||= []
|
11
|
+
end
|
12
|
+
|
11
13
|
def configure(*args, &block)
|
12
14
|
returning new(*args) do |instance|
|
13
15
|
if block_given?
|
@@ -28,4 +30,4 @@ module Outback
|
|
28
30
|
end
|
29
31
|
|
30
32
|
end
|
31
|
-
end
|
33
|
+
end
|
data/lib/outback/target.rb
CHANGED
@@ -1,22 +1,60 @@
|
|
1
1
|
module Outback
|
2
2
|
class Target
|
3
3
|
include Configurable
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
4
|
+
include Logging
|
5
|
+
|
6
|
+
attr_reader :backup_name
|
7
|
+
|
8
|
+
def initialize(backup_name)
|
9
|
+
@backup_name = backup_name
|
10
|
+
end
|
11
|
+
|
12
|
+
def purge!
|
13
|
+
if purged_archives = connect { purge_archives }
|
14
|
+
logger.info "Purged #{purged_archives.size} archives (#{purged_archives.sum(&:size)} bytes) from #{self}"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def connection
|
21
|
+
@connection
|
22
|
+
end
|
23
|
+
|
24
|
+
def connect
|
25
|
+
yield
|
26
|
+
end
|
27
|
+
|
28
|
+
def build_archive(filename, size)
|
29
|
+
TargetArchive.new(filename, size, self)
|
30
|
+
end
|
31
|
+
|
32
|
+
def list_all_archives
|
33
|
+
raise NotImplemented
|
34
|
+
end
|
35
|
+
|
36
|
+
def archives
|
37
|
+
list_all_archives.select(&_it.match?(backup_name))
|
38
|
+
end
|
39
|
+
|
40
|
+
def purge_archives
|
41
|
+
archives.select do |archive|
|
42
|
+
if archive.outdated?
|
43
|
+
begin
|
44
|
+
logger.debug "purging archive: #{archive}"
|
45
|
+
unlink_archive!(archive)
|
46
|
+
true
|
47
|
+
rescue => e
|
48
|
+
logger.error "could not unlink archive #{archive}: #{e} #{e.message}"
|
49
|
+
false
|
50
|
+
end
|
16
51
|
end
|
17
52
|
end
|
18
|
-
|
53
|
+
end
|
54
|
+
|
55
|
+
def unlink_archive!(archive)
|
56
|
+
raise NotImplemented
|
19
57
|
end
|
20
58
|
|
21
59
|
end
|
22
|
-
end
|
60
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Outback
|
2
|
+
class TargetArchive < Archive
|
3
|
+
attr_reader :target
|
4
|
+
|
5
|
+
def initialize(filename, size, target)
|
6
|
+
super(filename)
|
7
|
+
@size = size
|
8
|
+
@target = target
|
9
|
+
end
|
10
|
+
|
11
|
+
def match?(name)
|
12
|
+
name == backup_name
|
13
|
+
end
|
14
|
+
|
15
|
+
def ttl
|
16
|
+
target && target.ttl
|
17
|
+
end
|
18
|
+
|
19
|
+
def outdated?
|
20
|
+
if timestamp && ttl
|
21
|
+
Time.now - Time.strptime(timestamp, Outback::TIME_FORMAT) > ttl
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def to_s
|
26
|
+
"#{target}: #{filename}"
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
data/lib/outback.rb
CHANGED
@@ -3,35 +3,40 @@ require 'fileutils'
|
|
3
3
|
require 'tempfile'
|
4
4
|
require 'tmpdir'
|
5
5
|
|
6
|
-
require 'active_support/core_ext'
|
7
|
-
|
8
6
|
require 's3'
|
9
7
|
|
10
|
-
|
11
|
-
|
12
|
-
|
8
|
+
require_relative 'vendor/string_ext'
|
9
|
+
require_relative 'vendor/numeric_ext'
|
10
|
+
require_relative 'vendor/enumerable_ext'
|
11
|
+
require_relative 'vendor/mysql'
|
12
|
+
require_relative 'vendor/metaclass'
|
13
|
+
require_relative 'vendor/methodphitamine'
|
13
14
|
|
15
|
+
require 'outback/version'
|
14
16
|
require 'outback/support/returning'
|
15
17
|
require 'outback/support/attr_setter'
|
16
18
|
require 'outback/support/configurable'
|
17
19
|
require 'outback/support/mysql_ext'
|
18
20
|
require 'outback/support/pathname_ext'
|
21
|
+
require 'outback/errors'
|
22
|
+
require 'outback/logging'
|
19
23
|
require 'outback/configuration'
|
20
|
-
require 'outback/
|
24
|
+
require 'outback/archive'
|
21
25
|
require 'outback/source'
|
26
|
+
require 'outback/source_archive'
|
22
27
|
require 'outback/directory_source'
|
23
28
|
require 'outback/mysql_source'
|
24
|
-
require 'outback/
|
25
|
-
require 'outback/
|
29
|
+
require 'outback/processor'
|
30
|
+
require 'outback/encryption_processor'
|
26
31
|
require 'outback/target'
|
32
|
+
require 'outback/target_archive'
|
27
33
|
require 'outback/directory_target'
|
28
|
-
require 'outback/directory_archive'
|
29
34
|
require 'outback/s3_target'
|
30
|
-
require 'outback/
|
35
|
+
require 'outback/sftp_target'
|
31
36
|
require 'outback/backup'
|
32
37
|
|
33
38
|
module Outback
|
34
|
-
|
39
|
+
TIME_FORMAT = '%Y%m%d%H%M%S'.freeze
|
35
40
|
|
36
41
|
class << self
|
37
42
|
%w(verbose silent).each do |method|
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Kernel
|
2
|
+
protected
|
3
|
+
|
4
|
+
def its() It.new end
|
5
|
+
alias _its its
|
6
|
+
alias _it its
|
7
|
+
alias it its # RUBY34: deprecated method
|
8
|
+
end
|
9
|
+
|
10
|
+
class It < BasicObject
|
11
|
+
|
12
|
+
undef_method(*(instance_methods - [:__id__, :__send__]))
|
13
|
+
|
14
|
+
def initialize
|
15
|
+
@methods = []
|
16
|
+
end
|
17
|
+
|
18
|
+
def method_missing(*args, **kwargs, &block)
|
19
|
+
@methods << [args, kwargs, block] unless args == [:respond_to?, :to_proc]
|
20
|
+
self
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_proc
|
24
|
+
::Kernel.lambda do |obj|
|
25
|
+
@methods.inject(obj) { |current, (args, kwargs, block)| current.send(*args, **kwargs, &block) }
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,325 @@
|
|
1
|
+
# coding: ascii-8bit
|
2
|
+
# Copyright (C) 2008-2012 TOMITA Masahiro
|
3
|
+
# mailto:tommy@tmtm.org
|
4
|
+
|
5
|
+
#
|
6
|
+
class Mysql
|
7
|
+
# @!attribute [r] number
|
8
|
+
# @private
|
9
|
+
# @!attribute [r] name
|
10
|
+
# @return [String] charset name
|
11
|
+
# @!attribute [r] csname
|
12
|
+
# @return [String] collation name
|
13
|
+
class Charset
|
14
|
+
# @private
|
15
|
+
# @param [Integer] number
|
16
|
+
# @param [String] name
|
17
|
+
# @param [String] csname
|
18
|
+
def initialize(number, name, csname)
|
19
|
+
@number, @name, @csname = number, name, csname
|
20
|
+
@unsafe = false
|
21
|
+
end
|
22
|
+
|
23
|
+
attr_reader :number, :name, :csname
|
24
|
+
|
25
|
+
# @private
|
26
|
+
attr_accessor :unsafe
|
27
|
+
|
28
|
+
# [[charset_number, charset_name, collation_name, default], ...]
|
29
|
+
# @private
|
30
|
+
CHARSETS = [
|
31
|
+
[ 1, "big5", "big5_chinese_ci", true ],
|
32
|
+
[ 2, "latin2", "latin2_czech_cs", false],
|
33
|
+
[ 3, "dec8", "dec8_swedish_ci", true ],
|
34
|
+
[ 4, "cp850", "cp850_general_ci", true ],
|
35
|
+
[ 5, "latin1", "latin1_german1_ci", false],
|
36
|
+
[ 6, "hp8", "hp8_english_ci", true ],
|
37
|
+
[ 7, "koi8r", "koi8r_general_ci", true ],
|
38
|
+
[ 8, "latin1", "latin1_swedish_ci", true ],
|
39
|
+
[ 9, "latin2", "latin2_general_ci", true ],
|
40
|
+
[ 10, "swe7", "swe7_swedish_ci", true ],
|
41
|
+
[ 11, "ascii", "ascii_general_ci", true ],
|
42
|
+
[ 12, "ujis", "ujis_japanese_ci", true ],
|
43
|
+
[ 13, "sjis", "sjis_japanese_ci", true ],
|
44
|
+
[ 14, "cp1251", "cp1251_bulgarian_ci", false],
|
45
|
+
[ 15, "latin1", "latin1_danish_ci", false],
|
46
|
+
[ 16, "hebrew", "hebrew_general_ci", true ],
|
47
|
+
[ 17, "filename", "filename", true ],
|
48
|
+
[ 18, "tis620", "tis620_thai_ci", true ],
|
49
|
+
[ 19, "euckr", "euckr_korean_ci", true ],
|
50
|
+
[ 20, "latin7", "latin7_estonian_cs", false],
|
51
|
+
[ 21, "latin2", "latin2_hungarian_ci", false],
|
52
|
+
[ 22, "koi8u", "koi8u_general_ci", true ],
|
53
|
+
[ 23, "cp1251", "cp1251_ukrainian_ci", false],
|
54
|
+
[ 24, "gb2312", "gb2312_chinese_ci", true ],
|
55
|
+
[ 25, "greek", "greek_general_ci", true ],
|
56
|
+
[ 26, "cp1250", "cp1250_general_ci", true ],
|
57
|
+
[ 27, "latin2", "latin2_croatian_ci", false],
|
58
|
+
[ 28, "gbk", "gbk_chinese_ci", true ],
|
59
|
+
[ 29, "cp1257", "cp1257_lithuanian_ci", false],
|
60
|
+
[ 30, "latin5", "latin5_turkish_ci", true ],
|
61
|
+
[ 31, "latin1", "latin1_german2_ci", false],
|
62
|
+
[ 32, "armscii8", "armscii8_general_ci", true ],
|
63
|
+
[ 33, "utf8", "utf8_general_ci", true ],
|
64
|
+
[ 34, "cp1250", "cp1250_czech_cs", false],
|
65
|
+
[ 35, "ucs2", "ucs2_general_ci", true ],
|
66
|
+
[ 36, "cp866", "cp866_general_ci", true ],
|
67
|
+
[ 37, "keybcs2", "keybcs2_general_ci", true ],
|
68
|
+
[ 38, "macce", "macce_general_ci", true ],
|
69
|
+
[ 39, "macroman", "macroman_general_ci", true ],
|
70
|
+
[ 40, "cp852", "cp852_general_ci", true ],
|
71
|
+
[ 41, "latin7", "latin7_general_ci", true ],
|
72
|
+
[ 42, "latin7", "latin7_general_cs", false],
|
73
|
+
[ 43, "macce", "macce_bin", false],
|
74
|
+
[ 44, "cp1250", "cp1250_croatian_ci", false],
|
75
|
+
[ 45, "utf8mb4", "utf8mb4_general_ci", true ],
|
76
|
+
[ 46, "utf8mb4", "utf8mb4_bin", false],
|
77
|
+
[ 47, "latin1", "latin1_bin", false],
|
78
|
+
[ 48, "latin1", "latin1_general_ci", false],
|
79
|
+
[ 49, "latin1", "latin1_general_cs", false],
|
80
|
+
[ 50, "cp1251", "cp1251_bin", false],
|
81
|
+
[ 51, "cp1251", "cp1251_general_ci", true ],
|
82
|
+
[ 52, "cp1251", "cp1251_general_cs", false],
|
83
|
+
[ 53, "macroman", "macroman_bin", false],
|
84
|
+
[ 57, "cp1256", "cp1256_general_ci", true ],
|
85
|
+
[ 58, "cp1257", "cp1257_bin", false],
|
86
|
+
[ 59, "cp1257", "cp1257_general_ci", true ],
|
87
|
+
[ 63, "binary", "binary", true ],
|
88
|
+
[ 64, "armscii8", "armscii8_bin", false],
|
89
|
+
[ 65, "ascii", "ascii_bin", false],
|
90
|
+
[ 66, "cp1250", "cp1250_bin", false],
|
91
|
+
[ 67, "cp1256", "cp1256_bin", false],
|
92
|
+
[ 68, "cp866", "cp866_bin", false],
|
93
|
+
[ 69, "dec8", "dec8_bin", false],
|
94
|
+
[ 70, "greek", "greek_bin", false],
|
95
|
+
[ 71, "hebrew", "hebrew_bin", false],
|
96
|
+
[ 72, "hp8", "hp8_bin", false],
|
97
|
+
[ 73, "keybcs2", "keybcs2_bin", false],
|
98
|
+
[ 74, "koi8r", "koi8r_bin", false],
|
99
|
+
[ 75, "koi8u", "koi8u_bin", false],
|
100
|
+
[ 77, "latin2", "latin2_bin", false],
|
101
|
+
[ 78, "latin5", "latin5_bin", false],
|
102
|
+
[ 79, "latin7", "latin7_bin", false],
|
103
|
+
[ 80, "cp850", "cp850_bin", false],
|
104
|
+
[ 81, "cp852", "cp852_bin", false],
|
105
|
+
[ 82, "swe7", "swe7_bin", false],
|
106
|
+
[ 83, "utf8", "utf8_bin", false],
|
107
|
+
[ 84, "big5", "big5_bin", false],
|
108
|
+
[ 85, "euckr", "euckr_bin", false],
|
109
|
+
[ 86, "gb2312", "gb2312_bin", false],
|
110
|
+
[ 87, "gbk", "gbk_bin", false],
|
111
|
+
[ 88, "sjis", "sjis_bin", false],
|
112
|
+
[ 89, "tis620", "tis620_bin", false],
|
113
|
+
[ 90, "ucs2", "ucs2_bin", false],
|
114
|
+
[ 91, "ujis", "ujis_bin", false],
|
115
|
+
[ 92, "geostd8", "geostd8_general_ci", true ],
|
116
|
+
[ 93, "geostd8", "geostd8_bin", false],
|
117
|
+
[ 94, "latin1", "latin1_spanish_ci", false],
|
118
|
+
[ 95, "cp932", "cp932_japanese_ci", true ],
|
119
|
+
[ 96, "cp932", "cp932_bin", false],
|
120
|
+
[ 97, "eucjpms", "eucjpms_japanese_ci", true ],
|
121
|
+
[ 98, "eucjpms", "eucjpms_bin", false],
|
122
|
+
[ 99, "cp1250", "cp1250_polish_ci", false],
|
123
|
+
[128, "ucs2", "ucs2_unicode_ci", false],
|
124
|
+
[129, "ucs2", "ucs2_icelandic_ci", false],
|
125
|
+
[130, "ucs2", "ucs2_latvian_ci", false],
|
126
|
+
[131, "ucs2", "ucs2_romanian_ci", false],
|
127
|
+
[132, "ucs2", "ucs2_slovenian_ci", false],
|
128
|
+
[133, "ucs2", "ucs2_polish_ci", false],
|
129
|
+
[134, "ucs2", "ucs2_estonian_ci", false],
|
130
|
+
[135, "ucs2", "ucs2_spanish_ci", false],
|
131
|
+
[136, "ucs2", "ucs2_swedish_ci", false],
|
132
|
+
[137, "ucs2", "ucs2_turkish_ci", false],
|
133
|
+
[138, "ucs2", "ucs2_czech_ci", false],
|
134
|
+
[139, "ucs2", "ucs2_danish_ci", false],
|
135
|
+
[140, "ucs2", "ucs2_lithuanian_ci", false],
|
136
|
+
[141, "ucs2", "ucs2_slovak_ci", false],
|
137
|
+
[142, "ucs2", "ucs2_spanish2_ci", false],
|
138
|
+
[143, "ucs2", "ucs2_roman_ci", false],
|
139
|
+
[144, "ucs2", "ucs2_persian_ci", false],
|
140
|
+
[145, "ucs2", "ucs2_esperanto_ci", false],
|
141
|
+
[146, "ucs2", "ucs2_hungarian_ci", false],
|
142
|
+
[192, "utf8", "utf8_unicode_ci", false],
|
143
|
+
[193, "utf8", "utf8_icelandic_ci", false],
|
144
|
+
[194, "utf8", "utf8_latvian_ci", false],
|
145
|
+
[195, "utf8", "utf8_romanian_ci", false],
|
146
|
+
[196, "utf8", "utf8_slovenian_ci", false],
|
147
|
+
[197, "utf8", "utf8_polish_ci", false],
|
148
|
+
[198, "utf8", "utf8_estonian_ci", false],
|
149
|
+
[199, "utf8", "utf8_spanish_ci", false],
|
150
|
+
[200, "utf8", "utf8_swedish_ci", false],
|
151
|
+
[201, "utf8", "utf8_turkish_ci", false],
|
152
|
+
[202, "utf8", "utf8_czech_ci", false],
|
153
|
+
[203, "utf8", "utf8_danish_ci", false],
|
154
|
+
[204, "utf8", "utf8_lithuanian_ci", false],
|
155
|
+
[205, "utf8", "utf8_slovak_ci", false],
|
156
|
+
[206, "utf8", "utf8_spanish2_ci", false],
|
157
|
+
[207, "utf8", "utf8_roman_ci", false],
|
158
|
+
[208, "utf8", "utf8_persian_ci", false],
|
159
|
+
[209, "utf8", "utf8_esperanto_ci", false],
|
160
|
+
[210, "utf8", "utf8_hungarian_ci", false],
|
161
|
+
[211, "utf8", "utf8_sinhala_ci", false],
|
162
|
+
[224, "utf8mb4", "utf8mb4_unicode_ci", false],
|
163
|
+
[225, "utf8mb4", "utf8mb4_icelandic_ci", false],
|
164
|
+
[226, "utf8mb4", "utf8mb4_latvian_ci", false],
|
165
|
+
[227, "utf8mb4", "utf8mb4_romanian_ci", false],
|
166
|
+
[228, "utf8mb4", "utf8mb4_slovenian_ci", false],
|
167
|
+
[229, "utf8mb4", "utf8mb4_polish_ci", false],
|
168
|
+
[230, "utf8mb4", "utf8mb4_estonian_ci", false],
|
169
|
+
[231, "utf8mb4", "utf8mb4_spanish_ci", false],
|
170
|
+
[232, "utf8mb4", "utf8mb4_swedish_ci", false],
|
171
|
+
[233, "utf8mb4", "utf8mb4_turkish_ci", false],
|
172
|
+
[234, "utf8mb4", "utf8mb4_czech_ci", false],
|
173
|
+
[235, "utf8mb4", "utf8mb4_danish_ci", false],
|
174
|
+
[236, "utf8mb4", "utf8mb4_lithuanian_ci", false],
|
175
|
+
[237, "utf8mb4", "utf8mb4_slovak_ci", false],
|
176
|
+
[238, "utf8mb4", "utf8mb4_spanish2_ci", false],
|
177
|
+
[239, "utf8mb4", "utf8mb4_roman_ci", false],
|
178
|
+
[240, "utf8mb4", "utf8mb4_persian_ci", false],
|
179
|
+
[241, "utf8mb4", "utf8mb4_esperanto_ci", false],
|
180
|
+
[242, "utf8mb4", "utf8mb4_hungarian_ci", false],
|
181
|
+
[243, "utf8mb4", "utf8mb4_sinhala_ci", false],
|
182
|
+
[254, "utf8", "utf8_general_cs", false],
|
183
|
+
]
|
184
|
+
|
185
|
+
# @private
|
186
|
+
UNSAFE_CHARSET = [
|
187
|
+
"big5", "sjis", "filename", "gbk", "ucs2", "cp932",
|
188
|
+
]
|
189
|
+
|
190
|
+
# @private
|
191
|
+
NUMBER_TO_CHARSET = {}
|
192
|
+
# @private
|
193
|
+
COLLATION_TO_CHARSET = {}
|
194
|
+
# @private
|
195
|
+
CHARSET_DEFAULT = {}
|
196
|
+
CHARSETS.each do |number, csname, clname, default|
|
197
|
+
cs = Charset.new number, csname, clname
|
198
|
+
cs.unsafe = true if UNSAFE_CHARSET.include? csname
|
199
|
+
NUMBER_TO_CHARSET[number] = cs
|
200
|
+
COLLATION_TO_CHARSET[clname] = cs
|
201
|
+
CHARSET_DEFAULT[csname] = cs if default
|
202
|
+
end
|
203
|
+
|
204
|
+
# @private
|
205
|
+
BINARY_CHARSET_NUMBER = CHARSET_DEFAULT['binary'].number
|
206
|
+
|
207
|
+
# @private
|
208
|
+
# @param [Integer] n
|
209
|
+
# @return [Mysql::Charset]
|
210
|
+
def self.by_number(n)
|
211
|
+
raise ClientError, "unknown charset number: #{n}" unless NUMBER_TO_CHARSET.key? n
|
212
|
+
NUMBER_TO_CHARSET[n]
|
213
|
+
end
|
214
|
+
|
215
|
+
# @private
|
216
|
+
# @param [String] str
|
217
|
+
# @return [Mysql::Charset]
|
218
|
+
def self.by_name(str)
|
219
|
+
ret = COLLATION_TO_CHARSET[str] || CHARSET_DEFAULT[str]
|
220
|
+
raise ClientError, "unknown charset: #{str}" unless ret
|
221
|
+
ret
|
222
|
+
end
|
223
|
+
|
224
|
+
if defined? Encoding
|
225
|
+
|
226
|
+
# @private
|
227
|
+
# MySQL Charset -> Ruby's Encodeing
|
228
|
+
CHARSET_ENCODING = {
|
229
|
+
"armscii8" => nil,
|
230
|
+
"ascii" => Encoding::US_ASCII,
|
231
|
+
"big5" => Encoding::Big5,
|
232
|
+
"binary" => Encoding::ASCII_8BIT,
|
233
|
+
"cp1250" => Encoding::Windows_1250,
|
234
|
+
"cp1251" => Encoding::Windows_1251,
|
235
|
+
"cp1256" => Encoding::Windows_1256,
|
236
|
+
"cp1257" => Encoding::Windows_1257,
|
237
|
+
"cp850" => Encoding::CP850,
|
238
|
+
"cp852" => Encoding::CP852,
|
239
|
+
"cp866" => Encoding::IBM866,
|
240
|
+
"cp932" => Encoding::Windows_31J,
|
241
|
+
"dec8" => nil,
|
242
|
+
"eucjpms" => Encoding::EucJP_ms,
|
243
|
+
"euckr" => Encoding::EUC_KR,
|
244
|
+
"gb2312" => Encoding::EUC_CN,
|
245
|
+
"gbk" => Encoding::GBK,
|
246
|
+
"geostd8" => nil,
|
247
|
+
"greek" => Encoding::ISO_8859_7,
|
248
|
+
"hebrew" => Encoding::ISO_8859_8,
|
249
|
+
"hp8" => nil,
|
250
|
+
"keybcs2" => nil,
|
251
|
+
"koi8r" => Encoding::KOI8_R,
|
252
|
+
"koi8u" => Encoding::KOI8_U,
|
253
|
+
"latin1" => Encoding::ISO_8859_1,
|
254
|
+
"latin2" => Encoding::ISO_8859_2,
|
255
|
+
"latin5" => Encoding::ISO_8859_9,
|
256
|
+
"latin7" => Encoding::ISO_8859_13,
|
257
|
+
"macce" => Encoding::MacCentEuro,
|
258
|
+
"macroman" => Encoding::MacRoman,
|
259
|
+
"sjis" => Encoding::SHIFT_JIS,
|
260
|
+
"swe7" => nil,
|
261
|
+
"tis620" => Encoding::TIS_620,
|
262
|
+
"ucs2" => Encoding::UTF_16BE,
|
263
|
+
"ujis" => Encoding::EucJP_ms,
|
264
|
+
"utf8" => Encoding::UTF_8,
|
265
|
+
"utf8mb4" => Encoding::UTF_8,
|
266
|
+
}
|
267
|
+
|
268
|
+
# @private
|
269
|
+
# @param [String] value
|
270
|
+
# @return [String]
|
271
|
+
def self.to_binary(value)
|
272
|
+
value.force_encoding Encoding::ASCII_8BIT
|
273
|
+
end
|
274
|
+
|
275
|
+
# @private
|
276
|
+
# convert raw to encoding and convert to Encoding.default_internal
|
277
|
+
# @param [String] raw
|
278
|
+
# @param [Encoding] encoding
|
279
|
+
# @return [String] result
|
280
|
+
def self.convert_encoding(raw, encoding)
|
281
|
+
raw.force_encoding(encoding).encode
|
282
|
+
end
|
283
|
+
|
284
|
+
# @private
|
285
|
+
# retrun corresponding Ruby encoding
|
286
|
+
# @return [Encoding] encoding
|
287
|
+
def encoding
|
288
|
+
enc = CHARSET_ENCODING[@name.downcase]
|
289
|
+
raise Mysql::ClientError, "unsupported charset: #{@name}" unless enc
|
290
|
+
enc
|
291
|
+
end
|
292
|
+
|
293
|
+
# @private
|
294
|
+
# convert encoding to corrensponding to MySQL charset
|
295
|
+
# @param [String] value
|
296
|
+
# @return [String]
|
297
|
+
def convert(value)
|
298
|
+
if value.is_a? String and value.encoding != Encoding::ASCII_8BIT
|
299
|
+
value = value.encode encoding
|
300
|
+
end
|
301
|
+
value
|
302
|
+
end
|
303
|
+
|
304
|
+
else
|
305
|
+
# for Ruby 1.8
|
306
|
+
|
307
|
+
def self.to_binary(value)
|
308
|
+
value
|
309
|
+
end
|
310
|
+
|
311
|
+
def self.convert_encoding(raw, encoding)
|
312
|
+
raw
|
313
|
+
end
|
314
|
+
|
315
|
+
def encoding
|
316
|
+
nil
|
317
|
+
end
|
318
|
+
|
319
|
+
def convert(value)
|
320
|
+
value
|
321
|
+
end
|
322
|
+
|
323
|
+
end
|
324
|
+
end
|
325
|
+
end
|