From 0c90f6769de6a60f842c916d49b404d03bcc503a Mon Sep 17 00:00:00 2001 From: Andrew Dunstan Date: Sat, 9 May 2015 13:06:49 -0400 Subject: Add new OID alias type regrole The new type has the scope of whole the database cluster so it doesn't behave the same as the existing OID alias types which have database scope, concerning object dependency. To avoid confusion constants of the new type are prohibited from appearing where dependencies are made involving it. Also, add a note to the docs about possible MVCC violation and optimization issues, which are general over the all reg* types. Kyotaro Horiguchi --- src/backend/utils/adt/regproc.c | 104 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) (limited to 'src/backend/utils/adt/regproc.c') diff --git a/src/backend/utils/adt/regproc.c b/src/backend/utils/adt/regproc.c index 11d663b295d..8b5672875c2 100644 --- a/src/backend/utils/adt/regproc.c +++ b/src/backend/utils/adt/regproc.c @@ -40,6 +40,7 @@ #include "utils/lsyscache.h" #include "utils/syscache.h" #include "utils/tqual.h" +#include "utils/acl.h" static char *format_operator_internal(Oid operator_oid, bool force_qualify); static char *format_procedure_internal(Oid procedure_oid, bool force_qualify); @@ -1553,6 +1554,109 @@ regdictionarysend(PG_FUNCTION_ARGS) return oidsend(fcinfo); } +/* + * regrolein - converts "rolename" to role OID + * + * We also accept a numeric OID, for symmetry with the output routine. + * + * '-' signifies unknown (OID 0). In all other cases, the input must + * match an existing pg_authid entry. + * + * This function is not needed in bootstrap mode, so we don't worry about + * making it work then. + */ +Datum +regrolein(PG_FUNCTION_ARGS) +{ + char *role_name_or_oid = PG_GETARG_CSTRING(0); + Oid result; + + /* '-' ? */ + if (strcmp(role_name_or_oid, "-") == 0) + PG_RETURN_OID(InvalidOid); + + /* Numeric OID? */ + if (role_name_or_oid[0] >= '0' && + role_name_or_oid[0] <= '9' && + strspn(role_name_or_oid, "0123456789") == strlen(role_name_or_oid)) + { + result = DatumGetObjectId(DirectFunctionCall1(oidin, + CStringGetDatum(role_name_or_oid))); + PG_RETURN_OID(result); + } + + /* Normal case: see if the name matches any pg_authid entry. */ + result = get_role_oid(role_name_or_oid, false); + + PG_RETURN_OID(result); +} + +/* + * to_regrole - converts "rolename" to role OID + * + * If the name is not found, we return NULL. + */ +Datum +to_regrole(PG_FUNCTION_ARGS) +{ + char *role_name = PG_GETARG_CSTRING(0); + Oid result; + + result = get_role_oid(role_name, true); + + if (OidIsValid(result)) + PG_RETURN_OID(result); + else + PG_RETURN_NULL(); +} + +/* + * regroleout - converts role OID to "role_name" + */ +Datum +regroleout(PG_FUNCTION_ARGS) +{ + Oid roleoid = PG_GETARG_OID(0); + char *result; + + + if (roleoid == InvalidOid) + { + result = pstrdup("-"); + PG_RETURN_CSTRING(result); + } + + result = GetUserNameFromId(roleoid, true); + if (!result) + { + /* If OID doesn't match any role, return it numerically */ + result = (char *) palloc(NAMEDATALEN); + snprintf(result, NAMEDATALEN, "%u", roleoid); + } + PG_RETURN_CSTRING(result); +} + +/* + * regrolerecv - converts external binary format to regrole + */ +Datum +regrolerecv(PG_FUNCTION_ARGS) +{ + /* Exactly the same as oidrecv, so share code */ + return oidrecv(fcinfo); +} + +/* + * regrolesend - converts regrole to binary format + */ +Datum +regrolesend(PG_FUNCTION_ARGS) +{ + /* Exactly the same as oidsend, so share code */ + return oidsend(fcinfo); +} + + /* * text_regclass: convert text to regclass -- cgit v1.2.3