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.
- Enumerable
Class SQLite3::ResultSet::ArrayWithTypesAndFields
Class SQLite3::ResultSet::HashWithTypes
[ show source ]
# File lib/sqlite3/resultset.rb, line 34
34: def initialize( db, stmt )
35: @db = db
36: @driver = @db.driver
37: @stmt = stmt
38: commence
39: end
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 155
155: def close
156: @stmt.close
157: end
Queries whether the underlying statement has been closed or not.
[ show source ]
# File lib/sqlite3/resultset.rb, line 160
160: def closed?
161: @stmt.closed?
162: end
Returns the names of the columns returned by this result set.
[ show source ]
# File lib/sqlite3/resultset.rb, line 170
170: def columns
171: @stmt.columns
172: end
Required by the Enumerable mixin. Provides an internal iterator over the rows of the result set.
[ show source ]
# File lib/sqlite3/resultset.rb, line 146
146: def each
147: while row=self.next
148: yield row
149: end
150: end
Query whether the cursor has reached the end of the result set or not.
[ show source ]
# File lib/sqlite3/resultset.rb, line 72
72: def eof?
73: @eof
74: 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.
[ show source ]
# File lib/sqlite3/resultset.rb, line 89
89: def next
90: return nil if @eof
91:
92: @stmt.must_be_open!
93:
94: unless @first_row
95: result = @driver.step( @stmt.handle )
96: check result
97: end
98:
99: @first_row = false
100:
101: unless @eof
102: row = []
103: @driver.data_count( @stmt.handle ).times do |column|
104: type = @driver.column_type( @stmt.handle, column )
105:
106: if type == Constants::ColumnType::TEXT
107: row << @driver.column_text( @stmt.handle, column )
108: elsif type == Constants::ColumnType::NULL
109: row << nil
110: elsif type == Constants::ColumnType::BLOB
111: row << @driver.column_blob( @stmt.handle, column )
112: else
113: row << @driver.column_text( @stmt.handle, column )
114: end
115: end
116:
117: if @db.type_translation
118: row = @stmt.types.zip( row ).map do |type, value|
119: @db.translator.translate( type, value )
120: end
121: end
122:
123: if @db.results_as_hash
124: new_row = HashWithTypes[ *( @stmt.columns.zip( row ).to_a.flatten ) ]
125: row.each_with_index { |value,idx| new_row[idx] = value }
126: row = new_row
127: else
128: if row.respond_to?(:fields)
129: row = ArrayWithTypes.new(row)
130: else
131: row = ArrayWithTypesAndFields.new(row)
132: end
133: row.fields = @stmt.columns
134: end
135:
136: row.types = @stmt.types
137:
138: return row
139: end
140:
141: nil
142: end
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 62
62: def reset( *bind_params )
63: @stmt.must_be_open!
64: @stmt.reset!(false)
65: @driver.reset( @stmt.handle )
66: @stmt.bind_params( *bind_params )
67: @eof = false
68: commence
69: end
[ show source ]
# File lib/sqlite3/resultset.rb, line 165
165: def types
166: @stmt.types
167: end