[ruby-oci8-commit] [475] trunk/ruby-oci8: Add a test for OCI8.properties[:lenght_semantics].
nobody at rubyforge.org
nobody at rubyforge.org
Mon Dec 5 07:08:51 EST 2011
Revision: 475
Author: kubo
Date: 2011-12-05 07:08:51 -0500 (Mon, 05 Dec 2011)
Log Message:
-----------
Add a test for OCI8.properties[:lenght_semantics]. Add OCI8.client_charset_name.
Modified Paths:
--------------
trunk/ruby-oci8/ChangeLog
trunk/ruby-oci8/lib/oci8/encoding-init.rb
trunk/ruby-oci8/lib/oci8/oci8.rb
trunk/ruby-oci8/lib/oci8/properties.rb
trunk/ruby-oci8/test/config.rb
trunk/ruby-oci8/test/test_all.rb
trunk/ruby-oci8/test/test_encoding.rb
Added Paths:
-----------
trunk/ruby-oci8/test/test_bind_string.rb
Modified: trunk/ruby-oci8/ChangeLog
===================================================================
--- trunk/ruby-oci8/ChangeLog 2011-12-04 07:35:19 UTC (rev 474)
+++ trunk/ruby-oci8/ChangeLog 2011-12-05 12:08:51 UTC (rev 475)
@@ -1,3 +1,12 @@
+2011-12-05 KUBO Takehiro <kubo at jiubao.org>
+ * lib/oci8/encoding-init.rb, lib/oci8/oci8.rb: add OCI8.client_charset_name.
+ * test/config.rb: use OCI8.client_charset_name instead of OCI8.encoding.
+ Ruby-oci8 compiled for ruby 1.8 doesn't has the latter.
+ * lib/oci8/properties.rb, test/test_all.rb, test/test_bind_string.rb,
+ test/test_encoding.rb: add a test for OCI8.properties[:lenght_semantics]
+ and move the test for OCI8.properties[:bind_string_as_nchar] from
+ test/test_encoding.rb to test/test_bind_string.rb.
+
2011-12-04 KUBO Takehiro <kubo at jiubao.org>
* test/config.rb, test/test_encoding.rb: add tests for bind_string_as_nchar
property.
Modified: trunk/ruby-oci8/lib/oci8/encoding-init.rb
===================================================================
--- trunk/ruby-oci8/lib/oci8/encoding-init.rb 2011-12-04 07:35:19 UTC (rev 474)
+++ trunk/ruby-oci8/lib/oci8/encoding-init.rb 2011-12-05 12:08:51 UTC (rev 475)
@@ -2,73 +2,76 @@
# setup default OCI encoding from NLS_LANG.
#
-# get the environment variable NLS_LANG.
-nls_lang = ENV['NLS_LANG']
+class OCI8
-if nls_lang.is_a? String and nls_lang.length == 0
- nls_lang = nil
-end
+ # get the environment variable NLS_LANG.
+ nls_lang = ENV['NLS_LANG']
-# if NLS_LANG is not set, get it from the Windows registry.
-if nls_lang.nil? and defined? OCI8::Win32Util
- dll_path = OCI8::Win32Util.dll_path.upcase
- if dll_path =~ %r{\\BIN\\OCI.DLL}
- oracle_home = $`
- OCI8::Win32Util.enum_homes do |home, lang|
- if oracle_home == home.upcase
- nls_lang = lang
- break
+ if nls_lang.is_a? String and nls_lang.length == 0
+ nls_lang = nil
+ end
+
+ # if NLS_LANG is not set, get it from the Windows registry.
+ if nls_lang.nil? and defined? OCI8::Win32Util
+ dll_path = OCI8::Win32Util.dll_path.upcase
+ if dll_path =~ %r{\\BIN\\OCI.DLL}
+ oracle_home = $`
+ OCI8::Win32Util.enum_homes do |home, lang|
+ if oracle_home == home.upcase
+ nls_lang = lang
+ break
+ end
end
end
end
-end
-# extract the charset name.
-if nls_lang
- if nls_lang =~ /\.([[:alnum:]]+)$/
- charset = $1.upcase
+ # extract the charset name.
+ if nls_lang
+ if nls_lang =~ /\.([[:alnum:]]+)$/
+ @@client_charset_name = $1.upcase
+ else
+ raise "Invalid NLS_LANG format: #{nls_lang}"
+ end
else
- raise "Invalid NLS_LANG format: #{nls_lang}"
+ warn "Warning: NLS_LANG is not set. fallback to US7ASCII."
+ @@client_charset_name = 'US7ASCII'
end
-else
- warn "Warning: NLS_LANG is not set. fallback to US7ASCII."
- charset = 'US7ASCII'
-end
-# NLS ratio, maximum number of bytes per one chracter
-case charset
-when 'UTF8'
- OCI8.nls_ratio = 3
-when 'AL16UTF16'
- OCI8.nls_ratio = 4
-when /^[[:alpha:]]+(\d+)/
- # convert maximum number of bits per one chracter to NLS ratio.
- # charset name max bits max bytes
- # ------------ -------- ---------
- # US7ASCII 7 1
- # WE8ISO8859P1 8 1
- # JA16SJIS 16 2
- # AL32UTF8 32 4
- # ...
- OCI8.nls_ratio = (($1.to_i + 7) >> 3)
-else
- raise "Unknown NLS character set name format: #{charset}"
-end
+ # NLS ratio, maximum number of bytes per one chracter
+ case @@client_charset_name
+ when 'UTF8'
+ OCI8.nls_ratio = 3
+ when 'AL16UTF16'
+ OCI8.nls_ratio = 4
+ when /^[[:alpha:]]+(\d+)/
+ # convert maximum number of bits per one chracter to NLS ratio.
+ # charset name max bits max bytes
+ # ------------ -------- ---------
+ # US7ASCII 7 1
+ # WE8ISO8859P1 8 1
+ # JA16SJIS 16 2
+ # AL32UTF8 32 4
+ # ...
+ OCI8.nls_ratio = (($1.to_i + 7) >> 3)
+ else
+ raise "Unknown NLS character set name format: #{@@client_charset_name}"
+ end
-# Ruby encoding name for ruby 1.9.
-if OCI8.respond_to? :encoding
- if defined? DEFAULT_OCI8_ENCODING
- enc = DEFAULT_OCI8_ENCODING
- else
- require 'yaml'
- enc = YAML::load_file(File.dirname(__FILE__) + '/encoding.yml')[charset]
- if enc.nil?
- raise "Ruby encoding name is not found in encoding.yml for NLS_LANG #{nls_lang}."
+ # Ruby encoding name for ruby 1.9.
+ if OCI8.respond_to? :encoding
+ if defined? DEFAULT_OCI8_ENCODING
+ enc = DEFAULT_OCI8_ENCODING
+ else
+ require 'yaml'
+ enc = YAML::load_file(File.dirname(__FILE__) + '/encoding.yml')[@@client_charset_name]
+ if enc.nil?
+ raise "Ruby encoding name is not found in encoding.yml for NLS_LANG #{nls_lang}."
+ end
+ if enc.is_a? Array
+ # Use the first available encoding in the array.
+ enc = enc.find do |e| Encoding.find(e) rescue false; end
+ end
end
- if enc.is_a? Array
- # Use the first available encoding in the array.
- enc = enc.find do |e| Encoding.find(e) rescue false; end
- end
+ OCI8.encoding = enc
end
- OCI8.encoding = enc
end
Modified: trunk/ruby-oci8/lib/oci8/oci8.rb
===================================================================
--- trunk/ruby-oci8/lib/oci8/oci8.rb 2011-12-04 07:35:19 UTC (rev 474)
+++ trunk/ruby-oci8/lib/oci8/oci8.rb 2011-12-05 12:08:51 UTC (rev 475)
@@ -343,6 +343,16 @@
charset_id2name(server_handle.send(:attr_get_ub2, OCI_ATTR_CHARSET_ID))
end
+ # :call-seq:
+ # OCI8.client_charset_name -> string
+ #
+ # (new in 2.1.0)
+ #
+ # Returns the client character set name.
+ def self.client_charset_name
+ @@client_charset_name
+ 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/lib/oci8/properties.rb
===================================================================
--- trunk/ruby-oci8/lib/oci8/properties.rb 2011-12-04 07:35:19 UTC (rev 474)
+++ trunk/ruby-oci8/lib/oci8/properties.rb 2011-12-05 12:08:51 UTC (rev 475)
@@ -19,7 +19,7 @@
raise IndexError, "No such property name: #{name}" unless @@properties.has_key?(name)
case name
when :length_semantic
- if val != :byte and val != char
+ if val != :byte and val != :char
raise ArgumentError, "Invalid property value #{val} for :length_semantics."
end
when :bind_string_as_nchar
Modified: trunk/ruby-oci8/test/config.rb
===================================================================
--- trunk/ruby-oci8/test/config.rb 2011-12-04 07:35:19 UTC (rev 474)
+++ trunk/ruby-oci8/test/config.rb 2011-12-05 12:08:51 UTC (rev 475)
@@ -6,7 +6,7 @@
$dbname = nil
# for test_bind_string_as_nchar in test_encoding.rb
-ENV['ORA_NCHAR_LITERAL_REPLACE'] = 'TRUE' if OCI8.encoding.name == "UTF-8"
+ENV['ORA_NCHAR_LITERAL_REPLACE'] = 'TRUE' if OCI8.client_charset_name.include? 'UTF8'
# test_clob.rb
Modified: trunk/ruby-oci8/test/test_all.rb
===================================================================
--- trunk/ruby-oci8/test/test_all.rb 2011-12-04 07:35:19 UTC (rev 474)
+++ trunk/ruby-oci8/test/test_all.rb 2011-12-05 12:08:51 UTC (rev 475)
@@ -6,6 +6,7 @@
require "#{srcdir}/test_oradate"
require "#{srcdir}/test_oranumber"
+require "#{srcdir}/test_bind_string"
require "#{srcdir}/test_bind_time"
require "#{srcdir}/test_bind_raw"
if $test_clob
Added: trunk/ruby-oci8/test/test_bind_string.rb
===================================================================
--- trunk/ruby-oci8/test/test_bind_string.rb (rev 0)
+++ trunk/ruby-oci8/test/test_bind_string.rb 2011-12-05 12:08:51 UTC (rev 475)
@@ -0,0 +1,100 @@
+# -*- coding: utf-8 -*-
+require 'oci8'
+require 'test/unit'
+require File.dirname(__FILE__) + '/config'
+
+class TestBindString < Test::Unit::TestCase
+ def setup
+ @conn = get_oci8_connection
+ end
+
+ def teardown
+ @conn.logoff
+ end
+
+ if OCI8.client_charset_name.include? 'UTF8'
+
+ 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))")
+
+ orig_prop = OCI8.properties[:bind_string_as_nchar]
+ begin
+ utf8_string = "a¡あ"
+
+ OCI8.properties[:bind_string_as_nchar] = false
+ @conn.exec("INSERT INTO test_table VALUES (1, N'#{utf8_string}', N'#{utf8_string}', :1)", utf8_string)
+ v, nv1, nv2 = @conn.select_one('select V, NV1, NV2 from test_table where ID = 1')
+ assert_not_equal(utf8_string, v) # Some UTF-8 chracters should be garbled.
+ assert_equal(utf8_string, 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'#{utf8_string}', N'#{utf8_string}', :1)", utf8_string)
+ v, nv1, nv2 = @conn.select_one('select V, NV1, NV2 from test_table where ID = 2')
+ assert_not_equal(utf8_string, v) # Some UTF-8 chracters should be garbled.
+ assert_equal(utf8_string, 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
+
+ def test_length_semantics # This test needs to be revised.
+ orig_prop = OCI8.properties[:length_semantics]
+ begin
+ utf8_string = "a¡あ"
+
+ OCI8.properties[:length_semantics] = :byte
+ assert_equal(:byte, OCI8.properties[:length_semantics])
+ cursor = @conn.parse <<EOS
+DECLARE
+ TMP VARCHAR2(6);
+BEGIN
+ TMP := :in;
+ :out := TMP;
+END;
+EOS
+ cursor.bind_param(:in, utf8_string)
+ cursor.bind_param(:out, nil, String, 5)
+ begin
+ cursor.exec
+ rescue OCIError
+ assert_equal(6502, $!.code)
+ end
+ cursor.bind_param(:out, nil, String, 6)
+ cursor.exec
+ assert_equal(utf8_string, cursor[:out])
+
+ OCI8.properties[:length_semantics] = :char
+ assert_equal(:char, OCI8.properties[:length_semantics])
+ cursor = @conn.parse <<EOS
+DECLARE
+ TMP VARCHAR2(6);
+BEGIN
+ TMP := :in;
+ :out := TMP;
+END;
+EOS
+ cursor.bind_param(:in, utf8_string, String, 3)
+ cursor.bind_param(:out, nil, String, 2)
+ begin
+ cursor.exec
+ rescue OCIError
+ assert_equal(6502, $!.code)
+ end
+ cursor.bind_param(:out, nil, String, 3)
+ cursor.exec
+ assert_equal(utf8_string, cursor[:out])
+ ensure
+ OCI8.properties[:length_semantics] = orig_prop
+ end
+ end
+ end
+end
Modified: trunk/ruby-oci8/test/test_encoding.rb
===================================================================
--- trunk/ruby-oci8/test/test_encoding.rb 2011-12-04 07:35:19 UTC (rev 474)
+++ trunk/ruby-oci8/test/test_encoding.rb 2011-12-05 12:08:51 UTC (rev 475)
@@ -96,37 +96,6 @@
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