pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedreturning an array as a list fo single-column rows?
6+ messages / 2 participants
[nested] [flat]
* returning an array as a list fo single-column rows?
@ 2007-12-23 20:55 Louis-David Mitterrand <vindex+lists-pgsql-sql@apartia.org>
0 siblings, 2 replies; 6+ messages in thread
From: Louis-David Mitterrand @ 2007-12-23 20:55 UTC (permalink / raw)
To: pgsql-sql
Hi,
is there a way to return a Pg array as a list of single-column row
values?
I am trying to circumvent DBI's lack of support for native database
arrays and return the list of values from an ENUM as a perl array.
Thanks,
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: returning an array as a list fo single-column rows?
@ 2007-12-23 21:19 Pavel Stehule <pavel.stehule@gmail.com>
parent: Louis-David Mitterrand <vindex+lists-pgsql-sql@apartia.org>
1 sibling, 1 reply; 6+ messages in thread
From: Pavel Stehule @ 2007-12-23 21:19 UTC (permalink / raw)
To: pgsql-sql
Hello
try
create or replace function unpack(anyarray)
returns setof anyelement as $$
select $1[i]
from generate_series(array_lower($1,1), array_upper($1,1)) g(i);
$$ language sql;
postgres=# select * from unpack(array[1,2,3,4]);
unpack
--------
1
2
3
4
(4 rows)
Regards
Pavel Stehule
On 23/12/2007, Louis-David Mitterrand
<vindex+lists-pgsql-sql@apartia.org> wrote:
> Hi,
>
> is there a way to return a Pg array as a list of single-column row
> values?
>
> I am trying to circumvent DBI's lack of support for native database
> arrays and return the list of values from an ENUM as a perl array.
>
> Thanks,
>
> ---------------------------(end of broadcast)---------------------------
> TIP 7: You can help support the PostgreSQL project by donating at
>
> http://www.postgresql.org/about/donate
>
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: returning an array as a list fo single-column rows?
@ 2007-12-23 21:22 Louis-David Mitterrand <vindex+lists-pgsql-sql@apartia.org>
parent: Pavel Stehule <pavel.stehule@gmail.com>
0 siblings, 0 replies; 6+ messages in thread
From: Louis-David Mitterrand @ 2007-12-23 21:22 UTC (permalink / raw)
To: pgsql-sql
On Sun, Dec 23, 2007 at 10:19:26PM +0100, Pavel Stehule wrote:
> Hello
>
> try
>
> create or replace function unpack(anyarray)
> returns setof anyelement as $$
> select $1[i]
> from generate_series(array_lower($1,1), array_upper($1,1)) g(i);
> $$ language sql;
>
> postgres=# select * from unpack(array[1,2,3,4]);
> unpack
> --------
> 1
> 2
> 3
> 4
> (4 rows)
Beautiful. Thank you.
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: returning an array as a list fo single-column rows?
@ 2007-12-23 21:27 Pavel Stehule <pavel.stehule@gmail.com>
parent: Louis-David Mitterrand <vindex+lists-pgsql-sql@apartia.org>
1 sibling, 1 reply; 6+ messages in thread
From: Pavel Stehule @ 2007-12-23 21:27 UTC (permalink / raw)
To: pgsql-sql
On 23/12/2007, Louis-David Mitterrand
<vindex+lists-pgsql-sql@apartia.org> wrote:
> Hi,
>
> is there a way to return a Pg array as a list of single-column row
> values?
>
> I am trying to circumvent DBI's lack of support for native database
> arrays and return the list of values from an ENUM as a perl array.
>
> Thanks,
>
you can solve this problem with conversion to string with const separator
Like:
postgres=# select array_to_string(array[1,2,3,4],'|');
array_to_string
-----------------
1|2|3|4
(1 row)
[pavel@localhost ~]$ perl
@a = split(/\|/, "1|2|3");
print $a[1];
Regards
Pavel
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: returning an array as a list fo single-column rows?
@ 2007-12-23 21:33 Louis-David Mitterrand <vindex+lists-pgsql-sql@apartia.org>
parent: Pavel Stehule <pavel.stehule@gmail.com>
0 siblings, 1 reply; 6+ messages in thread
From: Louis-David Mitterrand @ 2007-12-23 21:33 UTC (permalink / raw)
To: pgsql-sql
On Sun, Dec 23, 2007 at 10:27:09PM +0100, Pavel Stehule wrote:
> On 23/12/2007, Louis-David Mitterrand
> <vindex+lists-pgsql-sql@apartia.org> wrote:
> > Hi,
> >
> > is there a way to return a Pg array as a list of single-column row
> > values?
> >
> > I am trying to circumvent DBI's lack of support for native database
> > arrays and return the list of values from an ENUM as a perl array.
> >
> > Thanks,
> >
>
> you can solve this problem with conversion to string with const separator
>
> Like:
>
> postgres=# select array_to_string(array[1,2,3,4],'|');
> array_to_string
> -----------------
> 1|2|3|4
> (1 row)
>
> [pavel@localhost ~]$ perl
> @a = split(/\|/, "1|2|3");
> print $a[1];
Yes I thought about it, but would rather have Pg do the array splitting.
For instance if the separator occurs in an array element there is no
built-in escaping:
% select array_to_string(array['ee','dd','rr','f|f'],'|');
array_to_string
-----------------
ee|dd|rr|f|f
... and then perl would have it all wrong.
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: returning an array as a list fo single-column rows?
@ 2007-12-23 21:45 Pavel Stehule <pavel.stehule@gmail.com>
parent: Louis-David Mitterrand <vindex+lists-pgsql-sql@apartia.org>
0 siblings, 0 replies; 6+ messages in thread
From: Pavel Stehule @ 2007-12-23 21:45 UTC (permalink / raw)
To: pgsql-sql
>
> Yes I thought about it, but would rather have Pg do the array splitting.
> For instance if the separator occurs in an array element there is no
> built-in escaping:
>
> % select array_to_string(array['ee','dd','rr','f|f'],'|');
> array_to_string
> -----------------
> ee|dd|rr|f|f
if you have not some special char, then unpack is one possible solution
theoretically you can use text output
postgres=# select array['aa','aaa,j']::text;
array
-----------------
{aa,"aaa,j"}
(1 row)
but nothing nice parse it :(
^ permalink raw reply [nested|flat] 6+ messages in thread
end of thread, other threads:[~2007-12-23 21:45 UTC | newest]
Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2007-12-23 20:55 returning an array as a list fo single-column rows? Louis-David Mitterrand <vindex+lists-pgsql-sql@apartia.org>
2007-12-23 21:19 ` Pavel Stehule <pavel.stehule@gmail.com>
2007-12-23 21:22 ` Louis-David Mitterrand <vindex+lists-pgsql-sql@apartia.org>
2007-12-23 21:27 ` Pavel Stehule <pavel.stehule@gmail.com>
2007-12-23 21:33 ` Louis-David Mitterrand <vindex+lists-pgsql-sql@apartia.org>
2007-12-23 21:45 ` Pavel Stehule <pavel.stehule@gmail.com>
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