Class: OraNumber

Inherits:
Numeric show all
Includes:
Comparable
Defined in:
lib/oci8/oci8.rb,
ext/oci8/ocinumber.c

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Numeric

#to_onum

Class Method Details

._load(string) ⇒ Object

Unmarshal a dumped OraNumber object.



1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
# File 'ext/oci8/ocinumber.c', line 1114

static VALUE
onum_s_load(VALUE klass, VALUE str)
{
    unsigned char *c;
    int size;
    OCINumber num;

    Check_Type(str, T_STRING);
    c = RSTRING_ORATEXT(str);
    size = RSTRING_LEN(str);
    if (size == 0 || size != c[0] + 1 || size > sizeof(num)) {
        rb_raise(rb_eTypeError, "marshaled OCI::Number format differ");
    }
    memset(&num, 0, sizeof(num));
    memcpy(&num, c, size);
    return oci8_make_ocinumber(&num, oci8_errhp);
}

Instance Method Details

#%(other) ⇒ Object

Returns the modulo after division of onum by other.



725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
# File 'ext/oci8/ocinumber.c', line 725

static VALUE onum_mod(VALUE lhs, VALUE rhs)
{
    OCIError *errhp = oci8_errhp;
    OCINumber n;
    OCINumber r;
    boolean is_zero;

    /* change to OCINumber */
    if (!set_oci_number_from_num(&n, rhs, 0, errhp))
        return rb_num_coerce_bin(lhs, rhs, '%');
    /* check whether argument is not zero. */
    oci_lc(OCINumberIsZero(errhp, &n, &is_zero));
    if (is_zero)
        rb_num_zerodiv();
    /* modulo */
    oci_lc(OCINumberMod(errhp, _NUMBER(lhs), &n, &r));
    return oci8_make_ocinumber(&r, errhp);
}

#*(other) ⇒ Object

Returns a new OraNumber which is the product of onum and other.



679
680
681
682
683
684
685
686
687
688
689
690
691
# File 'ext/oci8/ocinumber.c', line 679

static VALUE onum_mul(VALUE lhs, VALUE rhs)
{
    OCIError *errhp = oci8_errhp;
    OCINumber n;
    OCINumber r;

    /* change to OCINumber */
    if (!set_oci_number_from_num(&n, rhs, 0, errhp))
        return rb_num_coerce_bin(lhs, rhs, '*');
    /* multiply */
    oci_lc(OCINumberMul(errhp, _NUMBER(lhs), &n, &r));
    return oci8_make_ocinumber(&r, errhp);
}

#**(other) ⇒ Object

Raises onum the other power.



750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
# File 'ext/oci8/ocinumber.c', line 750

static VALUE onum_power(VALUE lhs, VALUE rhs)
{
    OCIError *errhp = oci8_errhp;
    OCINumber n;
    OCINumber r;

    if (FIXNUM_P(rhs)) {
        oci_lc(OCINumberIntPower(errhp, _NUMBER(lhs), FIX2INT(rhs), &r));
    } else {
        /* change to OCINumber */
        if (!set_oci_number_from_num(&n, rhs, 0, errhp))
            return rb_num_coerce_bin(lhs, rhs, id_power);
        oci_lc(OCINumberPower(errhp, _NUMBER(lhs), &n, &r));
    }
    return oci8_make_ocinumber(&r, errhp);
}

#+(other) ⇒ Object

Returns a new OraNumber which is the sum of onum and other.



637
638
639
640
641
642
643
644
645
646
647
648
649
# File 'ext/oci8/ocinumber.c', line 637

static VALUE onum_add(VALUE lhs, VALUE rhs)
{
    OCIError *errhp = oci8_errhp;
    OCINumber n;
    OCINumber r;

    /* change to OCINumber */
    if (!set_oci_number_from_num(&n, rhs, 0, errhp))
        return rb_num_coerce_bin(lhs, rhs, '+');
    /* add */
    oci_lc(OCINumberAdd(errhp, _NUMBER(lhs), &n, &r));
    return oci8_make_ocinumber(&r, errhp);
}

#-(other) ⇒ Object

Returns a new OraNumber which is the difference of onum and other.



658
659
660
661
662
663
664
665
666
667
668
669
670
# File 'ext/oci8/ocinumber.c', line 658

static VALUE onum_sub(VALUE lhs, VALUE rhs)
{
    OCIError *errhp = oci8_errhp;
    OCINumber n;
    OCINumber r;

    /* change to OCINumber */
    if (!set_oci_number_from_num(&n, rhs, 0, errhp))
        return rb_num_coerce_bin(lhs, rhs, '-');
    /* subtracting */
    oci_lc(OCINumberSub(errhp, _NUMBER(lhs), &n, &r));
    return oci8_make_ocinumber(&r, errhp);
}

#-Object

Returns an OraNumber, negated.



621
622
623
624
625
626
627
628
# File 'ext/oci8/ocinumber.c', line 621

static VALUE onum_neg(VALUE self)
{
    OCIError *errhp = oci8_errhp;
    OCINumber r;

    oci_lc(OCINumberNeg(errhp, _NUMBER(self), &r));
    return oci8_make_ocinumber(&r, errhp);
}

#/(other) ⇒ Object

Returns a new OraNumber which is the result of dividing onum by other.



700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
# File 'ext/oci8/ocinumber.c', line 700

static VALUE onum_div(VALUE lhs, VALUE rhs)
{
    OCIError *errhp = oci8_errhp;
    OCINumber n;
    OCINumber r;
    boolean is_zero;

    /* change to OCINumber */
    if (!set_oci_number_from_num(&n, rhs, 0, errhp))
        return rb_num_coerce_bin(lhs, rhs, '/');
    /* check whether argument is not zero. */
    oci_lc(OCINumberIsZero(errhp, &n, &is_zero));
    if (is_zero)
        rb_num_zerodiv();
    /* division */
    oci_lc(OCINumberDiv(errhp, _NUMBER(lhs), &n, &r));
    return oci8_make_ocinumber(&r, errhp);
}

#<=>(other) ⇒ -1, ...

Returns -1, 0, or +1 depending on whether onum is less than, equal to, or greater than other. This is the basis for the tests in Comparable.

Returns:

  • (-1, 0, +1)


775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
# File 'ext/oci8/ocinumber.c', line 775

static VALUE onum_cmp(VALUE lhs, VALUE rhs)
{
    OCIError *errhp = oci8_errhp;
    OCINumber n;
    sword r;

    /* change to OCINumber */
    if (!set_oci_number_from_num(&n, rhs, 0, errhp))
        return rb_num_coerce_cmp(lhs, rhs, id_cmp);
    /* compare */
    oci_lc(OCINumberCmp(errhp, _NUMBER(lhs), &n, &r));
    if (r > 0) {
        return INT2FIX(1);
    } else if (r == 0) {
        return INT2FIX(0);
    } else {
        return INT2FIX(-1);
    }
}

#_dumpString

Dump onum for marshaling.

Returns:



1098
1099
1100
1101
1102
1103
1104
1105
1106
# File 'ext/oci8/ocinumber.c', line 1098

static VALUE onum_dump(int argc, VALUE *argv, VALUE self)
{
    char *c  = DATA_PTR(self);
    int size = c[0] + 1;
    VALUE dummy;

    rb_scan_args(argc, argv, "01", &dummy);
    return rb_str_new(c, size);
}

#absObject

Returns the absolute value of onum.



1038
1039
1040
1041
1042
1043
1044
1045
# File 'ext/oci8/ocinumber.c', line 1038

static VALUE onum_abs(VALUE self)
{
    OCIError *errhp = oci8_errhp;
    OCINumber result;

    oci_lc(OCINumberAbs(errhp, _NUMBER(self), &result));
    return oci8_make_ocinumber(&result, errhp);
}

#ceilInteger

Returns the smallest Integer greater than or equal to onum.

Returns:

  • (Integer)


817
818
819
820
821
822
823
824
# File 'ext/oci8/ocinumber.c', line 817

static VALUE onum_ceil(VALUE self)
{
    OCIError *errhp = oci8_errhp;
    OCINumber r;

    oci_lc(OCINumberCeil(errhp, _NUMBER(self), &r));
    return oci8_make_integer(&r, errhp);
}

#floorInteger

Returns the largest Integer less than or equal to onum.

Returns:

  • (Integer)


801
802
803
804
805
806
807
808
# File 'ext/oci8/ocinumber.c', line 801

static VALUE onum_floor(VALUE self)
{
    OCIError *errhp = oci8_errhp;
    OCINumber r;

    oci_lc(OCINumberFloor(errhp, _NUMBER(self), &r));
    return oci8_make_integer(&r, errhp);
}

#roundInteger #round(decplace) ⇒ Object

Rounds onum to the nearest Integer when no argument. Rounds onum to a specified decimal place decplace when one argument.

OraNumber.new(1.234).round(1)  #=> 1.2
OraNumber.new(1.234).round(2)  #=> 1.23
OraNumber.new(1.234).round(3)  #=> 1.234

Overloads:

  • #roundInteger

    Returns:

    • (Integer)


838
839
840
841
842
843
844
845
846
847
848
849
850
851
# File 'ext/oci8/ocinumber.c', line 838

static VALUE onum_round(int argc, VALUE *argv, VALUE self)
{
    OCIError *errhp = oci8_errhp;
    VALUE decplace;
    OCINumber r;

    rb_scan_args(argc, argv, "01", &decplace /* 0 */);
    oci_lc(OCINumberRound(errhp, _NUMBER(self), NIL_P(decplace) ? 0 : NUM2INT(decplace), &r));
    if (argc == 0) {
        return oci8_make_integer(&r, errhp);
    } else {
        return oci8_make_ocinumber(&r, errhp);
    }
}

#round_prec(digits) ⇒ Object

Rounds onum to a specified number of decimal digits. This method is available on Oracle 8.1 client or upper.

OraNumber.new(1.234).round_prec(2)  #=> 1.2
OraNumber.new(12.34).round_prec(2)  #=> 12
OraNumber.new(123.4).round_prec(2)  #=> 120


883
884
885
886
887
888
889
890
# File 'ext/oci8/ocinumber.c', line 883

static VALUE onum_round_prec(VALUE self, VALUE ndigs)
{
    OCIError *errhp = oci8_errhp;
    OCINumber r;

    oci_lc(OCINumberPrec(errhp, _NUMBER(self), NUM2INT(ndigs), &r));
    return oci8_make_ocinumber(&r, errhp);
}

#shift(fixnum) ⇒ Object

Returns onum * 10**fixnum This method is available on Oracle 8.1 client or upper.



1054
1055
1056
1057
1058
1059
1060
1061
# File 'ext/oci8/ocinumber.c', line 1054

static VALUE onum_shift(VALUE self, VALUE exp)
{
    OCIError *errhp = oci8_errhp;
    OCINumber result;

    oci_lc(OCINumberShift(errhp, _NUMBER(self), NUM2INT(exp), &result));
    return oci8_make_ocinumber(&result, errhp);
}

#to_char(fmt = nil, nls_params = nil) ⇒ String

Returns a string containing a representation of self. fmt and nls_params are same meanings with TO_CHAR of Oracle function.

Returns:



900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
# File 'ext/oci8/ocinumber.c', line 900

static VALUE onum_to_char(int argc, VALUE *argv, VALUE self)
{
    OCIError *errhp = oci8_errhp;
    VALUE fmt;
    VALUE nls_params;
    char buf[512];
    ub4 buf_size = sizeof(buf);
    oratext *fmt_ptr;
    oratext *nls_params_ptr;
    ub4 fmt_len;
    ub4 nls_params_len;
    sword rv;

    rb_scan_args(argc, argv, "02", &fmt /* nil */, &nls_params /* nil */);
    if (NIL_P(fmt)) {
        OCINumber absval;
        sword sign;
        boolean is_int;

        oci_lc(OCINumberIsInt(errhp, _NUMBER(self), &is_int));
        if (is_int) {
            fmt_ptr = NUMBER_FORMAT_INT;
            fmt_len = NUMBER_FORMAT_INT_LEN;
        } else {
            oci_lc(OCINumberAbs(errhp, _NUMBER(self), &absval));
            oci_lc(OCINumberCmp(errhp, &absval, &const_shreshold, &sign));
            if (sign >= 0) {
                fmt_ptr = NUMBER_FORMAT2;
                fmt_len = NUMBER_FORMAT2_LEN;
            } else {
                fmt_ptr = NUMBER_FORMAT1;
                fmt_len = NUMBER_FORMAT1_LEN;
            }
        }
    } else {
        StringValue(fmt);
        fmt_ptr = RSTRING_ORATEXT(fmt);
        fmt_len = RSTRING_LEN(fmt);
    }
    if (NIL_P(nls_params)) {
        nls_params_ptr = NULL;
        nls_params_len = 0;
    } else {
        StringValue(nls_params);
        nls_params_ptr = RSTRING_ORATEXT(nls_params);
        nls_params_len = RSTRING_LEN(nls_params);
    }
    rv = OCINumberToText(errhp, _NUMBER(self),
                         fmt_ptr, fmt_len, nls_params_ptr, nls_params_len,
                         &buf_size, TO_ORATEXT(buf));
    if (rv == OCI_ERROR) {
        sb4 errcode;
        OCIErrorGet(errhp, 1, NULL, &errcode, NULL, 0, OCI_HTYPE_ERROR);
        if (errcode == 22065) {
            /* OCI-22065: number to text translation for the given format causes overflow */
            if (NIL_P(fmt)) /* implicit conversion */
                return rb_usascii_str_new_cstr("overflow");
        }
        oci8_raise(errhp, rv, NULL);
    }
    return rb_usascii_str_new(buf, buf_size);
}

#to_fFloat

Converts onum to a Float.

Returns:

  • (Float)


996
997
998
999
1000
1001
1002
1003
# File 'ext/oci8/ocinumber.c', line 996

static VALUE onum_to_f(VALUE self)
{
    OCIError *errhp = oci8_errhp;
    double dbl;

    oci_lc(OCINumberToReal(errhp, _NUMBER(self), sizeof(dbl), &dbl));
    return rb_float_new(dbl);
}

#to_iInteger

Returns onm truncated to an Integer.

Returns:

  • (Integer)


980
981
982
983
984
985
986
987
# File 'ext/oci8/ocinumber.c', line 980

static VALUE onum_to_i(VALUE self)
{
    OCIError *errhp = oci8_errhp;
    OCINumber num;

    oci_lc(OCINumberTrunc(errhp, _NUMBER(self), 0, &num));
    return oci8_make_integer(&num, errhp);
}

#to_json(options = nil) ⇒ Object

:nodoc:



536
537
538
# File 'lib/oci8/oci8.rb', line 536

def to_json(options=nil) # :nodoc:
  to_s
end

#to_sString

Returns a string containing a representation of self.

Returns:



969
970
971
972
# File 'ext/oci8/ocinumber.c', line 969

static VALUE onum_to_s(VALUE self)
{
    return onum_to_char(0, NULL, self);
}

#to_yaml(opts = {}) ⇒ Object

:nodoc:



530
531
532
533
534
# File 'lib/oci8/oci8.rb', line 530

def to_yaml(opts = {}) # :nodoc:
  YAML.quick_emit(object_id, opts) do |out|
    out.scalar(taguri, self.to_s, :plain)
  end
end

#truncateInteger #truncate(decplace) ⇒ Object

Truncates onum to the Integer when no argument. Truncates onum to a specified decimal place decplace when one argument.

Overloads:

  • #truncateInteger

    Returns:

    • (Integer)


861
862
863
864
865
866
867
868
869
870
# File 'ext/oci8/ocinumber.c', line 861

static VALUE onum_trunc(int argc, VALUE *argv, VALUE self)
{
    OCIError *errhp = oci8_errhp;
    VALUE decplace;
    OCINumber r;

    rb_scan_args(argc, argv, "01", &decplace /* 0 */);
    oci_lc(OCINumberTrunc(errhp, _NUMBER(self), NIL_P(decplace) ? 0 : NUM2INT(decplace), &r));
    return oci8_make_ocinumber(&r, errhp);
}

#yaml_initialize(type, val) ⇒ Object

:nodoc:



526
527
528
# File 'lib/oci8/oci8.rb', line 526

def yaml_initialize(type, val) # :nodoc:
  initialize(val)
end

#zero?Boolean

Returns true if onum is zero.

Returns:

  • (Boolean)


1022
1023
1024
1025
1026
1027
1028
1029
# File 'ext/oci8/ocinumber.c', line 1022

static VALUE onum_zero_p(VALUE self)
{
    OCIError *errhp = oci8_errhp;
    boolean result;

    oci_lc(OCINumberIsZero(errhp, _NUMBER(self), &result));
    return result ? Qtrue : Qfalse;
}