Class: TinyTds::Result

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/tiny_tds/result.rb,
ext/tiny_tds/result.c

Instance Method Summary collapse

Instance Method Details

#affected_rowsObject



645
646
647
648
649
650
651
652
653
654
# File 'ext/tiny_tds/result.c', line 645

static VALUE rb_tinytds_result_affected_rows(VALUE self)
{
  GET_RESULT_WRAPPER(self);

  if (rwrap->client) {
    return LONG2NUM((long)dbcount(rwrap->client));
  } else {
    return Qnil;
  }
}

#cancelObject



617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
# File 'ext/tiny_tds/result.c', line 617

static VALUE rb_tinytds_result_cancel(VALUE self)
{
  tinytds_client_userdata *userdata;
  GET_RESULT_WRAPPER(self);
  userdata = (tinytds_client_userdata *)dbgetuserdata(rwrap->client);

  if (rwrap->client && !userdata->dbcancel_sent) {
    rb_tinytds_result_ok_helper(rwrap->client);
    dbcancel(rwrap->client);
    userdata->dbcancel_sent = 1;
    userdata->dbsql_sent = 0;
  }

  return Qtrue;
}

#doObject



633
634
635
636
637
638
639
640
641
642
643
# File 'ext/tiny_tds/result.c', line 633

static VALUE rb_tinytds_result_do(VALUE self)
{
  GET_RESULT_WRAPPER(self);

  if (rwrap->client) {
    rb_tinytds_result_exec_helper(rwrap->client);
    return LONG2NUM((long)dbcount(rwrap->client));
  } else {
    return Qnil;
  }
}

#each(*args) ⇒ Object



486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
# File 'ext/tiny_tds/result.c', line 486

static VALUE rb_tinytds_result_each(int argc, VALUE * argv, VALUE self)
{
  /* Local Vars */
  VALUE qopts, opts, block;
  ID timezone;
  int symbolize_keys = 0, as_array = 0, cache_rows = 0, first = 0, empty_sets = 0;
  tinytds_client_userdata *userdata;
  GET_RESULT_WRAPPER(self);
  userdata = (tinytds_client_userdata *)dbgetuserdata(rwrap->client);
  /* Merge Options Hash To Query Options. Populate Opts & Block Var. */
  qopts = rb_iv_get(self, "@query_options");

  if (rb_scan_args(argc, argv, "01&", &opts, &block) == 1) {
    qopts = rb_funcall(qopts, intern_merge, 1, opts);
  }

  rb_iv_set(self, "@query_options", qopts);

  /* Locals From Options */
  if (rb_hash_aref(qopts, sym_first) == Qtrue) {
    first = 1;
  }

  if (rb_hash_aref(qopts, sym_symbolize_keys) == Qtrue) {
    symbolize_keys = 1;
  }

  if (rb_hash_aref(qopts, sym_as) == sym_array) {
    as_array = 1;
  }

  if (rb_hash_aref(qopts, sym_cache_rows) == Qtrue) {
    cache_rows = 1;
  }

  if (rb_hash_aref(qopts, sym_timezone) == sym_local) {
    timezone = intern_local;
  } else if (rb_hash_aref(qopts, sym_timezone) == sym_utc) {
    timezone = intern_utc;
  } else {
    rb_warn(":timezone option must be :utc or :local - defaulting to :local");
    timezone = intern_local;
  }

  if (rb_hash_aref(qopts, sym_empty_sets) == Qtrue) {
    empty_sets = 1;
  }

  /* Make The Results Or Yield Existing */
  if (NIL_P(rwrap->results)) {
    RETCODE dbsqlok_rc, dbresults_rc;
    rwrap->results = rb_ary_new();
    dbsqlok_rc = rb_tinytds_result_ok_helper(rwrap->client);
    dbresults_rc = rb_tinytds_result_dbresults_retcode(self);

    while ((dbsqlok_rc == SUCCEED) && (dbresults_rc == SUCCEED)) {
      int has_rows = (DBROWS(rwrap->client) == SUCCEED) ? 1 : 0;

      if (has_rows || empty_sets || (rwrap->number_of_results == 0)) {
        rb_tinytds_result_fields(self);
      }

      if ((has_rows || empty_sets) && rwrap->number_of_fields > 0) {
        /* Create rows for this result set. */
        unsigned long rowi = 0;
        VALUE result = rb_ary_new();

        while (nogvl_dbnextrow(rwrap->client) != NO_MORE_ROWS) {
          VALUE row = rb_tinytds_result_fetch_row(self, timezone, symbolize_keys, as_array);

          if (cache_rows) {
            rb_ary_store(result, rowi, row);
          }

          if (!NIL_P(block)) {
            rb_yield(row);
          }

          if (first) {
            dbcanquery(rwrap->client);
            userdata->dbcancel_sent = 1;
          }

          rowi++;
        }

        rwrap->number_of_rows = rowi;

        /* Store the result. */
        if (cache_rows) {
          if (rwrap->number_of_results == 0) {
            rwrap->results = result;
          } else if (rwrap->number_of_results == 1) {
            VALUE multi_resultsets = rb_ary_new();
            rb_ary_store(multi_resultsets, 0, rwrap->results);
            rb_ary_store(multi_resultsets, 1, result);
            rwrap->results = multi_resultsets;
          } else {
            rb_ary_store(rwrap->results, rwrap->number_of_results, result);
          }
        }

        // If we find results increment the counter that helpers use and setup the next loop.
        rwrap->number_of_results = rwrap->number_of_results + 1;
        dbresults_rc = rb_tinytds_result_dbresults_retcode(self);
        rb_ary_store(rwrap->fields_processed, rwrap->number_of_results, Qnil);
      } else {
        // If we do not find results, side step the rb_tinytds_result_dbresults_retcode helper and
        // manually populate its memoized array while nullifing any memoized fields too before loop.
        dbresults_rc = nogvl_dbresults(rwrap->client);
        rb_ary_store(rwrap->dbresults_retcodes, rwrap->number_of_results, INT2FIX(dbresults_rc));
        rb_ary_store(rwrap->fields_processed, rwrap->number_of_results, Qnil);
      }
    }

    if (dbresults_rc == FAIL) {
      rb_warn("TinyTDS: Something in the dbresults() while loop set the return code to FAIL.\n");
    }

    userdata->dbsql_sent = 0;
  } else if (!NIL_P(block)) {
    unsigned long i;

    for (i = 0; i < rwrap->number_of_rows; i++) {
      rb_yield(rb_ary_entry(rwrap->results, i));
    }
  }

  return rwrap->results;
}

#fieldsObject

TinyTds::Client (public)



435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
# File 'ext/tiny_tds/result.c', line 435

static VALUE rb_tinytds_result_fields(VALUE self)
{
  RETCODE dbsqlok_rc, dbresults_rc;
  VALUE fields_processed;
  GET_RESULT_WRAPPER(self);
  dbsqlok_rc = rb_tinytds_result_ok_helper(rwrap->client);
  dbresults_rc = rb_tinytds_result_dbresults_retcode(self);
  fields_processed = rb_ary_entry(rwrap->fields_processed, rwrap->number_of_results);

  if ((dbsqlok_rc == SUCCEED) && (dbresults_rc == SUCCEED) && (fields_processed == Qnil)) {
    /* Default query options. */
    int symbolize_keys = 0;
    VALUE qopts = rb_iv_get(self, "@query_options");

    if (rb_hash_aref(qopts, sym_symbolize_keys) == Qtrue) {
      symbolize_keys = 1;
    }

    /* Set number_of_fields count for this result set. */
    rwrap->number_of_fields = dbnumcols(rwrap->client);

    if (rwrap->number_of_fields > 0) {
      /* Create fields for this result set. */
      unsigned int fldi = 0;
      VALUE fields = rb_ary_new2(rwrap->number_of_fields);

      for (fldi = 0; fldi < rwrap->number_of_fields; fldi++) {
        char *colname = dbcolname(rwrap->client, fldi+1);
        VALUE field = symbolize_keys ? rb_str_intern(ENCODED_STR_NEW2(colname))  : rb_obj_freeze(ENCODED_STR_NEW2(colname));
        rb_ary_store(fields, fldi, field);
      }

      /* Store the fields. */
      if (rwrap->number_of_results == 0) {
        rwrap->fields = fields;
      } else if (rwrap->number_of_results == 1) {
        VALUE multi_rs_fields = rb_ary_new();
        rb_ary_store(multi_rs_fields, 0, rwrap->fields);
        rb_ary_store(multi_rs_fields, 1, fields);
        rwrap->fields = multi_rs_fields;
      } else {
        rb_ary_store(rwrap->fields, rwrap->number_of_results, fields);
      }
    }

    rb_ary_store(rwrap->fields_processed, rwrap->number_of_results, Qtrue);
  }

  return rwrap->fields;
}

#insertObject



668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
# File 'ext/tiny_tds/result.c', line 668

static VALUE rb_tinytds_result_insert(VALUE self)
{
  GET_RESULT_WRAPPER(self);

  if (rwrap->client) {
    VALUE identity = Qnil;
    rb_tinytds_result_exec_helper(rwrap->client);
    dbcmd(rwrap->client, rwrap->cwrap->identity_insert_sql);

    if (nogvl_dbsqlexec(rwrap->client) != FAIL
        && nogvl_dbresults(rwrap->client) != FAIL
        && DBROWS(rwrap->client) != FAIL) {
      while (nogvl_dbnextrow(rwrap->client) != NO_MORE_ROWS) {
        int col = 1;
        BYTE *data = dbdata(rwrap->client, col);
        DBINT data_len = dbdatlen(rwrap->client, col);
        int null_val = ((data == NULL) && (data_len == 0));

        if (!null_val) {
          identity = LL2NUM(*(DBBIGINT *)data);
        }
      }
    }

    return identity;
  } else {
    return Qnil;
  }
}

#return_codeObject

Duplicated in client.c



657
658
659
660
661
662
663
664
665
666
# File 'ext/tiny_tds/result.c', line 657

static VALUE rb_tinytds_result_return_code(VALUE self)
{
  GET_RESULT_WRAPPER(self);

  if (rwrap->client && dbhasretstat(rwrap->client)) {
    return LONG2NUM((long)dbretstatus(rwrap->client));
  } else {
    return Qnil;
  }
}