Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 6BBD8632EAE for ; Thu, 29 Oct 2009 17:19:02 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 57145-07 for ; Thu, 29 Oct 2009 20:18:47 +0000 (UTC) X-Greylist: delayed 00:06:49.095341 by SQLgrey-1.7.6 Received: from gv-out-0910.google.com (gv-out-0910.google.com [216.239.58.187]) by mail.postgresql.org (Postfix) with ESMTP id 47EBB632E50 for ; Thu, 29 Oct 2009 17:18:50 -0300 (ADT) Received: by gv-out-0910.google.com with SMTP id y18so386575gvf.7 for ; Thu, 29 Oct 2009 13:18:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=gamma; h=domainkey-signature:received:received:date:from:to:subject :message-id:mail-followup-to:mime-version:content-type :content-disposition:user-agent; bh=qtE5k+ZoQLRPbJ9ehkVVHFJq268IiE1RZKOEBdOReCU=; b=hcHVBxQD6ukyXcJ/VqdwV8ddyVVZj0XqHbXlBlDechKba9PRpWj7gfytg4iDpZUD9b RDOq7/PHoJiy+sZVuxeoTl1fO5AU+TQwDRZviagIydY6eZTAa99qpgEgcgg4oYsAfp5O zUHcUHzGLpCpJmgtlYnsZT10RGEo8mBWIkYQo= DomainKey-Signature: a=rsa-sha1; c=nofws; d=googlemail.com; s=gamma; h=date:from:to:subject:message-id:mail-followup-to:mime-version :content-type:content-disposition:user-agent; b=S+F3amVh4r9z7scc14l7xtSFvRpEv98CK15G85gQUYJSp0oMm8MEAzxHgQ3FVikiN1 AZS+DqC2qjjpi1UIlsVBIIUJ57+tQli2aghqo708v0qNmIoI3+0bAwvxfnEj0wgRw4Gm hiFe1AALgvvfvJ8l2/vnEhAbb9wDETVoI//ZY= Received: by 10.102.249.21 with SMTP id w21mr221080muh.58.1256847119636; Thu, 29 Oct 2009 13:11:59 -0700 (PDT) Received: from stan ([78.52.132.200]) by mx.google.com with ESMTPS id j6sm1894768mue.50.2009.10.29.13.11.58 (version=SSLv3 cipher=RC4-MD5); Thu, 29 Oct 2009 13:11:59 -0700 (PDT) Date: Thu, 29 Oct 2009 21:11:56 +0100 From: Viktor Rosenfeld To: pgsql-jdbc@postgresql.org Subject: Howto retrieve a custom type via JDBC Message-ID: <20091029201156.GA25285@stan> Mail-Followup-To: pgsql-jdbc@postgresql.org MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.20 (2009-06-14) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.599 tagged_above=-10 required=5 tests=AWL=0.000, BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 200910/43 X-Sequence-Number: 20045 Hi, I've created a custom datatype that I would like to retrieve via JDBC. To make the problem more fun, the values are aggregated into an array. The type definition is: CREATE TYPE annotation AS ( namespace varchar, name varchar, value varchar ); The query looks something like this: SELECT node_ref AS id, array_agg(DISTINCT ROW(namespace, name, value)::annotation) AS annotation ... GROUP BY id; I've hacked together some experimental code: Array array = rs.getArray(2); if (array != null) { ResultSet arrayRs = array.getResultSet(); while (arrayRs.next()) { PGobject anno = (PGobject) arrayRs.getObject("value"); // anno.getValue() returns the String "(namespace,name,value)" PGtokenizer t = new PGtokenizer(PGtokenizer.remove(anno.getValue(), "(", ")"), ','); if (t.getSize() != 3) throw new RuntimeException("Could not read to annotation type: " + anno.getValue()); String namespace = t.getToken(0); String name = t.getToken(1); String value = t.getToken(2); Annotation annotation = new Annotation(namespace, name, value); } } The code above is not only fugly, but it breaks if there's a comma in one of those values (e.g. (tiger,lemma,"1,62")). In this case, they are 4 tokens. BTW, the quotes around 1,62 are inserted by PostgreSQL, they're not actually there in database. If PostgreSQL is smart enough to insert the quotes, it should be able to correctly parse the string, shouldn't it? I got the idea to use PGtokenizer from looking at PGbox and stuff. But my reason to create a custom type in the first place was to get to its constituents in a clean way. Previously, I had simply concatenated the strings with ":" and "=" in SQL and splitted them back into the components in my Java code. So, the question is: How to cleanly retrieve the components of a custom datatype using JDBC? Cheers, Viktor