41
41
#include "utils/syscache.h"
42
42
#include "utils/varlena.h"
43
43
44
- static char * format_operator_internal (Oid operator_oid , bool force_qualify );
45
- static char * format_procedure_internal (Oid procedure_oid , bool force_qualify );
46
44
static void parseNameAndArgTypes (const char * string , bool allowNone ,
47
45
List * * names , int * nargs , Oid * argtypes );
48
46
@@ -323,24 +321,32 @@ to_regprocedure(PG_FUNCTION_ARGS)
323
321
char *
324
322
format_procedure (Oid procedure_oid )
325
323
{
326
- return format_procedure_internal (procedure_oid , false );
324
+ return format_procedure_extended (procedure_oid , 0 );
327
325
}
328
326
329
327
char *
330
328
format_procedure_qualified (Oid procedure_oid )
331
329
{
332
- return format_procedure_internal (procedure_oid , true );
330
+ return format_procedure_extended (procedure_oid , FORMAT_PROC_FORCE_QUALIFY );
333
331
}
334
332
335
333
/*
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
+ *
336
339
* Routine to produce regprocedure names; see format_procedure above.
337
340
*
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
341
347
*/
342
- static char *
343
- format_procedure_internal (Oid procedure_oid , bool force_qualify )
348
+ char *
349
+ format_procedure_extended (Oid procedure_oid , bits16 flags )
344
350
{
345
351
char * result ;
346
352
HeapTuple proctup ;
@@ -365,7 +371,8 @@ format_procedure_internal(Oid procedure_oid, bool force_qualify)
365
371
* Would this proc be found (given the right args) by regprocedurein?
366
372
* If not, or if caller requests it, we need to qualify it.
367
373
*/
368
- if (!force_qualify && FunctionIsVisible (procedure_oid ))
374
+ if ((flags & FORMAT_PROC_FORCE_QUALIFY ) == 0 &&
375
+ FunctionIsVisible (procedure_oid ))
369
376
nspname = NULL ;
370
377
else
371
378
nspname = get_namespace_name (procform -> pronamespace );
@@ -379,7 +386,7 @@ format_procedure_internal(Oid procedure_oid, bool force_qualify)
379
386
if (i > 0 )
380
387
appendStringInfoChar (& buf , ',' );
381
388
appendStringInfoString (& buf ,
382
- force_qualify ?
389
+ ( flags & FORMAT_PROC_FORCE_QUALIFY ) != 0 ?
383
390
format_type_be_qualified (thisargtype ) :
384
391
format_type_be (thisargtype ));
385
392
}
@@ -389,6 +396,11 @@ format_procedure_internal(Oid procedure_oid, bool force_qualify)
389
396
390
397
ReleaseSysCache (proctup );
391
398
}
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
+ }
392
404
else
393
405
{
394
406
/* If OID doesn't match any pg_proc entry, return it numerically */
@@ -747,13 +759,20 @@ to_regoperator(PG_FUNCTION_ARGS)
747
759
}
748
760
749
761
/*
750
- * format_operator - converts operator OID to "opr_name(args)"
762
+ * format_operator_extended - converts operator OID to "opr_name(args)"
751
763
*
752
764
* 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
754
773
*/
755
- static char *
756
- format_operator_internal (Oid operator_oid , bool force_qualify )
774
+ char *
775
+ format_operator_extended (Oid operator_oid , bits16 flags )
757
776
{
758
777
char * result ;
759
778
HeapTuple opertup ;
@@ -776,7 +795,8 @@ format_operator_internal(Oid operator_oid, bool force_qualify)
776
795
* Would this oper be found (given the right args) by regoperatorin?
777
796
* If not, or if caller explicitly requests it, we need to qualify it.
778
797
*/
779
- if (force_qualify || !OperatorIsVisible (operator_oid ))
798
+ if ((flags & FORMAT_OPERATOR_FORCE_QUALIFY ) != 0 ||
799
+ !OperatorIsVisible (operator_oid ))
780
800
{
781
801
nspname = get_namespace_name (operform -> oprnamespace );
782
802
appendStringInfo (& buf , "%s." ,
@@ -787,15 +807,15 @@ format_operator_internal(Oid operator_oid, bool force_qualify)
787
807
788
808
if (operform -> oprleft )
789
809
appendStringInfo (& buf , "%s," ,
790
- force_qualify ?
810
+ ( flags & FORMAT_OPERATOR_FORCE_QUALIFY ) != 0 ?
791
811
format_type_be_qualified (operform -> oprleft ) :
792
812
format_type_be (operform -> oprleft ));
793
813
else
794
814
appendStringInfoString (& buf , "NONE," );
795
815
796
816
if (operform -> oprright )
797
817
appendStringInfo (& buf , "%s)" ,
798
- force_qualify ?
818
+ ( flags & FORMAT_OPERATOR_FORCE_QUALIFY ) != 0 ?
799
819
format_type_be_qualified (operform -> oprright ) :
800
820
format_type_be (operform -> oprright ));
801
821
else
@@ -805,6 +825,11 @@ format_operator_internal(Oid operator_oid, bool force_qualify)
805
825
806
826
ReleaseSysCache (opertup );
807
827
}
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
+ }
808
833
else
809
834
{
810
835
/*
@@ -820,13 +845,14 @@ format_operator_internal(Oid operator_oid, bool force_qualify)
820
845
char *
821
846
format_operator (Oid operator_oid )
822
847
{
823
- return format_operator_internal (operator_oid , false );
848
+ return format_operator_extended (operator_oid , 0 );
824
849
}
825
850
826
851
char *
827
852
format_operator_qualified (Oid operator_oid )
828
853
{
829
- return format_operator_internal (operator_oid , true);
854
+ return format_operator_extended (operator_oid ,
855
+ FORMAT_OPERATOR_FORCE_QUALIFY );
830
856
}
831
857
832
858
void
0 commit comments