Class: RDBI::Cursor

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

Instance Method Summary collapse

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

#handleObject (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_countObject

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

#allObject

Returns all the tuples.



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

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.



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

def coerce_to_array
end

#eachObject

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?

Returns:

  • (Boolean)

Raises:



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

#finishObject

Finish this cursor and schedule it for termination.



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

def finish
end

#firstObject

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

#lastObject

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?

Returns:

  • (Boolean)

Raises:



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

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

#next_rowObject

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

#restObject

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_countObject

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

#rewindObject

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

#sizeObject

See result_count().



72
73
74
# File 'lib/rdbi/cursor.rb', line 72

def size
  result_count
end