Class: RDBI::Cursor

Inherits:
Object
  • Object
show all
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, NotRewindableError

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#handleObject (readonly)

underlying handle.



29
30
31
# File 'lib/rdbi/cursor.rb', line 29

def handle
  @handle
end

#rewindable_resultObject

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_countObject

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

#allObject

Returns all the tuples.



59
# File 'lib/rdbi/cursor.rb', line 59

def all; raise NotImplementedError, 'Subclasses must implement this method'; end

#coerce_to_arrayObject

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

#eachObject

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?

Returns:

  • (Boolean)

Raises:



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

#finishObject

Finish this cursor and schedule it for termination.



82
83
# File 'lib/rdbi/cursor.rb', line 82

def finish
end

#firstObject

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

#lastObject

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?

Returns:

  • (Boolean)

Raises:



68
# File 'lib/rdbi/cursor.rb', line 68

def last_row?; raise NotImplementedError, 'Subclasses must implement this method'; end

#next_rowObject

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

#restObject

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_countObject

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

#rewindObject

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

#sizeObject

See result_count().



77
78
79
# File 'lib/rdbi/cursor.rb', line 77

def size
  result_count
end