PostgreSQL Oracle 兼容性之 - COMPOSE , UNISTR , DECOMPOSE

背景

参考
http://www.th7.cn/db/Oracle/2011-06-30/8490.shtml

很多语言,包括英语在内,都使用沉音字符(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(‘a300'),而不要试图直接在代码中输入字符。

这个函数在任何字符集以及任何具有基于 Unicode 的国家字符集的数据库下都可以正常运行。

可以将多个组合字符放在函数中――可以在 UNISTR 函数中混合使用 ASCII 和 Unicode 占位符。

例如,可以像下面这样使用 UNISTR 函数:

select COMPOSE(UNISTR('Unless you are nai308ve, meet me at the cafe301 with your re301sume301.')) from dual;

在将 UNISTR 函数的输出与 COMPOSE 组合时,可以在不查找任何值的情况下生成一个 Unicode 字符。
例如:

select 'it is true' if compose(unistr('a300')) = unistr('0e0');

UNISTR用法
输入编码得到unicode编码的字符
http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions204.htm
UNISTR(string)

UNISTR takes as its argument a text literal or an expression that resolves to character data and returns it in the national character set.
The national character set of the database can be either AL16UTF16 or UTF8.
UNISTR provides support for Unicode string literals by letting you specify the Unicode encoding value of characters in the string.
This is useful, for example, for inserting data into NCHAR columns.

The Unicode encoding value has the form '\xxxx' where 'xxxx' is the hexadecimal value of a character in UCS-2 encoding format.
Supplementary characters are encoded as two code units, the first from the high-surrogates range (U+D800 to U+DBFF), and the second from the low-surrogates range (U+DC00 to U+DFFF).
To include the backslash in the string itself, precede it with another backslash (\\).

For portability and data preservation, Oracle recommends that in the UNISTR string argument you specify only ASCII characters and the Unicode encoding values.

SELECT UNISTR('abc\00e5\00f1\00f6') FROM DUAL;

UNISTR
------
abcåñö

COMPOSE用法
将两个unicode编码的字符合成,例如字母与沉音符合成为另一个UNICODE字符
http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions025.htm
COMPOSE(char)

COMPOSE takes as its argument a string, or an expression that resolves to a string, in any datatype, and returns a Unicode string in its fully normalized form in the same character set as the input.
char can be any of the datatypes CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, or NCLOB. For example, an o code point qualified by an umlaut code point will be returned as the o-umlaut code point.

CLOB and NCLOB values are supported through implicit conversion. If char is a character LOB value, it is converted to a VARCHAR value before the COMPOSE operation.
The operation will fail if the size of the LOB value exceeds the supported length of the VARCHAR in the particular development environment.

SELECT COMPOSE ( 'o' || UNISTR('\0308') ) FROM DUAL; 

CO
--
ö

DECOMPOSE用法
将带有合成字符的字符串,解析成合成前的UNICODE字符串
http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions041.htm
DECOMPOSE(string)

DECOMPOSE is valid only for Unicode characters. DECOMPOSE takes as its argument a string in any datatype and returns a Unicode string after decomposition in the same character set as the input.
For example, an o-umlaut code point will be returned as the "o" code point followed by an umlaut code point.

SELECT DECOMPOSE ('Châteaux') FROM DUAL; 

DECOMPOSE
---------
Cha^teaux

PostgreSQL 支持compose, decompose, unistr

自定义映射关系
需要用户自定义合成字母与字母,unicode沉音符的映射关系, 进行编码后存在表或者代码中。

自定义函数
基于对应关系的编码,合成,或解码字符串。

unicode码表

http://unicode-table.com/cn

参考

http://unicode-table.com/cn

http://www.th7.cn/db/Oracle/2011-06-30/8490.shtml

Count

时间: 2024-07-28 17:47:41

PostgreSQL Oracle 兼容性之 - COMPOSE , UNISTR , DECOMPOSE的相关文章

PostgreSQL Oracle兼容性之 - plpgsql 自治事务(autonomous_transaction)补丁

PostgreSQL Oracle兼容性之 - plpgsql 自治事务(autonomous_transaction)补丁 作者 digoal 日期 2016-11-04 标签 PostgreSQL , autonomous_transaction , 自治事务 , Oracle兼容性 , plpgsql 背景 PostgreSQL的plpgsql服务端编程语言与Oracle数据库的pl/sql编程语言非常类似,但是对于自治事务一直没有语法层面的支持. 以往如果要支持自治事务,可以使用exce

PostgreSQL Oracle 兼容性之 - rowid (CREATE TABLE WITH OIDS)

标签 PostgreSQL , Oracle , 兼容性 , 行号 , rowid , oid , ctid 背景 Oracle的数据中,通过ROWID可以定位到一条记录,当记录没有发生行迁移时,ROWID是不变的,因此即使不使用PK,也能很好的定位到一条记录. PostgreSQL中,也有行号,CTID,由BLOCK_ID和ITEM_ID组成,即哪个数据块的哪条记录. 但是PostgreSQL的引擎为多版本引擎,因此一条记录在被更新后CTID会发生变化(代表了新的版本). 不管是Oracle还

PostgreSQL Oracle兼容性 之 - parser SQL保留|关键字(keywrods)大全

标签 PostgreSQL , keywords , 关键字 , Oracle 兼容性 背景 PostgreSQL数据库中有哪些关键字,这些关键字的使用限制如何? https://www.postgresql.org/docs/10/static/sql-keywords-appendix.html 文档中的说明并不是特别清晰,(并且KEYWORDS与版本强相关),所以使用pg_get_keywords这个系统函数得到的,更加准确可读. https://www.postgresql.org/do

PostgreSQL Oracle 兼容性之 - timestamp 与 numeric 的运算

标签 PostgreSQL , Oracle , 兼容性 , timestamp , numeric 背景 Oracle里面支持时间戳与数字的加减,数字默认单位为天. PostgreSQL 支持时间戳与interval类型进行加减.日期支持与整型做加减. 为了兼容Oracle(时间戳与数字加减),我们可以复写操作符来实现时间戳与数字的加减. 复写操作符 1.自定义几个函数,用于时间戳与数字的加减. postgres=# create or replace function timestamp_a

PostgreSQL Oracle 兼容性 之 TABLE、PIPELINED函数

标签 PostgreSQL , 返回表 , 返回复合类型 , 返回游标 背景 Oracle 通过table, pipelined函数,用于格式化返回类型为table的函数的结果. Table function concepts There a couple of steps to take when you are working with table functions. Like when you are working with normal tables you have to desc

PostgreSQL Oracle 兼容性之 - 系统列(ctid, oid, cmin, cmax, xmin, xmax)

标签 PostgreSQL , Oracle , 兼容性 , ctid , cmin , cmax , xmin , xmax , oid 背景 PostgreSQL中有一些系统列(即行的头部信息的列),例如物理行号,COMMAND ID,事务号,以及OID. 当我们建表时,不能使用冲突的列名,否则会报错: postgres=# create table a(ctid int); 错误: 42701: 字段名 "ctid" 与系统字段名冲突 LOCATION: CheckAttribu

PostgreSQL Oracle 兼容性 之 - PL/SQL record, table类型定义

背景 Oracle PL/SQL是非常强大的一门SQL编程语言,许多Oracle用户也使用它来处理一些要求延迟低且数据一致性或可靠性要求很高的业务逻辑. PostgreSQL也有一门非常高级的内置SQL编程语言,plpgsql.与Oracle PL/SQL语法极其类似,但是还是有一些不一样的地方.(PS:除了plpgsql,PostgreSQL还支持C,java,python,perl等流行的语言作为数据库的函数编程语言) 本文是针对有Oracle用户遇到的一些函数语法与PostgreSQL不兼

PostgreSQL Oracle 兼容性 之 USERENV

标签 PostgreSQL , Oracle , USERENV , 会话环境变量 背景 USERENV 是Oracle 用来获取当前会话变量的函数.官方是这么介绍的: https://docs.oracle.com/cd/E11882_01/server.112/e41084/functions184.htm#SQLRF06117 Describes the current session. The predefined parameters of namespace USERENV are

PostgreSQL Oracle兼容性 - 计算字符长度与字节长度(char(?) 与varchar(?)空格如何计算长度)

标签 PostgreSQL , Oracle , 字符长度 , 字节长度 , 空格 , varchar , char , 定长 , 变长 , 末尾追加空格 背景 由于多字节字符的存在,所以在数据库应用中,通常会出现两种计算字符串长度的需求: 1.计算字符串个数 2.计算字节数 在不同的数据库中,使用的函数不一样. 如何计算字符和字节个数 https://stackoverflow.com/questions/17062065/how-to-select-data-items-of-a-certa