Skip to content

Commit f07f467

Browse files
committed
RAWTOHEX function implementation
1 parent 5b4875a commit f07f467

File tree

4 files changed

+114
-0
lines changed

4 files changed

+114
-0
lines changed

CN/modules/ROOT/nav.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
*** 内置函数
9494
**** xref:master/oracle_builtin_functions/sys_context.adoc[sys_context]
9595
**** xref:master/oracle_builtin_functions/userenv.adoc[userenv]
96+
**** xref:master/oracle_builtin_functions/rawtohex.adoc[rawtohex]
9697
*** xref:master/gb18030.adoc[国标GB18030]
9798
* 参考指南
9899
** xref:master/tools_reference.adoc[工具参考]
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2+
:sectnums:
3+
:sectnumlevels: 5
4+
5+
6+
= **功能概述**
7+
8+
IvorySQL提供兼容Oracle内置函数 ```RAWTOHEX('parameter')``` ,用于将RAW类型转换成十六进制字符串。
9+
10+
== 实现原理
11+
12+
PostgreSQL 提供的pg_catalog.encode(bytea, 'hex')函数,可直接完成二进制到十六进制的转换。
13+
考虑到当前系统中已经存在的HEXTORAW函数通过封装pg_catalog.decode 的SQL方式实现,本次开发的
14+
RAWTOHEX 函数将使用相同的方式来实现,即使用 SQL 函数包装 PostgreSQL 内置函数pg_catalog.encode,
15+
而非编写 C 扩展。
16+
17+
raw、text、bytea以及varchar2这四种类型做为输入需要被支持。
18+
sys.raw 是 bytea 的 domain 类型(typtype = 'd',typbasetype = bytea),PostgreSQL 支持 domain 到 base type 的隐式转换,RAWTOHEX(bytea) 可自动接受 sys.raw 输入。
19+
sys.oravarcharchar(即 varchar2)到 pg_catalog.text 存在 IMPLICIT cast(datatype--1.0.sql),RAWTOHEX(text) 可自动接受 varchar2 输入。
20+
因此,定义两个重载(而不是四个)。
21+
22+
```
23+
sys.rawtohex(bytea) RETURNS varchar2
24+
sys.rawtohex(text) RETURNS varchar2
25+
```
26+
27+
具体功能则是在 `builtin_functions--1.0.sql` 中实现。
28+
```sql
29+
/* support rawtohex function for oracle compatibility */
30+
CREATE OR REPLACE FUNCTION sys.rawtohex(bytea)
31+
RETURNS varchar2
32+
AS $$ SELECT CASE WHEN pg_catalog.octet_length($1) > 0 THEN upper(pg_catalog.encode($1, 'hex'))::varchar2 END; $$
33+
LANGUAGE SQL
34+
PARALLEL SAFE
35+
STRICT
36+
IMMUTABLE;
37+
38+
CREATE OR REPLACE FUNCTION sys.rawtohex(text)
39+
RETURNS varchar2
40+
AS $$ SELECT CASE WHEN pg_catalog.octet_length($1) > 0 THEN upper(pg_catalog.encode($1::bytea, 'hex'))::varchar2 END; $$
41+
LANGUAGE SQL
42+
PARALLEL SAFE
43+
STRICT
44+
IMMUTABLE;
45+
```
46+
47+
== RAWTOHEX 典型用例
48+
[cols="8,2"]
49+
|====
50+
|*用例语句*|*返回值*
51+
|SELECT sys.rawtohex('\xDEADBEEF'::bytea); | DEADBEEF
52+
|SELECT sys.rawtohex('\xFF'::raw); | FF
53+
|SELECT sys.rawtohex('hello'::text); | 68656C6C6F
54+
|SELECT sys.rawtohex('hello'::varchar2); | 68656C6C6F
55+
|====

EN/modules/ROOT/nav.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
** Built-in Functions
9494
*** xref:master/oracle_builtin_functions/sys_context.adoc[sys_context]
9595
*** xref:master/oracle_builtin_functions/userenv.adoc[userenv]
96+
*** xref:master/oracle_builtin_functions/rawtohex.adoc[rawtohex]
9697
** xref:master/gb18030.adoc[GB18030 Character Set]
9798
* Reference
9899
** xref:master/tools_reference.adoc[Tool Reference]
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
:sectnums:
3+
:sectnumlevels: 5
4+
5+
6+
= **Feature Overview**
7+
8+
IvorySQL provides compatibility with Oracle's built-in function ```RAWTOHEX('parameter')```, which is used to convert RAW to a character value containing its hexadecimal representation.
9+
10+
== Implementation Principle
11+
12+
The function pg_catalog.encode(bytea, 'hex') provided by PostgreSQL can directly convert binary data to hexadecimal.
13+
14+
Given that the existing HEXTORAW function in the current system is implemented by wrapping pg_catalog.decode using SQL, the RAWTOHEX function developed this time will be implemented in the same way. That is, it will be a SQL function wrapping the PostgreSQL built-in function pg_catalog.encode, rather than implementing it via a C extension.
15+
16+
The following four types need to be supported as input: raw, text, bytea, and varchar2.
17+
18+
sys.raw is a domain type of bytea (typtype = 'd', typbasetype = bytea). PostgreSQL supports implicit conversion from a domain type to its base type, so RAWTOHEX(bytea) can automatically accept sys.raw as input.
19+
20+
There is an IMPLICIT cast from sys.oravarcharchar (i.e., varchar2) to pg_catalog.text (defined in datatype--1.0.sql), so RAWTOHEX(text) can automatically accept varchar2 as input.
21+
22+
Therefore, two overloaded versions (rather than four) will be defined.
23+
24+
```
25+
sys.rawtohex(bytea) RETURNS varchar2
26+
sys.rawtohex(text) RETURNS varchar2
27+
```
28+
29+
The specific functionality is implemented in builtin_functions—1.0.sql.
30+
```sql
31+
/* support rawtohex function for oracle compatibility */
32+
CREATE OR REPLACE FUNCTION sys.rawtohex(bytea)
33+
RETURNS varchar2
34+
AS $$ SELECT CASE WHEN pg_catalog.octet_length($1) > 0 THEN upper(pg_catalog.encode($1, 'hex'))::varchar2 END; $$
35+
LANGUAGE SQL
36+
PARALLEL SAFE
37+
STRICT
38+
IMMUTABLE;
39+
40+
CREATE OR REPLACE FUNCTION sys.rawtohex(text)
41+
RETURNS varchar2
42+
AS $$ SELECT CASE WHEN pg_catalog.octet_length($1) > 0 THEN upper(pg_catalog.encode($1::bytea, 'hex'))::varchar2 END; $$
43+
LANGUAGE SQL
44+
PARALLEL SAFE
45+
STRICT
46+
IMMUTABLE;
47+
```
48+
49+
== RAWTOHEX use cases
50+
[cols="8,2"]
51+
|====
52+
|*SQL statement *|*return value*
53+
|SELECT sys.rawtohex('\xDEADBEEF'::bytea); | DEADBEEF
54+
|SELECT sys.rawtohex('\xFF'::raw); | FF
55+
|SELECT sys.rawtohex('hello'::text); | 68656C6C6F
56+
|SELECT sys.rawtohex('hello'::varchar2); | 68656C6C6F
57+
|====

0 commit comments

Comments
 (0)