Class: Output::MysqlPlugin
- Inherits:
-
OutputPlugin
- Object
- OutputPlugin
- Output::MysqlPlugin
- 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
-
#initialize(options) ⇒ MysqlPlugin
constructor
Create a mysql output plugin instance.
- #send_all(lines, &listener) ⇒ Object
Methods inherited from OutputPlugin
Constructor Details
#initialize(options) ⇒ MysqlPlugin
Create a mysql output plugin instance
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() raise 'Missing config username on output.mysql' unless .key? :username raise 'Missing config database on output.mysql' unless .key? :database raise 'Missing config table on output.mysql' unless .key? :table conf = DEFAULT_MYSQL.merge @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 |