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

Inherits:
ResultBase show all
Defined in:
lib/baza/drivers/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/drivers/sqlite3/result.rb', line 4

def initialize(driver, statement)
  @statement = statement

  begin
    @statement.execute
    @type_translation = driver.baza.opts[:type_translation]
    @types = statement.types if @type_translation == true
    @columns = statement.columns.map { |column| column.to_sym }
    read_results
    @index = -1
  ensure
    @statement.try(:close)
  end
end

Instance Method Details

#eachObject

Loops over every result yielding them.



35
36
37
38
39
# File 'lib/baza/drivers/sqlite3/result.rb', line 35

def each
  while data = fetch
    yield data
  end
end

#fetchObject

Returns a single result.



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

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

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

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