UTL_RAW Implementation

1. Purpose

ivorysql_ora provides Oracle-compatible UTL_RAW.CAST_TO_RAW to return text bytes in the database encoding as RAW. For examples, see UTL_RAW Package.

2. Implementation

2.1. Calls and Encoding

The package specification declares CAST_TO_RAW(c IN VARCHAR2) RETURN RAW. Its PL/iSQL body evaluates the following expression.

RETURN pg_catalog.convert_to(c::text, pg_catalog.getdatabaseencoding());
  1. Cast the input to text and retrieve the database encoding with getdatabaseencoding().

  2. convert_to() uses the database encoding as its source. The target is also the database encoding, so the output preserves the original bytes. For example, é stored in a LATIN1 database yields the single byte e9.

  3. convert_to() produces bytea, which the package returns as its declared type, sys.raw. sys.raw is a domain over bytea.

2.2. Display and Null Values

RAW uses the bytea_output display format. By default it appears as a hexadecimal string prefixed with \x. Use RAWTOHEX to return hexadecimal text.

convert_to() returns NULL for NULL input. The outcome for an empty string depends on the value passed to the package function, yielding either NULL or a zero-byte RAW value.

Paths are relative to contrib/ivorysql_ora/.

File Role

src/builtin_packages/utl_raw/utl_raw—​1.0.sql

Package specification, byte-order constants, and PL/iSQL function body.

src/builtin_packages/utl_raw/utl_raw.c

Module description. The package body performs the conversion through built-in functions.

ivorysql_ora_merge_sqls

Includes the package definition in the extension installation script.

sql/utl_raw.sql, expected/utl_raw.out

Regression coverage for ASCII, multibyte text, null input, the error when selecting a package constant in SQL, and byte preservation in LATIN1.