From: Andreas Joseph Krogh <andreak@officenet.no>
To: pgsql-jdbc@postgresql.org
Subject: Retrieving multidimentional array-column using JDBC
Date: Wed, 10 Mar 2010 00:39:14 +0100
Message-ID: <201003100039.14562.andreak@officenet.no> (raw)
Hi all.
I have a query which returns a multidimentional array (2D) of varchar and I'm having problems retrieving the results using JDBC.
First, since PG doesn't support arrays of arrays, I've created a custom type:
create type text_array as (c TEXT[]);
Then I have the following schema:
--=================8<==================
CREATE TABLE foo(
id integer primary key
);
CREATE TABLE pathway(
id integer primary key,
p_id integer references pathway(id),
element varchar
);
CREATE TABLE path_usage(
foo_id integer references foo(id),
pathway_id integer references pathway(id)
);
INSERT INTO foo VALUES(1);
insert into pathway(id, p_id, element) values(1, null, 'path');
insert into pathway(id, p_id, element) values(2, 1, 'to');
insert into pathway(id, p_id, element) values(3, 2, 'first');
insert into pathway(id, p_id, element) values(4, 2, 'second');
insert into path_usage(foo_id, pathway_id) values(1, 3);
insert into path_usage(foo_id, pathway_id) values(1, 4);
--=================8<==================
I use this query to retrieve a list of all "foo"s with their (complete) paths:
SELECT act.id, (SELECT array_agg(row(pathway_array)::text_array) FROM
(SELECT ARRAY(
WITH RECURSIVE parent(level, id, p_id, element) AS(
SELECT 1, pw.id, pw.p_id, pw.element
UNION ALL
SELECT rec.level + 1, pw_.id, pw_.p_id, pw_.element
FROM pathway pw_, parent rec
WHERE pw_.id = rec.p_id
) SELECT element FROM parent ORDER BY level DESC
) AS pathway_array
FROM pathway pw, path_usage pu
WHERE pu.foo_id = act.id
AND pu.pathway_id = pw.id
ORDER BY lower(pw.element) ASC
) x
) AS pathway_array
FROM foo act
;
This returns:
id | pathway_array
----+----------------------------------------------------
1 | {"(\"{path,to,first}\")","(\"{path,to,second}\")"}
(1 row)
No, so far all good, *but*, I now want to retrieve this "pathway_array" column using JDBC and getArray(). My code looks like:
Array qArray = rs.getArray("pathway_array");
Object o = qArray.getArray();
throws Exception:
...
...
Caused by: org.postgresql.util.PSQLException: Method org.postgresql.jdbc4.Jdbc4Array.getArrayImpl(long,int,Map) is not yet implemented.
at org.postgresql.Driver.notImplemented(Driver.java:753)
So - does anyone know how to retrieve such arrays using JDBC?
Anyone knows if there's another way of retrieving the array of arrays without using the "array_agg trick" using a custom-type?
--
Andreas Joseph Krogh <andreak@officenet.no>
Senior Software Developer / CTO
------------------------+---------------------------------------------+
OfficeNet AS | The most difficult thing in the world is to |
Rosenholmveien 25 | know how to do a thing and to watch |
1414 TrollÄsen | somebody else doing it wrong, without |
NORWAY | comment. |
| |
Tlf: +47 24 15 38 90 | |
Fax: +47 24 15 38 91 | |
Mobile: +47 909 56 963 | |
------------------------+---------------------------------------------+
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-jdbc@postgresql.org
Cc: andreak@officenet.no
Subject: Re: Retrieving multidimentional array-column using JDBC
In-Reply-To: <201003100039.14562.andreak@officenet.no>
References: <201003100039.14562.andreak@officenet.no>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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