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
Module SQLite3::ResultSet::TypesContainer
[ show source ]
# File lib/sqlite3/resultset.rb, line 25
25: def initialize( db, stmt )
26: @db = db
27: @driver = @db.driver
28: @stmt = stmt
29: commence
30: 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 143
143: def close
144: @stmt.close
145: end
Queries whether the underlying statement has been closed or not.
[ show source ]
# File lib/sqlite3/resultset.rb, line 148
148: def closed?
149: @stmt.closed?
150: end
Returns the names of the columns returned by this result set.
[ show source ]
# File lib/sqlite3/resultset.rb, line 158
158: def columns
159: @stmt.columns
160: 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 134
134: def each
135: while row=self.next
136: yield row
137: end
138: end
Query whether the cursor has reached the end of the result set or not.
[ show source ]
# File lib/sqlite3/resultset.rb, line 63
63: def eof?
64: @eof
65: 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 80
80: def next
81: return nil if @eof
82:
83: @stmt.must_be_open!
84:
85: unless @first_row
86: result = @driver.step( @stmt.handle )
87: check result
88: end
89:
90: @first_row = false
91:
92: unless @eof
93: row = []
94: @driver.data_count( @stmt.handle ).times do |column|
95: type = @driver.column_type( @stmt.handle, column )
96:
97: if type == Constants::ColumnType::TEXT
98: row << @driver.column_text( @stmt.handle, column )
99: elsif type == Constants::ColumnType::NULL
100: row << nil
101: elsif type == Constants::ColumnType::BLOB
102: row << @driver.column_blob( @stmt.handle, column )
103: else
104: row << @driver.column_text( @stmt.handle, column )
105: end
106: end
107:
108: if @db.type_translation
109: row = @stmt.types.zip( row ).map do |type, value|
110: @db.translator.translate( type, value )
111: end
112: end
113:
114: if @db.results_as_hash
115: new_row = Hash[ *( @stmt.columns.zip( row ).flatten ) ]
116: row.each_with_index { |value,idx| new_row[idx] = value }
117: row = new_row
118: else
119: row.extend FieldsContainer unless row.respond_to?(:fields)
120: row.fields = @stmt.columns
121: end
122:
123: row.extend TypesContainer
124: row.types = @stmt.types
125:
126: return row
127: end
128:
129: nil
130: 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 53
53: def reset( *bind_params )
54: @stmt.must_be_open!
55: @stmt.reset!(false)
56: @driver.reset( @stmt.handle )
57: @stmt.bind_params( *bind_params )
58: @eof = false
59: commence
60: end
[ show source ]
# File lib/sqlite3/resultset.rb, line 153
153: def types
154: @stmt.types
155: end