Oracle でこのテーブルを指定すると、
create table test (bytes raw(100), chset varchar2(50))
insert into test (bytes, chset) values (hextoraw('454647'), 'iso-8859-1')
またはMSSQLで
create table test (bytes varbinary(100), chset nvarchar(50))
insert into test (bytes, chset) values (0x454647, 'iso-8859-1')
Java のテキスト エンコーディング サポートを利用する Java で Oracle 用の UDF を作成する方法の包括的な例を探しています。
MSSQL では、次の .Net アセンブリを作成します。
using System.Text;
using Microsoft.SqlServer.Server;
namespace Whatever
{
public class Common
{
[SqlFunction]
public static string Decode(byte[] Bytes, string EncodingName)
{
return Encoding.GetEncoding(EncodingName).GetString(Bytes);
}
}
}
次のコマンドを使用して、アセンブリを登録し、udf を定義します。
create assembly MyAssembly from '...\MyAssembly.dll'
create function decode(@bytes varbinary(max), @chset nvarchar(100))
returns nvarchar(max) as external name MyAssembly.[Whatever.Common].Decode
次のようなクエリで使用します。
> select *, dbo.decode(bytes, chset) decoded from test
bytes chset decoded
0x454647 iso-8859-1 EFG
アップデート
これまでのところ、この Java クラスを作成しました。
import java.nio.*;
import java.nio.charset.*;
public class Common
{
public static String Decode(byte[] Bytes, String CharsetName)
{
return Charset.forName(CharsetName).decode(ByteBuffer.wrap(Bytes)).toString();
}
}
これらのコマンドを使用して UDF を作成しました。
create directory jdir as 'C:\...';
create java class using bfile (jdir, 'Common.class');
create function jdecode(bytes raw, chset varchar2) return nvarchar2 as language java
name 'Common.Decode(java.lang.byte[], java.lang.String) return java.lang.String';
しかし、それを使用しようとすると、次のエラーが発生します。
> select jdecode(hextoraw('454647'), 'iso-8859-1') from dual
ORA-29531: no method Decode in class Common
更新 2
java.lang.byte[] は問題ではないことがわかりました。それを単に byte[] に変更すると、機能するようになりました。ありがとうティム!
create function jdecode(bytes raw, chset varchar2) return nvarchar2 as language java
name 'Common.Decode(byte[], java.lang.String) return java.lang.String';
ここに便利な表があります: http://download.oracle.com/docs/cd/B19306_01/java.102/b14187/chsix.htm#BABJIJEB