24 #include "executor/spi.h"
26 #include "catalog/pg_type.h"
27 #if PGSQL_VERSION > 92
28 #include "access/htup_details.h"
35 Datum vrppdtw(PG_FUNCTION_ARGS);
39 #include "../../common/src/debug_macro.h"
40 #include "../../common/src/postgres_connection.h"
41 #include "./customers_input.h"
47 int compute_shortest_path(
49 int64_t vehicle_count,
52 size_t *length_results_struct) {
53 PGR_DBG(
"start shortest_path\n");
57 size_t total_customers = 0;
59 pgr_get_customers(sql, &customers, &total_customers);
62 PGR_DBG(
"Calling Solver Instance\n");
64 int ret = Solver(customers, total_customers, vehicle_count,
65 capacity, &err_msg, results, length_results_struct);
68 ereport(ERROR, (errcode(ERRCODE_E_R_E_CONTAINING_SQL_NOT_PERMITTED),
69 errmsg(
"Error computing path: %s", err_msg)));
73 PGR_DBG(
"*length_results_count = %i\n", *length_results_struct);
74 PGR_DBG(
"ret = %i\n", ret);
84 PG_FUNCTION_INFO_V1(vrppdtw);
86 vrppdtw(PG_FUNCTION_ARGS) {
87 FuncCallContext *funcctx;
96 if (SRF_IS_FIRSTCALL()) {
97 MemoryContext oldcontext;
98 size_t length_results_struct = 0;
99 funcctx = SRF_FIRSTCALL_INIT();
100 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
103 PGR_DBG(
"Calling compute_shortes_path");
105 compute_shortest_path(
106 pgr_text2char(PG_GETARG_TEXT_P(0)),
109 &results, &length_results_struct);
111 PGR_DBG(
"Back from solve_vrp, length_results: %d", length_results_struct);
114 funcctx->max_calls = length_results_struct;
115 funcctx->user_fctx = results;
118 if (get_call_result_type(fcinfo, NULL, &tuple_desc) != TYPEFUNC_COMPOSITE)
120 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
121 errmsg(
"function returning record called in context "
122 "that cannot accept type record")));
124 funcctx->tuple_desc = BlessTupleDesc(tuple_desc);
125 MemoryContextSwitchTo(oldcontext);
129 funcctx = SRF_PERCALL_SETUP();
131 call_cntr = funcctx->call_cntr;
132 max_calls = funcctx->max_calls;
133 tuple_desc = funcctx->tuple_desc;
137 if (call_cntr < max_calls) {
143 PGR_DBG(
"Till hereee ", NULL);
144 values = palloc(4 *
sizeof(Datum));
145 nulls = palloc(4 *
sizeof(
char));
151 values[0] = Int32GetDatum(results[call_cntr].seq);
152 values[1] = Int64GetDatum(results[call_cntr].rid);
153 values[2] = Int64GetDatum(results[call_cntr].nid);
154 values[3] = Float8GetDatum(results[call_cntr].cost);
155 tuple = heap_formtuple(tuple_desc, values, nulls);
158 result = HeapTupleGetDatum(tuple);
164 SRF_RETURN_NEXT(funcctx, result);
168 SRF_RETURN_DONE(funcctx);