Class: Baza::Driver::Pg::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/baza/driver/pg/result.rb

Instance Method Summary collapse

Constructor Details

#initialize(driver, result, args = nil) ⇒ Result

Returns a new instance of Result.



2
3
4
5
6
7
8
# File 'lib/baza/driver/pg/result.rb', line 2

def initialize(driver, result, args = nil)
  @result = result
  @unbuffered = true if args && args[:unbuffered]
  @type_translation = driver.db.opts[:type_translation]

  types
end

Instance Method Details

#each(&blk) ⇒ Object



50
51
52
53
54
# File 'lib/baza/driver/pg/result.rb', line 50

def each(&blk)
  to_enum.each(&blk)
rescue StopIteration
  nil
end

#fetchObject



44
45
46
47
48
# File 'lib/baza/driver/pg/result.rb', line 44

def fetch
  to_enum.next
rescue StopIteration
  nil
end

#to_aObject



56
57
58
59
60
61
62
63
# File 'lib/baza/driver/pg/result.rb', line 56

def to_a
  array = []
  each do |result|
    array << result
  end

  array
end

#to_a_enumObject



40
41
42
# File 'lib/baza/driver/pg/result.rb', line 40

def to_a_enum
  ArrayEnumerator.new(to_enum)
end

#to_enumObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/baza/driver/pg/result.rb', line 10

def to_enum
  @enum ||= Enumerator.new do |yielder|
    if @unbuffered
      @result.stream_each_row do |values|
        if @type_translation == :string
          values = translate_values_to_strings(values)
        elsif @type_translation
          values = translate_values_with_types(values)
        end

        result_sym = Hash[*keys.zip(values).flatten]
        yielder << result_sym
      end
    else
      @result.each do |result|
        values = result.values

        if @type_translation == :string
          values = translate_values_to_strings(values)
        elsif @type_translation
          values = translate_values_with_types(values)
        end

        result_sym = Hash[*keys.zip(values).flatten]
        yielder << result_sym
      end
    end
  end
end