Module: PostgreSQLCursor::ActiveRecord::SqlCursor

Defined in:
lib/postgresql_cursor/active_record/sql_cursor.rb

Instance Method Summary collapse

Instance Method Details

#each_instance(options = {}, &block) ⇒ Object

Public: Like each_row, but returns an instantiated model object to the block

Paramaters: same as each_row

Example:

Post.each_instance { |post| post.process }

Returns the number of rows yielded to the block



32
33
34
35
# File 'lib/postgresql_cursor/active_record/sql_cursor.rb', line 32

def each_instance(options={}, &block)
  options = {:connection => self.connection}.merge(options)
  all.each_instance(options, &block)
end

#each_instance_by_sql(sql, options = {}, &block) ⇒ Object

Public: Returns each row as a model instance to the given block As this instantiates a model object, it is slower than each_row_by_sql

Paramaters: see each_row_by_sql

Example:

Post.each_instance_by_sql("select * from posts") { |post| post.process }
Post.each_instance_by_sql("select * from posts").count

Returns the number of rows yielded to the block



70
71
72
73
74
75
# File 'lib/postgresql_cursor/active_record/sql_cursor.rb', line 70

def each_instance_by_sql(sql, options={}, &block)
  options = {:connection => self.connection}.merge(options)
  cursor  = PostgreSQLCursor::Cursor.new(sql, options)
  return cursor.each_instance(self, &block) if block_given?
  cursor.iterate_type(self)
end

#each_row(options = {}, &block) ⇒ Object Also known as: each_hash

Public: Executes the query, returning each row as a hash to the given block.

options - Hash to control

fraction: 0.1..1.0    - The cursor_tuple_fraction (default 1.0)
block_size: 1..n      - The number of rows to fetch per db block fetch
while: value          - Exits loop when block does not return this value.
until: value          - Exits loop when block returns this value.
with_hold: boolean    - Allows the query to remain open across commit points.

Example:

Post.each_row { |hash| Post.process(hash) }

Returns the number of rows yielded to the block



18
19
20
21
# File 'lib/postgresql_cursor/active_record/sql_cursor.rb', line 18

def each_row(options={}, &block)
  options = {:connection => self.connection}.merge(options)
  all.each_row(options, &block)
end

#each_row_by_sql(sql, options = {}, &block) ⇒ Object Also known as: each_hash_by_sql

sql - Full SQL statement, variables interpolated options - Hash to control

fraction: 0.1..1.0    - The cursor_tuple_fraction (default 1.0)
block_size: 1..n      - The number of rows to fetch per db block fetch
while: value          - Exits loop when block does not return this value.
until: value          - Exits loop when block returns this value.
with_hold: boolean    - Allows the query to remain open across commit points.

Example:

Post.each_row_by_sql("select * from posts") { |hash| Post.process(hash) }
Post.each_row_by_sql("select * from posts").count

Returns the number of rows yielded to the block



52
53
54
55
56
57
# File 'lib/postgresql_cursor/active_record/sql_cursor.rb', line 52

def each_row_by_sql(sql, options={}, &block)
  options = {:connection => self.connection}.merge(options)
  cursor  = PostgreSQLCursor::Cursor.new(sql, options)
  return cursor.each_row(&block) if block_given?
  cursor
end

#pluck_instances(*cols) ⇒ Object Also known as: pluck_instance

Returns and array of the given column names. Use if you need cursors and don’t expect this to comsume too much memory. Values are instance types. Like ActiveRecord’s pluck.



87
88
89
90
# File 'lib/postgresql_cursor/active_record/sql_cursor.rb', line 87

def pluck_instances(*cols)
  options = cols.last.is_a?(Hash) ? cols.pop : {}
  all.each_instance(options).pluck(*cols)
end

#pluck_rows(*cols) ⇒ Object Also known as: pluck_row

Returns and array of the given column names. Use if you need cursors and don’t expect this to comsume too much memory. Values are strings. Like ActiveRecord’s pluck.



79
80
81
82
# File 'lib/postgresql_cursor/active_record/sql_cursor.rb', line 79

def pluck_rows(*cols)
  options = cols.last.is_a?(Hash) ? cols.pop : {}
  all.each_row(options).pluck(*cols)
end