|
| 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