| 987 | #define CONNECTBY_NCOLS_NOBRANCH 3 |
| 988 | |
| 989 | Datum |
| 990 | connectby_text(PG_FUNCTION_ARGS) |
| 991 | { |
| 992 | char *relname = text_to_cstring(PG_GETARG_TEXT_PP(0)); |
| 993 | char *key_fld = text_to_cstring(PG_GETARG_TEXT_PP(1)); |
| 994 | char *parent_key_fld = text_to_cstring(PG_GETARG_TEXT_PP(2)); |
| 995 | char *start_with = text_to_cstring(PG_GETARG_TEXT_PP(3)); |
| 996 | int max_depth = PG_GETARG_INT32(4); |
| 997 | char *branch_delim = NULL; |
| 998 | bool show_branch = false; |
| 999 | bool show_serial = false; |
| 1000 | ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo; |
| 1001 | TupleDesc tupdesc; |
| 1002 | AttInMetadata *attinmeta; |
| 1003 | MemoryContext per_query_ctx; |
| 1004 | MemoryContext oldcontext; |
| 1005 | |
| 1006 | /* check to see if caller supports us returning a tuplestore */ |
| 1007 | if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo)) |
| 1008 | ereport(ERROR, |
| 1009 | (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 1010 | errmsg("set-valued function called in context that cannot accept a set"))); |
| 1011 | if (!(rsinfo->allowedModes & SFRM_Materialize) || |
| 1012 | rsinfo->expectedDesc == NULL) |
| 1013 | ereport(ERROR, |
| 1014 | (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 1015 | errmsg("materialize mode required, but it is not allowed in this context"))); |
| 1016 | |
| 1017 | if (fcinfo->nargs == 6) |
| 1018 | { |
| 1019 | branch_delim = text_to_cstring(PG_GETARG_TEXT_PP(5)); |
| 1020 | show_branch = true; |
| 1021 | } |
| 1022 | else |
| 1023 | /* default is no show, tilde for the delimiter */ |
| 1024 | branch_delim = pstrdup("~"); |
| 1025 | |
| 1026 | per_query_ctx = rsinfo->econtext->ecxt_per_query_memory; |
| 1027 | oldcontext = MemoryContextSwitchTo(per_query_ctx); |
| 1028 | |
| 1029 | /* get the requested return tuple description */ |
| 1030 | tupdesc = CreateTupleDescCopy(rsinfo->expectedDesc); |
| 1031 | |
| 1032 | /* does it meet our needs */ |
| 1033 | validateConnectbyTupleDesc(tupdesc, show_branch, show_serial); |
| 1034 | |
| 1035 | /* OK, use it then */ |
| 1036 | attinmeta = TupleDescGetAttInMetadata(tupdesc); |
| 1037 | |
| 1038 | /* OK, go to work */ |
| 1039 | rsinfo->returnMode = SFRM_Materialize; |
| 1040 | rsinfo->setResult = connectby(relname, |
| 1041 | key_fld, |
| 1042 | parent_key_fld, |
| 1043 | NULL, |
| 1044 | branch_delim, |
| 1045 | start_with, |
| 1046 | max_depth, |
nothing calls this directly
no test coverage detected