Module: PostgreSQLCursor::ActiveRecord::SqlCursor
- Defined in:
- lib/postgresql_cursor/active_record/sql_cursor.rb
Instance Method Summary collapse
-
#each_instance(options = {}, &block) ⇒ Object
Public: Like each_row, but returns an instantiated model object to the block.
-
#each_instance_batch(options = {}, &block) ⇒ Object
Public: Like each_row_batch, but yields an array of instantiated model objects to the block.
-
#each_instance_batch_by_sql(sql, options = {}, &block) ⇒ Object
Public: Yields each batch up to block_size of rows as model instances to the given block.
-
#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.
-
#each_row(options = {}, &block) ⇒ Object
(also: #each_hash)
Public: Executes the query, returning each row as a hash to the given block.
-
#each_row_batch(options = {}, &block) ⇒ Object
(also: #each_hash_batch)
Public: Executes the query, yielding an array of up to block_size rows where each row is a hash to the given block.
-
#each_row_batch_by_sql(sql, options = {}, &block) ⇒ Object
(also: #each_hash_batch_by_sql)
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.
-
#each_row_by_sql(sql, options = {}, &block) ⇒ Object
(also: #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.
-
#pluck_instances(*cols) ⇒ Object
(also: #pluck_instance)
Returns an array of the given column names.
-
#pluck_rows(*cols) ⇒ Object
(also: #pluck_row)
Returns an array of the given column names.
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(={}, &block) = {:connection => self.connection}.merge() all.each_instance(, &block) end |
#each_instance_batch(options = {}, &block) ⇒ Object
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
101 102 103 104 |
# File 'lib/postgresql_cursor/active_record/sql_cursor.rb', line 101 def each_instance_batch(={}, &block) = {:connection => self.connection}.merge() all.each_instance_batch(, &block) end |
#each_instance_batch_by_sql(sql, options = {}, &block) ⇒ Object
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
144 145 146 147 148 149 |
# File 'lib/postgresql_cursor/active_record/sql_cursor.rb', line 144 def each_instance_batch_by_sql(sql, ={}, &block) = {:connection => self.connection}.merge() cursor = PostgreSQLCursor::Cursor.new(sql, ) return cursor.each_instance_batch(self, &block) if block_given? cursor.iterate_type(self).iterate_batched 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, ={}, &block) = {:connection => self.connection}.merge() cursor = PostgreSQLCursor::Cursor.new(sql, ) 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(={}, &block) = {:connection => self.connection}.merge() all.each_row(, &block) end |
#each_row_batch(options = {}, &block) ⇒ Object Also known as: each_hash_batch
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
86 87 88 89 |
# File 'lib/postgresql_cursor/active_record/sql_cursor.rb', line 86 def each_row_batch(={}, &block) = {:connection => self.connection}.merge() all.each_row_batch(, &block) end |
#each_row_batch_by_sql(sql, options = {}, &block) ⇒ Object Also known as: each_hash_batch_by_sql
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
120 121 122 123 124 125 |
# File 'lib/postgresql_cursor/active_record/sql_cursor.rb', line 120 def each_row_batch_by_sql(sql, ={}, &block) = {:connection => self.connection}.merge() cursor = PostgreSQLCursor::Cursor.new(sql, ) return cursor.each_row_batch(&block) if block_given? cursor.iterate_batched 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, ={}, &block) = {:connection => self.connection}.merge() cursor = PostgreSQLCursor::Cursor.new(sql, ) return cursor.each_row(&block) if block_given? cursor end |
#pluck_instances(*cols) ⇒ Object Also known as: pluck_instance
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.
161 162 163 164 |
# File 'lib/postgresql_cursor/active_record/sql_cursor.rb', line 161 def pluck_instances(*cols) = cols.last.is_a?(Hash) ? cols.pop : {} all.each_instance().pluck(*cols) end |
#pluck_rows(*cols) ⇒ Object Also known as: pluck_row
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.
153 154 155 156 |
# File 'lib/postgresql_cursor/active_record/sql_cursor.rb', line 153 def pluck_rows(*cols) = cols.last.is_a?(Hash) ? cols.pop : {} all.each_row().pluck(*cols) end |