简体   繁体   中英

How to call postgresql function that returns TABLE type from Java code?

I have a postgresql function with this signature...

create function getProductsPerCodes(p_c_prodcodes VARCHAR(1000))
RETURNS 
    table (prodcode VARCHAR(1000), prodname VARCHAR(1000), prodline VARCHAR(1000), prodvendor VARCHAR(1000), quantity_in_stock int)
AS

The function returns TABLE type.

From SQL client, I can call this function like this:

select getProductsPerCodes('''c1'',''c4'',') rec

It prints the records fine.

Now, how do I call this function from Java code and access the data returned by the function? I need to access the table records and access all the fields that constitute the table record.

You should to use "table context syntax"

SELECT * FROM getProductsPerCodes($$'c1','c4',$$)

When your string contains quotes ' , you can use Postgres' custom string separators like $$ in example. Then you don't need to double quotes and code can be more readable.

Handling the result of a set returning function is no different than handling the result of a regular select * from some_table command.

But you should not pass parameters as part of the SQL string. Use a PreparedStatement instead:

PreparedStatement pstmt = connection.prepareStatement("select * from getproductspercode(?,?)");
pstmt.setString(1, "c1");
pstmt.setString(2, "c4");
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
  String code = rs.getString("prodcode");
  String name = rs.getString("prodname");
  ... 
  int stock = rs.getInt("quantity_in_stock");
}

I put together a small demo to reinforce the statement by @a_horse_with_no_name that "set returning function is no different than handling the result of a regular select." This demo defines and populates a small table sort on mimicking yours. It contains 2 TABLE returning functions. It then runs a standard query and each of the functions, all with the same parameters. Ad you can see they produce identical results. (I used comma separated list:( instead of 2 parameters as you have. Did not have to deal with nested quotes. (And I initially misread your parameter list and did not want to redo them.).

I then ran one of them with different parameter values, (c3,c1). It produced the following:

+----------------+-------------+--------------+
|    pc_name     | pc_code_set | pc_code_grp  |
+----------------+-------------+--------------+
| Test col n= 01 | c1          | Group Low    |
| Test col n= 03 | c3          | Group Low    |
| Test col n= 06 | c1          | Group Medium |
| Test col n= 08 | c3          | Group High   |
| Test col n= 11 | c1          | Group High   |
| Test col n= 13 | c3          | Group High   |
+----------------+-------------+--------------+

But which one was run, you cannot tell. So here a test for you. Build them in your environment and give each a try. You will need to change what is called, but not handling the result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM