Class: Kiba::Plus::Source::Pg

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/kiba/plus/source/pg.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper

#format_sql, #mysql2_connect_hash, #scheme

Constructor Details

#initialize(options = {}) ⇒ Pg

Returns a new instance of Pg.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/kiba/plus/source/pg.rb', line 15

def initialize(options = {})
  @options = options
  @options.assert_valid_keys(
    :connect_url,
    :schema,
    :query,
    :stream,
    :process_bar
  )
  @client = PG.connect(connect_url)
  @client.exec "SET search_path TO %s" % [ options[:schema] ] if options[:schema]
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



13
14
15
# File 'lib/kiba/plus/source/pg.rb', line 13

def client
  @client
end

#optionsObject (readonly)

Returns the value of attribute options.



13
14
15
# File 'lib/kiba/plus/source/pg.rb', line 13

def options
  @options
end

Instance Method Details

#eachObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/kiba/plus/source/pg.rb', line 28

def each
  Kiba::Plus.logger.info query
  if stream?
    # http://www.rubydoc.info/github/ged/ruby-pg/PG%2FConnection%3Aset_single_row_mode
    client.send_query(query)
    client.set_single_row_mode
    loop do
      res = client.get_result or break
      res.check
      res.each do |row|
        print_process_bar if process_bar?
        yield(row)
      end
    end
  else
    results = client.query(query)
    results.each do |row|
      print_process_bar if process_bar?
      yield(row)
    end
  end
end