diff options
author | Robert Haas <rhaas@postgresql.org> | 2014-04-08 10:27:56 -0400 |
---|---|---|
committer | Robert Haas <rhaas@postgresql.org> | 2014-04-08 10:27:56 -0400 |
commit | 0886fc6a5c75b294544263ea979b9cf6195407d9 (patch) | |
tree | 2cf04aae147dc80a980cab40a39d23af92822d0d /src/backend/utils/adt/regproc.c | |
parent | 7ca32e255ba4f7e55bbdbcf0df996ac62798672b (diff) | |
download | postgresql-0886fc6a5c75b294544263ea979b9cf6195407d9.tar.gz postgresql-0886fc6a5c75b294544263ea979b9cf6195407d9.zip |
Add new to_reg* functions for error-free OID lookups.
These functions won't throw an error if the object doesn't exist,
or if (for functions and operators) there's more than one matching
object.
Yugo Nagata and Nozomi Anzai, reviewed by Amit Khandekar, Marti
Raudsepp, Amit Kapila, and me.
Diffstat (limited to 'src/backend/utils/adt/regproc.c')
-rw-r--r-- | src/backend/utils/adt/regproc.c | 108 |
1 files changed, 104 insertions, 4 deletions
diff --git a/src/backend/utils/adt/regproc.c b/src/backend/utils/adt/regproc.c index 5d73562a4fc..ed2bdbfb097 100644 --- a/src/backend/utils/adt/regproc.c +++ b/src/backend/utils/adt/regproc.c @@ -153,6 +153,31 @@ regprocin(PG_FUNCTION_ARGS) } /* + * to_regproc - converts "proname" to proc OID + * + * If the name is not found, we return NULL. + */ +Datum +to_regproc(PG_FUNCTION_ARGS) +{ + char *pro_name = PG_GETARG_CSTRING(0); + List *names; + FuncCandidateList clist; + + /* + * Parse the name into components and see if it matches any pg_proc entries + * in the current search path. + */ + names = stringToQualifiedNameList(pro_name); + clist = FuncnameGetCandidates(names, -1, NIL, false, false, true); + + if (clist == NULL || clist->next != NULL) + PG_RETURN_NULL(); + + PG_RETURN_OID(clist->oid); +} + +/* * regprocout - converts proc OID to "pro_name" */ Datum @@ -502,7 +527,7 @@ regoperin(PG_FUNCTION_ARGS) * pg_operator entries in the current search path. */ names = stringToQualifiedNameList(opr_name_or_oid); - clist = OpernameGetCandidates(names, '\0'); + clist = OpernameGetCandidates(names, '\0', false); if (clist == NULL) ereport(ERROR, @@ -520,6 +545,31 @@ regoperin(PG_FUNCTION_ARGS) } /* + * to_regoper - converts "oprname" to operator OID + * + * If the name is not found, we return NULL. + */ +Datum +to_regoper(PG_FUNCTION_ARGS) +{ + char *opr_name = PG_GETARG_CSTRING(0); + List *names; + FuncCandidateList clist; + + /* + * Parse the name into components and see if it matches any pg_operator + * entries in the current search path. + */ + names = stringToQualifiedNameList(opr_name); + clist = OpernameGetCandidates(names, '\0', true); + + if (clist == NULL || clist->next != NULL) + PG_RETURN_NULL(); + + PG_RETURN_OID(clist->oid); +} + +/* * regoperout - converts operator OID to "opr_name" */ Datum @@ -558,7 +608,7 @@ regoperout(PG_FUNCTION_ARGS) * qualify it. */ clist = OpernameGetCandidates(list_make1(makeString(oprname)), - '\0'); + '\0', false); if (clist != NULL && clist->next == NULL && clist->oid == oprid) result = pstrdup(oprname); @@ -873,6 +923,33 @@ regclassin(PG_FUNCTION_ARGS) } /* + * to_regclass - converts "classname" to class OID + * + * If the name is not found, we return NULL. + */ +Datum +to_regclass(PG_FUNCTION_ARGS) +{ + char *class_name = PG_GETARG_CSTRING(0); + Oid result; + List *names; + + /* + * Parse the name into components and see if it matches any pg_class entries + * in the current search path. + */ + names = stringToQualifiedNameList(class_name); + + /* We might not even have permissions on this relation; don't lock it. */ + result = RangeVarGetRelid(makeRangeVarFromNameList(names), NoLock, true); + + if (OidIsValid(result)) + PG_RETURN_OID(result); + else + PG_RETURN_NULL(); +} + +/* * regclassout - converts class OID to "class_name" */ Datum @@ -1028,12 +1105,35 @@ regtypein(PG_FUNCTION_ARGS) * Normal case: invoke the full parser to deal with special cases such as * array syntax. */ - parseTypeString(typ_name_or_oid, &result, &typmod); + parseTypeString(typ_name_or_oid, &result, &typmod, false); PG_RETURN_OID(result); } /* + * to_regtype - converts "typename" to type OID + * + * If the name is not found, we return NULL. + */ +Datum +to_regtype(PG_FUNCTION_ARGS) +{ + char *typ_name = PG_GETARG_CSTRING(0); + Oid result; + int32 typmod; + + /* + * Invoke the full parser to deal with special cases such as array syntax. + */ + parseTypeString(typ_name, &result, &typmod, true); + + if (OidIsValid(result)) + PG_RETURN_OID(result); + else + PG_RETURN_NULL(); +} + +/* * regtypeout - converts type OID to "typ_name" */ Datum @@ -1523,7 +1623,7 @@ parseNameAndArgTypes(const char *string, bool allowNone, List **names, else { /* Use full parser to resolve the type name */ - parseTypeString(typename, &typeid, &typmod); + parseTypeString(typename, &typeid, &typmod, false); } if (*nargs >= FUNC_MAX_ARGS) ereport(ERROR, |