class MssqlOutput

Attributes

handler[RW]

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_mssql.rb, line 20
def initialize
  super
  require 'dbi'
end

Public Instance Methods

client() click to toggle source
# File lib/fluent/plugin/out_mssql.rb, line 67
def client
  if @odbc_label.nil?
    raise Fluent::ConfigError, "odbc_label MUST be specified, but missing"
  else
    begin
      dbh = DBI.connect("dbi:ODBC:#{@odbc_label}", @username, @password)
    rescue
      raise Fluent::ConfigError, "Cannot open database, check user or password"
    end
  end
  dbh
end
configure(conf) click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_mssql.rb, line 25
def configure(conf)
  super

  if @format == 'json'
    @format_proc = Proc.new{|tag, time, record| record.to_json}
  else
    @key_names = @key_names.split(',').map(&:strip)
    @format_proc = Proc.new{|tag, time, record| @key_names.map{|k| record[k]}}
  end

  if @columns.nil? and @sql.nil?
    raise Fluent::ConfigError, "columns or sql MUST be specified, but missing"
  end

  if @sql.nil?
    raise Fluent::ConfigError, "table missing" unless @table

    @columns = @columns.split(',')
    cols = @columns.join(',')
    placeholders = if @format == 'json'
                     '?'
                   else
                     @key_names.map{|k| '?'}.join(',')
                   end
    @sql = "INSERT INTO #{@table} (#{cols}) VALUES (#{placeholders})"
  end
end
format(tag, time, record) click to toggle source
# File lib/fluent/plugin/out_mssql.rb, line 61
def format(tag, time, record)
  tmp = [tag, time, @format_proc.call(tag, time, record)]
  mp = tmp.to_msgpack
  mp
end
shutdown() click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_mssql.rb, line 57
def shutdown
  super
end
start() click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_mssql.rb, line 53
def start
  super
end
write(chunk) click to toggle source
# File lib/fluent/plugin/out_mssql.rb, line 80
def write(chunk)
  handler = self.client
  chunk.msgpack_each { |tag, time, data|
    begin
      query = handler.prepare(@sql)
      num = 1
      data.each { |d|
        query.bind_param(num, d)
        num += 1
      }
      query.execute
    rescue
      raise Fluent::ConfigError, "SQL Execute Error #{@sql} - #{data}"
    end
  }
  handler.disconnect
end