Class: RDBI::Cursor
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, NotRewindableError
Instance Attribute Summary collapse
-
#handle ⇒ Object
readonly
underlying handle.
-
#rewindable_result ⇒ Object
Returns the value of attribute rewindable_result.
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.
34 35 36 |
# File 'lib/rdbi/cursor.rb', line 34 def initialize(handle) @handle = handle end |
Instance Attribute Details
#handle ⇒ Object (readonly)
underlying handle.
29 30 31 |
# File 'lib/rdbi/cursor.rb', line 29 def handle @handle end |
#rewindable_result ⇒ Object
Returns the value of attribute rewindable_result.
30 31 32 |
# File 'lib/rdbi/cursor.rb', line 30 def rewindable_result @rewindable_result end |
Instance Method Details
#[](index) ⇒ Object
Fetches the tuple at position index
.
65 |
# File 'lib/rdbi/cursor.rb', line 65 def [](index); raise NotImplementedError, 'Subclasses must implement this method'; end |
#affected_count ⇒ Object
Returns the number of affected rows (DML) in this result.
46 |
# File 'lib/rdbi/cursor.rb', line 46 def affected_count; raise NotImplementedError, 'Subclasses must implement this method'; end |
#all ⇒ Object
Returns all the tuples.
59 |
# File 'lib/rdbi/cursor.rb', line 59 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.
88 89 |
# File 'lib/rdbi/cursor.rb', line 88 def coerce_to_array end |
#each ⇒ Object
Enumerable helper. Iterate over each item and yield it to a block.
92 93 94 |
# File 'lib/rdbi/cursor.rb', line 92 def each yield next_row until last_row? end |
#empty? ⇒ Boolean
Is this result empty?
71 |
# File 'lib/rdbi/cursor.rb', line 71 def empty?; raise NotImplementedError, 'Subclasses must implement this method'; end |
#fetch(count = 1) ⇒ Object
Fetches count
tuples from the result and returns them.
62 |
# File 'lib/rdbi/cursor.rb', line 62 def fetch(count=1); raise NotImplementedError, 'Subclasses must implement this method'; end |
#finish ⇒ Object
Finish this cursor and schedule it for termination.
82 83 |
# File 'lib/rdbi/cursor.rb', line 82 def finish end |
#first ⇒ Object
Returns the first tuple in the result.
49 |
# File 'lib/rdbi/cursor.rb', line 49 def first; raise NotImplementedError, 'Subclasses must implement this method'; end |
#last ⇒ Object
Returns the last tuple in the result.
52 |
# File 'lib/rdbi/cursor.rb', line 52 def last; raise NotImplementedError, 'Subclasses must implement this method'; end |
#last_row? ⇒ Boolean
Are we on the last row?
68 |
# File 'lib/rdbi/cursor.rb', line 68 def last_row?; raise NotImplementedError, 'Subclasses must implement this method'; end |
#next_row ⇒ Object
Returns the next row in the result.
40 |
# File 'lib/rdbi/cursor.rb', line 40 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.
56 |
# File 'lib/rdbi/cursor.rb', line 56 def rest; raise NotImplementedError, 'Subclasses must implement this method'; end |
#result_count ⇒ Object
Returns the count of rows that exist in this result.
43 |
# File 'lib/rdbi/cursor.rb', line 43 def result_count; raise NotImplementedError, 'Subclasses must implement this method'; end |
#rewind ⇒ Object
rewind the result to start again from the top.
74 |
# File 'lib/rdbi/cursor.rb', line 74 def rewind; raise NotImplementedError, 'Subclasses must implement this method'; end |
#size ⇒ Object
See result_count().
77 78 79 |
# File 'lib/rdbi/cursor.rb', line 77 def size result_count end |