Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
:sectnums:
:sectnumlevels: 5

= UTL_URL 包

== 目的

`UTL_URL` 包用于对 URL 进行百分号编码和解码。

== 功能说明

=== ESCAPE

[source,sql]
----
UTL_URL.ESCAPE(url IN VARCHAR2,
escape_reserved_chars IN BOOLEAN DEFAULT FALSE,
url_charset IN VARCHAR2 DEFAULT NULL) RETURN VARCHAR2
----

`ESCAPE` 将 URL 中需要转义的字节转换为 `%XX` 格式。字母、数字及 `- _ . ! ~ * ' ( )` 始终保持原样。保留字符 `; / ? : @ & = + $ ,` 仅在 `escape_reserved_chars` 为 `TRUE` 时转义;`FALSE` 或 `NULL` 时保留。空格、`#`、`%`、其他非法 ASCII 字符和非 ASCII 字节始终转义,已有的 `%XX` 也会再次转义。

`url_charset` 指定转义前转换使用的字符集,可使用 PostgreSQL 编码名或支持的字符集别名。省略或传入 `NULL` 均使用数据库编码。`url` 为 `NULL` 时返回 `NULL`。

[source,sql]
----
SELECT utl_url.escape('a b/c?d=e&f'); -- a%20b/c?d=e&f
SELECT utl_url.escape('a b/c?d=e&f', TRUE); -- a%20b%2Fc%3Fd%3De%26f
SELECT utl_url.escape('中文', FALSE, 'UTF8'); -- %E4%B8%AD%E6%96%87
SELECT utl_url.escape('a%20b'); -- a%2520b
----

=== UNESCAPE

[source,sql]
----
UTL_URL.UNESCAPE(url IN VARCHAR2,
url_charset IN VARCHAR2 DEFAULT NULL) RETURN VARCHAR2
----

`UNESCAPE` 将 `%XX` 序列解码为字节,再按 `url_charset` 指定的编码转换为数据库编码;连续的转义字节可以组成多字节字符。省略字符集或传入 `NULL` 均使用数据库编码。未转义的文字保持原样,`+` 不会转换为空格。函数只解码一层;`url` 为 `NULL` 时返回 `NULL`。

[source,sql]
----
SELECT utl_url.unescape('a%20b%2Fc'); -- a b/c
SELECT utl_url.unescape('%E4%B8%AD%E6%96%87', 'UTF8'); -- 中文
SELECT utl_url.unescape('a%2520b'); -- a%20b
----

百分号后缺少两个十六进制数字、包含 `%00`、字符集不受支持或字节序列无法按指定编码转换时会报错。编码和解码时应使用相同的字符集。

== Oracle 兼容性

与 Oracle 省略字符集时使用 `utl_http.body_charset` 的行为不同,IvorySQL 在省略字符集或显式传入 `NULL` 时均使用数据库编码。非法百分号序列会报错,但不保证使用 Oracle 的 `UTL_URL.BAD_URL` 异常;`%00` 也会被拒绝,因为数据库文本不能包含 NUL 字节。
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
:sectnums:
:sectnumlevels: 5

= UTL_URL package

The `UTL_URL` package encodes and decodes URLs using percent escapes.

== Feature Description

=== ESCAPE

[source,sql]
----
UTL_URL.ESCAPE(url IN VARCHAR2,
escape_reserved_chars IN BOOLEAN DEFAULT FALSE,
url_charset IN VARCHAR2 DEFAULT NULL) RETURN VARCHAR2
----

Converts bytes requiring escaping to `%XX`. Letters, digits and `- _ . ! ~ * ' ( )` are never escaped. Reserved characters `; / ? : @ & = + $ ,` are escaped only when `escape_reserved_chars` is `TRUE`; `FALSE` and `NULL` leave them unchanged. Spaces, `#`, `%`, other illegal ASCII characters and non-ASCII bytes are always escaped. Existing `%XX` sequences are escaped again.

`url_charset` specifies the encoding used before escaping; PostgreSQL encoding names and supported charset aliases are accepted. An omitted or NULL charset selects the database encoding. A NULL `url` returns NULL.

[source,sql]
----
SELECT utl_url.escape('a b/c?d=e&f'); -- a%20b/c?d=e&f
SELECT utl_url.escape('a b/c?d=e&f', TRUE); -- a%20b%2Fc%3Fd%3De%26f
SELECT utl_url.escape('中文', FALSE, 'UTF8'); -- %E4%B8%AD%E6%96%87
SELECT utl_url.escape('a%20b'); -- a%2520b
----

=== UNESCAPE

[source,sql]
----
UTL_URL.UNESCAPE(url IN VARCHAR2,
url_charset IN VARCHAR2 DEFAULT NULL) RETURN VARCHAR2
----

Decodes `%XX` sequences into bytes and converts them from `url_charset` to the database encoding. Consecutive escaped bytes can form multibyte characters. An omitted or NULL charset selects the database encoding. Literal characters remain unchanged and `+` is not converted to a space. Only one layer is decoded; a NULL `url` returns NULL.

[source,sql]
----
SELECT utl_url.unescape('a%20b%2Fc'); -- a b/c
SELECT utl_url.unescape('%E4%B8%AD%E6%96%87', 'UTF8'); -- 中文
SELECT utl_url.unescape('a%2520b'); -- a%20b
----

An incomplete or non-hexadecimal percent escape, `%00`, an unsupported charset, or bytes invalid for the specified encoding cause an error. Use the same charset for encoding and decoding.

== Oracle compatibility

Unlike Oracle, which uses `utl_http.body_charset` when the charset is omitted, IvorySQL uses the database encoding both for an omitted charset and an explicit NULL. Malformed escapes raise an error, but the Oracle `UTL_URL.BAD_URL` exception is not guaranteed. `%00` is rejected because database text cannot contain a NUL byte.
Loading