Class: Embulk::Formatter::MysqlXml

Inherits:
FormatterPlugin
  • Object
show all
Defined in:
lib/embulk/formatter/mysql_xml.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.transaction(config, schema) {|task| ... } ⇒ Object

Yields:

  • (task)


9
10
11
12
13
14
15
16
17
# File 'lib/embulk/formatter/mysql_xml.rb', line 9

def self.transaction(config, schema, &control)
  task = {
    "root_element"     => config.param("root_element", :string),
    "row_element"      => config.param("row_element", :string),
    "timestamp_format" => config.param("timestamp_format", :string, default: "%Y-%m-%d %H:%M:%S"),
  }

  yield(task)
end

Instance Method Details

#add(page) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/embulk/formatter/mysql_xml.rb', line 32

def add(page)
  column_names = page.schema.map(&:name)
  page.each do |record|
    row = REXML::Element.new(@row_element)
    column_names.zip(record).each do |column_name, value|
      if value.is_a?(Time)
        value = value.strftime(@timestamp_format)
      end
      row.add_attribute(column_name, value)
    end
    @document.root.add_element(row)
  end
end

#closeObject



29
30
# File 'lib/embulk/formatter/mysql_xml.rb', line 29

def close
end

#finishObject



46
47
48
49
50
51
52
53
54
# File 'lib/embulk/formatter/mysql_xml.rb', line 46

def finish
  buf = ''
  @document.write(output: buf, indent: 4)

  file = file_output.next_file
  file.write(buf)
  file.write("\n")
  file_output.finish
end

#initObject



19
20
21
22
23
24
25
26
27
# File 'lib/embulk/formatter/mysql_xml.rb', line 19

def init
  @root_element = task["root_element"]
  @row_element = task["row_element"]
  @timestamp_format = task["timestamp_format"]

  @document = REXML::Document.new
  @document.add(REXML::XMLDecl.new)
  @document.add(REXML::Element.new(@root_element))
end