Class: Fluent::SQLInput::TableElement

Inherits:
Object
  • Object
show all
Includes:
Configurable
Defined in:
lib/fluent/plugin/in_sql.rb

Instance Method Summary collapse

Instance Method Details

#configure(conf) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/fluent/plugin/in_sql.rb', line 45

def configure(conf)
  super

  unless @state_file
    $log.warn "'state_file PATH' parameter is not set to a 'sql' source."
    $log.warn "this parameter is highly recommended to save the last rows to resume tailing."
  end
end

#emit_next_records(last_record, limit) ⇒ Object

emits next records and returns the last record of emitted records



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/fluent/plugin/in_sql.rb', line 94

def emit_next_records(last_record, limit)
  relation = @model
  if last_record && last_update_value = last_record[@update_column]
    relation = relation.where("#{@update_column} > ?", last_update_value)
  end
  relation = relation.order("#{@update_column} ASC").limit(limit)

  now = Engine.now
  entry_name = @model.table_name.singularize

  me = MultiEventStream.new
  relation.each do |obj|
    record = obj.serializable_hash rescue nil
    if record
      if @time_column && tv = obj.read_attribute(@time_column)
        if tv.is_a?(Time)
          time = tv.to_i
        else
          time = Time.parse(tv.to_s).to_i rescue now
        end
      else
        time = now
      end
      me.add(time, record)
      last_record = record
    end
  end

  last_record = last_record.dup  # some plugin rewrites record :(
  Engine.emit_stream(@tag, me)

  return last_record
end

#init(tag_prefix, base_model) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/fluent/plugin/in_sql.rb', line 54

def init(tag_prefix, base_model)
  @tag = "#{tag_prefix}.#{@tag}" if tag_prefix

  # creates a model for this table
  table_name = @table
  @model = Class.new(base_model) do
    self.table_name = table_name
    self.inheritance_column = '_never_use_'
    #self.include_root_in_json = false

    def read_attribute_for_serialization(n)
      v = send(n)
      if v.respond_to?(:to_msgpack)
        v
      else
        v.to_s
      end
    end
  end

  # ActiveRecord requires model class to have a name.
  class_name = table_name.singularize.camelize
  base_model.const_set(class_name, @model)

  # Sets model_name otherwise ActiveRecord causes errors
  model_name = ActiveModel::Name.new(@model, nil, class_name)
  @model.define_singleton_method(:model_name) { model_name }

  # if update_column is not set, here uses primary key
  unless @update_column
    columns = Hash[@model.columns.map {|c| [c.name, c] }]
    pk = columns[@model.primary_key]
    unless pk
      raise "Composite primary key is not supported. Set update_column parameter to <table> section."
    end
    @update_column = pk.name
  end
end