Class: Baza::Driver::Sqlite3::Result

Inherits:
ResultBase show all
Defined in:
lib/baza/driver/sqlite3/result.rb

Overview

This class handels the result when running MRI (or others).

Instance Method Summary collapse

Methods inherited from ResultBase

#to_a, #to_a_enum, #to_enum

Constructor Details

#initialize(driver, statement) ⇒ Result

Constructor. This should not be called manually.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/baza/driver/sqlite3/result.rb', line 4

def initialize(driver, statement)
  @statement = statement

  begin
    @statement.execute
    @type_translation = driver.db.opts[:type_translation]
    @types = statement.types if @type_translation == true
    @columns = statement.columns.map(&:to_sym)
    read_results
    @index = -1
  ensure
    @statement.close
  end
end

Instance Method Details

#eachObject

Loops over every result yielding them.



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/baza/driver/sqlite3/result.rb', line 34

def each
  loop do
    data = fetch

    if data
      yield data
    else
      break
    end
  end
end

#fetchObject

Returns a single result.



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/baza/driver/sqlite3/result.rb', line 20

def fetch
  row = @results[@index += 1]
  return unless row

  if @types
    row.map!.with_index { |value, index| translate_type(value, @types[index]) } if @types
  elsif @type_translation == :string
    row.map!(&:to_s)
  end

  Hash[*@columns.zip(row).flatten]
end