pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Do you know a json_populate_record alternative method to create a ROW with a named field notation?
2+ messages / 2 participants
[nested] [flat]

* Do you know a json_populate_record alternative method to create a ROW with a named field notation?
@ 2022-03-06 18:41 Stéphane Klein <contact@stephane-klein.info>
  2022-03-06 18:54 ` Re: Do you know a json_populate_record alternative method to create a ROW with a named field notation? Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 2+ messages in thread

From: Stéphane Klein @ 2022-03-06 18:41 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

Hello,

In this example:

```
CREATE TYPE contact AS (
   firstname VARCHAR,
   lastname VARCHAR
);

postgres=# SELECT json_populate_record(NULL::contact,
postgres(#   '{
postgres'#      "firstname": "John",
postgres'#      "lastname": "Doe"
postgres'#    }'
postgres'# );
 json_populate_record
----------------------
 (John,Doe)
(1 row)
```

**Question:** do you know a method like json_populate_record (
https://www.postgresql.org/docs/13/functions-json.html), which allows
creating a `ROW` with named field notation without using a json format?

I know the `ROW` syntax expression:

```
postgres=# SELECT ROW('John', 'Doe')::contact;
    row
------------
 (John,Doe)
(1 row)
```

But I didn't find a `ROW` constructors (
https://www.postgresql.org/docs/13/sql-expressions.html#SQL-SYNTAX-ROW-CONSTRUCTORS)
syntax allowing a named field notation.

Best regards,
Stéphane
-- 
Stéphane Klein <contact@stephane-klein.info>
Homepage: http://stephane-klein.info

^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: Do you know a json_populate_record alternative method to create a ROW with a named field notation?
  2022-03-06 18:41 Do you know a json_populate_record alternative method to create a ROW with a named field notation? Stéphane Klein <contact@stephane-klein.info>
@ 2022-03-06 18:54 ` Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 2+ messages in thread

From: Tom Lane @ 2022-03-06 18:54 UTC (permalink / raw)
  To: Stéphane Klein <contact@stephane-klein.info>; +Cc: pgsql-general@lists.postgresql.org

=?UTF-8?Q?St=C3=A9phane_Klein?= <contact@stephane-klein.info> writes:
> **Question:** do you know a method like json_populate_record (
> https://www.postgresql.org/docs/13/functions-json.html), which allows
> creating a `ROW` with named field notation without using a json format?

You mean you want to give the field names explicitly in the expression?

The closest thing I can think of is to make a constructor function and
invoke it with named-argument notation:

regression=# CREATE TYPE contact AS (
regression(#    firstname VARCHAR,
regression(#    lastname VARCHAR
regression(# );
CREATE TYPE
regression=# create function make_contact(firstname varchar, lastname varchar)
regression-# returns contact as 'select row(firstname, lastname)::contact'
regression-# language sql;
CREATE FUNCTION
regression=# select make_contact(firstname => 'John', lastname => 'Doe');
 make_contact 
--------------
 (John,Doe)
(1 row)

A bit tedious to set up, but it would have some advantages, eg you
could provide default values.

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2022-03-06 18:54 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2022-03-06 18:41 Do you know a json_populate_record alternative method to create a ROW with a named field notation? Stéphane Klein <contact@stephane-klein.info>
2022-03-06 18:54 ` Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox