| 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.
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.
# File lib/sqlite3/resultset.rb, line 169
169: def close
170: @stmt.close
171: end
Queries whether the underlying statement has been closed or not.
# File lib/sqlite3/resultset.rb, line 174
174: def closed?
175: @stmt.closed?
176: end
Returns the names of the columns returned by this result set.
# File lib/sqlite3/resultset.rb, line 184
184: def columns
185: @stmt.columns
186: end
Required by the Enumerable mixin. Provides an internal iterator over the rows of the result set.
# File lib/sqlite3/resultset.rb, line 160
160: def each
161: while row=self.next
162: yield row
163: end
164: end
Query whether the cursor has reached the end of the result set or not.
# File lib/sqlite3/resultset.rb, line 92
92: def eof?
93: @eof
94: end
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.
# File lib/sqlite3/resultset.rb, line 109
109: def next
110: return nil if @eof
111:
112: @stmt.must_be_open!
113:
114: unless @first_row
115: result = @driver.step( @stmt.handle )
116: check result
117: end
118:
119: @first_row = false
120:
121: unless @eof
122: row = []
123: @driver.data_count( @stmt.handle ).times do |column|
124: case @driver.column_type( @stmt.handle, column )
125: when Constants::ColumnType::NULL then
126: row << nil
127: when Constants::ColumnType::BLOB then
128: row << @driver.column_blob( @stmt.handle, column )
129: else
130: row << @driver.column_text( @stmt.handle, column )
131: end
132: end
133:
134: if @db.type_translation
135: row = @stmt.types.zip( row ).map do |type, value|
136: @db.translator.translate( type, value )
137: end
138: end
139:
140: if @db.results_as_hash
141: new_row = Hash[ *( @stmt.columns.zip( row ).flatten ) ]
142: row.each_with_index { |value,idx| new_row[idx] = value }
143: row = new_row
144: else
145: row.extend FieldsContainer unless row.respond_to?(:fields)
146: row.fields = @stmt.columns
147: end
148:
149: row.extend TypesContainer
150: row.types = @stmt.types
151:
152: return row
153: end
154:
155: nil
156: end
Reset the cursor, so that a result set which has reached end-of-file can be rewound and reiterated.
# File lib/sqlite3/resultset.rb, line 82
82: def reset( *bind_params )
83: @stmt.must_be_open!
84: @stmt.reset!(false)
85: @driver.reset( @stmt.handle )
86: @stmt.bind_params( *bind_params )
87: @eof = false
88: commence
89: end