Sha256: 1bab83404eb12bdaa8d95b9ba4b0c3c1072cf4742e97c1214db5b0e2890e95d1

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

# This class handels the result when running MRI (or others).
class Baza::Driver::Sqlite3::Result < Baza::ResultBase
  # Constructor. This should not be called manually.
  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

  # Returns a single result.
  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

  # Loops over every result yielding them.
  def each
    while data = fetch
      yield data
    end
  end

private

  def read_results
    @results = []

    loop do
      row = @statement.step
      break if @statement.done?
      @results << row
    end
  end

  def translate_type(value, type)
    if value
      if type == 'datetime'
        return Time.parse(value)
      elsif type == 'date'
        return Date.parse(value)
      else
        return value
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
baza-0.0.18 lib/baza/drivers/sqlite3/result.rb