Class: Mysql2xxxx::Writer
- Inherits:
-
Object
- Object
- Mysql2xxxx::Writer
- Defined in:
- lib/mysql2xxxx/writer.rb
Instance Attribute Summary collapse
-
#properties ⇒ Object
readonly
Returns the value of attribute properties.
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
#initialize(options = {}) ⇒ Writer
Returns a new instance of Writer.
12 13 14 |
# File 'lib/mysql2xxxx/writer.rb', line 12 def initialize( = {}) @properties = Properties.new end |
Instance Attribute Details
#properties ⇒ Object (readonly)
Returns the value of attribute properties.
10 11 12 |
# File 'lib/mysql2xxxx/writer.rb', line 10 def properties @properties end |
Instance Method Details
#close ⇒ Object
78 79 80 81 82 |
# File 'lib/mysql2xxxx/writer.rb', line 78 def close result.free dbh.close @dead = true 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, properties.charset @dbh.real_connect properties.host, properties.user, properties.password, properties.database, properties.port, properties.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 = properties.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 68 69 70 71 72 73 74 75 76 |
# File 'lib/mysql2xxxx/writer.rb', line 63 def recode_as_utf8(raw_str) return if raw_str.nil? if ::RUBY_VERSION >= '1.9' $stderr.puts "[mysql2xxxx] Raw - #{raw_str}" if ::ENV['MYSQL2XXXX_DEBUG'] == 'true' recoded_str = raw_str.ensure_encoding 'UTF-8', :external_encoding => properties.encoding, :invalid_characters => :transcode $stderr.puts "[mysql2xxxx] Recoded - #{recoded_str}" if ::ENV['MYSQL2XXXX_DEBUG'] == 'true' recoded_str else $stderr.puts "[mysql2xxxx] Raw - #{raw_str}" if ::ENV['MYSQL2XXXX_DEBUG'] == 'true' recoded_str = ::Iconv.conv('UTF-8//TRANSLIT', properties.encoding, raw_str.to_s + ' ')[0..-2] $stderr.puts "[mysql2xxxx] Recoded - #{recoded_str}" if ::ENV['MYSQL2XXXX_DEBUG'] == 'true' recoded_str 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
96 97 98 99 100 101 |
# File 'lib/mysql2xxxx/writer.rb', line 96 def to_path(path) f = ::File.open(path, 'w') to_file f f.close nil end |
#to_s ⇒ Object
84 85 86 87 88 89 |
# File 'lib/mysql2xxxx/writer.rb', line 84 def to_s s = ::StringIO.new to_file s s.rewind s.read end |
#to_stdout ⇒ Object
91 92 93 94 |
# File 'lib/mysql2xxxx/writer.rb', line 91 def to_stdout to_file $stdout nil end |