pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
PreparedStatement for set membership (The IN operator)
6+ messages / 3 participants
[nested] [flat]

* PreparedStatement for set membership (The IN operator)
@ 2011-04-05 07:17 Daron Ryan <daron.ryan@gmail.com>
  2011-04-05 08:27 ` Re: PreparedStatement for set membership (The IN operator) Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
  2011-04-05 12:52 ` Re: PreparedStatement for set membership (The IN operator) Thomas Markus <t.markus@proventis.net>
  0 siblings, 2 replies; 6+ messages in thread

From: Daron Ryan @ 2011-04-05 07:17 UTC (permalink / raw)
  To: pgsql-jdbc

  Hello,

I need to use a SELECT statement with varying data for set membership. IE.

SELECT link_id
FROM links
WHERE heading_id IN (?, ?, ?)

It won't always be the same number of members in the set. I am using an 
ordinary statement with an executeQuery call but would prefer to use a 
PreparedStatement. Is this possible? If so how do I set the values in 
the set?

Regards,
Daron.



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

* Re: PreparedStatement for set membership (The IN operator)
  2011-04-05 07:17 PreparedStatement for set membership (The IN operator) Daron Ryan <daron.ryan@gmail.com>
@ 2011-04-05 08:27 ` Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
  2011-04-05 12:39   ` Re: PreparedStatement for set membership (The IN operator) Daron Ryan <daron.ryan@gmail.com>
  1 sibling, 1 reply; 6+ messages in thread

From: Heikki Linnakangas @ 2011-04-05 08:27 UTC (permalink / raw)
  To: Daron Ryan <daron.ryan@gmail.com>; +Cc: pgsql-jdbc

On 05.04.2011 10:17, Daron Ryan wrote:
> Hello,
>
> I need to use a SELECT statement with varying data for set membership. IE.
>
> SELECT link_id
> FROM links
> WHERE heading_id IN (?, ?, ?)
>
> It won't always be the same number of members in the set. I am using an
> ordinary statement with an executeQuery call but would prefer to use a
> PreparedStatement. Is this possible? If so how do I set the values in
> the set?

You can do "WHERE heading_id = ANY (?)", and pass an array of the ids 
for the parameter.

-- 
   Heikki Linnakangas
   EnterpriseDB   http://www.enterprisedb.com



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

* Re: PreparedStatement for set membership (The IN operator)
  2011-04-05 07:17 PreparedStatement for set membership (The IN operator) Daron Ryan <daron.ryan@gmail.com>
  2011-04-05 08:27 ` Re: PreparedStatement for set membership (The IN operator) Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
@ 2011-04-05 12:39   ` Daron Ryan <daron.ryan@gmail.com>
  2011-04-05 12:56     ` Re: PreparedStatement for set membership (The IN operator) Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Daron Ryan @ 2011-04-05 12:39 UTC (permalink / raw)
  To: Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>; pgsql-jdbc

  Thanks Heikki. I have tried using the setArray method but I am still 
running into an error.

Exception in thread "main" org.postgresql.util.PSQLException: Unknown 
type _INTEGER.
         at 
org.postgresql.jdbc2.AbstractJdbc2Statement.setArray(AbstractJdbc2Statement.java:2800)
         at dictionary.test.Main.main(Main.java:85)

This is the Array implementation I have created.
http://pastebin.com/tkzPRL4A

For baseTypeName I have also tried "int" and received the same error 
referring to type _int instead.

This is the code I used to test the implementation
calling code
http://pastebin.com/T1mvADaF

Any idea what I need to do?


On 5/04/2011 5:57 PM, Heikki Linnakangas wrote:
> On 05.04.2011 10:17, Daron Ryan wrote:
>> Hello,
>>
>> I need to use a SELECT statement with varying data for set 
>> membership. IE.
>>
>> SELECT link_id
>> FROM links
>> WHERE heading_id IN (?, ?, ?)
>>
>> It won't always be the same number of members in the set. I am using an
>> ordinary statement with an executeQuery call but would prefer to use a
>> PreparedStatement. Is this possible? If so how do I set the values in
>> the set?
>
> You can do "WHERE heading_id = ANY (?)", and pass an array of the ids 
> for the parameter.
>




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

* Re: PreparedStatement for set membership (The IN operator)
  2011-04-05 07:17 PreparedStatement for set membership (The IN operator) Daron Ryan <daron.ryan@gmail.com>
  2011-04-05 08:27 ` Re: PreparedStatement for set membership (The IN operator) Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
  2011-04-05 12:39   ` Re: PreparedStatement for set membership (The IN operator) Daron Ryan <daron.ryan@gmail.com>
@ 2011-04-05 12:56     ` Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
  2011-04-05 13:42       ` Re: PreparedStatement for set membership (The IN operator) Daron Ryan <daron.ryan@gmail.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Heikki Linnakangas @ 2011-04-05 12:56 UTC (permalink / raw)
  To: Daron Ryan <daron.ryan@gmail.com>; +Cc: pgsql-jdbc

On 05.04.2011 15:39, Daron Ryan wrote:
> Thanks Heikki. I have tried using the setArray method but I am still
> running into an error.
>
> Exception in thread "main" org.postgresql.util.PSQLException: Unknown
> type _INTEGER.
> at
> org.postgresql.jdbc2.AbstractJdbc2Statement.setArray(AbstractJdbc2Statement.java:2800)
>
> at dictionary.test.Main.main(Main.java:85)
>
> This is the Array implementation I have created.
> http://pastebin.com/tkzPRL4A

Starting with JDBC4, you can use conn.createArrayOf() function. No need 
to create a custom Array class anymore. This is what we have in the test 
suite:

   public void testCreateArrayOfInt() throws SQLException {
         PreparedStatement pstmt = _conn.prepareStatement("SELECT 
?::int[]");
         Integer in[] = new Integer[3];
         in[0] = 0;
         in[1] = -1;
         in[2] = 2;
         pstmt.setArray(1, _conn.createArrayOf("int4", in));

         ResultSet rs = pstmt.executeQuery();
         assertTrue(rs.next());
         Array arr = rs.getArray(1);
         Integer out[] = (Integer [])arr.getArray();

         assertEquals(3, out.length);
         assertEquals(0, out[0].intValue());
         assertEquals(-1, out[1].intValue());
         assertEquals(2, out[2].intValue());
     }

-- 
   Heikki Linnakangas
   EnterpriseDB   http://www.enterprisedb.com



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

* Re: PreparedStatement for set membership (The IN operator)
  2011-04-05 07:17 PreparedStatement for set membership (The IN operator) Daron Ryan <daron.ryan@gmail.com>
  2011-04-05 08:27 ` Re: PreparedStatement for set membership (The IN operator) Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
  2011-04-05 12:39   ` Re: PreparedStatement for set membership (The IN operator) Daron Ryan <daron.ryan@gmail.com>
  2011-04-05 12:56     ` Re: PreparedStatement for set membership (The IN operator) Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
@ 2011-04-05 13:42       ` Daron Ryan <daron.ryan@gmail.com>
  0 siblings, 0 replies; 6+ messages in thread

From: Daron Ryan @ 2011-04-05 13:42 UTC (permalink / raw)
  To: Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>; +Cc: pgsql-jdbc

  On 5/04/2011 10:26 PM, Heikki Linnakangas wrote:
> On 05.04.2011 15:39, Daron Ryan wrote:
>> Thanks Heikki. I have tried using the setArray method but I am still
>> running into an error.
>>
>> Exception in thread "main" org.postgresql.util.PSQLException: Unknown
>> type _INTEGER.
>> at
>> org.postgresql.jdbc2.AbstractJdbc2Statement.setArray(AbstractJdbc2Statement.java:2800) 
>>
>>
>> at dictionary.test.Main.main(Main.java:85)
>>
>> This is the Array implementation I have created.
>> http://pastebin.com/tkzPRL4A
>
> Starting with JDBC4, you can use conn.createArrayOf() function. No 
> need to create a custom Array class anymore. This is what we have in 
> the test suite:
>
>   public void testCreateArrayOfInt() throws SQLException {
>         PreparedStatement pstmt = _conn.prepareStatement("SELECT 
> ?::int[]");
>         Integer in[] = new Integer[3];
>         in[0] = 0;
>         in[1] = -1;
>         in[2] = 2;
>         pstmt.setArray(1, _conn.createArrayOf("int4", in));
>
>         ResultSet rs = pstmt.executeQuery();
>         assertTrue(rs.next());
>         Array arr = rs.getArray(1);
>         Integer out[] = (Integer [])arr.getArray();
>
>         assertEquals(3, out.length);
>         assertEquals(0, out[0].intValue());
>         assertEquals(-1, out[1].intValue());
>         assertEquals(2, out[2].intValue());
>     }
>
Thanks, my code is working now. I changed my baseTypeName to int4 and 
after a few more fixes my code worked. Then I tried the 
Connection.createArrayOf method and that worked too.




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

* Re: PreparedStatement for set membership (The IN operator)
  2011-04-05 07:17 PreparedStatement for set membership (The IN operator) Daron Ryan <daron.ryan@gmail.com>
@ 2011-04-05 12:52 ` Thomas Markus <t.markus@proventis.net>
  1 sibling, 0 replies; 6+ messages in thread

From: Thomas Markus @ 2011-04-05 12:52 UTC (permalink / raw)
  To: Daron Ryan <daron.ryan@gmail.com>; +Cc: pgsql-jdbc

hi,

try

select unnest(ARRAY[?,?,?])

regards
Thomas


Am 05.04.2011 09:17, schrieb Daron Ryan:
>  Hello,
>
> I need to use a SELECT statement with varying data for set membership. 
> IE.
>
> SELECT link_id
> FROM links
> WHERE heading_id IN (?, ?, ?)
>
> It won't always be the same number of members in the set. I am using 
> an ordinary statement with an executeQuery call but would prefer to 
> use a PreparedStatement. Is this possible? If so how do I set the 
> values in the set?
>
> Regards,
> Daron.
>





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


end of thread, other threads:[~2011-04-05 13:42 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2011-04-05 07:17 PreparedStatement for set membership (The IN operator) Daron Ryan <daron.ryan@gmail.com>
2011-04-05 08:27 ` Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
2011-04-05 12:39   ` Daron Ryan <daron.ryan@gmail.com>
2011-04-05 12:56     ` Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
2011-04-05 13:42       ` Daron Ryan <daron.ryan@gmail.com>
2011-04-05 12:52 ` Thomas Markus <t.markus@proventis.net>

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