Sha256: 9fa2c12149bec4b59df2e1d5c2f40f72732b12906f1727ad9beda8552e2982c8

Contents?: true

Size: 1.42 KB

Versions: 18

Compression:

Stored size: 1.42 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.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

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

  # Loops over every result yielding them.
  def each
    loop do
      data = fetch

      if data
        yield data
      else
        break
      end
    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)
    return if value.to_s.empty?

    if type == "datetime"
      return nil if value == "NULL"
      return Time.parse(value)
    elsif type == "date"
      return nil if value == "NULL"
      return Date.parse(value)
    else
      value
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
baza-0.0.38 lib/baza/driver/sqlite3/result.rb
baza-0.0.37 lib/baza/driver/sqlite3/result.rb
baza-0.0.36 lib/baza/driver/sqlite3/result.rb
baza-0.0.35 lib/baza/driver/sqlite3/result.rb
baza-0.0.34 lib/baza/driver/sqlite3/result.rb
baza-0.0.33 lib/baza/driver/sqlite3/result.rb
baza-0.0.32 lib/baza/driver/sqlite3/result.rb
baza-0.0.31 lib/baza/driver/sqlite3/result.rb
baza-0.0.30 lib/baza/driver/sqlite3/result.rb
baza-0.0.29 lib/baza/driver/sqlite3/result.rb
baza-0.0.28 lib/baza/driver/sqlite3/result.rb
baza-0.0.27 lib/baza/driver/sqlite3/result.rb
baza-0.0.26 lib/baza/driver/sqlite3/result.rb
baza-0.0.25 lib/baza/driver/sqlite3/result.rb
baza-0.0.24 lib/baza/driver/sqlite3/result.rb
baza-0.0.23 lib/baza/driver/sqlite3/result.rb
baza-0.0.22 lib/baza/driver/sqlite3/result.rb
baza-0.0.21 lib/baza/driver/sqlite3/result.rb