@@ -86,6 +86,20 @@ def stmt_count
86
86
expect ( result . to_a ) . to eq ( [ 'max1' => int64_max1 , 'max2' => int64_max2 , 'max3' => int64_max3 , 'min1' => int64_min1 , 'min2' => int64_min2 , 'min3' => int64_min3 ] )
87
87
end
88
88
89
+ it "should accept keyword arguments on statement execute" do
90
+ stmt = @client . prepare 'SELECT 1 AS a'
91
+
92
+ expect ( stmt . execute ( as : :hash ) . first ) . to eq ( "a" => 1 )
93
+ expect ( stmt . execute ( as : :array ) . first ) . to eq ( [ 1 ] )
94
+ end
95
+
96
+ it "should accept bind arguments and keyword arguments on statement execute" do
97
+ stmt = @client . prepare 'SELECT ? AS a'
98
+
99
+ expect ( stmt . execute ( 1 , as : :hash ) . first ) . to eq ( "a" => 1 )
100
+ expect ( stmt . execute ( 1 , as : :array ) . first ) . to eq ( [ 1 ] )
101
+ end
102
+
89
103
it "should keep its result after other query" do
90
104
@client . query 'USE test'
91
105
@client . query 'CREATE TABLE IF NOT EXISTS mysql2_stmt_q(a int)'
@@ -186,10 +200,9 @@ def stmt_count
186
200
end
187
201
188
202
it "should warn but still work if cache_rows is set to false" do
189
- @client . query_options [ :cache_rows ] = false
190
203
statement = @client . prepare 'SELECT 1'
191
204
result = nil
192
- expect { result = statement . execute . to_a } . to output ( /:cache_rows is forced for prepared statements/ ) . to_stderr
205
+ expect { result = statement . execute ( cache_rows : false ) . to_a } . to output ( /:cache_rows is forced for prepared statements/ ) . to_stderr
193
206
expect ( result . length ) . to eq ( 1 )
194
207
end
195
208
@@ -238,10 +251,7 @@ def stmt_count
238
251
it "should be able to stream query result" do
239
252
n = 1
240
253
stmt = @client . prepare ( "SELECT 1 UNION SELECT 2" )
241
-
242
- @client . query_options . merge! ( stream : true , cache_rows : false , as : :array )
243
-
244
- stmt . execute . each do |r |
254
+ stmt . execute ( stream : true , cache_rows : false , as : :array ) . each do |r |
245
255
case n
246
256
when 1
247
257
expect ( r ) . to eq ( [ 1 ] )
@@ -267,23 +277,17 @@ def stmt_count
267
277
end
268
278
269
279
it "should yield rows as hash's with symbol keys if :symbolize_keys was set to true" do
270
- @client . query_options [ :symbolize_keys ] = true
271
- @result = @client . prepare ( "SELECT 1" ) . execute
280
+ @result = @client . prepare ( "SELECT 1" ) . execute ( symbolize_keys : true )
272
281
@result . each do |row |
273
282
expect ( row . keys . first ) . to be_an_instance_of ( Symbol )
274
283
end
275
- @client . query_options [ :symbolize_keys ] = false
276
284
end
277
285
278
286
it "should be able to return results as an array" do
279
- @client . query_options [ :as ] = :array
280
-
281
- @result = @client . prepare ( "SELECT 1" ) . execute
287
+ @result = @client . prepare ( "SELECT 1" ) . execute ( as : :array )
282
288
@result . each do |row |
283
289
expect ( row ) . to be_an_instance_of ( Array )
284
290
end
285
-
286
- @client . query_options [ :as ] = :hash
287
291
end
288
292
289
293
it "should cache previously yielded results by default" do
@@ -292,35 +296,21 @@ def stmt_count
292
296
end
293
297
294
298
it "should yield different value for #first if streaming" do
295
- @client . query_options [ :stream ] = true
296
- @client . query_options [ :cache_rows ] = false
297
-
298
- result = @client . prepare ( "SELECT 1 UNION SELECT 2" ) . execute
299
+ result = @client . prepare ( "SELECT 1 UNION SELECT 2" ) . execute ( stream : true , cache_rows : true )
299
300
expect ( result . first ) . not_to eql ( result . first )
300
-
301
- @client . query_options [ :stream ] = false
302
- @client . query_options [ :cache_rows ] = true
303
301
end
304
302
305
303
it "should yield the same value for #first if streaming is disabled" do
306
- @client . query_options [ :stream ] = false
307
- result = @client . prepare ( "SELECT 1 UNION SELECT 2" ) . execute
304
+ result = @client . prepare ( "SELECT 1 UNION SELECT 2" ) . execute ( stream : false )
308
305
expect ( result . first ) . to eql ( result . first )
309
306
end
310
307
311
308
it "should throw an exception if we try to iterate twice when streaming is enabled" do
312
- @client . query_options [ :stream ] = true
313
- @client . query_options [ :cache_rows ] = false
314
-
315
- result = @client . prepare ( "SELECT 1 UNION SELECT 2" ) . execute
316
-
309
+ result = @client . prepare ( "SELECT 1 UNION SELECT 2" ) . execute ( stream : true , cache_rows : false )
317
310
expect do
318
311
result . each { }
319
312
result . each { }
320
313
end . to raise_exception ( Mysql2 ::Error )
321
-
322
- @client . query_options [ :stream ] = false
323
- @client . query_options [ :cache_rows ] = true
324
314
end
325
315
end
326
316
@@ -369,21 +359,20 @@ def stmt_count
369
359
370
360
context "cast booleans for TINYINT if :cast_booleans is enabled" do
371
361
# rubocop:disable Style/Semicolon
372
- let ( :client ) { new_client ( cast_booleans : true ) }
373
- let ( :id1 ) { client . query 'INSERT INTO mysql2_test (bool_cast_test) VALUES ( 1)' ; client . last_id }
374
- let ( :id2 ) { client . query 'INSERT INTO mysql2_test (bool_cast_test) VALUES ( 0)' ; client . last_id }
375
- let ( :id3 ) { client . query 'INSERT INTO mysql2_test (bool_cast_test) VALUES (-1)' ; client . last_id }
362
+ let ( :id1 ) { @client . query 'INSERT INTO mysql2_test (bool_cast_test) VALUES ( 1)' ; @client . last_id }
363
+ let ( :id2 ) { @client . query 'INSERT INTO mysql2_test (bool_cast_test) VALUES ( 0)' ; @client . last_id }
364
+ let ( :id3 ) { @client . query 'INSERT INTO mysql2_test (bool_cast_test) VALUES (-1)' ; @client . last_id }
376
365
# rubocop:enable Style/Semicolon
377
366
378
367
after do
379
- client . query "DELETE from mysql2_test WHERE id IN(#{ id1 } ,#{ id2 } ,#{ id3 } )"
368
+ @ client. query "DELETE from mysql2_test WHERE id IN(#{ id1 } ,#{ id2 } ,#{ id3 } )"
380
369
end
381
370
382
371
it "should return TrueClass or FalseClass for a TINYINT value if :cast_booleans is enabled" do
383
- query = client . prepare 'SELECT bool_cast_test FROM mysql2_test WHERE id = ?'
384
- result1 = query . execute id1
385
- result2 = query . execute id2
386
- result3 = query . execute id3
372
+ query = @ client. prepare 'SELECT bool_cast_test FROM mysql2_test WHERE id = ?'
373
+ result1 = query . execute id1 , cast_booleans : true
374
+ result2 = query . execute id2 , cast_booleans : true
375
+ result3 = query . execute id3 , cast_booleans : true
387
376
expect ( result1 . first [ 'bool_cast_test' ] ) . to be true
388
377
expect ( result2 . first [ 'bool_cast_test' ] ) . to be false
389
378
expect ( result3 . first [ 'bool_cast_test' ] ) . to be true
@@ -392,19 +381,18 @@ def stmt_count
392
381
393
382
context "cast booleans for BIT(1) if :cast_booleans is enabled" do
394
383
# rubocop:disable Style/Semicolon
395
- let ( :client ) { new_client ( cast_booleans : true ) }
396
- let ( :id1 ) { client . query 'INSERT INTO mysql2_test (single_bit_test) VALUES (1)' ; client . last_id }
397
- let ( :id2 ) { client . query 'INSERT INTO mysql2_test (single_bit_test) VALUES (0)' ; client . last_id }
384
+ let ( :id1 ) { @client . query 'INSERT INTO mysql2_test (single_bit_test) VALUES (1)' ; @client . last_id }
385
+ let ( :id2 ) { @client . query 'INSERT INTO mysql2_test (single_bit_test) VALUES (0)' ; @client . last_id }
398
386
# rubocop:enable Style/Semicolon
399
387
400
388
after do
401
- client . query "DELETE from mysql2_test WHERE id IN(#{ id1 } ,#{ id2 } )"
389
+ @ client. query "DELETE from mysql2_test WHERE id IN(#{ id1 } ,#{ id2 } )"
402
390
end
403
391
404
392
it "should return TrueClass or FalseClass for a BIT(1) value if :cast_booleans is enabled" do
405
- query = client . prepare 'SELECT single_bit_test FROM mysql2_test WHERE id = ?'
406
- result1 = query . execute id1
407
- result2 = query . execute id2
393
+ query = @ client. prepare 'SELECT single_bit_test FROM mysql2_test WHERE id = ?'
394
+ result1 = query . execute id1 , cast_booleans : true
395
+ result2 = query . execute id2 , cast_booleans : true
408
396
expect ( result1 . first [ 'single_bit_test' ] ) . to be true
409
397
expect ( result2 . first [ 'single_bit_test' ] ) . to be false
410
398
end
0 commit comments