@@ -470,6 +470,51 @@ static void rb_mysql_row_query_options(VALUE opts, ID *db_timezone, ID *app_time
470
470
}
471
471
}
472
472
473
+ static VALUE rb_mysql_result_element (VALUE self , VALUE seek ) {
474
+ VALUE row , opts ;
475
+ ID db_timezone , app_timezone ;
476
+ long offset ;
477
+ int symbolizeKeys = 0 , asArray = 0 , castBool = 0 , cacheRows = 1 , cast = 1 , streaming = 0 ;
478
+ mysql2_result_wrapper * wrapper ;
479
+
480
+ GetMysql2Result (self , wrapper );
481
+
482
+ offset = NUM2LONG (seek );
483
+
484
+ if (!wrapper -> numberOfRows ) {
485
+ wrapper -> numberOfRows = mysql_num_rows (wrapper -> result );
486
+ }
487
+
488
+ opts = rb_iv_get (self , "@query_options" );
489
+ rb_mysql_row_query_options (opts , & db_timezone , & app_timezone , & symbolizeKeys , & asArray , & castBool , & cast , & streaming , & cacheRows );
490
+
491
+ if (streaming ) {
492
+ rb_raise (cMysql2Error , "Element reference operator #[] cannot be used in streaming mode." );
493
+ }
494
+
495
+ /* count back from the end if passed a negative number */
496
+ if (offset < 0 ) {
497
+ offset = wrapper -> numberOfRows + offset ;
498
+ }
499
+
500
+ /* negative offset was too big */
501
+ if (offset < 0 ) {
502
+ return Qnil ;
503
+ /* rb_raise(cMysql2Error, "Out of range: offset %ld is beyond %lu rows (offset begins at 0).", offset, wrapper->numberOfRows); */
504
+ }
505
+
506
+ if (wrapper -> numberOfRows <= (unsigned long )offset ) {
507
+ return Qnil ;
508
+ /* rb_raise(cMysql2Error, "Out of range: offset %ld is beyond %lu rows (offset begins at 0).", offset, wrapper->numberOfRows); */
509
+ }
510
+
511
+ mysql_data_seek (wrapper -> result , offset );
512
+
513
+ row = rb_mysql_result_fetch_row (self , db_timezone , app_timezone , symbolizeKeys , asArray , castBool , cast );
514
+
515
+ return row ;
516
+ }
517
+
473
518
static VALUE rb_mysql_result_each (int argc , VALUE * argv , VALUE self ) {
474
519
VALUE defaults , opts , block ;
475
520
ID db_timezone , app_timezone ;
@@ -613,6 +658,7 @@ void init_mysql2_result() {
613
658
cDateTime = rb_const_get (rb_cObject , rb_intern ("DateTime" ));
614
659
615
660
cMysql2Result = rb_define_class_under (mMysql2 , "Result" , rb_cObject );
661
+ rb_define_method (cMysql2Result , "[]" , rb_mysql_result_element , 1 );
616
662
rb_define_method (cMysql2Result , "each" , rb_mysql_result_each , -1 );
617
663
rb_define_method (cMysql2Result , "fields" , rb_mysql_result_fetch_fields , 0 );
618
664
rb_define_method (cMysql2Result , "count" , rb_mysql_result_count , 0 );
0 commit comments