Class: RR::ResultFetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyrep/proxy_connection.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, options) ⇒ ResultFetcher

Creates a new fetcher.

  • connection: the current ProxyConnection

  • options: hash of select options as described under ProxyConnection#select_cursor



33
34
35
36
# File 'lib/rubyrep/proxy_connection.rb', line 33

def initialize(connection, options)
  self.connection = connection
  self.options = options.clone
end

Instance Attribute Details

#connectionObject

The current database ProxyConnection



16
17
18
# File 'lib/rubyrep/proxy_connection.rb', line 16

def connection
  @connection
end

#current_row_indexObject

Index to the current row in rows



28
29
30
# File 'lib/rubyrep/proxy_connection.rb', line 28

def current_row_index
  @current_row_index
end

#last_rowObject

column_name => value hash of the last returned row



22
23
24
# File 'lib/rubyrep/proxy_connection.rb', line 22

def last_row
  @last_row
end

#optionsObject

hash of select options as described under ProxyConnection#select_cursor



19
20
21
# File 'lib/rubyrep/proxy_connection.rb', line 19

def options
  @options
end

#rowsObject

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



25
26
27
# File 'lib/rubyrep/proxy_connection.rb', line 25

def rows
  @rows
end

Instance Method Details

#clearObject

Frees up all ressources



106
107
108
# File 'lib/rubyrep/proxy_connection.rb', line 106

def clear
  self.rows = nil
end

#next?Boolean

Returns true if there are more rows to read.

Returns:

  • (Boolean)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# 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_rowObject

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



93
94
95
96
97
98
99
100
101
102
103
# 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