Class: ActiveCursor

Inherits:
Object
  • Object
show all
Defined in:
lib/active_cursor.rb,
lib/active_cursor/version.rb

Defined Under Namespace

Modules: QueryMethods

Constant Summary collapse

VERSION =
"0.1.0"

Instance Method Summary collapse

Constructor Details

#initialize(relation, batch_size: 1_000) ⇒ ActiveCursor

Returns a new instance of ActiveCursor.



13
14
15
16
# File 'lib/active_cursor.rb', line 13

def initialize(relation, batch_size: 1_000)
  @relation = relation
  @batch_size = batch_size
end

Instance Method Details

#each(&block) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/active_cursor.rb', line 18

def each(&block)
  return enum_for(__method__) unless block_given?

  iterate do |name|
    records = model.find_by_sql("FETCH #{batch_size} FROM #{name}")
    records.each(&block)
    records.length
  end
end

#each_row(&block) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/active_cursor.rb', line 28

def each_row(&block)
  return enum_for(__method__) unless block_given?

  iterate do |name|
    result = connection.execute("FETCH #{batch_size} FROM #{name}")
    result.each(&block)
    result.ntuples
  end
end

#each_tuple(&block) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/active_cursor.rb', line 38

def each_tuple(&block)
  return enum_for(__method__) unless block_given?

  iterate do |name|
    result = connection.execute("FETCH #{batch_size} FROM #{name}")
    result.each_row(&block)
    result.ntuples
  end
end