Class: RDBI::Cursor
- Inherits:
-
Object
- Object
- RDBI::Cursor
- Extended by:
- MethLab
- Includes:
- Enumerable
- Defined in:
- lib/rdbi/cursor.rb
Overview
RDBI::Cursor is a method of abstractly encapsulating result handles that we get back from databases. It has a consistent interface and therefore can be used by RDBI::Result and its drivers.
Drivers should make a whole-hearted attempt to do iterative fetching instead of array fetching.. this will perform much better for larger results.
RDBI::Cursor is largely an abstract class and will error if methods are not implemented in an inheriting class. Please read the individual method documentation for what each call should yield.
Defined Under Namespace
Classes: NotImplementedError
Instance Attribute Summary collapse
-
#handle ⇒ Object
readonly
underlying handle.
Instance Method Summary collapse
-
#[](index) ⇒ Object
Fetches the tuple at position
index
. -
#affected_count ⇒ Object
Returns the number of affected rows (DML) in this result.
-
#all ⇒ Object
Returns all the tuples.
-
#coerce_to_array ⇒ Object
If your result handles cannot support operation disconnected from the statement, you will want to implement this method to fetch all values in certain situations.
-
#each ⇒ Object
Enumerable helper.
-
#empty? ⇒ Boolean
Is this result empty?.
-
#fetch(count = 1) ⇒ Object
Fetches
count
tuples from the result and returns them. -
#finish ⇒ Object
Finish this cursor and schedule it for termination.
-
#first ⇒ Object
Returns the first tuple in the result.
-
#initialize(handle) ⇒ Cursor
constructor
Default constructor.
-
#last ⇒ Object
Returns the last tuple in the result.
-
#last_row? ⇒ Boolean
Are we on the last row?.
-
#next_row ⇒ Object
Returns the next row in the result.
-
#rest ⇒ Object
Returns the items that have not been fetched yet in this result.
-
#result_count ⇒ Object
Returns the count of rows that exist in this result.
-
#rewind ⇒ Object
rewind the result to start again from the top.
-
#size ⇒ Object
See result_count().
Constructor Details
#initialize(handle) ⇒ Cursor
Default constructor. Feel free to override this.
29 30 31 |
# File 'lib/rdbi/cursor.rb', line 29 def initialize(handle) @handle = handle end |
Instance Attribute Details
#handle ⇒ Object (readonly)
underlying handle.
25 26 27 |
# File 'lib/rdbi/cursor.rb', line 25 def handle @handle end |
Instance Method Details
#[](index) ⇒ Object
Fetches the tuple at position index
.
60 |
# File 'lib/rdbi/cursor.rb', line 60 def [](index); raise NotImplementedError, 'Subclasses must implement this method'; end |
#affected_count ⇒ Object
Returns the number of affected rows (DML) in this result.
41 |
# File 'lib/rdbi/cursor.rb', line 41 def affected_count; raise NotImplementedError, 'Subclasses must implement this method'; end |
#all ⇒ Object
Returns all the tuples.
54 |
# File 'lib/rdbi/cursor.rb', line 54 def all; raise NotImplementedError, 'Subclasses must implement this method'; end |
#coerce_to_array ⇒ Object
If your result handles cannot support operation disconnected from the statement, you will want to implement this method to fetch all values in certain situations.
83 84 |
# File 'lib/rdbi/cursor.rb', line 83 def coerce_to_array end |
#each ⇒ Object
Enumerable helper. Iterate over each item and yield it to a block.
87 88 89 |
# File 'lib/rdbi/cursor.rb', line 87 def each yield next_row until last_row? end |
#empty? ⇒ Boolean
Is this result empty?
66 |
# File 'lib/rdbi/cursor.rb', line 66 def empty?; raise NotImplementedError, 'Subclasses must implement this method'; end |
#fetch(count = 1) ⇒ Object
Fetches count
tuples from the result and returns them.
57 |
# File 'lib/rdbi/cursor.rb', line 57 def fetch(count=1); raise NotImplementedError, 'Subclasses must implement this method'; end |
#finish ⇒ Object
Finish this cursor and schedule it for termination.
77 78 |
# File 'lib/rdbi/cursor.rb', line 77 def finish end |
#first ⇒ Object
Returns the first tuple in the result.
44 |
# File 'lib/rdbi/cursor.rb', line 44 def first; raise NotImplementedError, 'Subclasses must implement this method'; end |
#last ⇒ Object
Returns the last tuple in the result.
47 |
# File 'lib/rdbi/cursor.rb', line 47 def last; raise NotImplementedError, 'Subclasses must implement this method'; end |
#last_row? ⇒ Boolean
Are we on the last row?
63 |
# File 'lib/rdbi/cursor.rb', line 63 def last_row?; raise NotImplementedError, 'Subclasses must implement this method'; end |
#next_row ⇒ Object
Returns the next row in the result.
35 |
# File 'lib/rdbi/cursor.rb', line 35 def next_row; raise NotImplementedError, 'Subclasses must implement this method'; end |
#rest ⇒ Object
Returns the items that have not been fetched yet in this result. Equivalent to all() if the fetched count is zero.
51 |
# File 'lib/rdbi/cursor.rb', line 51 def rest; raise NotImplementedError, 'Subclasses must implement this method'; end |
#result_count ⇒ Object
Returns the count of rows that exist in this result.
38 |
# File 'lib/rdbi/cursor.rb', line 38 def result_count; raise NotImplementedError, 'Subclasses must implement this method'; end |
#rewind ⇒ Object
rewind the result to start again from the top.
69 |
# File 'lib/rdbi/cursor.rb', line 69 def rewind; raise NotImplementedError, 'Subclasses must implement this method'; end |
#size ⇒ Object
See result_count().
72 73 74 |
# File 'lib/rdbi/cursor.rb', line 72 def size result_count end |