mysql2xxxx 0.0.4 → 0.1.0

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.
data/.gemtest ADDED
File without changes
data/README.rdoc CHANGED
@@ -2,9 +2,9 @@
2
2
 
3
3
  ==Binaries
4
4
 
5
- mysql2csv --user=dbuser --password=dbpassword --database=dbname --execute="select * from automobile_makes"
6
- mysql2json --user=dbuser --password=dbpassword --database=dbname --execute="select * from automobile_makes"
7
- mysql2xml --user=dbuser --password=dbpassword --database=dbname --execute="select * from automobile_makes"
5
+ mysql2csv --user=dbuser --password=dbpassword --database=dbname [--charset=utf8] [--encoding=UTF-8] --execute="select * from automobile_makes"
6
+ mysql2json --user=dbuser --password=dbpassword --database=dbname [--charset=utf8] [--encoding=UTF-8] --execute="select * from automobile_makes"
7
+ mysql2xml --user=dbuser --password=dbpassword --database=dbname [--charset=utf8] [--encoding=UTF-8] --execute="select * from automobile_makes"
8
8
 
9
9
  To see all options
10
10
 
@@ -3,7 +3,7 @@ require 'mixlib/cli'
3
3
  module Mysql2xxxx
4
4
  class Cli
5
5
  include ::Mixlib::CLI
6
- %w{ user password host Port database }.each do |o|
6
+ %w{ user password host Port database charset encoding }.each do |o|
7
7
  option o.downcase.to_sym,
8
8
  :short => "-#{o[0,1]} #{o.upcase}",
9
9
  :long => "--#{o.downcase}=#{o.upcase}",
@@ -18,14 +18,22 @@ module Mysql2xxxx
18
18
  options['host'] || active_record_config.try(:[], :host) || '127.0.0.1'
19
19
  end
20
20
 
21
- # Force utf8 as mysql connection charset
21
+ # MySQL connection charset
22
+ #
23
+ # If you change this, you also have to change :encoding
24
+ #
25
+ # Default: utf8
22
26
  def charset
23
- 'utf8'
27
+ options['charset'] || 'utf8'
24
28
  end
25
29
 
26
- # Force UTF-8 as ruby string encoding (ruby 1.9 only)
30
+ # Encoding
31
+ #
32
+ # If you change this, you also have to change :charset
33
+ #
34
+ # Default: UTF-8
27
35
  def encoding
28
- 'UTF-8'
36
+ options['encoding'] || 'UTF-8'
29
37
  end
30
38
 
31
39
  def port
@@ -1,3 +1,3 @@
1
1
  module Mysql2xxxx
2
- VERSION = "0.0.4"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -1,4 +1,10 @@
1
1
  require 'stringio'
2
+ if ::RUBY_VERSION >= '1.9'
3
+ require 'ensure/encoding'
4
+ else
5
+ require 'iconv'
6
+ end
7
+
2
8
  module Mysql2xxxx
3
9
  class Writer
4
10
  attr_reader :properties
@@ -41,7 +47,7 @@ module Mysql2xxxx
41
47
  def stream_arrays
42
48
  raise "dead connection" if @dead
43
49
  while ary = result.fetch_row do
44
- yield ary
50
+ yield ary.map { |v| recode_as_utf8 v }
45
51
  end
46
52
  close
47
53
  end
@@ -49,11 +55,26 @@ module Mysql2xxxx
49
55
  def stream_hashes
50
56
  raise "dead connection" if @dead
51
57
  while hsh = result.fetch_hash do
52
- yield hsh
58
+ yield hsh.inject({}) { |memo, (k, v)| memo[recode_as_utf8(k)] = recode_as_utf8(v); memo }
53
59
  end
54
60
  close
55
61
  end
56
62
 
63
+ def recode_as_utf8(raw_str)
64
+ return if raw_str.nil?
65
+ if ::RUBY_VERSION >= '1.9'
66
+ $stderr.puts "[mysql2xxxx] Raw - #{raw_str}" if ::ENV['MYSQL2XXXX_DEBUG'] == 'true'
67
+ recoded_str = raw_str.ensure_encoding 'UTF-8', :external_encoding => properties.encoding, :invalid_characters => :transcode
68
+ $stderr.puts "[mysql2xxxx] Recoded - #{recoded_str}" if ::ENV['MYSQL2XXXX_DEBUG'] == 'true'
69
+ recoded_str
70
+ else
71
+ $stderr.puts "[mysql2xxxx] Raw - #{raw_str}" if ::ENV['MYSQL2XXXX_DEBUG'] == 'true'
72
+ recoded_str = ::Iconv.conv('UTF-8//TRANSLIT', properties.encoding, raw_str.to_s + ' ')[0..-2]
73
+ $stderr.puts "[mysql2xxxx] Recoded - #{recoded_str}" if ::ENV['MYSQL2XXXX_DEBUG'] == 'true'
74
+ recoded_str
75
+ end
76
+ end
77
+
57
78
  def close
58
79
  result.free
59
80
  dbh.close
@@ -15,11 +15,6 @@ module Mysql2xxxx
15
15
  else
16
16
  f.write ','
17
17
  end
18
- if RUBY_VERSION >= '1.9'
19
- # the mysql gem isn't encoding aware, so even if mysql returns proper utf-8, ruby 1.9 treats it as ASCII-8BIT
20
- # here we force ruby to treat these strings as utf-8 without any further conversion
21
- hsh.each { |k, v| hsh[k] = v.nil? ? nil : v.force_encoding(properties.encoding) }
22
- end
23
18
  f.write hsh.to_json
24
19
  end
25
20
  f.write ']'
data/mysql2xxxx.gemspec CHANGED
@@ -23,9 +23,8 @@ Gem::Specification.new do |s|
23
23
  s.add_dependency 'mysql'
24
24
  s.add_dependency 'mixlib-cli'
25
25
  s.add_dependency 'fast_xs'
26
- unless RUBY_VERSION >= '1.9'
27
- s.add_dependency 'fastercsv'
28
- end
26
+ s.add_dependency 'fastercsv'
27
+ s.add_dependency 'ensure-encoding'
29
28
 
30
29
  s.add_development_dependency 'activerecord'
31
30
  s.add_development_dependency 'shell-executer'
@@ -6,7 +6,7 @@ class TestMysql2xxxx < Test::Unit::TestCase
6
6
  a = Mysql2xxxx::JSON.new @options
7
7
  str = a.to_s
8
8
  assert str.include?('Acura')
9
- assert str.include?('Citro\\u00ebn')
9
+ assert str.include?(%q{Citro\u00ebn})
10
10
  assert !str.include?('DaimlerChrysler')
11
11
  end
12
12
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: mysql2xxxx
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.4
5
+ version: 0.1.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Seamus Abshere
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-03-15 00:00:00 -05:00
13
+ date: 2011-05-08 00:00:00 -05:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -58,7 +58,7 @@ dependencies:
58
58
  type: :runtime
59
59
  version_requirements: *id004
60
60
  - !ruby/object:Gem::Dependency
61
- name: activerecord
61
+ name: fastercsv
62
62
  prerelease: false
63
63
  requirement: &id005 !ruby/object:Gem::Requirement
64
64
  none: false
@@ -66,10 +66,10 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: "0"
69
- type: :development
69
+ type: :runtime
70
70
  version_requirements: *id005
71
71
  - !ruby/object:Gem::Dependency
72
- name: shell-executer
72
+ name: ensure-encoding
73
73
  prerelease: false
74
74
  requirement: &id006 !ruby/object:Gem::Requirement
75
75
  none: false
@@ -77,10 +77,10 @@ dependencies:
77
77
  - - ">="
78
78
  - !ruby/object:Gem::Version
79
79
  version: "0"
80
- type: :development
80
+ type: :runtime
81
81
  version_requirements: *id006
82
82
  - !ruby/object:Gem::Dependency
83
- name: posix-spawn
83
+ name: activerecord
84
84
  prerelease: false
85
85
  requirement: &id007 !ruby/object:Gem::Requirement
86
86
  none: false
@@ -91,7 +91,7 @@ dependencies:
91
91
  type: :development
92
92
  version_requirements: *id007
93
93
  - !ruby/object:Gem::Dependency
94
- name: ruby-debug19
94
+ name: shell-executer
95
95
  prerelease: false
96
96
  requirement: &id008 !ruby/object:Gem::Requirement
97
97
  none: false
@@ -101,6 +101,28 @@ dependencies:
101
101
  version: "0"
102
102
  type: :development
103
103
  version_requirements: *id008
104
+ - !ruby/object:Gem::Dependency
105
+ name: posix-spawn
106
+ prerelease: false
107
+ requirement: &id009 !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: "0"
113
+ type: :development
114
+ version_requirements: *id009
115
+ - !ruby/object:Gem::Dependency
116
+ name: ruby-debug19
117
+ prerelease: false
118
+ requirement: &id010 !ruby/object:Gem::Requirement
119
+ none: false
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: "0"
124
+ type: :development
125
+ version_requirements: *id010
104
126
  description: Gives you binaries like mysql2csv, mysql2json, and mysql2xml, and Ruby classes to match.
105
127
  email:
106
128
  - seamus@abshere.net
@@ -113,6 +135,7 @@ extensions: []
113
135
  extra_rdoc_files: []
114
136
 
115
137
  files:
138
+ - .gemtest
116
139
  - .gitignore
117
140
  - Gemfile
118
141
  - README.rdoc