UTL_URL package

The UTL_URL package encodes and decodes URLs using percent escapes.

1. Feature Description

1.1. ESCAPE

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.

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

1.2. UNESCAPE

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.

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.

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