[ruby-oci8-commit] [474] trunk/ruby-oci8: add tests for bind_string_as_nchar property.

nobody at rubyforge.org nobody at rubyforge.org
Sun Dec 4 02:35:20 EST 2011


Revision: 474
Author:   kubo
Date:     2011-12-04 02:35:19 -0500 (Sun, 04 Dec 2011)

Log Message:
-----------
add tests for bind_string_as_nchar property.

Modified Paths:
--------------
    trunk/ruby-oci8/ChangeLog
    trunk/ruby-oci8/ext/oci8/metadata.c
    trunk/ruby-oci8/lib/oci8/metadata.rb
    trunk/ruby-oci8/lib/oci8/oci8.rb
    trunk/ruby-oci8/test/config.rb
    trunk/ruby-oci8/test/test_encoding.rb

Modified: trunk/ruby-oci8/ChangeLog
===================================================================
--- trunk/ruby-oci8/ChangeLog	2011-12-03 08:57:15 UTC (rev 473)
+++ trunk/ruby-oci8/ChangeLog	2011-12-04 07:35:19 UTC (rev 474)
@@ -1,3 +1,11 @@
+2011-12-04  KUBO Takehiro  <kubo at jiubao.org>
+	* test/config.rb, test/test_encoding.rb: add tests for bind_string_as_nchar
+	    property.
+	* ext/oci8/metadata.c, lib/oci8/metadata.rb: delete the internally used
+	    OCI8::Metadata::Base#__charset_name method.
+	* lib/oci8/oci8.rb: add OCI8#database_charset_name. (added to test
+	    bind_string_as_nchar.)
+
 2011-12-03  KUBO Takehiro  <kubo at jiubao.org>
 	* lib/oci8.rb.in: fix to work with ruby 2.0.
 

Modified: trunk/ruby-oci8/ext/oci8/metadata.c
===================================================================
--- trunk/ruby-oci8/ext/oci8/metadata.c	2011-12-03 08:57:15 UTC (rev 473)
+++ trunk/ruby-oci8/ext/oci8/metadata.c	2011-12-04 07:35:19 UTC (rev 474)
@@ -109,13 +109,6 @@
     return oci8_metadata_create(value, md->svc, self);
 }
 
-static VALUE metadata_get_charset_name(VALUE self, VALUE charset_id)
-{
-    oci8_metadata_t *md = DATA_PTR(self);
-
-    return oci8_charset_id2name(md->svc, charset_id);
-}
-
 static VALUE metadata_get_con(VALUE self)
 {
     oci8_metadata_t *md = DATA_PTR(self);
@@ -222,7 +215,6 @@
     rb_define_singleton_method(cOCI8MetadataBase, "register_ptype", metadata_s_register_ptype, 1);
     rb_define_private_method(cOCI8MetadataBase, "__param", metadata_get_param, 1);
     rb_define_private_method(cOCI8MetadataBase, "__param_at", metadata_get_param_at, 1);
-    rb_define_private_method(cOCI8MetadataBase, "__charset_name", metadata_get_charset_name, 1);
     rb_define_private_method(cOCI8MetadataBase, "__con", metadata_get_con, 0);
     rb_define_private_method(cOCI8MetadataBase, "__is_implicit?", metadata_is_implicit_p, 0);
 

Modified: trunk/ruby-oci8/lib/oci8/metadata.rb
===================================================================
--- trunk/ruby-oci8/lib/oci8/metadata.rb	2011-12-03 08:57:15 UTC (rev 473)
+++ trunk/ruby-oci8/lib/oci8/metadata.rb	2011-12-04 07:35:19 UTC (rev 474)
@@ -1060,7 +1060,7 @@
 
       # character set name if the type attribute is of a string/character type.
       def charset_name
-        __charset_name(charset_id)
+        __con.charset_id2name(charset_id)
       end
 
       def inspect # :nodoc:

Modified: trunk/ruby-oci8/lib/oci8/oci8.rb
===================================================================
--- trunk/ruby-oci8/lib/oci8/oci8.rb	2011-12-03 08:57:15 UTC (rev 473)
+++ trunk/ruby-oci8/lib/oci8/oci8.rb	2011-12-04 07:35:19 UTC (rev 474)
@@ -333,6 +333,16 @@
     @oracle_server_version
   end
 
+  # :call-seq:
+  #   database_charset_name -> string
+  #
+  # (new in 2.1.0)
+  #
+  # Returns the database character set name.
+  def database_charset_name
+    charset_id2name(server_handle.send(:attr_get_ub2, OCI_ATTR_CHARSET_ID))
+  end
+
   # The instance of this class corresponds to cursor in the term of
   # Oracle, which corresponds to java.sql.Statement of JDBC and statement
   # handle $sth of Perl/DBI.

Modified: trunk/ruby-oci8/test/config.rb
===================================================================
--- trunk/ruby-oci8/test/config.rb	2011-12-03 08:57:15 UTC (rev 473)
+++ trunk/ruby-oci8/test/config.rb	2011-12-04 07:35:19 UTC (rev 474)
@@ -5,6 +5,9 @@
 $dbpass = "oci8"
 $dbname = nil
 
+# for test_bind_string_as_nchar in test_encoding.rb
+ENV['ORA_NCHAR_LITERAL_REPLACE'] = 'TRUE' if OCI8.encoding.name == "UTF-8"
+
 # test_clob.rb
 
 nls_lang = ENV['NLS_LANG']

Modified: trunk/ruby-oci8/test/test_encoding.rb
===================================================================
--- trunk/ruby-oci8/test/test_encoding.rb	2011-12-03 08:57:15 UTC (rev 473)
+++ trunk/ruby-oci8/test/test_encoding.rb	2011-12-04 07:35:19 UTC (rev 474)
@@ -45,9 +45,9 @@
       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")
+      if OCI8.encoding.name == "UTF-8" and ['WE8ISO8859P1', 'WE8ISO8859P15', 'AL32UTF8', 'UTF8'].include? @conn.database_charset_name
+        utf_8 = "\u00A1\u00A2\u00A3\u00A5\u00A7\u00A9"
+        iso_8859_1 = utf_8.encode("ISO-8859-15")
         # CLOB
         lob = row[4]
         lob.rewind
@@ -66,11 +66,15 @@
         lob.write(iso_8859_1) # written without encoding conversion
         lob.rewind
         assert_equal(iso_8859_1.force_encoding('ASCII-8BIT'), lob.read)
+      else
+        warn "Skip some asserts because the database character set is neither WE8ISO8859P1, WE8ISO8859P15, AL32UTF8 nor UTF8." if OCI8.encoding.name == "UTF-8"
       end
     end
     drop_table('test_table')
   end
 
+  warn "Skip some tests which runs only when NLS_CHARACTERSETS is unicode." if OCI8.encoding.name != "UTF-8"
+
   if OCI8.encoding.name == "UTF-8"
     def test_bind_string_with_code_conversion
       drop_table('test_table')
@@ -92,6 +96,37 @@
       end
       drop_table('test_table')
     end
+
+    def test_bind_string_as_nchar
+      if ['AL32UTF8', 'UTF8', 'ZHS32GB18030'].include? @conn.database_charset_name
+        warn "Skip test_bind_string_as_nchar. It needs Oracle server whose database chracter set is incompatible with unicode."
+      else
+        drop_table('test_table')
+        @conn.exec("CREATE TABLE test_table (ID NUMBER(5), V VARCHAR2(10), NV1 NVARCHAR2(10), NV2 NVARCHAR2(10))")
+
+        test_data = "a\u00A1\u3042"
+        orig_prop = OCI8.properties[:bind_string_as_nchar]
+        begin
+          OCI8.properties[:bind_string_as_nchar] = false
+          @conn.exec("INSERT INTO test_table VALUES (1, N'#{test_data}', N'#{test_data}', :1)", test_data)
+          v, nv1, nv2 = @conn.select_one('select V, NV1, NV2 from test_table where ID = 1')
+          assert_not_equal(test_data, v) # Some UTF-8 chracters should be garbled.
+          assert_equal(test_data, nv1) # No garbled characters
+          assert_equal(v, nv2) # Garbled as VARCHAR2 column.
+
+          OCI8.properties[:bind_string_as_nchar] = true
+          @conn.exec("INSERT INTO test_table VALUES (2, N'#{test_data}', N'#{test_data}', :1)", test_data)
+          v, nv1, nv2 = @conn.select_one('select V, NV1, NV2 from test_table where ID = 2')
+          assert_not_equal(test_data, v) # Some UTF-8 chracters should be garbled.
+          assert_equal(test_data, nv1) # No garbled characters
+          assert_equal(nv1, nv2) # Same as NVARCHAR2.
+
+          @conn.commit
+        ensure
+          OCI8.properties[:bind_string_as_nchar] = orig_prop
+        end
+      end
+    end
   end
 
   def teardown




More information about the ruby-oci8-commit mailing list