Re: string = any() - Mailing list pgsql-general

From David Johnston
Subject Re: string = any()
Date
Msg-id 01ff01cccfe2$20fc83c0$62f58b40$@yahoo.com
Whole thread Raw
In response to Re: string = any()  (Filip Rembiałkowski <plk.zuber@gmail.com>)
Responses Re: string = any()
List pgsql-general
-----Original Message-----
From: pgsql-general-owner@postgresql.org [mailto:pgsql-general-owner@postgresql.org] On Behalf Of Filip Rembialkowski
Sent: Tuesday, January 10, 2012 4:27 PM
To: Andy Colson
Cc: PostgreSQL
Subject: Re: [GENERAL] string = any()


no, I meant array constructor with reserved word ARRAY:

prepare x as select 'bob' = any( ARRAY[$2,$3] );
execute x( 'joe', 'bob' );

and in PHP:
<?php
$names = array( "joe", "bob" );
$placeholders = implode( ",", array_map( function($x){return "?"},
$names ) ); #I love Perl
$sql = "select 'bob' = any( ARRAY[$placeholders] )";
$stmt = $dbh->prepare($sql);
$stmt->execute($names);
?>

-----------------------------------------------------------------------------
Filip,

If you are going to supply one parameter per "possible value" anyway skip the whole "ANY" and "ARRAY" and just say "
'bob'IN ($1, $2 [,...]) ";  The whole point of the exercise is to avoid dynamic SQL on the language side by ALWAYS
havinga single input regardless of how many possible values exists.  Now, ideally you could pass in an actual ARRAY
objectfrom your programming language but as that, for whatever reason, tends to be clumsy or difficult the next best
optionis to pass in a single delimited string and then let PostGRESql convert it into an ARRAY and then use "=
ANY($1)".

David J.



pgsql-general by date:

Previous
From: Yoann MOREAU
Date:
Subject: Efficient map looking table
Next
From: "David Johnston"
Date:
Subject: Re: Efficient map looking table