Class: Output::MysqlPlugin

Inherits:
OutputPlugin show all
Defined in:
lib/fileminer/output/mysql.rb

Constant Summary collapse

DEFAULT_MYSQL =
{
  host: 'localhost',
  port: 3306,
  password: '',
  encoding: 'utf8mb4',
  ssl_mode: :disabled
}

Instance Method Summary collapse

Methods inherited from OutputPlugin

#batch?, #close

Constructor Details

#initialize(options) ⇒ MysqlPlugin

Create a mysql output plugin instance

Parameters:

Options Hash (options):

  • :host (String) — default: 'localhost'
  • :port (Integer) — default: 3306
  • :username (String)
  • :password (String) — default: ''
  • :database (String)
  • :encoding (String) — default: 'utf8mb4'
  • :ssl_mode (Symbol) — default: :disabled
  • :table (String)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/fileminer/output/mysql.rb', line 29

def initialize(options)
  raise 'Missing config username on output.mysql' unless options.key? :username
  raise 'Missing config database on output.mysql' unless options.key? :database
  raise 'Missing config table on output.mysql' unless options.key? :table
  conf = DEFAULT_MYSQL.merge options
  @table = conf.delete :table
  conf[:port] = conf[:port].to_i
  conf[:password] = conf[:password].to_s
  @encoding = conf[:encoding]
  conf[:ssl_mode] = :disabled if conf[:ssl_mode] != :enabled
  @mysql = Mysql2::Client.new conf
  @mysql_conf = conf
  create_table_if_not_exists
  @sqls = Hash.new { |hash, key| hash[key] = generate_batch_sql key }
end

Instance Method Details

#send_all(lines, &listener) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/fileminer/output/mysql.rb', line 88

def send_all(lines, &listener)
  values = lines.flat_map { |line| [line[:host], line[:path], line[:pos], line[:end], line[:data]] }
  sql = @sqls[lines.size]
  mysql_client = get_mysql_client
  stmt = @mysql_client.prepare sql
  stmt.execute *values
  stmt.close
  listener.call
end