Skip to content

Commit aa38434

Browse files
committed
Refactor routines for name lookups of procedures and operators
This introduces a new set of extended routines for procedure and operator name lookups, with a flag bitmask argument that can modify the result. The following options are available: - Force schema qualification, ignoring search_path. This is similar to the existing option for format_{operator|procedure}_qualified(). - Force NULL as result instead of a numeric OID for an undefined object. This option is new. This is a refactoring similar to 1185c78, that will be used for a future patch to improve the SQL functions providing information using object addresses for undefined objects. Author: Michael Paquier Reviewed-by: Aleksander Alekseev, Dmitry Dolgov, Daniel Gustafsson, Álvaro Herrera Discussion: https://postgr.es/m/CAB7nPqSZxrSmdHK-rny7z8mi=EAFXJ5J-0RbzDw6aus=wB5azQ@mail.gmail.com
1 parent 04c7f41 commit aa38434

File tree

2 files changed

+56
-20
lines changed

2 files changed

+56
-20
lines changed

src/backend/utils/adt/regproc.c

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@
4141
#include "utils/syscache.h"
4242
#include "utils/varlena.h"
4343

44-
static char *format_operator_internal(Oid operator_oid, bool force_qualify);
45-
static char *format_procedure_internal(Oid procedure_oid, bool force_qualify);
4644
static void parseNameAndArgTypes(const char *string, bool allowNone,
4745
List **names, int *nargs, Oid *argtypes);
4846

@@ -323,24 +321,32 @@ to_regprocedure(PG_FUNCTION_ARGS)
323321
char *
324322
format_procedure(Oid procedure_oid)
325323
{
326-
return format_procedure_internal(procedure_oid, false);
324+
return format_procedure_extended(procedure_oid, 0);
327325
}
328326

329327
char *
330328
format_procedure_qualified(Oid procedure_oid)
331329
{
332-
return format_procedure_internal(procedure_oid, true);
330+
return format_procedure_extended(procedure_oid, FORMAT_PROC_FORCE_QUALIFY);
333331
}
334332

335333
/*
334+
* format_procedure_extended - converts procedure OID to "pro_name(args)"
335+
*
336+
* This exports the useful functionality of regprocedureout for use
337+
* in other backend modules. The result is a palloc'd string, or NULL.
338+
*
336339
* Routine to produce regprocedure names; see format_procedure above.
337340
*
338-
* force_qualify says whether to schema-qualify; if true, the name is always
339-
* qualified regardless of search_path visibility. Otherwise the name is only
340-
* qualified if the function is not in path.
341+
* The following bits in 'flags' modify the behavior:
342+
* - FORMAT_PROC_INVALID_AS_NULL
343+
* if the procedure OID is invalid or unknown, return NULL instead
344+
* of the numeric OID.
345+
* - FORMAT_PROC_FORCE_QUALIFY
346+
* always schema-qualify procedure names, regardless of search_path
341347
*/
342-
static char *
343-
format_procedure_internal(Oid procedure_oid, bool force_qualify)
348+
char *
349+
format_procedure_extended(Oid procedure_oid, bits16 flags)
344350
{
345351
char *result;
346352
HeapTuple proctup;
@@ -365,7 +371,8 @@ format_procedure_internal(Oid procedure_oid, bool force_qualify)
365371
* Would this proc be found (given the right args) by regprocedurein?
366372
* If not, or if caller requests it, we need to qualify it.
367373
*/
368-
if (!force_qualify && FunctionIsVisible(procedure_oid))
374+
if ((flags & FORMAT_PROC_FORCE_QUALIFY) == 0 &&
375+
FunctionIsVisible(procedure_oid))
369376
nspname = NULL;
370377
else
371378
nspname = get_namespace_name(procform->pronamespace);
@@ -379,7 +386,7 @@ format_procedure_internal(Oid procedure_oid, bool force_qualify)
379386
if (i > 0)
380387
appendStringInfoChar(&buf, ',');
381388
appendStringInfoString(&buf,
382-
force_qualify ?
389+
(flags & FORMAT_PROC_FORCE_QUALIFY) != 0 ?
383390
format_type_be_qualified(thisargtype) :
384391
format_type_be(thisargtype));
385392
}
@@ -389,6 +396,11 @@ format_procedure_internal(Oid procedure_oid, bool force_qualify)
389396

390397
ReleaseSysCache(proctup);
391398
}
399+
else if ((flags & FORMAT_PROC_INVALID_AS_NULL) != 0)
400+
{
401+
/* If object is undefined, return NULL as wanted by caller */
402+
result = NULL;
403+
}
392404
else
393405
{
394406
/* If OID doesn't match any pg_proc entry, return it numerically */
@@ -747,13 +759,20 @@ to_regoperator(PG_FUNCTION_ARGS)
747759
}
748760

749761
/*
750-
* format_operator - converts operator OID to "opr_name(args)"
762+
* format_operator_extended - converts operator OID to "opr_name(args)"
751763
*
752764
* This exports the useful functionality of regoperatorout for use
753-
* in other backend modules. The result is a palloc'd string.
765+
* in other backend modules. The result is a palloc'd string, or NULL.
766+
*
767+
* The following bits in 'flags' modify the behavior:
768+
* - FORMAT_OPERATOR_INVALID_AS_NULL
769+
* if the operator OID is invalid or unknown, return NULL instead
770+
* of the numeric OID.
771+
* - FORMAT_OPERATOR_FORCE_QUALIFY
772+
* always schema-qualify operator names, regardless of search_path
754773
*/
755-
static char *
756-
format_operator_internal(Oid operator_oid, bool force_qualify)
774+
char *
775+
format_operator_extended(Oid operator_oid, bits16 flags)
757776
{
758777
char *result;
759778
HeapTuple opertup;
@@ -776,7 +795,8 @@ format_operator_internal(Oid operator_oid, bool force_qualify)
776795
* Would this oper be found (given the right args) by regoperatorin?
777796
* If not, or if caller explicitly requests it, we need to qualify it.
778797
*/
779-
if (force_qualify || !OperatorIsVisible(operator_oid))
798+
if ((flags & FORMAT_OPERATOR_FORCE_QUALIFY) != 0 ||
799+
!OperatorIsVisible(operator_oid))
780800
{
781801
nspname = get_namespace_name(operform->oprnamespace);
782802
appendStringInfo(&buf, "%s.",
@@ -787,15 +807,15 @@ format_operator_internal(Oid operator_oid, bool force_qualify)
787807

788808
if (operform->oprleft)
789809
appendStringInfo(&buf, "%s,",
790-
force_qualify ?
810+
(flags & FORMAT_OPERATOR_FORCE_QUALIFY) != 0 ?
791811
format_type_be_qualified(operform->oprleft) :
792812
format_type_be(operform->oprleft));
793813
else
794814
appendStringInfoString(&buf, "NONE,");
795815

796816
if (operform->oprright)
797817
appendStringInfo(&buf, "%s)",
798-
force_qualify ?
818+
(flags & FORMAT_OPERATOR_FORCE_QUALIFY) != 0 ?
799819
format_type_be_qualified(operform->oprright) :
800820
format_type_be(operform->oprright));
801821
else
@@ -805,6 +825,11 @@ format_operator_internal(Oid operator_oid, bool force_qualify)
805825

806826
ReleaseSysCache(opertup);
807827
}
828+
else if ((flags & FORMAT_OPERATOR_INVALID_AS_NULL) != 0)
829+
{
830+
/* If object is undefined, return NULL as wanted by caller */
831+
result = NULL;
832+
}
808833
else
809834
{
810835
/*
@@ -820,13 +845,14 @@ format_operator_internal(Oid operator_oid, bool force_qualify)
820845
char *
821846
format_operator(Oid operator_oid)
822847
{
823-
return format_operator_internal(operator_oid, false);
848+
return format_operator_extended(operator_oid, 0);
824849
}
825850

826851
char *
827852
format_operator_qualified(Oid operator_oid)
828853
{
829-
return format_operator_internal(operator_oid, true);
854+
return format_operator_extended(operator_oid,
855+
FORMAT_OPERATOR_FORCE_QUALIFY);
830856
}
831857

832858
void

src/include/utils/regproc.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@
1515

1616
#include "nodes/pg_list.h"
1717

18+
/* Control flags for format_procedure_extended */
19+
#define FORMAT_PROC_INVALID_AS_NULL 0x01 /* NULL if undefined */
20+
#define FORMAT_PROC_FORCE_QUALIFY 0x02 /* force qualification */
21+
extern char *format_procedure_extended(Oid procedure_oid, bits16 flags);
22+
23+
/* Control flags for format_operator_extended */
24+
#define FORMAT_OPERATOR_INVALID_AS_NULL 0x01 /* NULL if undefined */
25+
#define FORMAT_OPERATOR_FORCE_QUALIFY 0x02 /* force qualification */
26+
extern char *format_operator_extended(Oid operator_oid, bits16 flags);
27+
1828
extern List *stringToQualifiedNameList(const char *string);
1929
extern char *format_procedure(Oid procedure_oid);
2030
extern char *format_procedure_qualified(Oid procedure_oid);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy