[ruby-oci8-commit] [335] trunk/ruby-oci8: * ext/oci8/lob.c: OCI8:: BLOB#read should return ASCII-8BIT.

nobody at rubyforge.org nobody at rubyforge.org
Sun Apr 12 05:03:27 EDT 2009


Revision: 335
Author:   kubo
Date:     2009-04-12 05:03:24 -0400 (Sun, 12 Apr 2009)

Log Message:
-----------
* ext/oci8/lob.c: OCI8::BLOB#read should return ASCII-8BIT.
    OCI8::BLOB#write should not convert the specified string
    to OCI8.encoding.
* test/test_dbi.rb: suppress warning: "shadowing outer local
    variable - i"
* test/test_all.rb, test/test_encoding.rb: add string encoding
    tests.

Modified Paths:
--------------
    trunk/ruby-oci8/ChangeLog
    trunk/ruby-oci8/ext/oci8/lob.c
    trunk/ruby-oci8/test/test_all.rb
    trunk/ruby-oci8/test/test_dbi.rb

Added Paths:
-----------
    trunk/ruby-oci8/test/test_encoding.rb

Modified: trunk/ruby-oci8/ChangeLog
===================================================================
--- trunk/ruby-oci8/ChangeLog	2009-04-12 02:59:34 UTC (rev 334)
+++ trunk/ruby-oci8/ChangeLog	2009-04-12 09:03:24 UTC (rev 335)
@@ -1,4 +1,13 @@
 2009-04-12  KUBO Takehiro  <kubo at jiubao.org>
+	* ext/oci8/lob.c: OCI8::BLOB#read should return ASCII-8BIT.
+	    OCI8::BLOB#write should not convert the specified string
+	    to OCI8.encoding.
+	* test/test_dbi.rb: suppress warning: "shadowing outer local
+	    variable - i"
+	* test/test_all.rb, test/test_encoding.rb: add string encoding
+	    tests.
+
+2009-04-12  KUBO Takehiro  <kubo at jiubao.org>
 	* lib/dbd/OCI8.rb: fix DBI::DBD::OCI8::BindType::DBIStatementHandle
 	    to pass a newly added sanity check in dbi 0.4.1.
 	    (reported by Dirk Herzhauser)

Modified: trunk/ruby-oci8/ext/oci8/lob.c
===================================================================
--- trunk/ruby-oci8/ext/oci8/lob.c	2009-04-12 02:59:34 UTC (rev 334)
+++ trunk/ruby-oci8/ext/oci8/lob.c	2009-04-12 09:03:24 UTC (rev 335)
@@ -26,6 +26,7 @@
     ub4 pos;
     int char_width;
     ub1 csfrm;
+    ub1 lobtype;
     enum state state;
 } oci8_lob_t;
 
@@ -149,6 +150,7 @@
     lob->pos = 0;
     lob->char_width = 1;
     lob->csfrm = csfrm;
+    lob->lobtype = lobtype;
     lob->state = S_NO_OPEN_CLOSE;
     oci8_link_to_parent((oci8_base_t*)lob, (oci8_base_t*)DATA_PTR(svc));
     if (!NIL_P(val)) {
@@ -360,8 +362,14 @@
     }
     v = rb_ary_join(v, Qnil);
     OBJ_TAINT(v);
-    rb_enc_associate(v, oci8_encoding);
-    return rb_str_conv_enc(v, oci8_encoding, rb_default_internal_encoding());
+    if (lob->lobtype == OCI_TEMP_CLOB) {
+        /* set encoding */
+        rb_enc_associate(v, oci8_encoding);
+        return rb_str_conv_enc(v, oci8_encoding, rb_default_internal_encoding());
+    } else {
+        /* ASCII-8BIT */
+        return v;
+    }
 }
 
 static VALUE oci8_lob_write(VALUE self, VALUE data)
@@ -371,7 +379,11 @@
     ub4 amt;
 
     lob_open(lob);
-    OCI8StringValue(data);
+    if (lob->lobtype == OCI_TEMP_CLOB) {
+        OCI8StringValue(data);
+    } else {
+        StringValue(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;
@@ -503,6 +515,7 @@
     lob->pos = 0;
     lob->char_width = 1;
     lob->csfrm = SQLCS_IMPLICIT;
+    lob->lobtype = OCI_TEMP_BLOB;
     lob->state = S_BFILE_CLOSE;
     if (argc != 1) {
         OCI8SafeStringValue(dir_alias);

Modified: trunk/ruby-oci8/test/test_all.rb
===================================================================
--- trunk/ruby-oci8/test/test_all.rb	2009-04-12 02:59:34 UTC (rev 334)
+++ trunk/ruby-oci8/test/test_all.rb	2009-04-12 09:03:24 UTC (rev 335)
@@ -21,6 +21,10 @@
 require "#{srcdir}/test_rowid"
 require "#{srcdir}/test_oracle_version"
 
+if OCI8.respond_to? :encoding
+  require "#{srcdir}/test_encoding"
+end
+
 # Ruby/DBI
 begin
   require 'dbi'

Modified: trunk/ruby-oci8/test/test_dbi.rb
===================================================================
--- trunk/ruby-oci8/test/test_dbi.rb	2009-04-12 02:59:34 UTC (rev 334)
+++ trunk/ruby-oci8/test/test_dbi.rb	2009-04-12 09:03:24 UTC (rev 335)
@@ -252,7 +252,7 @@
 
     drop_table('test_table')
     @dbh.execute(<<-EOS)
-CREATE TABLE test_table (#{i = 0; coldef.collect do |c| i += 1; "C#{i} " + c[1] + (c[8] ? ' PRIMARY KEY' : ''); end.join(',')})
+CREATE TABLE test_table (#{n = 0; coldef.collect do |c| n += 1; "C#{n} " + c[1] + (c[8] ? ' PRIMARY KEY' : ''); end.join(',')})
 STORAGE (
    INITIAL 100k
    NEXT 100k

Added: trunk/ruby-oci8/test/test_encoding.rb
===================================================================
--- trunk/ruby-oci8/test/test_encoding.rb	                        (rev 0)
+++ trunk/ruby-oci8/test/test_encoding.rb	2009-04-12 09:03:24 UTC (rev 335)
@@ -0,0 +1,77 @@
+require 'oci8'
+require 'test/unit'
+require File.dirname(__FILE__) + '/config'
+
+class TestEncoding < Test::Unit::TestCase
+  def setup
+    @conn = get_oci8_connection
+  end
+
+  def test_select
+    drop_table('test_table')
+    @conn.exec(<<EOS)
+CREATE TABLE test_table
+  (C CHAR(10),
+   V VARCHAR2(10),
+   R RAW(10),
+   LR LONG RAW,
+   CL CLOB,
+   NCL NCLOB,
+   BL BLOB)
+STORAGE (
+   INITIAL 4k
+   NEXT 4k
+   MINEXTENTS 1
+   MAXEXTENTS UNLIMITED
+   PCTINCREASE 0)
+EOS
+    ascii_8bit = Encoding.find('ASCII-8BIT')
+    @conn.exec(<<EOS)
+INSERT INTO test_table VALUES ('abcd', 'abcd', 'abcd', 'abcd', 'abcd', 'abcd', 'abcd')
+EOS
+    @conn.exec("SELECT * FROM test_table") do |row|
+      assert_equal('abcd      ', row[0], 'CHAR(10)')
+      assert_equal(OCI8.encoding, row[0].encoding);
+      assert_equal('abcd', row[1], 'VARCHAR2(10)')
+      assert_equal(OCI8.encoding, row[1].encoding);
+      assert_equal("\xab\xcd", row[2], 'RAW(10)')
+      assert_equal(ascii_8bit, row[2].encoding);
+      assert_equal("\xab\xcd", row[3], 'LONG RAW')
+      assert_equal(ascii_8bit, row[3].encoding);
+      assert_equal('abcd', (data = row[4].read), 'CLOB')
+      assert_equal(OCI8.encoding, data.encoding);
+      assert_equal('abcd', (data = row[5].read), 'NCLOB')
+      assert_equal(OCI8.encoding, data.encoding);
+      assert_equal("\xab\xcd", (data = row[6].read), 'BLOB')
+      assert_equal(ascii_8bit, data.encoding);
+
+      if OCI8.encoding.name == "UTF-8"
+        utf_8 = "\u00A1\u00A2\u00A3\u00A4\u00A5\u00A6\u00A7\u00A8\u00A9"
+        iso_8859_1 = utf_8.encode("ISO-8859-1")
+        # CLOB
+        lob = row[4]
+        lob.rewind
+        lob.write(iso_8859_1) # converted to OCI8.encoding(UTF-8)
+        lob.rewind
+        assert_equal(utf_8, lob.read)
+        # NCLOB
+        lob = row[5]
+        lob.rewind
+        lob.write(iso_8859_1) # converted to OCI8.encoding(UTF-8)
+        lob.rewind
+        assert_equal(utf_8, lob.read)
+        # BLOB
+        lob = row[6]
+        lob.rewind
+        lob.write(iso_8859_1) # written without encoding conversion
+        lob.rewind
+        assert_equal(iso_8859_1.force_encoding('ASCII-8BIT'), lob.read)
+      end
+    end
+    drop_table('test_table')
+  end
+
+  def teardown
+    @conn.logoff
+  end
+end




More information about the ruby-oci8-commit mailing list