Class: Mysqlexport::Json

Inherits:
Writer
  • Object
show all
Defined in:
lib/mysqlexport/writer/json.rb

Instance Attribute Summary

Attributes inherited from Writer

#client, #config, #sql

Instance Method Summary collapse

Methods inherited from Writer

#filter_path, #to_path, #to_s, #to_stdout

Constructor Details

#initialize(options = {}) ⇒ Json

Returns a new instance of Json.



5
6
7
# File 'lib/mysqlexport/writer/json.rb', line 5

def initialize(options = {})
  super(**options)
end

Instance Method Details

#to_file(file) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mysqlexport/writer/json.rb', line 9

def to_file(file)
  first = true
  json_o = json_options
  file.write "["
  client.query(config.execute, stream: true, cache_rows: false).each do |h|
    next if h.nil?

    if first
      first = false
    else
      file.write ","
    end
    file.write ::MultiJson.dump(h, **json_o)
  end
  file.write "]"
  nil
end