Class: Kiba::Plus::Destination::Pg

Inherits:
Object
  • Object
show all
Defined in:
lib/kiba/plus/destination/pg.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Pg



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/kiba/plus/destination/pg.rb', line 12

def initialize(options = {})
  @options = options
  @options.assert_valid_keys(
    :connect_url,
    :schema,
    :table_name,
    :columns
  )
  @conn = PG.connect(connect_url)
  @conn.exec "SET search_path TO %s" % [ options[:schema] ] if options[:schema]
  init
end

Instance Attribute Details

#connObject (readonly)

Returns the value of attribute conn.



10
11
12
# File 'lib/kiba/plus/destination/pg.rb', line 10

def conn
  @conn
end

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/kiba/plus/destination/pg.rb', line 10

def options
  @options
end

Instance Method Details

#closeObject



35
36
37
38
# File 'lib/kiba/plus/destination/pg.rb', line 35

def close
  @conn.close
  @conn = nil
end

#write(row) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/kiba/plus/destination/pg.rb', line 25

def write(row)
  @conn.exec_prepared(prepare_name,
    row.values_at(*columns))
rescue PG::Error => ex
  Kiba::Plus.logger.error "ERROR for #{row}"
  Kiba::Plus.logger.error ex.message
  # Maybe, write to db table or file
  raise ex
end