Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass custom type array to Postgres function

I have a custom type

CREATE TYPE mytype as (id uuid, amount numeric(13,4));

I want to pass it to a function with the following signature:

CREATE FUNCTION myschema.myfunction(id uuid, mytypes mytype[])
  RETURNS BOOLEAN AS...

How can I call this in postgres query and inevitably from PHP?

like image 736
Ryan Fisch Avatar asked Aug 17 '12 16:08

Ryan Fisch


People also ask

How to CREATE custom data type in postgres?

We can create a custom datatype using either CREATE DOMAIN or CREATE TYPE. CREATE DOMAIN creates the user defined datatype with support to use constraints such as NOT NULL, CHECK, etc. CREATE TYPE creates a composite user defined datatype, which is used in a stored procedure as the data type of the returned value.

What is $$ in PostgreSQL?

It can be used to replace single quotes enclosing string literals (constants) anywhere in SQL scripts. The body of a function happens to be such a string literal. Dollar-quoting is a PostgreSQL-specific substitute for single quotes to avoid escaping of nested single quotes (recursively).

How do I loop through an array in PostgreSQL?

The FOREACH statement to loop over an array is: [ << label >> ] FOREACH target [ SLICE number ] IN ARRAY expression LOOP statements END LOOP [ label ];

Can we commit inside a function in PostgreSQL?

Basically, PostgreSQL version 11 allows users to perform autonomous transactions like COMMIT or ROLLBACK inside a procedural code that can be invoked using the CALL keyword.


2 Answers

You can use the alternative syntax with a array literal instead of the array constructor, which is a Postgres function-like construct and may cause trouble when you need to pass values - like in a prepared statement:

SELECT myschema.myfunc('0d6311cc-0d74-4a32-8cf9-87835651e1ee'
                  , '{"(0d6311cc-0d74-4a32-8cf9-87835651e1ee, 25)"
                    , "(6449fb3b-844e-440e-8973-31eb6bbefc81, 10)"}'::mytype[]);

I added a line break between the two row types in the array for display. That's legal.

How to find the correct syntax for any literal?

Just ask Postgres. Here is a demo:

CREATE TABLE mytype (id uuid, amount numeric(13,4));

INSERT INTO mytype VALUES
  ('0d6311cc-0d74-4a32-8cf9-87835651e1ee', 25)
 ,('6449fb3b-844e-440e-8973-31eb6bbefc81', 10);

SELECT ARRAY(SELECT m FROM mytype m);

Returns:

{"(0d6311cc-0d74-4a32-8cf9-87835651e1ee,25.0000)","(6449fb3b-844e-440e-8973-31eb6bbefc81,10.0000)"}

db<>fiddle here

Any table (including temporary tables) implicitly creates a row type of the same name.

like image 91
Erwin Brandstetter Avatar answered Sep 22 '22 06:09

Erwin Brandstetter


select myschema.myfunc('0d6311cc-0d74-4a32-8cf9-87835651e1ee'
                , ARRAY[('ac747f0e-93d4-43a9-bc5b-09df06593239', '25.00')
                              , ('6449fb3b-844e-440e-8973-31eb6bbefc81', '10.00')]::mytype[]
    );

Still need PHP portion of this resolved though, still not sure how to call a function populating with the custom array parameter.

like image 30
Ryan Fisch Avatar answered Sep 23 '22 06:09

Ryan Fisch