[ruby-oci8-commit] [358] trunk/ruby-oci8: * ext/oci8/lob.c, ext/oci8/oci8.h, test/test_clob.rb : Change

nobody at rubyforge.org nobody at rubyforge.org
Sun Sep 13 04:37:55 EDT 2009


Revision: 358
Author:   kubo
Date:     2009-09-13 04:37:53 -0400 (Sun, 13 Sep 2009)

Log Message:
-----------
* ext/oci8/lob.c, ext/oci8/oci8.h, test/test_clob.rb: Change
    OCI8::LOB#write to accept an object which is not a String and
    doesn't respond to 'to_str' as IO#write does.
    (requested by Christopher Jones)

Modified Paths:
--------------
    branches/ruby-oci8-1.0/ChangeLog
    branches/ruby-oci8-1.0/ext/oci8/lob.c
    branches/ruby-oci8-1.0/ext/oci8/oci8.h
    branches/ruby-oci8-1.0/test/test_clob.rb
    trunk/ruby-oci8/ChangeLog
    trunk/ruby-oci8/ext/oci8/lob.c
    trunk/ruby-oci8/ext/oci8/oci8.h
    trunk/ruby-oci8/test/test_clob.rb

Modified: branches/ruby-oci8-1.0/ChangeLog
===================================================================
--- branches/ruby-oci8-1.0/ChangeLog	2009-09-12 14:25:23 UTC (rev 357)
+++ branches/ruby-oci8-1.0/ChangeLog	2009-09-13 08:37:53 UTC (rev 358)
@@ -1,3 +1,9 @@
+2009-09-13  KUBO Takehiro  <kubo at jiubao.org>
+	* ext/oci8/lob.c, ext/oci8/oci8.h, test/test_clob.rb: Change
+	    OCI8::LOB#write to accept an object which is not a String and
+	    doesn't respond to 'to_str' as IO#write does.
+	    (requested by Christopher Jones)
+
 2009-09-12  KUBO Takehiro  <kubo at jiubao.org>
 	* ext/oci8/oraconf.rb: Fixed to compile for AIX instant clients.
 	    (reported by Kazuya Teramoto)

Modified: branches/ruby-oci8-1.0/ext/oci8/lob.c
===================================================================
--- branches/ruby-oci8-1.0/ext/oci8/lob.c	2009-09-12 14:25:23 UTC (rev 357)
+++ branches/ruby-oci8-1.0/ext/oci8/lob.c	2009-09-13 08:37:53 UTC (rev 358)
@@ -184,6 +184,10 @@
   Get_Handle(self, h); /* 0 */
   Check_Handle(vsvc, OCISvcCtx, svch); /* 1 */
   offset = NUM2UINT(voffset); /* 2 */
+  if (TYPE(vbuf) != T_STRING) {
+    vbuf = rb_obj_as_string(vbuf);
+  }
+  RB_GC_GUARD(vbuf);
   Get_String(vbuf, buf); /* 3 */
   csid = NIL_P(vcsid) ? 0 : NUM2INT(vcsid); /* 4 */
   csfrm = NIL_P(vcsfrm) ? SQLCS_IMPLICIT : NUM2INT(vcsfrm); /* 5 */

Modified: branches/ruby-oci8-1.0/ext/oci8/oci8.h
===================================================================
--- branches/ruby-oci8-1.0/ext/oci8/oci8.h	2009-09-12 14:25:23 UTC (rev 357)
+++ branches/ruby-oci8-1.0/ext/oci8/oci8.h	2009-09-13 08:37:53 UTC (rev 358)
@@ -52,6 +52,10 @@
 #define RARRAY_LEN(obj) RARRAY(obj)->len
 #endif
 
+#ifndef RB_GC_GUARD
+#define RB_GC_GUARD(v) (*(volatile VALUE *)&(v))
+#endif
+
 #ifndef HAVE_TYPE_ORATEXT
 typedef unsigned char oratext;
 #endif

Modified: branches/ruby-oci8-1.0/test/test_clob.rb
===================================================================
--- branches/ruby-oci8-1.0/test/test_clob.rb	2009-09-12 14:25:23 UTC (rev 357)
+++ branches/ruby-oci8-1.0/test/test_clob.rb	2009-09-13 08:37:53 UTC (rev 358)
@@ -64,6 +64,27 @@
     lob.free()
   end
 
+  def test_insert_symbol
+    filename = 'test_symbol'
+    value = :foo_bar
+    @stmt.prepare("DELETE FROM test_clob WHERE filename = :1")
+    @stmt.bindByPos(1, String, filename.size).set(filename)
+    @stmt.execute(@svc)
+
+    @stmt.prepare("INSERT INTO test_clob(filename, content) VALUES (:1, EMPTY_CLOB())")
+    @stmt.bindByPos(1, String, filename.size).set(filename)
+    @stmt.execute(@svc)
+
+    lob = @env.alloc(OCILobLocator)
+    @stmt.prepare("SELECT content FROM test_clob WHERE filename = :1 FOR UPDATE")
+    @stmt.bindByPos(1, String, filename.size).set(filename)
+    @stmt.defineByPos(1, OCI_TYPECODE_CLOB, lob)
+    @stmt.execute(@svc, 1)
+    lob.write(@svc, 1, value)
+    assert_equal(value.to_s, lob.read(@svc, 1, 30))
+    lob.free()
+  end
+
   def test_read
     filename = File.basename($lobfile)
     test_insert() # first insert data.

Modified: trunk/ruby-oci8/ChangeLog
===================================================================
--- trunk/ruby-oci8/ChangeLog	2009-09-12 14:25:23 UTC (rev 357)
+++ trunk/ruby-oci8/ChangeLog	2009-09-13 08:37:53 UTC (rev 358)
@@ -1,3 +1,9 @@
+2009-09-13  KUBO Takehiro  <kubo at jiubao.org>
+	* ext/oci8/lob.c, ext/oci8/oci8.h, test/test_clob.rb: Change
+	    OCI8::LOB#write to accept an object which is not a String and
+	    doesn't respond to 'to_str' as IO#write does.
+	    (requested by Christopher Jones)
+
 2009-09-12  KUBO Takehiro  <kubo at jiubao.org>
 	* ext/oci8/extconf.rb, lib/oci8.rb.in: Fixed for ruby 1.9.2 preview1.
 	    (pointed by Raimonds Simanovskis)

Modified: trunk/ruby-oci8/ext/oci8/lob.c
===================================================================
--- trunk/ruby-oci8/ext/oci8/lob.c	2009-09-12 14:25:23 UTC (rev 357)
+++ trunk/ruby-oci8/ext/oci8/lob.c	2009-09-13 08:37:53 UTC (rev 358)
@@ -379,11 +379,13 @@
     ub4 amt;
 
     lob_open(lob);
+    if (TYPE(data) != T_STRING) {
+        data = rb_obj_as_string(data);
+    }
     if (lob->lobtype == OCI_TEMP_CLOB) {
-        OCI8StringValue(data);
-    } else {
-        StringValue(data);
+        data = rb_str_export_to_enc(data, oci8_encoding);
     }
+    RB_GC_GUARD(data);
     amt = RSTRING_LEN(data);
     oci_lc(OCILobWrite_nb(svcctx, svcctx->base.hp.svc, oci8_errhp, lob->base.hp.lob, &amt, lob->pos + 1, RSTRING_PTR(data), amt, OCI_ONE_PIECE, NULL, NULL, 0, lob->csfrm));
     lob->pos += amt;

Modified: trunk/ruby-oci8/ext/oci8/oci8.h
===================================================================
--- trunk/ruby-oci8/ext/oci8/oci8.h	2009-09-12 14:25:23 UTC (rev 357)
+++ trunk/ruby-oci8/ext/oci8/oci8.h	2009-09-13 08:37:53 UTC (rev 358)
@@ -120,6 +120,9 @@
 #ifndef STRINGIZE
 #define STRINGIZE(name) #name
 #endif
+#ifndef RB_GC_GUARD
+#define RB_GC_GUARD(v) (*(volatile VALUE *)&(v))
+#endif
 
 /* new functions in ruby 1.9.
  * define compatible macros for ruby 1.8 or lower.
@@ -136,6 +139,7 @@
 #define rb_external_str_new_with_enc(ptr, len, enc) rb_tainted_str_new((ptr), (len))
 #define rb_locale_str_new_cstr(ptr)  rb_str_new2(ptr)
 #define rb_str_conv_enc(str, from, to) (str)
+#define rb_str_export_to_enc(str, enc) (str)
 #define rb_usascii_str_new(ptr, len) rb_str_new((ptr), (len))
 #define rb_usascii_str_new_cstr(ptr) rb_str_new2(ptr)
 #endif

Modified: trunk/ruby-oci8/test/test_clob.rb
===================================================================
--- trunk/ruby-oci8/test/test_clob.rb	2009-09-12 14:25:23 UTC (rev 357)
+++ trunk/ruby-oci8/test/test_clob.rb	2009-09-13 08:37:53 UTC (rev 358)
@@ -39,6 +39,19 @@
     lob.close
   end
 
+  def test_insert_symbol
+    filename = 'test_symbol'
+    value = :foo_bar
+    @conn.exec("DELETE FROM test_clob WHERE filename = :1", filename)
+    @conn.exec("INSERT INTO test_clob(filename, content) VALUES (:1, EMPTY_CLOB())", filename)
+    cursor = @conn.exec("SELECT content FROM test_clob WHERE filename = :1 FOR UPDATE", filename)
+    lob = cursor.fetch[0]
+    lob.write(value)
+    lob.rewind
+    assert_equal(value.to_s, lob.read);
+    lob.close
+  end
+
   def test_read
     test_insert() # first insert data.
     filename = File.basename($lobfile)




More information about the ruby-oci8-commit mailing list