Skip to content

Commit 77ba7e8

Browse files
committed
Compatible with Oracle's sys_context function
- add sys_context(varchar2, varchar2) and sys_context(varchar2, varchar2, number) - Currently supports querying some parameters in USERENV and SYS_SESSION_ROLES, as well as the existing configuration parameters in pg_settings, the rest return NULL.
1 parent 00e95cc commit 77ba7e8

1 file changed

Lines changed: 158 additions & 0 deletions

File tree

contrib/ivorysql_ora/src/builtin_functions/builtin_functions--1.0.sql

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,3 +1194,161 @@ RETURNS sys.number
11941194
AS 'select sys.to_number($1::text, $2::text)'
11951195
LANGUAGE SQL IMMUTABLE PARALLEL SAFE STRICT;
11961196

1197+
1198+
/* Begin - SYS_CONTEXT */
1199+
create or replace function sys.sys_context(a varchar2, b varchar2)
1200+
return varchar2 as $$
1201+
declare
1202+
res varchar2;
1203+
begin
1204+
if upper(a) = 'USERENV' then
1205+
case upper(b)
1206+
when 'CURRENT_SCHEMA' then
1207+
select current_schema() into res;
1208+
when 'CURRENT_SCHEMAID' then
1209+
select current_schema()::regnamespace::oid into res;
1210+
when 'SESSION_USER' then
1211+
select session_user into res;
1212+
when 'SESSION_USERID' then
1213+
select session_user::regrole::oid into res;
1214+
when 'PROXY_USER' then
1215+
select session_user into res;
1216+
when 'PROXY_USERID' then
1217+
select session_user::regrole::oid into res;
1218+
when 'CURRENT_USER' then
1219+
select current_user into res;
1220+
when 'CURRENT_USERID' then
1221+
select current_user::regrole::oid into res;
1222+
when 'CURRENT_EDITION_NAME' then
1223+
select version() into res;
1224+
when 'CLIENT_PROGRAM_NAME' then
1225+
select application_name into res from pg_stat_activity where pid = pg_backend_pid();
1226+
when 'IP_ADDRESS' then
1227+
select client_addr into res from pg_stat_activity where pid = pg_backend_pid();
1228+
when 'HOST' then
1229+
select client_hostname into res from pg_stat_activity where pid = pg_backend_pid();
1230+
when 'ISDBA' then
1231+
select sys.get_isdba() into res;
1232+
when 'LANGUAGE' then
1233+
select sys.get_language() into res;
1234+
when 'LANG' then
1235+
select sys.get_lang() into res;
1236+
when 'NLS_CURRENCY' then
1237+
select null into res;-- show nls_currency into res;
1238+
when 'NLS_DATE_FORMAT' then
1239+
show nls_date_format into res;
1240+
when 'NLS_DATE_LANGUAGE' then
1241+
select null into res;-- show nls_iso_currency into res;
1242+
when 'NLS_SORT' then
1243+
select null into res;-- show nls_sort into res;
1244+
when 'NLS_TERRITORY' then
1245+
select null into res;-- show nls_territory into res;
1246+
when 'ORACLE_HOME' then
1247+
show data_directory into res;
1248+
when 'PLATFORM_SLASH' then
1249+
select
1250+
case
1251+
when substring((select setting from pg_settings where name = 'data_directory') from 1 for 1) = '/' then 'LINUX'
1252+
else 'WINDOWS'
1253+
end into res;
1254+
when 'DB_NAME' then
1255+
select current_database into res;
1256+
when 'SESSION_DEFAULT_COLLATION' then
1257+
select null into res;-- show default_collation into res;
1258+
when 'SID' then
1259+
select sys.get_sid() into res;
1260+
when 'ACTION' then select NULL into res;
1261+
when 'IS_APPLICATION_ROOT' then select NULL into res;
1262+
when 'IS_APPLICATION_PDB' then select NULL into res;
1263+
when 'AUDITED_CURSORID' then select NULL into res;
1264+
when 'AUTHENTICATED_IDENTITY' then select NULL into res;
1265+
when 'AUTHENTICATION_DATA' then select NULL into res;
1266+
when 'AUTHENTICATION_METHOD' then select NULL into res;
1267+
when 'BG_JOB_ID' then select NULL into res;
1268+
when 'CDB_DOMAIN' then select NULL into res;
1269+
when 'CDB_NAME' then select NULL into res;
1270+
when 'CLIENT_IDENTIFIER' then select NULL into res;
1271+
when 'CLIENT_INFO' then
1272+
select sys.get_client_info() into res;
1273+
when 'CON_ID' then select NULL into res;
1274+
when 'CON_NAME' then select NULL into res;
1275+
when 'CURRENT_BIND' then select NULL into res;
1276+
when 'CURRENT_EDITION_ID' then select NULL into res;
1277+
when 'CURRENT_SQL' then select NULL into res;
1278+
when 'CURRENT_SQL1' then select NULL into res;
1279+
when 'CURRENT_SQL2' then select NULL into res;
1280+
when 'CURRENT_SQL3' then select NULL into res;
1281+
when 'CURRENT_SQL4' then select NULL into res;
1282+
when 'CURRENT_SQL5' then select NULL into res;
1283+
when 'CURRENT_SQL6' then select NULL into res;
1284+
when 'CURRENT_SQL7' then select NULL into res;
1285+
when 'CURRENT_SQL_LENGTH' then select NULL into res;
1286+
when 'DATABASE_ROLE' then select NULL into res;
1287+
when 'DB_DOMAIN' then select NULL into res;
1288+
when 'DB_SUPPLEMENTAL_LOG_LEVEL' then select NULL into res;
1289+
when 'DB_UNIQUE_NAME' then select NULL into res;
1290+
when 'DBLINK_INFO' then select NULL into res;
1291+
when 'DRAIN_STATUS' then select NULL into res;
1292+
when 'ENTRYID' then
1293+
select sys.get_entryid() into res;
1294+
when 'ENTERPRISE_IDENTITY' then select NULL into res;
1295+
when 'FG_JOB_ID' then select NULL into res;
1296+
when 'GLOBAL_CONTEXT_MEMORY' then select NULL into res;
1297+
when 'GLOBAL_UID' then select NULL into res;
1298+
when 'IDENTIFICATION_TYPE' then select NULL into res;
1299+
when 'INSTANCE' then select NULL into res;
1300+
when 'INSTANCE_NAME' then select NULL into res;
1301+
when 'IS_APPLY_SERVER' then select 'FALSE' into res;
1302+
when 'IS_DG_ROLLING_UPGRADE' then select 'FALSE' into res;
1303+
when 'LDAP_SERVER_TYPE' then select NULL into res;
1304+
when 'MODULE' then select NULL into res;
1305+
when 'NETWORK_PROTOCOL' then select NULL into res;
1306+
when 'NLS_CALENDAR' then select NULL into res;
1307+
when 'OS_USER' then select NULL into res;
1308+
when 'POLICY_INVOKER' then select NULL into res;
1309+
when 'PROXY_ENTERPRISE_IDENTITY' then select NULL into res;
1310+
when 'SCHEDULER_JOB' then select NULL into res;
1311+
when 'SERVER_HOST' then select NULL into res;
1312+
when 'SERVICE_NAME' then select NULL into res;
1313+
when 'SESSION_EDITION_ID' then select NULL into res;
1314+
when 'SESSION_EDITION_NAME' then select NULL into res;
1315+
when 'SESSIONID' then
1316+
select sys.get_sessionid() into res;
1317+
when 'STATEMENTID' then select NULL into res;
1318+
when 'TERMINAL' then
1319+
select sys.get_terminal() into res;
1320+
when 'UNIFIED_AUDIT_SESSIONID' then select NULL into res;
1321+
else
1322+
RAISE EXCEPTION 'invalid USERENV parameter: %', b;
1323+
end case;
1324+
elsif upper(a) = 'SYS_SESSION_ROLES' then
1325+
case upper(b)
1326+
when 'DBA' then
1327+
select sys.get_isdba() into res;
1328+
when 'LOGIN' then
1329+
select case when rolcanlogin = 't' then 'TRUE' else 'FALSE' end into res from pg_roles where oid = current_user::regrole::oid;
1330+
when 'CREATEROLE' then
1331+
select case when rolcreaterole = 't' then 'TRUE' else 'FALSE' end into res from pg_roles where oid = current_user::regrole::oid;
1332+
when 'CREATEDB' then
1333+
select case when rolcreatedb = 't' then 'TRUE' else 'FALSE' end into res from pg_roles where oid = current_user::regrole::oid;
1334+
else
1335+
RAISE EXCEPTION 'invalid SYS_SESSION_ROLES parameter: %', b;
1336+
end case;
1337+
else
1338+
select current_setting(a||'.'||b, true) into res;
1339+
end if;
1340+
return res;
1341+
end;
1342+
$$ LANGUAGE plisql SECURITY INVOKER;
1343+
1344+
1345+
create or replace function sys.sys_context(a varchar2, b varchar2, c number)
1346+
return varchar2 as $$
1347+
declare
1348+
res varchar2;
1349+
begin
1350+
select left(sys.sys_context(a, b), c::integer) into res;
1351+
return res;
1352+
end;
1353+
$$ LANGUAGE plisql SECURITY INVOKER;
1354+
/* End - SYS_CONTEXT */

0 commit comments

Comments
 (0)