Class: Embulk::Plugin::MySQL::OutputMysql

Inherits:
OutputPlugin
  • Object
show all
Defined in:
lib/embulk/output_mysql.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task, schema, index) ⇒ OutputMysql

Returns a new instance of OutputMysql.



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/embulk/output_mysql.rb', line 34

def initialize(task, schema, index)
  super

  @task = task
  @schema = schema

  @mysql = self.class.connect task

  sql_schema = self.class.to_mysql_schema schema

  @mysql.query("CREATE TABLE IF NOT EXISTS #{quoted_table_name} (#{sql_schema})")
end

Class Method Details

.resume(task, schema, count) {|task| ... } ⇒ Object

Yields:

  • (task)


28
29
30
31
32
# File 'lib/embulk/output_mysql.rb', line 28

def self.resume(task, schema, count, &control)
  # TODO: Supporting resume
  yield(task)
  {}
end

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

TODO: use Mysql.prepare

Yields:

  • (task)


15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/embulk/output_mysql.rb', line 15

def self.transaction(config, schema, count, &control)
  task = {
    'host' => config.param('host', :string),
    'port' => config.param('port', :integer, default: 3306),
    'username' => config.param('username', :string),
    'password' => config.param('password', :string, default: ''),
    'database' => config.param('database', :string),
    'table' => config.param('table', :string),
  }
  yield task
  {}
end

Instance Method Details

#add(page) ⇒ Object



51
52
53
54
55
56
# File 'lib/embulk/output_mysql.rb', line 51

def add(page)
  page.each do |record|
    # TODO: Support BULK-INSERT or LOAD INFILE
    @mysql.query("INSERT INTO #{quoted_table_name} (#{quoted_column_names}) VALUES (#{self.class.to_sql_values(record)})")
  end
end

#closeObject



47
48
49
# File 'lib/embulk/output_mysql.rb', line 47

def close
  @mysql.close
end