Class: Mysql2xxxx::Writer
- Inherits:
-
Object
- Object
- Mysql2xxxx::Writer
- Defined in:
- lib/mysql2xxxx/writer.rb
Constant Summary collapse
- MYSQL_CHARSET =
'utf8'
- ICONV_TO =
'UTF-8//TRANSLIT'
- ICONV_FROM =
'UTF-8'
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
- #close ⇒ Object
- #dbh ⇒ Object
-
#initialize(options = {}) ⇒ Writer
constructor
A new instance of Writer.
- #keys ⇒ Object
- #last_statement ⇒ Object
- #recode_as_utf8(raw_str) ⇒ Object
- #result ⇒ Object
- #stream_arrays ⇒ Object
- #stream_hashes ⇒ Object
- #to_path(path) ⇒ Object
- #to_s ⇒ Object
- #to_stdout ⇒ Object
Constructor Details
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
10 11 12 |
# File 'lib/mysql2xxxx/writer.rb', line 10 def config @config end |
Instance Method Details
#close ⇒ Object
69 70 71 72 73 74 |
# File 'lib/mysql2xxxx/writer.rb', line 69 def close @dead = true iconv.close result.free dbh.close end |
#dbh ⇒ Object
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. ::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 |
#keys ⇒ Object
16 17 18 |
# File 'lib/mysql2xxxx/writer.rb', line 16 def keys @keys ||= result.fetch_fields.map { |field| field.name } end |
#last_statement ⇒ Object
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 |
#result ⇒ Object
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_arrays ⇒ Object
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_hashes ⇒ Object
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_s ⇒ Object
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_stdout ⇒ Object
83 84 85 86 |
# File 'lib/mysql2xxxx/writer.rb', line 83 def to_stdout to_file $stdout nil end |