[ruby-oci8-commit] [420] trunk/ruby-oci8: * ext/oci8/ocinumber.c, lib/oci8/bindtype.rb, test/ test_oranumber.rb:

nobody at rubyforge.org nobody at rubyforge.org
Sun Dec 26 06:18:23 EST 2010


Revision: 420
Author:   kubo
Date:     2010-12-26 06:18:22 -0500 (Sun, 26 Dec 2010)

Log Message:
-----------
* ext/oci8/ocinumber.c, lib/oci8/bindtype.rb, test/test_oranumber.rb:
    add OraNumber#has_decimal_part? and OCI8::BindType::BasicNumberType.

Modified Paths:
--------------
    branches/ruby-oci8-2.0/ChangeLog
    branches/ruby-oci8-2.0/ext/oci8/ocinumber.c
    branches/ruby-oci8-2.0/lib/oci8/bindtype.rb
    branches/ruby-oci8-2.0/test/test_oranumber.rb
    trunk/ruby-oci8/ChangeLog
    trunk/ruby-oci8/ext/oci8/ocinumber.c
    trunk/ruby-oci8/lib/oci8/bindtype.rb
    trunk/ruby-oci8/test/test_oranumber.rb

Modified: branches/ruby-oci8-2.0/ChangeLog
===================================================================
--- branches/ruby-oci8-2.0/ChangeLog	2010-12-14 11:30:44 UTC (rev 419)
+++ branches/ruby-oci8-2.0/ChangeLog	2010-12-26 11:18:22 UTC (rev 420)
@@ -1,3 +1,7 @@
+2010-12-26  KUBO Takehiro  <kubo at jiubao.org>
+	* ext/oci8/ocinumber.c, lib/oci8/bindtype.rb, test/test_oranumber.rb:
+	    add OraNumber#has_decimal_part? and OCI8::BindType::BasicNumberType.
+
 2010-12-14  KUBO Takehiro  <kubo at jiubao.org>
 	* dist-files, lib/oci8.rb.in, lib/oci8/.document, lib/oci8/properties.rb:
 	    add OCI8.properties to get and set ruby-oci8 global setting.

Modified: branches/ruby-oci8-2.0/ext/oci8/ocinumber.c
===================================================================
--- branches/ruby-oci8-2.0/ext/oci8/ocinumber.c	2010-12-14 11:30:44 UTC (rev 419)
+++ branches/ruby-oci8-2.0/ext/oci8/ocinumber.c	2010-12-26 11:18:22 UTC (rev 420)
@@ -1187,6 +1187,24 @@
 
 /*
  *  call-seq:
+ *     onum.has_decimal_part? -> true or false
+ *
+ *  Returns +true+ if <i>self</i> has a decimal part.
+ *
+ *    OraNumber(10).has_decimal_part?   # => false
+ *    OraNumber(10.1).has_decimal_part? # => true
+ */
+static VALUE onum_has_decimal_part_p(VALUE self)
+{
+    OCIError *errhp = oci8_errhp;
+    boolean result;
+
+    oci_lc(OCINumberIsInt(errhp, _NUMBER(self), &result));
+    return result ? Qfalse : Qtrue;
+}
+
+/*
+ *  call-seq:
  *     onum.to_onum -> oranumber
  *
  *  Returns self.
@@ -1507,6 +1525,7 @@
     rb_define_method(cOCINumber, "to_f", onum_to_f, 0);
     rb_define_method(cOCINumber, "to_r", onum_to_r, 0);
     rb_define_method(cOCINumber, "to_d", onum_to_d, 0);
+    rb_define_method(cOCINumber, "has_decimal_part?", onum_has_decimal_part_p, 0);
     rb_define_method_nodoc(cOCINumber, "to_onum", onum_to_onum, 0);
 
     rb_define_method(cOCINumber, "zero?", onum_zero_p, 0);

Modified: branches/ruby-oci8-2.0/lib/oci8/bindtype.rb
===================================================================
--- branches/ruby-oci8-2.0/lib/oci8/bindtype.rb	2010-12-14 11:30:44 UTC (rev 419)
+++ branches/ruby-oci8-2.0/lib/oci8/bindtype.rb	2010-12-26 11:18:22 UTC (rev 420)
@@ -46,6 +46,12 @@
       end
     end
 
+    class BasicNumberType < OCI8::BindType::OraNumber
+      def get()
+        (val = super()) && (val.has_decimal_part? ? val.to_f : val.to_i)
+      end
+    end
+
     # get/set Number (for OCI8::SQLT_NUM)
     class Number
       def self.create(con, val, param, max_array_size)

Modified: branches/ruby-oci8-2.0/test/test_oranumber.rb
===================================================================
--- branches/ruby-oci8-2.0/test/test_oranumber.rb	2010-12-14 11:30:44 UTC (rev 419)
+++ branches/ruby-oci8-2.0/test/test_oranumber.rb	2010-12-26 11:18:22 UTC (rev 420)
@@ -185,6 +185,21 @@
     end
   end
 
+  def test_bind_basic_number_type
+    conn = get_oci8_connection
+    bind_type = OCI8::BindType::Mapping[:number]
+    begin
+      OCI8::BindType::Mapping[:number] = OCI8::BindType::BasicNumberType
+      assert_kind_of(NilClass, conn.select_one('select CAST(NULL AS NUMBER) from dual')[0])
+      assert_kind_of(Integer,  conn.select_one('select 0.0 from dual')[0])
+      assert_kind_of(Integer,  conn.select_one('select 10.0 from dual')[0])
+      assert_kind_of(Float,    conn.select_one('select 10.1 from dual')[0])
+    ensure
+      conn.logoff
+      OCI8::BindType::Mapping[:number] = bind_type
+    end
+  end
+
   def test_dup
     (LARGE_RANGE_VALUES + ['~', '-~']).each do |x|
       n = OraNumber.new(x)
@@ -723,4 +738,9 @@
     end
     LARGE_RANGE_VALUES
   end
+
+  def test_has_decimal_part
+    assert_equal(false, OraNumber(10.0).has_decimal_part?)
+    assert_equal(true,  OraNumber(10.1).has_decimal_part?)
+  end
 end

Modified: trunk/ruby-oci8/ChangeLog
===================================================================
--- trunk/ruby-oci8/ChangeLog	2010-12-14 11:30:44 UTC (rev 419)
+++ trunk/ruby-oci8/ChangeLog	2010-12-26 11:18:22 UTC (rev 420)
@@ -1,3 +1,7 @@
+2010-12-26  KUBO Takehiro  <kubo at jiubao.org>
+	* ext/oci8/ocinumber.c, lib/oci8/bindtype.rb, test/test_oranumber.rb:
+	    add OraNumber#has_decimal_part? and OCI8::BindType::BasicNumberType.
+
 2010-12-14  KUBO Takehiro  <kubo at jiubao.org>
 	* dist-files, lib/oci8.rb.in, lib/oci8/.document, lib/oci8/properties.rb:
 	    add OCI8.properties to get and set ruby-oci8 global setting.

Modified: trunk/ruby-oci8/ext/oci8/ocinumber.c
===================================================================
--- trunk/ruby-oci8/ext/oci8/ocinumber.c	2010-12-14 11:30:44 UTC (rev 419)
+++ trunk/ruby-oci8/ext/oci8/ocinumber.c	2010-12-26 11:18:22 UTC (rev 420)
@@ -1187,6 +1187,24 @@
 
 /*
  *  call-seq:
+ *     onum.has_decimal_part? -> true or false
+ *
+ *  Returns +true+ if <i>self</i> has a decimal part.
+ *
+ *    OraNumber(10).has_decimal_part?   # => false
+ *    OraNumber(10.1).has_decimal_part? # => true
+ */
+static VALUE onum_has_decimal_part_p(VALUE self)
+{
+    OCIError *errhp = oci8_errhp;
+    boolean result;
+
+    oci_lc(OCINumberIsInt(errhp, _NUMBER(self), &result));
+    return result ? Qfalse : Qtrue;
+}
+
+/*
+ *  call-seq:
  *     onum.to_onum -> oranumber
  *
  *  Returns self.
@@ -1507,6 +1525,7 @@
     rb_define_method(cOCINumber, "to_f", onum_to_f, 0);
     rb_define_method(cOCINumber, "to_r", onum_to_r, 0);
     rb_define_method(cOCINumber, "to_d", onum_to_d, 0);
+    rb_define_method(cOCINumber, "has_decimal_part?", onum_has_decimal_part_p, 0);
     rb_define_method_nodoc(cOCINumber, "to_onum", onum_to_onum, 0);
 
     rb_define_method(cOCINumber, "zero?", onum_zero_p, 0);

Modified: trunk/ruby-oci8/lib/oci8/bindtype.rb
===================================================================
--- trunk/ruby-oci8/lib/oci8/bindtype.rb	2010-12-14 11:30:44 UTC (rev 419)
+++ trunk/ruby-oci8/lib/oci8/bindtype.rb	2010-12-26 11:18:22 UTC (rev 420)
@@ -46,6 +46,12 @@
       end
     end
 
+    class BasicNumberType < OCI8::BindType::OraNumber
+      def get()
+        (val = super()) && (val.has_decimal_part? ? val.to_f : val.to_i)
+      end
+    end
+
     # get/set Number (for OCI8::SQLT_NUM)
     class Number
       def self.create(con, val, param, max_array_size)

Modified: trunk/ruby-oci8/test/test_oranumber.rb
===================================================================
--- trunk/ruby-oci8/test/test_oranumber.rb	2010-12-14 11:30:44 UTC (rev 419)
+++ trunk/ruby-oci8/test/test_oranumber.rb	2010-12-26 11:18:22 UTC (rev 420)
@@ -185,6 +185,21 @@
     end
   end
 
+  def test_bind_basic_number_type
+    conn = get_oci8_connection
+    bind_type = OCI8::BindType::Mapping[:number]
+    begin
+      OCI8::BindType::Mapping[:number] = OCI8::BindType::BasicNumberType
+      assert_kind_of(NilClass, conn.select_one('select CAST(NULL AS NUMBER) from dual')[0])
+      assert_kind_of(Integer,  conn.select_one('select 0.0 from dual')[0])
+      assert_kind_of(Integer,  conn.select_one('select 10.0 from dual')[0])
+      assert_kind_of(Float,    conn.select_one('select 10.1 from dual')[0])
+    ensure
+      conn.logoff
+      OCI8::BindType::Mapping[:number] = bind_type
+    end
+  end
+
   def test_dup
     (LARGE_RANGE_VALUES + ['~', '-~']).each do |x|
       n = OraNumber.new(x)
@@ -723,4 +738,9 @@
     end
     LARGE_RANGE_VALUES
   end
+
+  def test_has_decimal_part
+    assert_equal(false, OraNumber(10.0).has_decimal_part?)
+    assert_equal(true,  OraNumber(10.1).has_decimal_part?)
+  end
 end




More information about the ruby-oci8-commit mailing list