|
145 | 145 | context "streaming result" do |
146 | 146 | it "should be able to stream query result" do |
147 | 147 | n = 1 |
148 | | - stmt = @client.prepare("SELECT 1 UNION SELECT 2") |
149 | | - stmt.query_options.merge!({:stream => true, :cache_rows => false, :as => :array}) |
150 | | - |
| 148 | + stmt = @client.prepare("SELECT 1 UNION SELECT 2", :stream => true, :cache_rows => false, :as => :array) |
151 | 149 | stmt.execute.each do |r| |
152 | 150 | case n |
153 | 151 | when 1 |
|
167 | 165 | # The drawback of this is that args of Result#each is ignored... |
168 | 166 |
|
169 | 167 | it "should yield rows as hash's" do |
170 | | - @result = @client.prepare("SELECT 1").execute |
171 | | - @result.each do |row| |
| 168 | + result = @client.prepare("SELECT 1").execute |
| 169 | + result.each do |row| |
172 | 170 | expect(row.class).to eql(Hash) |
173 | 171 | end |
174 | 172 | end |
175 | 173 |
|
176 | 174 | it "should yield rows as hash's with symbol keys if :symbolize_keys was set to true" do |
177 | | - @client.query_options[:symbolize_keys] = true |
178 | | - @result = @client.prepare("SELECT 1").execute |
179 | | - @result.each do |row| |
| 175 | + result = @client.prepare("SELECT 1", :symbolize_keys => true).execute |
| 176 | + result.each do |row| |
180 | 177 | expect(row.keys.first.class).to eql(Symbol) |
181 | 178 | end |
182 | | - @client.query_options[:symbolize_keys] = false |
183 | 179 | end |
184 | 180 |
|
185 | | - it "should be able to return results as an array" do |
186 | | - @client.query_options[:as] = :array |
| 181 | + it "should be able to return results as a hash" do |
| 182 | + result = @client.prepare("SELECT 1", :as => :hash).execute |
| 183 | + result.each do |row| |
| 184 | + expect(row.class).to eql(Hash) |
| 185 | + end |
| 186 | + end |
187 | 187 |
|
188 | | - @result = @client.prepare("SELECT 1").execute |
189 | | - @result.each do |row| |
| 188 | + it "should be able to return results as an array" do |
| 189 | + result = @client.prepare("SELECT 1", :as => :array).execute |
| 190 | + result.each do |row| |
190 | 191 | expect(row.class).to eql(Array) |
191 | 192 | end |
192 | | - |
193 | | - @client.query_options[:as] = :hash |
194 | 193 | end |
195 | 194 |
|
196 | 195 | it "should cache previously yielded results by default" do |
197 | | - @result = @client.prepare("SELECT 1").execute |
198 | | - expect(@result.first.object_id).to eql(@result.first.object_id) |
| 196 | + result = @client.prepare("SELECT 1").execute |
| 197 | + expect(result.first.object_id).to eql(result.first.object_id) |
199 | 198 | end |
200 | 199 |
|
201 | 200 | it "should yield different value for #first if streaming" do |
202 | | - @client.query_options[:stream] = true |
203 | | - @client.query_options[:cache_rows] = false |
204 | | - |
205 | | - result = @client.prepare("SELECT 1 UNION SELECT 2").execute |
| 201 | + result = @client.prepare("SELECT 1 UNION SELECT 2", :stream => true, :cache_rows => false).execute |
206 | 202 | expect(result.first).not_to eql(result.first) |
207 | | - |
208 | | - @client.query_options[:stream] = false |
209 | | - @client.query_options[:cache_rows] = true |
210 | 203 | end |
211 | 204 |
|
212 | 205 | it "should yield the same value for #first if streaming is disabled" do |
213 | | - @client.query_options[:stream] = false |
214 | | - result = @client.prepare("SELECT 1 UNION SELECT 2").execute |
| 206 | + result = @client.prepare("SELECT 1 UNION SELECT 2", :stream => false).execute |
215 | 207 | expect(result.first).to eql(result.first) |
216 | 208 | end |
217 | 209 |
|
218 | 210 | it "should throw an exception if we try to iterate twice when streaming is enabled" do |
219 | | - @client.query_options[:stream] = true |
220 | | - @client.query_options[:cache_rows] = false |
221 | | - |
222 | | - result = @client.prepare("SELECT 1 UNION SELECT 2").execute |
| 211 | + result = @client.prepare("SELECT 1 UNION SELECT 2", :stream => true, :cache_rows => false).execute |
223 | 212 |
|
224 | 213 | expect { |
225 | 214 | result.each {} |
226 | 215 | result.each {} |
227 | 216 | }.to raise_exception(Mysql2::Error) |
228 | | - |
229 | | - @client.query_options[:stream] = false |
230 | | - @client.query_options[:cache_rows] = true |
231 | 217 | end |
232 | 218 | end |
233 | 219 |
|
|
0 commit comments