module PostgreSQLCursor::ActiveRecord::SqlCursor

Public Instance Methods

each_hash(options={}, &block)
Alias for: each_row
each_hash_batch(options={}, &block)
Alias for: each_row_batch
each_hash_batch_by_sql(sql, options={}, &block)
each_hash_by_sql(sql, options={}, &block)
Alias for: each_row_by_sql
each_instance(options={}, &block) click to toggle source

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

# 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_batch(options={}, &block) click to toggle source

Public: Like each_row_batch, but yields an array of instantiated model objects to the block

Parameters: same as each_row

Example:

Post.each_instance_batch { |batch| Post.process_batch(batch) }

Returns the number of rows yielded to the block

# File lib/postgresql_cursor/active_record/sql_cursor.rb, line 101
def each_instance_batch(options={}, &block)
  options = {:connection => self.connection}.merge(options)
  all.each_instance_batch(options, &block)
end
each_instance_batch_by_sql(sql, options={}, &block) click to toggle source

Public: Yields each batch up to block_size of rows as model instances to the given block

As this instantiates a model object, it is slower than each_row_batch_by_sql

Paramaters: see each_row_by_sql

Example:

Post.each_instance_batch_by_sql("select * from posts") do |batch|
  Post.process_batch(batch)
end
Post.each_instance_batch_by_sql("select * from posts").map do |batch|
  Post.transform_batch(batch)
end

Returns the number of rows yielded to the block

# File lib/postgresql_cursor/active_record/sql_cursor.rb, line 144
def each_instance_batch_by_sql(sql, options={}, &block)
  options = {:connection => self.connection}.merge(options)
  cursor  = PostgreSQLCursor::Cursor.new(sql, options)
  return cursor.each_instance_batch(self, &block) if block_given?
  cursor.iterate_type(self).iterate_batched
end
each_instance_by_sql(sql, options={}, &block) click to toggle source

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

# 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) click to toggle source

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

# 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
Also aliased as: each_hash
each_row_batch(options={}, &block) click to toggle source

Public: Executes the query, yielding an array of up to block_size rows where each row is a hash to the given block.

Parameters: same as each_row

Example:

Post.each_row_batch { |batch| Post.process_batch(batch) }

Returns the number of rows yielded to the block

# File lib/postgresql_cursor/active_record/sql_cursor.rb, line 86
def each_row_batch(options={}, &block)
  options = {:connection => self.connection}.merge(options)
  all.each_row_batch(options, &block)
end
Also aliased as: each_hash_batch
each_row_batch_by_sql(sql, options={}, &block) click to toggle source

Public: Yields each batch of up to block_size rows as an array of rows where each row as a hash to the given block

Parameters: see each_row_by_sql

Example:

Post.each_row_batch_by_sql("select * from posts") do |batch|
  Post.process_batch(batch)
end
Post.each_row_batch_by_sql("select * from posts").map do |batch|
  Post.transform_batch(batch)
end

Returns the number of rows yielded to the block

# File lib/postgresql_cursor/active_record/sql_cursor.rb, line 120
def each_row_batch_by_sql(sql, options={}, &block)
  options = {:connection => self.connection}.merge(options)
  cursor  = PostgreSQLCursor::Cursor.new(sql, options)
  return cursor.each_row_batch(&block) if block_given?
  cursor.iterate_batched
end
Also aliased as: each_hash_batch_by_sql
each_row_by_sql(sql, options={}, &block) click to toggle source

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

# 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
Also aliased as: each_hash_by_sql
pluck_instance(*cols)
Alias for: pluck_instances
pluck_instances(*cols) click to toggle source

Returns an 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.

# File lib/postgresql_cursor/active_record/sql_cursor.rb, line 161
def pluck_instances(*cols)
  options = cols.last.is_a?(Hash) ? cols.pop : {}
  all.each_instance(options).pluck(*cols)
end
Also aliased as: pluck_instance
pluck_row(*cols)
Alias for: pluck_rows
pluck_rows(*cols) click to toggle source

Returns an 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.

# File lib/postgresql_cursor/active_record/sql_cursor.rb, line 153
def pluck_rows(*cols)
  options = cols.last.is_a?(Hash) ? cols.pop : {}
  all.each_row(options).pluck(*cols)
end
Also aliased as: pluck_row