pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
PQexecParams and IN query?
3+ messages / 3 participants
[nested] [flat]

* PQexecParams and IN query?
@ 2007-05-11 12:03 Andrew McNamara <andrewm@object-craft.com.au>
  2007-05-11 12:31 ` Re: PQexecParams and IN query? Gregory Stark <stark@enterprisedb.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Andrew McNamara @ 2007-05-11 12:03 UTC (permalink / raw)
  To: pgsql-interfaces

I'm trying to use PQexecParams to do an "IN" query, where the set of
values is supplied as a single parameter - essentially:

    SELECT * FROM sometable WHERE colvalue IN $1

With $1 as a set or array of some sort. Unfortunately, the above doesn't
pass parsing.  Is there any way to do an IN query via PQexecParams that
doesn't involve enumerating the set of values as individual parameters?

Previously, we were using PQexec(), and the adapter layer was conveniently
expanding the set into the command. 

-- 
Andrew McNamara, Senior Developer, Object Craft
http://www.object-craft.com.au/



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

* Re: PQexecParams and IN query?
  2007-05-11 12:03 PQexecParams and IN query? Andrew McNamara <andrewm@object-craft.com.au>
@ 2007-05-11 12:31 ` Gregory Stark <stark@enterprisedb.com>
  2007-05-11 13:12   ` Re: PQexecParams and IN query? Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 3+ messages in thread

From: Gregory Stark @ 2007-05-11 12:31 UTC (permalink / raw)
  To: Andrew McNamara <andrewm@object-craft.com.au>; +Cc: pgsql-interfaces

"Andrew McNamara" <andrewm@object-craft.com.au> writes:

> I'm trying to use PQexecParams to do an "IN" query, where the set of
> values is supplied as a single parameter - essentially:
>
>     SELECT * FROM sometable WHERE colvalue IN $1
>
> With $1 as a set or array of some sort. Unfortunately, the above doesn't
> pass parsing.  Is there any way to do an IN query via PQexecParams that
> doesn't involve enumerating the set of values as individual parameters?

No but there is a way to do something equivalent with arrays:

SELECT * FROM sometable WHERE colvalue = any($1::integer[]);

I'm not sure which (if any) drivers have good support for arrays though. I
think with most if not all drivers you'll have to crunch your list of elements
down into a string argument of the form "{1,2,3}" (without the quotes). 

If you're using something more complex than integers then you'll have to deal
with quoting the array elements which will start to get annoying.

-- 
  Gregory Stark
  EnterpriseDB          http://www.enterprisedb.com




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

* Re: PQexecParams and IN query?
  2007-05-11 12:03 PQexecParams and IN query? Andrew McNamara <andrewm@object-craft.com.au>
  2007-05-11 12:31 ` Re: PQexecParams and IN query? Gregory Stark <stark@enterprisedb.com>
@ 2007-05-11 13:12   ` Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 3+ messages in thread

From: Tom Lane @ 2007-05-11 13:12 UTC (permalink / raw)
  To: Gregory Stark <stark@enterprisedb.com>; +Cc: Andrew McNamara <andrewm@object-craft.com.au>; pgsql-interfaces

Gregory Stark <stark@enterprisedb.com> writes:
> "Andrew McNamara" <andrewm@object-craft.com.au> writes:
>> I'm trying to use PQexecParams to do an "IN" query, where the set of
>> values is supplied as a single parameter - essentially:
>> 
>> SELECT * FROM sometable WHERE colvalue IN $1
>> 
>> With $1 as a set or array of some sort. Unfortunately, the above doesn't
>> pass parsing.  Is there any way to do an IN query via PQexecParams that
>> doesn't involve enumerating the set of values as individual parameters?

> No but there is a way to do something equivalent with arrays:

> SELECT * FROM sometable WHERE colvalue = any($1::integer[]);

> I'm not sure which (if any) drivers have good support for arrays though.

Be aware also that if you're hoping that this will use an index on
colvalue, you need 8.2 or later to get such a plan from an "= any"
construct.

			regards, tom lane



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


end of thread, other threads:[~2007-05-11 13:12 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2007-05-11 12:03 PQexecParams and IN query? Andrew McNamara <andrewm@object-craft.com.au>
2007-05-11 12:31 ` Gregory Stark <stark@enterprisedb.com>
2007-05-11 13:12   ` 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