|
148 | 148 | end
|
149 | 149 | end
|
150 | 150 |
|
| 151 | + context "#each" do |
| 152 | + # note: The current impl. of prepared statement requires results to be cached on #execute except for streaming queries |
| 153 | + # The drawback of this is that args of Result#each is ignored... |
| 154 | + |
| 155 | + it "should yield rows as hash's" do |
| 156 | + @result = @client.prepare("SELECT 1").execute |
| 157 | + @result.each do |row| |
| 158 | + row.class.should eql(Hash) |
| 159 | + end |
| 160 | + end |
| 161 | + |
| 162 | + it "should yield rows as hash's with symbol keys if :symbolize_keys was set to true" do |
| 163 | + @client.query_options[:symbolize_keys] = true |
| 164 | + @result = @client.prepare("SELECT 1").execute |
| 165 | + @result.each do |row| |
| 166 | + row.keys.first.class.should eql(Symbol) |
| 167 | + end |
| 168 | + @client.query_options[:symbolize_keys] = false |
| 169 | + end |
| 170 | + |
| 171 | + it "should be able to return results as an array" do |
| 172 | + @client.query_options[:as] = :array |
| 173 | + |
| 174 | + @result = @client.prepare("SELECT 1").execute |
| 175 | + @result.each do |row| |
| 176 | + row.class.should eql(Array) |
| 177 | + end |
| 178 | + |
| 179 | + @client.query_options[:as] = :hash |
| 180 | + end |
| 181 | + |
| 182 | + it "should cache previously yielded results by default" do |
| 183 | + @result = @client.prepare("SELECT 1").execute |
| 184 | + @result.first.object_id.should eql(@result.first.object_id) |
| 185 | + end |
| 186 | + |
| 187 | + it "should yield different value for #first if streaming" do |
| 188 | + @client.query_options[:stream] = true |
| 189 | + @client.query_options[:cache_rows] = false |
| 190 | + |
| 191 | + result = @client.prepare("SELECT 1 UNION SELECT 2").execute |
| 192 | + result.first.should_not eql(result.first) |
| 193 | + |
| 194 | + @client.query_options[:stream] = false |
| 195 | + @client.query_options[:cache_rows] = true |
| 196 | + end |
| 197 | + |
| 198 | + it "should yield the same value for #first if streaming is disabled" do |
| 199 | + @client.query_options[:stream] = false |
| 200 | + result = @client.prepare("SELECT 1 UNION SELECT 2").execute |
| 201 | + result.first.should eql(result.first) |
| 202 | + end |
| 203 | + |
| 204 | + it "should throw an exception if we try to iterate twice when streaming is enabled" do |
| 205 | + @client.query_options[:stream] = true |
| 206 | + @client.query_options[:cache_rows] = false |
| 207 | + |
| 208 | + result = @client.prepare("SELECT 1 UNION SELECT 2").execute |
| 209 | + |
| 210 | + expect { |
| 211 | + result.each {} |
| 212 | + result.each {} |
| 213 | + }.to raise_exception(Mysql2::Error) |
| 214 | + |
| 215 | + @client.query_options[:stream] = false |
| 216 | + @client.query_options[:cache_rows] = true |
| 217 | + end |
| 218 | + end |
| 219 | + |
| 220 | + context "#fields" do |
| 221 | + before(:each) do |
| 222 | + @client.query "USE test" |
| 223 | + @test_result = @client.prepare("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").execute |
| 224 | + end |
| 225 | + |
| 226 | + it "method should exist" do |
| 227 | + @test_result.should respond_to(:fields) |
| 228 | + end |
| 229 | + |
| 230 | + it "should return an array of field names in proper order" do |
| 231 | + result = @client.prepare("SELECT 'a', 'b', 'c'").execute |
| 232 | + result.fields.should eql(['a', 'b', 'c']) |
| 233 | + end |
| 234 | + end |
| 235 | + |
151 | 236 | context "row data type mapping" do
|
152 | 237 | before(:each) do
|
153 | 238 | @client.query "USE test"
|
|
0 commit comments