很多語言,包括英語在內,都使用沉音字符(accented character)。因為這些字符不屬于 ASCII 字符集,所以假如不查看 Unicode 值也不使用 Unicode 編輯器并將其轉成一個已知字符集,就很難編寫使用這些字符的代碼。
Oracle9i 引入了 COMPOSE 函數,該函數接受一串 Unicode 字符并規則化其文本。這就意味著它可以接受一個字母和一個組合標記,比如說‘a'(Unicode 字符0097)和沉音符(Unicode 字符0300),然后創建一個單獨的由兩個標記組合而成的字符。COMPOSE 使用非凡的組合標記,而沒有使用 ASCII 中相應的音節標記,它所使用的非凡的組合標記是 Unicode 標準 的一部分。上面的例子的結果應該是 Unicode 字符00E0(有一個沉音符的小寫拉丁字母‘a')。
在 ANSI 中最常見的組合字符有:
· U+0300:沉音符(grave accent)( ` )
· U+0301:重音符(acute accent)( ' )
· U+0302:抑揚音符號(circumflex accent)(^)
· U+0303:顎化符號(tilde)(~)
· U+0308:元音變音
假如沒有非凡的軟件或者鍵盤驅動程序的話,很難在鍵盤上輸入 Unicode 字符0097和0300。因此,以純 ASCII 文本輸入 Unicode 序列的一個方法是使用 UNISTR 函數。這個函數接受一個 ASCII 字符串然后以國家字符集(通常作為16位 Unicode 或者 UTF-8 字符集安裝)創建一個 Unicode 字符的序列。它使用十六進制占位符序列映射任何非 ASCII 字符,映射方式與 java 類似。
要輸入 a 后接一個沉音符組合字符的序列,可以使用 UNISTR(‘a/0300'),而不要試圖直接在代碼中輸入字符。這個函數在任何字符集以及任何具有基于 Unicode 的國家字符集的數據庫下都可以正常運行。可以將多個組合字符放在函數中——可以在 UNISTR 函數中混合使用 ASCII 和 Unicode 占位符。例如,可以像下面這樣使用 UNISTR 函數:
select COMPOSE(UNISTR('Unless you are nai/0308ve, meet me at the cafe/0301 with your re/0301sume/0301.')) from dual;
在將 UNISTR 函數的輸出與 COMPOSE 組合時,可以在不查找任何值的情況下生成一個 Unicode 字符。例如:
select 'it is true' if compose(unistr('a/0300')) = unistr('/00e0');
COMPOSE 函數返回一個NVARCHAR2 字符串,返回的NVARCHAR2 字符串通常是基于 Unicode 的。假如是在本地使用這些字符,在結果中具有一個隱式地 TO_CHAR 時,數據庫將嘗試將 Unicode 字符映射到本地字符集。不是所有的字符都可以被映射,有一些字符組合在 COMPOSE 中不能工作,因為 Unicode 協會沒有在 Oracle 所用的級別定義它們。
要快速地檢查字符如何在一個特定的環境下查詢,可以運行一個與下面的腳本類似的腳本,以查看在輸出組合字符如何被映射。你可能需要確定一下NLS_LANG 設置以確保這些字符正確地返回:
create or replace type hexrange_tbl as table of varchar2(4);
/
show errors;
create or replace function hexrange(n1 varchar2,n2 varchar2)
return hexrange_tbl pipelined
is
begin
for i in to_number(n1,'000X') .. to_number(n2,'000X') loop
pipe row(to_char(i,'FM000X'));
end loop;
return;
end hexrange;
/
show errors;
select column_value composer,
compose(unistr('a/'column_value)) a,
compose(unistr('c/'column_value)) c,
compose(unistr('e/'column_value)) e,
compose(unistr('i/'column_value)) i,
compose(unistr('n/'column_value)) n,
compose(unistr('o/'column_value)) o,
compose(unistr('r/'column_value)) r,
compose(unistr('s/'column_value)) s,
compose(unistr('u/'column_value)) u,
compose(unistr('y/'column_value)) y
from table(hexrange('0300','0327')) x;
下面輕松一下,這里有一小段 PL/SQL 腳本,這段腳本使用COMPOSE 和UNISTR 創建一種非凡效果,很多 SMS 用戶、黑客和垃圾郵件發送者都使用這種效果使可讀英文文本難于掃描,因為它使用字符重音版本的一個隨機序列。
我使用DBMS_RANDOM 隨機選取一個可由不同字符使用的組合字符,然后讓 SQL 進行組合并進行反向轉換以生成 ANSI/Latin-1 輸出。這段腳本在代碼中使用了 EMP 表的 ENAME 字段。
set serveroutput on;
declare
-- these combinations work under ANSI, at least
a_comb nvarchar2(50) := unistr('/0300/0301/0302/0303/0308/ 030A ');
c_comb nvarchar2(50) := unistr('/0327');
e_comb nvarchar2(50) := unistr('/0300/0301/0302/0308');
i_comb nvarchar2(50) := unistr('/0300/0301/0308');
n_comb nvarchar2(50) := unistr('/0303');
o_comb nvarchar2(50) := unistr('/0300/0301/0302/0303/0308');
u_comb nvarchar2(50) := unistr('/0300/0301/0302/0308');
y_comb nvarchar2(50) := unistr('/0301/0308');
l_idx integer;
l_ename nvarchar2(50);
ch nchar;
l_junk varchar2(50);
begin
dbms_random.initialize(to_char(sysdate,'SSSSS'));
for row in (select ename from emp) loop
l_ename := row.ename;
l_junk := null;
for i in 1..length(l_ename) loop
ch := substr(l_ename,i,1);
case lower(ch)
when 'a' then
l_junk := l_junk compose(ch substr(a_comb,
mod(abs(dbms_random.random),length(a_comb)) + 1,1));
when 'c' then
l_junk := l_junk compose(ch substr(c_comb,
mod(abs(dbms_random.random),length(c_comb)) + 1,1));
when 'e' then
l_junk := l_junk compose(ch substr(e_comb,
mod(abs(dbms_random.random),length(e_comb)) + 1,1));
when 'i' then
l_junk := l_junk compose(ch substr(i_comb,
mod(abs(dbms_random.random),length(i_comb)) + 1,1));
when 'n' then
l_junk := l_junk compose(ch substr(n_comb,
mod(abs(dbms_random.random),length(n_comb)) + 1,1));
when 'o' then
l_junk := l_junk compose(ch substr(o_comb,
mod(abs(dbms_random.random),length(o_comb)) + 1,1));
when 'u' then
l_junk := l_junk compose(ch substr(u_comb,
mod(abs(dbms_random.random),length(u_comb)) + 1,1));
when 'y' then
l_junk := l_junk compose(ch substr(y_comb,
mod(abs(dbms_random.random),length(y_comb)) + 1,1));
else
l_junk := l_junk ch;
end case;
end loop;
dbms_output.put_line(to_char(l_junk));
end loop;
end;
/
show errors;