mysql2xxxx 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/lib/mysql2xxxx.rb CHANGED
@@ -7,13 +7,13 @@ require 'active_support/version'
7
7
  }.each do |active_support_3_requirement|
8
8
  require active_support_3_requirement
9
9
  end if ::ActiveSupport::VERSION::MAJOR == 3
10
+ require 'mysql2'
10
11
 
11
12
  module Mysql2xxxx
12
13
  autoload :JSON, 'mysql2xxxx/json'
13
14
  autoload :CSV, 'mysql2xxxx/csv'
14
15
  autoload :XML, 'mysql2xxxx/xml'
15
16
  autoload :Properties, 'mysql2xxxx/properties'
16
- autoload :Client, 'mysql2xxxx/client'
17
17
  autoload :ExtraOutputs, 'mysql2xxxx/extra_outputs'
18
18
  autoload :Cli, 'mysql2xxxx/cli'
19
19
  end
@@ -14,13 +14,10 @@ module Mysql2xxxx
14
14
  @properties = Properties.new options
15
15
  end
16
16
 
17
- def client
18
- @client ||= Client.new properties
19
- end
20
-
21
17
  def to_file(f)
18
+ @client = ::Mysql2::Client.new properties.database_config
22
19
  keys = nil
23
- client.select_each(properties.execute) do |hsh|
20
+ @client.query(properties.execute).each do |hsh|
24
21
  unless keys
25
22
  keys = hsh.keys
26
23
  f.write keys.to_csv
@@ -28,6 +25,8 @@ module Mysql2xxxx
28
25
  f.write keys.inject([]) { |memo, k| memo.push hsh[k] }.to_csv
29
26
  end
30
27
  nil
28
+ ensure
29
+ @client.try :close
31
30
  end
32
31
  end
33
32
  end
@@ -14,14 +14,11 @@ module Mysql2xxxx
14
14
  @properties = Properties.new options
15
15
  end
16
16
 
17
- def client
18
- @client ||= Client.new properties
19
- end
20
-
21
17
  def to_file(f)
18
+ @client = ::Mysql2::Client.new properties.database_config
22
19
  first = true
23
20
  f.write '['
24
- client.select_each(properties.execute) do |hsh|
21
+ @client.query(properties.execute).each do |hsh|
25
22
  line = if first
26
23
  first = false
27
24
  hsh.to_json
@@ -32,6 +29,8 @@ module Mysql2xxxx
32
29
  end
33
30
  f.write ']'
34
31
  nil
32
+ ensure
33
+ @client.try :close
35
34
  end
36
35
  end
37
36
  end
@@ -43,9 +43,11 @@ module Mysql2xxxx
43
43
  private
44
44
 
45
45
  def active_record_connection
46
- if defined? ::ActiveRecord
46
+ if defined?(::ActiveRecord)
47
47
  ::ActiveRecord::Base.connection
48
48
  end
49
+ rescue
50
+ # oh well
49
51
  end
50
52
 
51
53
  def active_record_config
@@ -1,3 +1,3 @@
1
1
  module Mysql2xxxx
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -8,16 +8,13 @@ module Mysql2xxxx
8
8
  @properties = Properties.new options
9
9
  end
10
10
 
11
- def client
12
- @client ||= Client.new properties
13
- end
14
-
15
11
  # this matches the xml generated by "mysql --xml"
16
12
  # i tried to use builder, but the String#to_xs nonsense got in the way
17
13
  def to_file(f)
14
+ @client = ::Mysql2::Client.new properties.database_config
18
15
  f.write %{<?xml version="1.0" encoding="utf-8" ?>}
19
16
  f.write %{<resultset statement="#{properties.execute.to_xs}" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">}
20
- client.select_each(properties.execute) do |hsh|
17
+ @client.query(properties.execute).each do |hsh|
21
18
  f.write %{<row>}
22
19
  hsh.each do |k, v|
23
20
  f.write %{<field name="#{k.to_xs}"#{' xsi:nil="true"' if v.nil?}>#{v.to_s.to_xs}</field>}
@@ -26,6 +23,8 @@ module Mysql2xxxx
26
23
  end
27
24
  f.write %{</resultset>}
28
25
  nil
26
+ ensure
27
+ @client.try :close
29
28
  end
30
29
  end
31
30
  end
data/mysql2xxxx.gemspec CHANGED
@@ -8,7 +8,7 @@ Gem::Specification.new do |s|
8
8
  s.platform = Gem::Platform::RUBY
9
9
  s.authors = ["Seamus Abshere"]
10
10
  s.email = ["seamus@abshere.net"]
11
- s.homepage = ""
11
+ s.homepage = "https://github.com/seamusabshere/mysql2xxxx"
12
12
  s.summary = %q{In a memory-sensitive way (but not fast), dump mysql tables to JSON, CSV, XML.}
13
13
  s.description = %q{Gives you binaries like mysql2csv, mysql2json, and mysql2xml, and Ruby classes to match.}
14
14
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mysql2xxxx
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Seamus Abshere
@@ -153,7 +153,6 @@ files:
153
153
  - bin/mysql2xml
154
154
  - lib/mysql2xxxx.rb
155
155
  - lib/mysql2xxxx/cli.rb
156
- - lib/mysql2xxxx/client.rb
157
156
  - lib/mysql2xxxx/csv.rb
158
157
  - lib/mysql2xxxx/extra_outputs.rb
159
158
  - lib/mysql2xxxx/json.rb
@@ -170,7 +169,7 @@ files:
170
169
  - test/test_mysql2xxxx.rb
171
170
  - test/test_well_formedness.rb
172
171
  has_rdoc: true
173
- homepage: ""
172
+ homepage: https://github.com/seamusabshere/mysql2xxxx
174
173
  licenses: []
175
174
 
176
175
  post_install_message:
@@ -1,18 +0,0 @@
1
- require 'mysql2'
2
- module Mysql2xxxx
3
- class Client
4
- attr_reader :properties
5
- def initialize(properties)
6
- @properties = properties
7
- end
8
- def select_each(sql, &blk)
9
- mysql2.query(sql).each do |hsh|
10
- yield hsh
11
- end
12
- end
13
- private
14
- def mysql2
15
- @mysql2 ||= ::Mysql2::Client.new properties.database_config
16
- end
17
- end
18
- end