Class: Mysql2xxxx::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/mysql2xxxx/writer.rb

Direct Known Subclasses

CSV, JSON, XML

Constant Summary collapse

MYSQL_CHARSET =
'utf8'
ICONV_TO =
'UTF-8//TRANSLIT'
ICONV_FROM =
'UTF-8'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Writer

Returns a new instance of Writer.



12
13
14
# File 'lib/mysql2xxxx/writer.rb', line 12

def initialize(options = {})
  @config = Config.new options
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



10
11
12
# File 'lib/mysql2xxxx/writer.rb', line 10

def config
  @config
end

Instance Method Details

#closeObject



69
70
71
72
73
74
# File 'lib/mysql2xxxx/writer.rb', line 69

def close
  @dead = true
  iconv.close
  result.free
  dbh.close
end

#dbhObject



37
38
39
40
41
42
43
44
45
# File 'lib/mysql2xxxx/writer.rb', line 37

def dbh
  return @dbh if @dbh.is_a? ::Mysql
  @dbh = ::Mysql.init
  @dbh.options ::Mysql::SET_CHARSET_NAME, MYSQL_CHARSET
  @dbh.real_connect config.host, config.user, config.password, config.database, config.port, config.socket
  # so that we can use_result instead of store_result
  @dbh.query_with_result = false
  @dbh
end

#keysObject



16
17
18
# File 'lib/mysql2xxxx/writer.rb', line 16

def keys
  @keys ||= result.fetch_fields.map { |field| field.name }
end

#last_statementObject



20
21
22
23
24
25
26
27
28
29
# File 'lib/mysql2xxxx/writer.rb', line 20

def last_statement
  return @last_statement if @last_statement.is_a? ::String
  statements = config.execute.split(';').select { |statement| statement.to_s.strip.length > 0 }
  @last_statement = statements.pop
  statements.each do |statement|
    dbh.query statement
    # but we're not using the results
  end
  @last_statement
end

#recode_as_utf8(raw_str) ⇒ Object



63
64
65
66
67
# File 'lib/mysql2xxxx/writer.rb', line 63

def recode_as_utf8(raw_str)
  unless raw_str.nil?
    [ iconv.iconv(raw_str), iconv.iconv(nil) ].join
  end
end

#resultObject



31
32
33
34
35
# File 'lib/mysql2xxxx/writer.rb', line 31

def result
  return @result if @result.is_a? ::Mysql::Result
  dbh.query last_statement
  @result = dbh.use_result
end

#stream_arraysObject



47
48
49
50
51
52
53
# File 'lib/mysql2xxxx/writer.rb', line 47

def stream_arrays
  raise "dead connection" if @dead
  while ary = result.fetch_row do
    yield ary.map { |v| recode_as_utf8 v }
  end
  close
end

#stream_hashesObject



55
56
57
58
59
60
61
# File 'lib/mysql2xxxx/writer.rb', line 55

def stream_hashes
  raise "dead connection" if @dead
  while hsh = result.fetch_hash do
    yield hsh.inject({}) { |memo, (k, v)| memo[recode_as_utf8(k)] = recode_as_utf8(v); memo }
  end
  close
end

#to_path(path) ⇒ Object



88
89
90
91
92
93
# File 'lib/mysql2xxxx/writer.rb', line 88

def to_path(path)
  f = ::RUBY_VERSION >= '1.9' ? ::File.open(path, 'w', :binmode => true) : ::File.open(path, 'w')
  to_file f
  f.close
  nil
end

#to_sObject



76
77
78
79
80
81
# File 'lib/mysql2xxxx/writer.rb', line 76

def to_s
  s = ::StringIO.new
  to_file s
  s.rewind
  s.read
end

#to_stdoutObject



83
84
85
86
# File 'lib/mysql2xxxx/writer.rb', line 83

def to_stdout
  to_file $stdout
  nil
end