Skip to content

Commit

Permalink
new c function for not args
Browse files Browse the repository at this point in the history
Signed-off-by: pranav jain <[email protected]>
  • Loading branch information
pranavJ23 committed Jan 22, 2025
1 parent c2397ed commit 1b83874
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
6 changes: 2 additions & 4 deletions contrib/babelfishpg_tsql/sql/sys_functions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -294,10 +294,8 @@ LANGUAGE SQL IMMUTABLE PARALLEL RESTRICTED;

CREATE OR REPLACE FUNCTION sys.suser_name()
RETURNS sys.NVARCHAR(128)
AS $$
SELECT sys.suser_name_internal(-1);
$$
LANGUAGE SQL IMMUTABLE PARALLEL RESTRICTED;
AS 'babelfishpg_tsql', 'suser_name_current_session'
LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;

-- Since SIDs are currently not supported in Babelfish, this essentially behaves the same as suser_name but
-- with a different input data type
Expand Down
36 changes: 33 additions & 3 deletions contrib/babelfishpg_tsql/src/rolecmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,39 @@ get_original_login_name(char *login)
return result;
}

PG_FUNCTION_INFO_V1(suser_name_current_session);
Datum
suser_name_current_session(PG_FUNCTION_ARGS)
{
Oid server_user_id;
char *ret;
char *orig_loginname;

server_user_id = GetSessionUserId();

if (server_user_id == InvalidOid)
PG_RETURN_NULL();

ret = GetUserNameFromId(server_user_id, true);

if (!ret)
PG_RETURN_NULL();

if (!is_login(server_user_id))
{
pfree(ret);
PG_RETURN_NULL();
}

orig_loginname = get_original_login_name(ret);

pfree(ret);
if (!orig_loginname)
PG_RETURN_NULL();

PG_RETURN_TEXT_P(cstring_to_text(orig_loginname));
}

PG_FUNCTION_INFO_V1(suser_name);
Datum
suser_name(PG_FUNCTION_ARGS)
Expand All @@ -978,9 +1011,6 @@ suser_name(PG_FUNCTION_ARGS)

server_user_id = PG_ARGISNULL(0) ? InvalidOid : PG_GETARG_OID(0);

if (server_user_id == -1)
server_user_id = GetSessionUserId();

if (server_user_id == InvalidOid)
PG_RETURN_NULL();

Expand Down

0 comments on commit 1b83874

Please sign in to comment.