class Fluent::DbiOutput

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_dbi.rb, line 12
def initialize
  super

  require 'dbi'
end

Public Instance Methods

configure(conf) click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_dbi.rb, line 18
def configure(conf)
  super

  @keys = @keys.split(",")
end
format(tag, time, record) click to toggle source
# File lib/fluent/plugin/out_dbi.rb, line 24
def format(tag, time, record)
  [tag, time, record].to_msgpack
end
write(chunk) click to toggle source
# File lib/fluent/plugin/out_dbi.rb, line 28
def write(chunk)
  begin
    dbh = DBI.connect(@dsn, @db_user, @db_pass)
    dbh['AutoCommit'] = false
    sth = dbh.prepare(@query)
    chunk.msgpack_each { |tag, time, record|
      record.key?('time') || record['time'] = time
      record.key('tag') || record['tag'] = tag
      values = []
      @keys.each { |key|
        values.push(record[key])
      }
      rows = sth.execute(*values)
    }
  rescue
    dbh.rollback if dbh
    raise
  else
    sth.finish
    dbh.commit
  ensure
    dbh.disconnect if dbh
  end
end