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