class Charrington::CreatePostgresTable

Constants

CreateFailed
Error

Attributes

column_types[RW]
columns[R]
connection[R]
event[R]
opts[R]
schema[R]
table_name[R]
transformer[R]

Public Class Methods

new(connection, event, schema, table_name, columns, opts = {}) click to toggle source
# File lib/logstash/outputs/charrington/create_postgres_table.rb, line 18
def initialize(connection, event, schema, table_name, columns, opts = {})
  @connection = connection
  @event = event.to_hash
  @table_name = table_name
  @schema = schema
  @columns = columns
  @transformer = opts[:transformer]
  @column_types = initial_columns
end

Public Instance Methods

call() click to toggle source
# File lib/logstash/outputs/charrington/create_postgres_table.rb, line 28
def call
  set_column_types
  create_table
  true
rescue => e
  raise CreateFailed, e.message
ensure
  @column_types.clear if @column_types.is_a? Array
end

Private Instance Methods

create_table() click to toggle source
# File lib/logstash/outputs/charrington/create_postgres_table.rb, line 79
def create_table
  execute("CREATE TABLE IF NOT EXISTS #{schema}#{table_name} (#{column_types.join(', ')})")
end
execute(sql) click to toggle source
# File lib/logstash/outputs/charrington/create_postgres_table.rb, line 83
def execute(sql)
  statement = connection.prepareStatement( sql.gsub(/\s+/, " ").strip )
  statement.execute()
rescue Java::OrgPostgresqlUtil::PSQLException => e
  self.logger.error "PSQLException: #{e.message} sql=#{sql}"
ensure
  statement.close unless statement.nil?
end
initial_columns() click to toggle source
# File lib/logstash/outputs/charrington/create_postgres_table.rb, line 66
def initial_columns
  if transformer == "postgres"
    [
      "id SERIAL PRIMARY KEY",
      "inserted_at TIMESTAMP DEFAULT NOW()"
    ]
  else
    [
      "uuid_ts TIMESTAMP DEFAULT NOW()"
    ]
  end
end
set_column_types() click to toggle source
# File lib/logstash/outputs/charrington/create_postgres_table.rb, line 40
def set_column_types
  columns.each do |column|
    if @@timestamp_columns.include?(column)
      column_types << "#{column} TIMESTAMP"
      next
    end

    case event[column]
    when Time, LogStash::Timestamp
      column_types << "#{column} TIMESTAMP"
    when Date
      column_types << "#{column} DATE"
    when Integer
      column_types << "#{column} BIGINT"
    when BigDecimal
      column_types << "#{column} DECIMAL"
    when Float
      column_types << "#{column} DOUBLE PRECISION"
    when true, false
      column_types << "#{column} BOOLEAN"
    else
      column_types << "#{column} VARCHAR(512)"
    end
  end
end