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



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

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



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

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.

Example:

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

Returns the number of rows yielded to the block



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

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.

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



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

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.



85
86
87
88
# File 'lib/postgresql_cursor/active_record/sql_cursor.rb', line 85

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.



77
78
79
80
# File 'lib/postgresql_cursor/active_record/sql_cursor.rb', line 77

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