Class: SqlPostgres::Cursor
- Inherits:
-
Object
- Object
- SqlPostgres::Cursor
- Defined in:
- lib/sqlpostgres/Cursor.rb
Overview
This class creates and manages a cursor.
Example: ** example: cursor
Transaction.new(connection) do
select = Select.new(connection)
select.select('i')
select.from('foo')
Cursor.new('my_cursor', select, {}, connection) do |cursor|
while !(rows = cursor.fetch).empty?
for row in rows
p row # {"i"=>0}
# {"i"=>1}
# {"i"=>2}
# {"i"=>3}
# {"i"=>4}
end
end
end
end
**
Fetching a single row at a time, the default for fetch, is slow. Usually you will want to speed things up by calling, for example, fetch(1000).
Instance Method Summary collapse
-
#close ⇒ Object
Close the cursor.
-
#fetch(direction = 'NEXT') ⇒ Object
Fetch a row or rows from the cursor.
-
#initialize(name, select, opts = {}, connection = Connection.default) ⇒ Cursor
constructor
Create a cursor.
-
#move(direction = 'NEXT') ⇒ Object
Seek a cursor.
Constructor Details
#initialize(name, select, opts = {}, connection = Connection.default) ⇒ Cursor
Create a cursor. If a block is given, yield the cursor to the block, closing the cursor when the block returns, and returning results of the block.
If no connection is given, then the default connection is used.
- name
-
The cursor’s name
- select
-
a Select statement
- opts
-
Options hash. Keys are:
:scroll=>(boolean) If true, create a SCROLL cursor. If false, create a NO SCROLL cursor. If not specified, the default is to allow scrolling in cases where performance will not suffer. :hold=>(boolean) If true, create a WITH HOLD cursor. If false, create a HOLD cursor. WITHOUT HOLD is the default. A HOLD cursor can be used outside of the transaction that created it.
- connection
-
The database connection
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/sqlpostgres/Cursor.rb', line 54 def initialize(name, select, opts = {}, connection = Connection.default) if block_given? cursor = self.class.new(name, select, opts, connection) result = yield(cursor) cursor.close result else @name = name @select = select @opts = opts @connection = connection declare_cursor end end |
Instance Method Details
#close ⇒ Object
Close the cursor. Once closed, it may not be closed or fetched from again.
107 108 109 110 |
# File 'lib/sqlpostgres/Cursor.rb', line 107 def close statement = "close #{@name}" @connection.exec(statement) end |
#fetch(direction = 'NEXT') ⇒ Object
Fetch a row or rows from the cursor.
- direction
-
A string specifying which row or rows to fetch. See the postgres documentation for the “DECLARE” statement.
NEXT PRIOR FIRST LAST ABSOLUTE count RELATIVE count count ALL FORWARD FORWARD count FORWARD ALL BACKWARD BACKWARD count BACKWARD ALL
89 90 91 |
# File 'lib/sqlpostgres/Cursor.rb', line 89 def fetch(direction = 'NEXT') @select.fetch_by_cursor(@name, direction, @connection) end |
#move(direction = 'NEXT') ⇒ Object
Seek a cursor. Works exactly the same (and takes the same arguments) as fetch, but returns no rows.
- direction
-
See #fetch
99 100 101 102 |
# File 'lib/sqlpostgres/Cursor.rb', line 99 def move(direction = 'NEXT') statement = "move #{direction} from #{@name}" @connection.exec(statement) end |