mysql2xxxx 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,149 @@
1
+ mysql2xxxx
2
+ Version: 0.0.4
3
+ Run: Tue Mar 15 11:05:35 -0500 2011
4
+ System: Darwin vidalia 10.6.0 Darwin Kernel Version 10.6.0: Wed Nov 10 18:13:17 PST 2010; root:xnu-1504.9.26~3/RELEASE_I386 i386
5
+
6
+ ##################################################
7
+ # CSV
8
+ ##################################################
9
+ Real memory over time (sampled every 0.3 sec):
10
+ 0:00.30 11672
11
+ 0:00.60 19936
12
+ 0:00.91 19408
13
+ 0:01.29 20460
14
+ 0:01.56 20460
15
+ 0:01.87 20460
16
+ 0:02.17 20460
17
+ Memprof object counts (top 25):
18
+ 5044 ./benchmark/../lib/mysql2xxxx/writer.rb:43:String
19
+ 5019 /Users/seamus/.rvm/gems/ruby-1.8.7-p334/gems/fastercsv-1.5.4/lib/faster_csv.rb:1798:String
20
+ 1617 /Users/seamus/.rvm/gems/ruby-1.8.7-p334/gems/fastercsv-1.5.4/lib/faster_csv.rb:1852:String
21
+ 808 /Users/seamus/.rvm/gems/ruby-1.8.7-p334/gems/fastercsv-1.5.4/lib/faster_csv.rb:1849:String
22
+ 408 /Users/seamus/.rvm/gems/ruby-1.8.7-p334/gems/fastercsv-1.5.4/lib/faster_csv.rb:1849:Array
23
+ 406 /Users/seamus/.rvm/gems/ruby-1.8.7-p334/gems/fastercsv-1.5.4/lib/faster_csv.rb:1092:__scope__
24
+ 405 /Users/seamus/.rvm/gems/ruby-1.8.7-p334/gems/fastercsv-1.5.4/lib/faster_csv.rb:1091:Hash
25
+ 404 /Users/seamus/.rvm/gems/ruby-1.8.7-p334/gems/fastercsv-1.5.4/lib/faster_csv.rb:1852:Method
26
+ 404 /Users/seamus/.rvm/gems/ruby-1.8.7-p334/gems/fastercsv-1.5.4/lib/faster_csv.rb:1852:MatchData
27
+ 404 /Users/seamus/.rvm/gems/ruby-1.8.7-p334/gems/fastercsv-1.5.4/lib/faster_csv.rb:1490:String
28
+ 372 (__FORWARDABLE__):5:__node__
29
+ 279 (__FORWARDABLE__):3:__node__
30
+ 217 (__FORWARDABLE__):1:__node__
31
+ 206 /Users/seamus/.rvm/gems/ruby-1.8.7-p334/gems/fastercsv-1.5.4/lib/faster_csv.rb:1092:String
32
+ 205 /Users/seamus/.rvm/gems/ruby-1.8.7-p334/gems/fastercsv-1.5.4/lib/faster_csv.rb:1826:String
33
+ 205 /Users/seamus/.rvm/gems/ruby-1.8.7-p334/gems/fastercsv-1.5.4/lib/faster_csv.rb:1734:String
34
+ 205 /Users/seamus/.rvm/gems/ruby-1.8.7-p334/gems/fastercsv-1.5.4/lib/faster_csv.rb:1423:Hash
35
+ 204 /Users/seamus/.rvm/gems/ruby-1.8.7-p334/gems/fastercsv-1.5.4/lib/faster_csv.rb:2004:Hash
36
+ 204 /Users/seamus/.rvm/gems/ruby-1.8.7-p334/gems/fastercsv-1.5.4/lib/faster_csv.rb:1831:Hash
37
+ 204 /Users/seamus/.rvm/gems/ruby-1.8.7-p334/gems/fastercsv-1.5.4/lib/faster_csv.rb:1830:Regexp
38
+ 204 /Users/seamus/.rvm/gems/ruby-1.8.7-p334/gems/fastercsv-1.5.4/lib/faster_csv.rb:1826:Regexp
39
+ 204 /Users/seamus/.rvm/gems/ruby-1.8.7-p334/gems/fastercsv-1.5.4/lib/faster_csv.rb:1823:String
40
+ 204 /Users/seamus/.rvm/gems/ruby-1.8.7-p334/gems/fastercsv-1.5.4/lib/faster_csv.rb:1823:Regexp
41
+ 204 /Users/seamus/.rvm/gems/ruby-1.8.7-p334/gems/fastercsv-1.5.4/lib/faster_csv.rb:1820:Regexp
42
+ 204 /Users/seamus/.rvm/gems/ruby-1.8.7-p334/gems/fastercsv-1.5.4/lib/faster_csv.rb:1791:Proc
43
+
44
+ ##################################################
45
+ # JSON
46
+ ##################################################
47
+ Real memory over time (sampled every 0.3 sec):
48
+ 0:00.30 12364
49
+ 0:00.61 17128
50
+ 0:00.94 17536
51
+ 0:01.24 17536
52
+ 0:01.54 17536
53
+ 0:01.83 17536
54
+ 0:02.12 17536
55
+ 0:02.41 17536
56
+ 0:02.70 17536
57
+ 0:02.93 17536
58
+ 0:03.06 17536
59
+ 0:03.37 17536
60
+ 0:03.68 17536
61
+ 0:03.98 17536
62
+ 0:04.28 17536
63
+ 0:04.58 17536
64
+ 0:04.88 17536
65
+ 0:05.18 17536
66
+ 0:05.48 17536
67
+ 0:05.79 17536
68
+ 0:06.10 17536
69
+ 0:06.40 17536
70
+ 0:06.71 17536
71
+ 0:07.02 17536
72
+ 0:07.36 17536
73
+ 0:07.67 17536
74
+ 0:07.97 17536
75
+ 0:08.28 17536
76
+ 0:08.59 17536
77
+ 0:08.89 17536
78
+ 0:09.20 17536
79
+ 0:09.52 17536
80
+ 0:09.82 17536
81
+ 0:10.13 17536
82
+ 0:10.44 17536
83
+ 0:10.74 17536
84
+ 0:11.04 17536
85
+ Memprof object counts (top 25):
86
+ 3792 /Users/seamus/.rvm/gems/ruby-1.8.7-p334/gems/activesupport-3.0.5/lib/active_support/json/encoding.rb:125:String
87
+ 2882 /Users/seamus/.rvm/gems/ruby-1.8.7-p334/gems/activesupport-3.0.5/lib/active_support/json/encoding.rb:73:__node__
88
+ 1922 /Users/seamus/.rvm/gems/ruby-1.8.7-p334/gems/activesupport-3.0.5/lib/active_support/json/encoding.rb:235:Array
89
+ 1898 /Users/seamus/.rvm/gems/ruby-1.8.7-p334/gems/activesupport-3.0.5/lib/active_support/json/encoding.rb:132:String
90
+ 1094 /Users/seamus/.rvm/gems/ruby-1.8.7-p334/gems/activesupport-3.0.5/lib/active_support/json/encoding.rb:247:String
91
+ 1022 ./benchmark/../lib/mysql2xxxx/writer.rb:51:String
92
+ 989 /Users/seamus/.rvm/gems/ruby-1.8.7-p334/gems/activesupport-3.0.5/lib/active_support/json/encoding.rb:247:Array
93
+ 950 /Users/seamus/.rvm/gems/ruby-1.8.7-p334/gems/activesupport-3.0.5/lib/active_support/json/encoding.rb:236:Array
94
+ 280 /Users/seamus/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/1.8/yaml/tag.rb:69:__node__
95
+ 260 /Users/seamus/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/1.8/yaml/tag.rb:70:__node__
96
+ 170 /Users/seamus/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/1.8/i686-darwin10.6.0/syck.bundle:0:__node__
97
+ 160 /Users/seamus/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/1.8/yaml/tag.rb:67:__node__
98
+ 160 /Users/seamus/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/1.8/yaml/tag.rb:65:__node__
99
+ 148 /Users/seamus/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/1.8/yaml/tag.rb:75:__node__
100
+ 120 /Users/seamus/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/1.8/i686-darwin10.6.0/stringio.bundle:0:__node__
101
+ 117 /Users/seamus/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/1.8/i686-darwin10.6.0/bigdecimal.bundle:0:__node__
102
+ 114 /Users/seamus/.rvm/gems/ruby-1.8.7-p334/gems/activesupport-3.0.5/lib/active_support/json/encoding.rb:61:Hash
103
+ 105 /Users/seamus/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/1.8/yaml/tag.rb:68:__node__
104
+ 73 /Users/seamus/.rvm/gems/ruby-1.8.7-p334/gems/activesupport-3.0.5/lib/active_support/json/encoding.rb:247:__node__
105
+ 64 /Users/seamus/.rvm/gems/ruby-1.8.7-p334/gems/activesupport-3.0.5/lib/active_support/deprecation/proxy_wrappers.rb:13:__node__
106
+ 63 /Users/seamus/.rvm/gems/ruby-1.8.7-p334/gems/activesupport-3.0.5/lib/active_support/json/encoding.rb:237:__node__
107
+ 60 /Users/seamus/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/1.8/yaml/tag.rb:72:__node__
108
+ 60 /Users/seamus/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/1.8/yaml/tag.rb:71:__node__
109
+ 60 /Users/seamus/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/1.8/yaml/tag.rb:64:__node__
110
+ 60 /Users/seamus/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/1.8/yaml/tag.rb:63:__node__
111
+
112
+ ##################################################
113
+ # XML
114
+ ##################################################
115
+ Real memory over time (sampled every 0.3 sec):
116
+ 0:00.29 10920
117
+ 0:00.54 18088
118
+ 0:00.83 17836
119
+ 0:01.12 17496
120
+ 0:01.43 17836
121
+ 0:01.73 18520
122
+ 0:02.03 17496
123
+ 0:02.33 18520
124
+ Memprof object counts (top 25):
125
+ 7638 ./benchmark/../lib/mysql2xxxx/writer/xml.rb:16:String
126
+ 2539 ./benchmark/../lib/mysql2xxxx/writer.rb:43:String
127
+ 2538 ./benchmark/../lib/mysql2xxxx/writer.rb:7:Array
128
+ 2537 ./benchmark/../lib/mysql2xxxx/writer/xml.rb:16:Array
129
+ 120 /Users/seamus/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/1.8/i686-darwin10.6.0/stringio.bundle:0:__node__
130
+ 109 ./benchmark/../lib/mysql2xxxx/writer/xml.rb:15:__node__
131
+ 103 ./benchmark/../lib/mysql2xxxx/writer/xml.rb:18:String
132
+ 103 ./benchmark/../lib/mysql2xxxx/writer/xml.rb:14:String
133
+ 102 ./benchmark/../lib/mysql2xxxx/writer.rb:43:Array
134
+ 28 ./benchmark/../lib/mysql2xxxx/writer/xml.rb:16:__node__
135
+ 25 ./benchmark/../lib/mysql2xxxx/writer/xml.rb:4:String
136
+ 25 ./benchmark/../lib/mysql2xxxx/writer.rb:11:String
137
+ 22 ./benchmark/../lib/mysql2xxxx/writer.rb:35:__node__
138
+ 20 ./benchmark/../lib/mysql2xxxx/writer.rb:16:__node__
139
+ 14 ./benchmark/../lib/mysql2xxxx/writer/xml.rb:12:__node__
140
+ 14 ./benchmark/../lib/mysql2xxxx/properties.rb:18:__node__
141
+ 12 ./benchmark/../lib/mysql2xxxx/writer.rb:11:__node__
142
+ 12 ./benchmark/../lib/mysql2xxxx/properties.rb:36:__node__
143
+ 12 ./benchmark/../lib/mysql2xxxx/properties.rb:32:__node__
144
+ 12 ./benchmark/../lib/mysql2xxxx/properties.rb:14:__node__
145
+ 12 ./benchmark/../lib/mysql2xxxx/properties.rb:10:__node__
146
+ 11 ./benchmark/../lib/mysql2xxxx/writer/xml.rb:4:__node__
147
+ 10 ./benchmark/../lib/mysql2xxxx/writer.rb:34:__node__
148
+ 10 ./benchmark/../lib/mysql2xxxx/properties.rb:40:__node__
149
+ 9 ./benchmark/../lib/mysql2xxxx/writer.rb:26:__node__
@@ -0,0 +1,43 @@
1
+ mysql2xxxx
2
+ Version: 0.0.4
3
+ Run: 2011-03-15 11:06:13 -0500
4
+ System: Darwin vidalia 10.6.0 Darwin Kernel Version 10.6.0: Wed Nov 10 18:13:17 PST 2010; root:xnu-1504.9.26~3/RELEASE_I386 i386
5
+
6
+ ##################################################
7
+ # CSV
8
+ ##################################################
9
+ Real memory over time (sampled every 0.3 sec):
10
+ 0:00.31 12416
11
+ 0:00.61 15264
12
+ 0:00.92 15800
13
+ 0:01.23 15800
14
+ 0:01.54 15800
15
+ Memprof object counts not available (ruby 1.9)
16
+
17
+ ##################################################
18
+ # JSON
19
+ ##################################################
20
+ Real memory over time (sampled every 0.3 sec):
21
+ 0:00.31 12424
22
+ 0:00.61 15576
23
+ 0:00.91 15684
24
+ 0:01.22 15852
25
+ 0:01.53 15852
26
+ 0:01.83 15852
27
+ 0:02.14 15852
28
+ 0:02.45 15852
29
+ 0:02.75 15852
30
+ 0:03.06 15852
31
+ 0:03.37 15852
32
+ 0:03.67 15852
33
+ 0:03.97 15852
34
+ Memprof object counts not available (ruby 1.9)
35
+
36
+ ##################################################
37
+ # XML
38
+ ##################################################
39
+ Real memory over time (sampled every 0.3 sec):
40
+ 0:00.30 12928
41
+ 0:00.60 14616
42
+ 0:00.91 14616
43
+ Memprof object counts not available (ruby 1.9)
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env ruby
2
+ unless RUBY_VERSION >= '1.9'
3
+ require 'rubygems'
4
+ require 'memprof'
5
+ end
6
+ $LOAD_PATH.unshift File.join(ENV['HERE'], '..', 'lib')
7
+ require 'mysql2xxxx'
8
+
9
+ format = ARGV[0]
10
+
11
+ if RUBY_VERSION >= '1.9'
12
+ # unfortunately can't use memprof
13
+ a = Mysql2xxxx.const_get(format).new :user => ENV['DBUSER'], :password => ENV['DBPASSWORD'], :database => ENV['DBNAME'], :execute => ENV['EXECUTE']
14
+ a.to_path ENV['DUMP_PATH']
15
+ else
16
+ Memprof.track(ENV['MEMPROF_REPORT_PATH']) do
17
+ a = Mysql2xxxx.const_get(format).new :user => ENV['DBUSER'], :password => ENV['DBPASSWORD'], :database => ENV['DBNAME'], :execute => ENV['EXECUTE']
18
+ a.to_path ENV['DUMP_PATH']
19
+ end
20
+ end
data/lib/mysql2xxxx.rb CHANGED
@@ -1,3 +1,6 @@
1
+ unless RUBY_VERSION >= '1.9'
2
+ $KCODE = 'U'
3
+ end
1
4
  require 'active_support'
2
5
  require 'active_support/version'
3
6
  %w{
@@ -7,13 +10,13 @@ require 'active_support/version'
7
10
  }.each do |active_support_3_requirement|
8
11
  require active_support_3_requirement
9
12
  end if ::ActiveSupport::VERSION::MAJOR == 3
10
- require 'mysql2'
13
+ require 'mysql'
11
14
 
12
15
  module Mysql2xxxx
13
- autoload :JSON, 'mysql2xxxx/json'
14
- autoload :CSV, 'mysql2xxxx/csv'
15
- autoload :XML, 'mysql2xxxx/xml'
16
+ autoload :JSON, 'mysql2xxxx/writer/json'
17
+ autoload :CSV, 'mysql2xxxx/writer/csv'
18
+ autoload :XML, 'mysql2xxxx/writer/xml'
19
+ autoload :Writer, 'mysql2xxxx/writer'
16
20
  autoload :Properties, 'mysql2xxxx/properties'
17
- autoload :ExtraOutputs, 'mysql2xxxx/extra_outputs'
18
21
  autoload :Cli, 'mysql2xxxx/cli'
19
22
  end
@@ -6,16 +6,6 @@ module Mysql2xxxx
6
6
  @options.stringify_keys!
7
7
  end
8
8
 
9
- def database_config
10
- {
11
- :username => user,
12
- :password => password,
13
- :host => host,
14
- :port => port,
15
- :database => database
16
- }
17
- end
18
-
19
9
  def user
20
10
  options['user'] || active_record_config.try(:[], :username)
21
11
  end
@@ -25,13 +15,27 @@ module Mysql2xxxx
25
15
  end
26
16
 
27
17
  def host
28
- options['host'] || active_record_config.try(:[], :host)
18
+ options['host'] || active_record_config.try(:[], :host) || '127.0.0.1'
19
+ end
20
+
21
+ # Force utf8 as mysql connection charset
22
+ def charset
23
+ 'utf8'
24
+ end
25
+
26
+ # Force UTF-8 as ruby string encoding (ruby 1.9 only)
27
+ def encoding
28
+ 'UTF-8'
29
29
  end
30
30
 
31
31
  def port
32
32
  options['port'] || active_record_config.try(:[], :port)
33
33
  end
34
34
 
35
+ def socket
36
+ options['socket'] || active_record_config.try(:[], :socket)
37
+ end
38
+
35
39
  def database
36
40
  options['database'] || active_record_connection.try(:current_database)
37
41
  end
@@ -1,3 +1,3 @@
1
1
  module Mysql2xxxx
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -0,0 +1,82 @@
1
+ require 'stringio'
2
+ module Mysql2xxxx
3
+ class Writer
4
+ attr_reader :properties
5
+
6
+ def initialize(options = {})
7
+ @properties = Properties.new options
8
+ end
9
+
10
+ def keys
11
+ @keys ||= result.fetch_fields.map { |field| field.name }
12
+ end
13
+
14
+ def last_statement
15
+ return @last_statement if @last_statement.is_a? ::String
16
+ statements = properties.execute.split(';').select { |statement| statement.to_s.strip.length > 0 }
17
+ @last_statement = statements.pop
18
+ statements.each do |statement|
19
+ dbh.query statement
20
+ # but we're not using the results
21
+ end
22
+ @last_statement
23
+ end
24
+
25
+ def result
26
+ return @result if @result.is_a? ::Mysql::Result
27
+ dbh.query last_statement
28
+ @result = dbh.use_result
29
+ end
30
+
31
+ def dbh
32
+ return @dbh if @dbh.is_a? ::Mysql
33
+ @dbh = ::Mysql.init
34
+ @dbh.options ::Mysql::SET_CHARSET_NAME, properties.charset
35
+ @dbh.real_connect properties.host, properties.user, properties.password, properties.database, properties.port, properties.socket
36
+ # so that we can use_result instead of store_result
37
+ @dbh.query_with_result = false
38
+ @dbh
39
+ end
40
+
41
+ def stream_arrays
42
+ raise "dead connection" if @dead
43
+ while ary = result.fetch_row do
44
+ yield ary
45
+ end
46
+ close
47
+ end
48
+
49
+ def stream_hashes
50
+ raise "dead connection" if @dead
51
+ while hsh = result.fetch_hash do
52
+ yield hsh
53
+ end
54
+ close
55
+ end
56
+
57
+ def close
58
+ result.free
59
+ dbh.close
60
+ @dead = true
61
+ end
62
+
63
+ def to_s
64
+ s = ::StringIO.new
65
+ to_file s
66
+ s.rewind
67
+ s.read
68
+ end
69
+
70
+ def to_stdout
71
+ to_file $stdout
72
+ nil
73
+ end
74
+
75
+ def to_path(path)
76
+ f = ::File.open(path, 'w')
77
+ to_file f
78
+ f.close
79
+ nil
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,17 @@
1
+ if RUBY_VERSION >= '1.9'
2
+ require 'csv'
3
+ else
4
+ require 'fastercsv'
5
+ end
6
+
7
+ module Mysql2xxxx
8
+ class CSV < Writer
9
+ def to_file(f)
10
+ f.write keys.to_csv
11
+ stream_arrays do |ary|
12
+ f.write ary.to_csv
13
+ end
14
+ nil
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,29 @@
1
+ %w{
2
+ active_support/json/encoding
3
+ }.each do |active_support_3_requirement|
4
+ require active_support_3_requirement
5
+ end if ::ActiveSupport::VERSION::MAJOR == 3
6
+
7
+ module Mysql2xxxx
8
+ class JSON < Writer
9
+ def to_file(f)
10
+ first = true
11
+ f.write '['
12
+ stream_hashes do |hsh|
13
+ if first
14
+ first = false
15
+ else
16
+ f.write ','
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
+ f.write hsh.to_json
24
+ end
25
+ f.write ']'
26
+ nil
27
+ end
28
+ end
29
+ end
@@ -1,30 +1,24 @@
1
1
  module Mysql2xxxx
2
- class XML
3
- include ExtraOutputs
4
-
5
- attr_reader :properties
6
-
7
- def initialize(options = {})
8
- @properties = Properties.new options
2
+ class XML < Writer
3
+ def escaped_keys
4
+ @escaped_keys ||= keys.map { |k| k.to_xs }
9
5
  end
10
6
 
7
+ # sabshere 2/10/11 dkastner pointed out that this doesn't include metadata like later versions of mysql do
11
8
  # this matches the xml generated by "mysql --xml"
12
9
  # i tried to use builder, but the String#to_xs nonsense got in the way
13
10
  def to_file(f)
14
- @client = ::Mysql2::Client.new properties.database_config
15
11
  f.write %{<?xml version="1.0" encoding="utf-8" ?>}
16
12
  f.write %{<resultset statement="#{properties.execute.to_xs}" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">}
17
- @client.query(properties.execute).each do |hsh|
13
+ stream_arrays do |ary|
18
14
  f.write %{<row>}
19
- hsh.each do |k, v|
20
- f.write %{<field name="#{k.to_xs}"#{' xsi:nil="true"' if v.nil?}>#{v.to_s.to_xs}</field>}
15
+ ary.each_with_index do |v, i|
16
+ f.write %{<field name="#{escaped_keys[i]}"#{' xsi:nil="true"' if v.nil?}>#{v.to_s.to_xs}</field>}
21
17
  end
22
18
  f.write %{</row>}
23
19
  end
24
20
  f.write %{</resultset>}
25
21
  nil
26
- ensure
27
- @client.try :close
28
22
  end
29
23
  end
30
24
  end