Skip to content

Commit 164860b

Browse files
committed
add userenv .adoc
1 parent 9a19c93 commit 164860b

File tree

4 files changed

+116
-0
lines changed

4 files changed

+116
-0
lines changed

CN/modules/ROOT/nav.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
**** xref:master/6.3.5.adoc[NLS 参数]
2929
*** 内置函数
3030
**** xref:master/6.4.1.adoc[sys_context]
31+
**** xref:master/6.4.2.adoc[userenv]
3132
*** xref:master/6.5.adoc[国标GB18030]
3233
** Oracle兼容功能列表
3334
*** xref:master/7.1.adoc[1、框架设计]
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
:sectnums:
3+
:sectnumlevels: 5
4+
5+
6+
= **功能概述**
7+
8+
IvorySQL提供兼容Oracle内置函数`USERENV('parameter')`,用于返回当前会话的相关信息。
9+
这是一个遗留函数,建议使用 SYS_CONTEXT 函数及其内置的 USERENV 命名空间来获取当前功能。
10+
11+
== 实现原理
12+
13+
`USERENV` 通过Bison规则解析函数调用,检查参数合法性并映射到对应的具体SQL函数。
14+
解析规则在 `ora_gram.y` 中实现,逻辑如下:
15+
```c
16+
USERENV '(' Sconst ')'
17+
{
18+
char *normalized_param = downcase_identifier($3, strlen($3), true, true);
19+
20+
#define CHECK_AND_CALL(param, func_name) \
21+
if (strcmp(normalized_param, param) == 0) \
22+
$$ = (Node *) makeFuncCall(OracleSystemFuncName(func_name), NIL, COERCE_EXPLICIT_CALL, @1);
23+
24+
CHECK_AND_CALL("client_info", "get_client_info")
25+
else CHECK_AND_CALL("entryid", "get_entryid")
26+
else CHECK_AND_CALL("terminal", "get_terminal")
27+
else CHECK_AND_CALL("isdba", "get_isdba")
28+
else CHECK_AND_CALL("lang", "get_lang")
29+
else CHECK_AND_CALL("language", "get_language")
30+
else CHECK_AND_CALL("sessionid", "get_sessionid")
31+
else CHECK_AND_CALL("sid", "get_sid")
32+
else
33+
ereport(ERROR,
34+
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
35+
errmsg("invalid USERENV parameter: \"%s\".", $3)));
36+
#undef CHECK_AND_CALL
37+
}
38+
```
39+
具体功能则是在 `builtin_functions--1.0.sql` 中实现。例如:
40+
```sql
41+
CREATE OR REPLACE FUNCTION sys.get_language()
42+
RETURNS varchar2
43+
AS $$
44+
SELECT (regexp_split_to_array(current_setting('lc_monetary'), '\.'))[1]||'.'||pg_client_encoding();
45+
$$ LANGUAGE sql STRICT;
46+
```
47+
48+
== USERENV支持的参数
49+
[cols="2,8"]
50+
|====
51+
|*参数名称*|*返回值*
52+
|sid | 当前session的用户ID。
53+
|sessionid | 返回审计会话标识符。
54+
|language | 返回数据库当前会话的语言、地域和字符集。
55+
|lang | 返回ISO缩写语言名称。
56+
|isdba | 如果用户已经被认证为管理员;或者是通过操作系统或口令文件具有管理员特权的,返回“TRUE",否则返回"FALSE"。
57+
|====

EN/modules/ROOT/nav.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
*** xref:master/6.3.5.adoc[NLS Parameters]
2828
** Built-in Functions
2929
*** xref:master/6.4.1.adoc[sys_context]
30+
*** xref:master/6.4.2.adoc[userenv]
3031
** xref:master/6.5.adoc[GB18030 Character Set]
3132
* List of Oracle compatible features
3233
** xref:master/7.1.adoc[1、Ivorysql frame design]
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
:sectnums:
2+
:sectnumlevels: 5
3+
4+
= **Feature Overview**
5+
6+
IvorySQL provides compatibility with Oracle's built-in function `USERENV('parameter')`, which is used to return information about the current session.
7+
This is a legacy function, and IvorySQL recommends taht you can use the `SYS_CONTEXT` function with its built-in `USERENV` namespace for current functionality.
8+
9+
== Implementation Principle
10+
11+
`USERENV` parses function calls through Bison rules, checks parameter validity, and maps them to corresponding SQL functions.
12+
The parsing rules are implemented in `ora_gram.y`, with the following logic:
13+
14+
```c
15+
USERENV '(' Sconst ')'
16+
{
17+
char *normalized_param = downcase_identifier($3, strlen($3), true, true);
18+
19+
#define CHECK_AND_CALL(param, func_name) \
20+
if (strcmp(normalized_param, param) == 0) \
21+
$$ = (Node *) makeFuncCall(OracleSystemFuncName(func_name), NIL, COERCE_EXPLICIT_CALL, @1);
22+
23+
CHECK_AND_CALL("client_info", "get_client_info")
24+
else CHECK_AND_CALL("entryid", "get_entryid")
25+
else CHECK_AND_CALL("terminal", "get_terminal")
26+
else CHECK_AND_CALL("isdba", "get_isdba")
27+
else CHECK_AND_CALL("lang", "get_lang")
28+
else CHECK_AND_CALL("language", "get_language")
29+
else CHECK_AND_CALL("sessionid", "get_sessionid")
30+
else CHECK_AND_CALL("sid", "get_sid")
31+
else
32+
ereport(ERROR,
33+
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
34+
errmsg("invalid USERENV parameter: \"%s\".", $3)));
35+
#undef CHECK_AND_CALL
36+
}
37+
```
38+
The specific functionality is implemented in `builtin_functions--1.0.sql`. For example:
39+
```sql
40+
CREATE OR REPLACE FUNCTION sys.get_language()
41+
RETURNS varchar2
42+
AS $$
43+
SELECT (regexp_split_to_array(current_setting('lc_monetary'), '\\.'))[1]||'.'||pg_client_encoding();
44+
$$ LANGUAGE sql STRICT;
45+
```
46+
47+
== Supported Parameters for USERENV
48+
49+
[cols="3,7"]
50+
|====
51+
|Parameter Name|Return Value
52+
|sid | The user ID of the current session.
53+
|sessionid | Returns the audit session identifier.
54+
|language | Returns the language, territory, and character set of the current database session.
55+
|lang | Returns the ISO abbreviation of the language name.
56+
|isdba | Returns "TRUE" if the user has been authenticated as an administrator or has administrator privileges through the operating system or password file; otherwise, returns "FALSE".
57+
|====

0 commit comments

Comments
 (0)