From 963ccfb6064a06da8a1488c8ca2e3bfd4d980ba1 Mon Sep 17 00:00:00 2001 From: liuxh-Desktop-U24 Date: Mon, 30 Nov 2026 16:10:53 +0800 Subject: [PATCH] add utl_raw docs --- .../utl_raw_impl.adoc | 50 ++++++++++++++ .../oracle_compatibility/compat_utl_raw.adoc | 67 +++++++++++++++++++ .../utl_raw_impl.adoc | 50 ++++++++++++++ .../oracle_compatibility/compat_utl_raw.adoc | 66 ++++++++++++++++++ 4 files changed, 233 insertions(+) create mode 100644 CN/modules/ROOT/pages/master/compatibility_features_design/utl_raw_impl.adoc create mode 100644 CN/modules/ROOT/pages/master/oracle_compatibility/compat_utl_raw.adoc create mode 100644 EN/modules/ROOT/pages/master/compatibility_features_design/utl_raw_impl.adoc create mode 100644 EN/modules/ROOT/pages/master/oracle_compatibility/compat_utl_raw.adoc diff --git a/CN/modules/ROOT/pages/master/compatibility_features_design/utl_raw_impl.adoc b/CN/modules/ROOT/pages/master/compatibility_features_design/utl_raw_impl.adoc new file mode 100644 index 00000000..2df0ebc2 --- /dev/null +++ b/CN/modules/ROOT/pages/master/compatibility_features_design/utl_raw_impl.adoc @@ -0,0 +1,50 @@ +:sectnums: +:sectnumlevels: 5 + += UTL_RAW 实现说明 + +== 目的 + +`ivorysql_ora` 提供 Oracle 兼容的 `UTL_RAW.CAST_TO_RAW`,将文本的数据库编码字节作为 `RAW` 返回。调用示例见 xref:master/oracle_compatibility/compat_utl_raw.adoc[UTL_RAW 包]。 + +== 实现说明 + +=== 调用与编码 + +包规范声明 `CAST_TO_RAW(c IN VARCHAR2) RETURN RAW`,包体在 PL/iSQL 中执行以下表达式。 + +[source,sql] +---- +RETURN pg_catalog.convert_to(c::text, pg_catalog.getdatabaseencoding()); +---- + +. 将输入转为 `text`,并用 `getdatabaseencoding()` 取得当前数据库编码。 +. `convert_to()` 以数据库编码作为源编码。目标编码也取数据库编码,因此输出保留原字节。以 `LATIN1` 编码存储的 `é` 返回单字节 `e9`。 +. `convert_to()` 的结果为 `bytea`,包按声明返回 `sys.raw`。`sys.raw` 是基于 `bytea` 的域类型。 + +=== 显示与空值 + +`RAW` 沿用 `bytea_output` 的显示格式。默认以 `\x` 开头显示十六进制字节串。需要返回十六进制文本时可调用 xref:master/oracle_builtin_functions/rawtohex.adoc[RAWTOHEX]。 + +`convert_to()` 对 `NULL` 输入返回 `NULL`。空字符串的结果取决于传入包函数的值,分别对应 `NULL` 或零字节的 `RAW`。 + +== 相关文件与验证 + +以下路径相对于 `contrib/ivorysql_ora/`。 + +[cols="2,3", options="header"] +|=== +|文件 |职责 + +|`src/builtin_packages/utl_raw/utl_raw--1.0.sql` +|包规范、字节序常量及 PL/iSQL 函数体。 + +|`src/builtin_packages/utl_raw/utl_raw.c` +|保留模块说明。转换由包体调用内置函数完成。 + +|`ivorysql_ora_merge_sqls` +|将包定义合并到扩展安装脚本。 + +|`sql/utl_raw.sql`、`expected/utl_raw.out` +|回归用例及预期输出,覆盖 ASCII、多字节文本、空值、SQL 中引用包常量的错误以及 `LATIN1` 字节保留。 +|=== diff --git a/CN/modules/ROOT/pages/master/oracle_compatibility/compat_utl_raw.adoc b/CN/modules/ROOT/pages/master/oracle_compatibility/compat_utl_raw.adoc new file mode 100644 index 00000000..7498086d --- /dev/null +++ b/CN/modules/ROOT/pages/master/oracle_compatibility/compat_utl_raw.adoc @@ -0,0 +1,67 @@ +:sectnums: +:sectnumlevels: 5 +:note-caption: 注意 + += UTL_RAW 包 + +== 目的 + +`UTL_RAW.CAST_TO_RAW` 将 `VARCHAR2` 文本转换为 `RAW`,保留文本在当前数据库编码中的字节。 + +== 功能说明 + +需要将文本的原始字节写入 `RAW` 列,或传给接收 `RAW` 参数的函数时,可以使用 `CAST_TO_RAW`。 + +`UTL_RAW` 由 `ivorysql_ora` 扩展提供。以下示例使用 IvorySQL 的 `psql` 连接 Oracle 兼容端口(默认 1521),该端口会自动为会话启用 Oracle 兼容模式。 + +=== 接口 + +[source,sql] +---- +FUNCTION CAST_TO_RAW(c IN VARCHAR2) RETURN RAW; +---- + +输入 `NULL` 时返回 `NULL`。空字符串由 `ivorysql.enable_emptystring_to_NULL` 控制。该参数为默认值 `on` 时返回 `NULL`,设为 `off` 时返回零字节的 `RAW`。 + +=== 兼容范围 + +==== 函数 + +IvorySQL 当前只实现了 Oracle `UTL_RAW` 中的 `CAST_TO_RAW` 函数。原版包还提供 `CAST_TO_VARCHAR2`、`CONVERT`、`BIT_AND` 和 `CONCAT` 等函数。 https://docs.oracle.com/en/database/oracle/oracle-database/19/arpls/UTL_RAW.html[Oracle UTL_RAW 参考] + +==== 常量 + +Oracle 的二进制数值转换函数用以下常量指定多字节数值的字节顺序。 + +- `BIG_ENDIAN`(1)表示大端序,最高有效字节在前。 +- `LITTLE_ENDIAN`(2)表示小端序,最低有效字节在前。 +- `MACHINE_ENDIAN`(3)表示使用数据库服务器的本机字节序。 + +IvorySQL 保留了这三个常量。`CAST_TO_RAW` 按原顺序返回文本字节。 + +[NOTE] +==== +这些常量可在 PL/iSQL 中引用。直接在 SQL 的 `SELECT` 中引用会报无效标识符。 +==== + +== 使用示例 + +=== 转换 ASCII 文本 + +[source,sql] +---- +SELECT UTL_RAW.CAST_TO_RAW('ABC') AS raw_value, + RAWTOHEX(UTL_RAW.CAST_TO_RAW('ABC')) AS hex_value; +---- + +默认十六进制显示格式下,`raw_value` 为 `\x414243`。`hex_value` 为 `414243`,是 xref:master/oracle_builtin_functions/rawtohex.adoc[RAWTOHEX] 返回的十六进制文本。 + +=== 转换多字节文本 + +[source,sql] +---- +SELECT pg_catalog.getdatabaseencoding() AS db_encoding, + pg_catalog.encode(UTL_RAW.CAST_TO_RAW('你好'), 'hex') AS raw_hex; +---- + +数据库编码为 `UTF8` 时,`raw_hex` 为 `e4bda0e5a5bd`。其他数据库编码下,结果反映该编码中的字节。 diff --git a/EN/modules/ROOT/pages/master/compatibility_features_design/utl_raw_impl.adoc b/EN/modules/ROOT/pages/master/compatibility_features_design/utl_raw_impl.adoc new file mode 100644 index 00000000..67251e1b --- /dev/null +++ b/EN/modules/ROOT/pages/master/compatibility_features_design/utl_raw_impl.adoc @@ -0,0 +1,50 @@ +:sectnums: +:sectnumlevels: 5 + += UTL_RAW Implementation + +== Purpose + +`ivorysql_ora` provides Oracle-compatible `UTL_RAW.CAST_TO_RAW` to return text bytes in the database encoding as `RAW`. For examples, see xref:master/oracle_compatibility/compat_utl_raw.adoc[UTL_RAW Package]. + +== Implementation + +=== Calls and Encoding + +The package specification declares `CAST_TO_RAW(c IN VARCHAR2) RETURN RAW`. Its PL/iSQL body evaluates the following expression. + +[source,sql] +---- +RETURN pg_catalog.convert_to(c::text, pg_catalog.getdatabaseencoding()); +---- + +. Cast the input to `text` and retrieve the database encoding with `getdatabaseencoding()`. +. `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`. +. `convert_to()` produces `bytea`, which the package returns as its declared type, `sys.raw`. `sys.raw` is a domain over `bytea`. + +=== Display and Null Values + +`RAW` uses the `bytea_output` display format. By default it appears as a hexadecimal string prefixed with `\x`. Use xref:master/oracle_builtin_functions/rawtohex.adoc[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. + +== Related Files and Tests + +Paths are relative to `contrib/ivorysql_ora/`. + +[cols="2,3", options="header"] +|=== +|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`. +|=== diff --git a/EN/modules/ROOT/pages/master/oracle_compatibility/compat_utl_raw.adoc b/EN/modules/ROOT/pages/master/oracle_compatibility/compat_utl_raw.adoc new file mode 100644 index 00000000..f7a0715b --- /dev/null +++ b/EN/modules/ROOT/pages/master/oracle_compatibility/compat_utl_raw.adoc @@ -0,0 +1,66 @@ +:sectnums: +:sectnumlevels: 5 + += UTL_RAW Package + +== Purpose + +`UTL_RAW.CAST_TO_RAW` converts `VARCHAR2` text to `RAW`, preserving the text bytes in the current database encoding. + +== Functionality + +Use `CAST_TO_RAW` to store text bytes in a `RAW` column or pass them to a function that takes a `RAW` argument. + +The `ivorysql_ora` extension provides `UTL_RAW`. The examples use IvorySQL's `psql` on the Oracle compatibility port, which defaults to 1521 and selects Oracle compatibility mode for the session. + +=== Interface + +[source,sql] +---- +FUNCTION CAST_TO_RAW(c IN VARCHAR2) RETURN RAW; +---- + +A `NULL` input returns `NULL`. The `ivorysql.enable_emptystring_to_NULL` setting controls empty strings. With the default `on`, an empty string returns `NULL`. With `off`, it returns a zero-byte `RAW` value. + +=== Oracle Compatibility Scope + +==== Functions + +IvorySQL currently implements only the `CAST_TO_RAW` function from Oracle's `UTL_RAW` package. Oracle also provides functions such as `CAST_TO_VARCHAR2`, `CONVERT`, `BIT_AND`, and `CONCAT`. https://docs.oracle.com/en/database/oracle/oracle-database/19/arpls/UTL_RAW.html[Oracle UTL_RAW Reference] + +==== Constants + +Oracle's binary numeric conversions use these constants to specify the byte order for multibyte values. + +- `BIG_ENDIAN` (1) puts the most significant byte first. +- `LITTLE_ENDIAN` (2) puts the least significant byte first. +- `MACHINE_ENDIAN` (3) uses the database server's native byte order. + +IvorySQL retains these constants. `CAST_TO_RAW` returns the text bytes in their original order. + +[NOTE] +==== +PL/iSQL can reference these constants. Referencing them directly in a SQL `SELECT` raises an invalid identifier error. +==== + +== Examples + +=== Convert ASCII Text + +[source,sql] +---- +SELECT UTL_RAW.CAST_TO_RAW('ABC') AS raw_value, + RAWTOHEX(UTL_RAW.CAST_TO_RAW('ABC')) AS hex_value; +---- + +With the default hexadecimal display format, `raw_value` appears as `\x414243`. The xref:master/oracle_builtin_functions/rawtohex.adoc[RAWTOHEX] function returns the hexadecimal text `414243` in `hex_value`. + +=== Convert Multibyte Text + +[source,sql] +---- +SELECT pg_catalog.getdatabaseencoding() AS db_encoding, + pg_catalog.encode(UTL_RAW.CAST_TO_RAW('你好'), 'hex') AS raw_hex; +---- + +In a `UTF8` database, `raw_hex` is `e4bda0e5a5bd`. Other database encodings return the bytes for that encoding.