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.



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

def initialize(task, schema, index)
  super

  @task = task
  @schema = schema

  @mysql = self.class.connect task

  sql_schema = self.class.to_mysql_schema schema

  # TODO: RESUMING IS NOT SUPPORTED ON THIS PLUGIN!!!
  @mysql.query("DROP TABLE IF EXISTS #{quoted_table_name}")
  @mysql.query("CREATE TABLE #{quoted_table_name} (#{sql_schema})")
end

Class Method Details

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

Yields:

  • (task)


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

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)


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

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



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

def add(page)
  require 'pp'
  statement = 
  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



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

def close
  @mysql.close
end