Class: SQLite3::ResultSet

Class SQLite3::ResultSet |
In: | lib/sqlite3/resultset.rb
Parent: | Object

The ResultSet object encapsulates the enumerability of a query’s output. It is a simple cursor over the data that the query returns. It will very rarely (if ever) be instantiated directly. Instead, client’s should obtain a ResultSet instance via Statement#execute.

Methods

- close
- closed?
- columns
- each
- eof?
- new
- next
- reset
- types

Included Modules

- Enumerable

Classes and Modules
Module SQLite3::ResultSet::FieldsContainer
Module SQLite3::ResultSet::TypesContainer
Public Class methods
new( db, stmt )

Create a new ResultSet attached to the given database, using the given sql text.

[ show source ]

        # File lib/sqlite3/resultset.rb, line 57
    57:     def initialize( db, stmt )
    58:       @db = db
    59:       @driver = @db.driver
    60:       @stmt = stmt
    61:       commence
    62:     end

Public Instance methods
close()

Closes the statement that spawned this result set. Use with caution! Closing a result set will automatically close any other result sets that were spawned from the same statement.

[ show source ]

         # File lib/sqlite3/resultset.rb, line 168
    168:     def close
    169:       @stmt.close
    170:     end

closed?()

Queries whether the underlying statement has been closed or not.

[ show source ]

         # File lib/sqlite3/resultset.rb, line 173
    173:     def closed?
    174:       @stmt.closed?
    175:     end

columns()

Returns the names of the columns returned by this result set.

[ show source ]

         # File lib/sqlite3/resultset.rb, line 183
    183:     def columns
    184:       @stmt.columns
    185:     end

each() {|row| ...}

Required by the Enumerable mixin. Provides an internal iterator over the rows of the result set.

[ show source ]

         # File lib/sqlite3/resultset.rb, line 159
    159:     def each
    160:       while row=self.next
    161:         yield row
    162:       end
    163:     end

eof?()

Query whether the cursor has reached the end of the result set or not.

[ show source ]

        # File lib/sqlite3/resultset.rb, line 91
    91:     def eof?
    92:       @eof
    93:     end

next()

Obtain the next row from the cursor. If there are no more rows to be had, this will return `nil`. If type translation is active on the corresponding database, the values in the row will be translated according to their types.

The returned value will be an array, unless Database#results_as_hash has been set to `true`, in which case the returned value will be a hash.

For arrays, the column names are accessible via the `fields` property, and the column types are accessible via the `types` property.

For hashes, the column names are the keys of the hash, and the column types are accessible via the `types` property.

[ show source ]

         # File lib/sqlite3/resultset.rb, line 108
    108:     def next
    109:       return nil if @eof
    110:
    111:       @stmt.must_be_open!
    112:
    113:       unless @first_row
    114:         result = @driver.step( @stmt.handle )
    115:         check result
    116:       end
    117:
    118:       @first_row = false
    119:
    120:       unless @eof
    121:         row = []
    122:         @driver.data_count( @stmt.handle ).times do |column|
    123:           case @driver.column_type( @stmt.handle, column )
    124:             when Constants::ColumnType::NULL then
    125:               row << nil
    126:             when Constants::ColumnType::BLOB then
    127:               row << @driver.column_blob( @stmt.handle, column )
    128:             else
    129:               row << @driver.column_text( @stmt.handle, column )
    130:           end
    131:         end
    132:
    133:         if @db.type_translation
    134:           row = @stmt.types.zip( row ).map do |type, value|
    135:             @db.translator.translate( type, value )
    136:           end
    137:         end
    138:
    139:         if @db.results_as_hash
    140:           new_row = Hash[ *( @stmt.columns.zip( row ).flatten ) ]
    141:           row.each_with_index { |value,idx| new_row[idx] = value }
    142:           row = new_row
    143:         else
    144:           row.extend FieldsContainer unless row.respond_to?(:fields)
    145:           row.fields = @stmt.columns
    146:         end
    147:
    148:         row.extend TypesContainer
    149:         row.types = @stmt.types
    150:
    151:         return row
    152:       end
    153:
    154:       nil
    155:     end

reset( *bind_params )

Reset the cursor, so that a result set which has reached end-of-file can be rewound and reiterated.

[ show source ]

        # File lib/sqlite3/resultset.rb, line 82
    82:     def reset( *bind_params )
    83:       @stmt.must_be_open!
    84:       @driver.reset( @stmt.handle )
    85:       @stmt.bind_params( *bind_params )
    86:       @eof = false
    87:       commence
    88:     end

types()

Returns the types of the columns returned by this result set.

[ show source ]

         # File lib/sqlite3/resultset.rb, line 178
    178:     def types
    179:       @stmt.types
    180:     end

[Validate]