* connectby - does the real work for connectby_text() */
| 1147 | * connectby - does the real work for connectby_text() |
| 1148 | */ |
| 1149 | static Tuplestorestate * |
| 1150 | connectby(char *relname, |
| 1151 | char *key_fld, |
| 1152 | char *parent_key_fld, |
| 1153 | char *orderby_fld, |
| 1154 | char *branch_delim, |
| 1155 | char *start_with, |
| 1156 | int max_depth, |
| 1157 | bool show_branch, |
| 1158 | bool show_serial, |
| 1159 | MemoryContext per_query_ctx, |
| 1160 | bool randomAccess, |
| 1161 | AttInMetadata *attinmeta) |
| 1162 | { |
| 1163 | Tuplestorestate *tupstore = NULL; |
| 1164 | int ret; |
| 1165 | MemoryContext oldcontext; |
| 1166 | |
| 1167 | int serial = 1; |
| 1168 | |
| 1169 | /* Connect to SPI manager */ |
| 1170 | if ((ret = SPI_connect()) < 0) |
| 1171 | /* internal error */ |
| 1172 | elog(ERROR, "connectby: SPI_connect returned %d", ret); |
| 1173 | |
| 1174 | /* switch to longer term context to create the tuple store */ |
| 1175 | oldcontext = MemoryContextSwitchTo(per_query_ctx); |
| 1176 | |
| 1177 | /* initialize our tuplestore */ |
| 1178 | tupstore = tuplestore_begin_heap(randomAccess, false, work_mem); |
| 1179 | |
| 1180 | MemoryContextSwitchTo(oldcontext); |
| 1181 | |
| 1182 | /* now go get the whole tree */ |
| 1183 | build_tuplestore_recursively(key_fld, |
| 1184 | parent_key_fld, |
| 1185 | relname, |
| 1186 | orderby_fld, |
| 1187 | branch_delim, |
| 1188 | start_with, |
| 1189 | start_with, /* current_branch */ |
| 1190 | 0, /* initial level is 0 */ |
| 1191 | &serial, /* initial serial is 1 */ |
| 1192 | max_depth, |
| 1193 | show_branch, |
| 1194 | show_serial, |
| 1195 | per_query_ctx, |
| 1196 | attinmeta, |
| 1197 | tupstore); |
| 1198 | |
| 1199 | SPI_finish(); |
| 1200 | |
| 1201 | return tupstore; |
| 1202 | } |
| 1203 | |
| 1204 | static void |
| 1205 | build_tuplestore_recursively(char *key_fld, |
no test coverage detected