class RR::ResultFetcher

Enables the fetching of (potential large) result sets in chunks.

Attributes

connection[RW]

The current database ProxyConnection

current_row_index[RW]

Index to the current row in rows

last_row[RW]

column_name => value hash of the last returned row

options[RW]

hash of select options as described under ProxyConnection#select_cursor

rows[RW]

The current row set: an array of column_name => value hashes

Public Class Methods

new(connection, options) click to toggle source

Creates a new fetcher.

# File lib/rubyrep/proxy_connection.rb, line 33
def initialize(connection, options)
  self.connection = connection
  self.options = options.clone
end

Public Instance Methods

clear() click to toggle source

Frees up all ressources

# File lib/rubyrep/proxy_connection.rb, line 106
def clear
  self.rows = nil
end
next?() click to toggle source

Returns true if there are more rows to read.

# File lib/rubyrep/proxy_connection.rb, line 39
def next?
  unless self.rows
    # Try to load some records
    
    if options[:query] and last_row != nil
      # A query was directly specified and all it's rows were returned
      # ==> Finished.
      return false
    end

    if options[:query]
      # If a query has been directly specified, just directly execute it
      query = options[:query]
    else
      # Otherwise build the query
      if last_row
        # There was a previous batch.
        # Next batch will start after the last returned row
        options.merge! :from => last_row, :exclude_starting_row => true
      end

      query = connection.table_select_query(options[:table], options)

      if options[:row_buffer_size]
        # Set the batch size
        query += " limit #{options[:row_buffer_size]}"
      end
    end

    self.rows = connection.select_all query

    ###############
    # Below approach can only be used starting with activerecord version 5.
    # And as of 2017-05-18 jruby (i.e. version 9.1.7.0) supports only up to activerecord version 4.2.
    ###############
    # result = connection.select_all(query)
    # column_types = result.column_types
    # columns = result.columns
    # identity_type = ActiveRecord::Type::Value.new
    # types = columns.map { |name| column_types.fetch(name, identity_type) }
    # self.rows = result.rows.map do |values|
    #   row = {}
    #   columns.zip(types, values).each do |name, type, value|
    #     row[name] = type.deserialize(value)
    #   end
    #   row
    # end

    self.current_row_index = 0
  end
  self.current_row_index < self.rows.length
end
next_row() click to toggle source

Returns the row as a column => value hash and moves the cursor to the next row.

# File lib/rubyrep/proxy_connection.rb, line 93
def next_row
  raise("no more rows available") unless next?
  self.last_row = self.rows[self.current_row_index]
  self.current_row_index += 1

  if self.current_row_index == self.rows.length
    self.rows = nil
  end

  self.last_row
end