pgfe icon indicating copy to clipboard operation
pgfe copied to clipboard

how to work with refcursor?

Open stan-yu opened this issue 1 year ago • 3 comments

Please add documentation and examples on working with refcursors. When calling a procedure with Connection::call() that has an INOUT refcursor parameter, in the lambda handler, in row, one record with one column is returned, with the value <unnamed portal 1>.

Procedure example: CREATE OR REPLACE PROCEDURE public.get_objects( IN pstartofinterval bigint, IN pendofinterval bigint, INOUT cr refcursor);

In my example, the refcursor cr should return jsonb objects. The pstartofinterval and pendofinterval parameters determine which specific objects to return. In this case, it doesn't matter which. The question is how to get them.

Example code:

pgfe::Connection conn(pgfe::Connection_options{}.set(pgfe::Communication_mode::net).set_hostname( ...)....;

auto get_values = [&json_value_callback](pgfe::Row&& row) {
  std::cout << row.field_count() << std::endl;    // output 1
  std::cout << row.is_empty() << std::endl;       // output 0
  for (size_t i = 0; i < row.field_count(); ++i) {
    std::cout << row.field_name(i) << std::endl;   // output "cr"
  }
  std::cout << pgfe::to<std::string>(row[0]) << std::endl;     // output "<unnamed portal 1>"
};

conn.call(get_values, "public.get_objects", start_of_interval, end_of_interval, nullptr);

Thanks.

stan-yu avatar Aug 29 '23 14:08 stan-yu