Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1khh5v-0006BV-L3 for pgsql-general@arkaria.postgresql.org; Tue, 24 Nov 2020 22:47:31 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1khh5u-0003xA-J6 for pgsql-general@arkaria.postgresql.org; Tue, 24 Nov 2020 22:47:30 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1khh5t-0003x2-Di for pgsql-general@lists.postgresql.org; Tue, 24 Nov 2020 22:47:30 +0000 Received: from sonic317-27.consmr.mail.bf2.yahoo.com ([74.6.129.82]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1khh5q-0007za-Nx for pgsql-general@postgresql.org; Tue, 24 Nov 2020 22:47:28 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1606258044; bh=Sb75fPy8Wi7omlwbJFPH/5IYX3HY+yINJuuoMaLq1AI=; h=Date:From:To:Cc:In-Reply-To:References:Subject:From:Subject; b=nH+iQLSlaYnTgU/7WJOJoE5QGv06umKT3pXwdTRh8V4gmnkSLMj26vi26a+gPmPYvelB077OYwtd3uAx1h8E8UqYBLOIg6rMHka8zfrvY9/717L+QGPwtrPInemoGoYByM/BjtTSvhI7ohWT/MeWPnl0uO2lRsVaqxw8o4IkiJYZXIQFvwOGDstmNqQhvf/07CrkhJM96rNqYW9lriJyEB4S6QSgI8+Ni8ilVdt5wHh6ooqN35jsNqCK98vgg0+CPp94U+nKviv14Rz9md4RqKB30LMyE73UJE5Z3YHzm3ENKKKRxj8DrFnA2NvWWo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OlIdZoz72NlQ4dzzfoW3JkxY2JNaJUqdj SzWBoB7iFG145Rt31VGmJbbwje6ugAro6ZuW3QEZOxSgQ2aZmJUGoSfMbf8.EjJXc8Beu78769vo xO_dwR3UDyleMkoEm1FPDqVgsdsNU_jEkRWTESEHcJfgfqLC.Vcw37N4Vdd1kNUjZomHW5fIFnga FUZTTVunikH2L7Fuy9E.xYuG2DOocckziWe28bNBQBGidSSGOcGfQlkYOmfJDI8PuSTOva6QbMqu 2jqhmwpPabVuPCyKu8zQNEDpMBAgPrBJYoo4hSu4rzKxIozorsfYH5gKGmBtrknIQrxcFqV4Q8NB lAmJamblYFaDlsp3bFmBGUomZqKJgRQEWPF4E5Ui1.KzWUN52XuXqGvtLvGVMHfQGNmennURMnoH HhQ4XtFHJTSg4phS.g2o6PrNRLzNkvBiUcU9nwQwYIjiWMsFCSfmvN.l4C7eSjV2K3XIfkkBvNmK YeUPVfOQwvndZvkvfTh1HXjUjfk_tXBUvSaVvQ3XTKrUGeI1X5yqB4N1SnRSepis.npC1zZYaE._ Zv1P01RBcTlBhyCnn_EhwGs7LL1YEIJrFamXJPV15BPkWDyyhcLxINN8ID24HgOxGH8zwISB.Qkp mmRbJQmW7FwWa1K7Df.X.hluvJaIJRFdYskFzXPJVMnUf7k.Ij9mdMj8Y9fJjQEh15E1isgyHrNi 7WnRpdkimZ7F9lrpXjXX2bSQVX0h43A5jByeqypRPDR4gef1XQnOxLADiHj_Kxqe5_9gku3PIPvh 1HMQlZj7z0gW6thzLBhIDHiikDAHp9ht.zjIExKhiLWeEqAw3dFQXKnw_kizljWz6uTyKl1WSo.7 pDFGlXj6xNmsdYV9gdcDUGL.4IJGCbZaWzeHBhGslvsyfJKSa6ORzxE42BhJqY5.9hxExna57_SW rjZCx6bu3DwkjSc.zPNdati29G94_jqBttQ-- Received: from sonic.gate.mail.ne1.yahoo.com by sonic317.consmr.mail.bf2.yahoo.com with HTTP; Tue, 24 Nov 2020 22:47:24 +0000 Date: Tue, 24 Nov 2020 22:47:19 +0000 (UTC) From: Dave Greeko To: Tom Lane Cc: "David G. Johnston" , pgsql-general Message-ID: <1047012595.795292.1606258039511@mail.yahoo.com> In-Reply-To: <310537.1606252691@sss.pgh.pa.us> References: <160624463958.2014.4816164987028270278@malur.postgresql.org> <418457940.732204.1606245243551@mail.yahoo.com> <486897627.767571.1606252454106@mail.yahoo.com> <310537.1606252691@sss.pgh.pa.us> Subject: Re: libpq Prepared Statement with dynamic IN operator MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_795291_779104159.1606258039510" X-Mailer: WebService/1.1.17111 YMailNodin Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:83.0) Gecko/20100101 Firefox/83.0 Content-Length: 5903 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk ------=_Part_795291_779104159.1606258039510 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable I am sorry I used different query in my my last reply and yes you are corr= ect Tom. Using the $1 worked and the back-end indeed prepared the statement= successfully but this will force me to do some work on the input array tha= t contains the dynamic elements to comply with string_to_array delimiter wh= en calling PQexecPrepared(). I would really like to just pass an array of f= ilters of type (const char* const*) to PQexecPrepared's paramValues[] param= eter instead of making it some sort of csv string.=20 Here is a sample working code: char *query=3D"select codec_id,fs_name,pt from codec_defs where fs_name =3D= ANY(string_to_array($1, ','))"; PGresult *res=3DPQprepare(conn,"codecs",query,1,NULL); char *input[1] =3D{"G729,PCMU"}; PQclear(res); res=3DPQexecPrepared(conn,"codecs",1,(const char* const*)input,NULL ,NULL,0= ); //The second version( ANY($1::text[] )=20 char *query=3D"select codec_id,fs_name,pt from codec_defs where fs_name =3D= ANY($1::text[])"; //this gets prepared successfully PGresult *res=3DPQprepare(conn,"codecs",query,1,NULL); char *input[2] =3D{"G729","PCMU"}; PQclear(res); //the below generate an erro: res=3DPQexecPrepared(conn,"codecs",2,(const char* const*)input,NULL ,NULL,0= ); //PQexecPrepared() generates Error: ERROR: bind message supplies 2 parameters, but prepared statement "codecs" = requires 1 On Tuesday, November 24, 2020, 01:18:15 PM PST, Tom Lane wrote: =20 =20 Dave Greeko writes: > I tried both and I am getting syntax error. > char *query=3D"select codec_id,fs_name,pt from codec_defs where pt =3D AN= Y(string_to_array(?, ','))"; > OR > char *query=3D"select codec_id,fs_name,pt from codec_defs where pt =3D AN= Y(?::text)"; > PGresult *res=3DPQprepare(conn,"codecs",query,1,NULL); Well, your first problem is that "?" is not the parameter symbol understood by libpq+backend.=C2=A0 Try "$1".=C2=A0 The other problem, at least for the second version of that, is that you want to be passing a text array not a single text value --- so it needs to look more like "where pt =3D ANY($1::text[])". =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 regards, tom lane = =20 ------=_Part_795291_779104159.1606258039510 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
I am sorry I used different query in my my last reply = and yes you are correct Tom. Using the $1 worked and the back-end indeed pr= epared the statement successfully but this will force me to do some work on= the input array that contains the dynamic elements to comply with string_t= o_array delimiter when calling PQexecPrepared(). I would really like to jus= t pass an array of filters of type (const char* const*) to PQexecPrepared&#= 39;s paramValues[] parameter instead of making it some sort of csv string. =


Here is a sample working code:

char *query=3D"selec= t codec_id,fs_name,pt from codec_defs where fs_name =3D ANY(string_to_arra= y($1, ','))";
PGresult *res=3DPQprepare(conn,"codecs&q= uot;,query,1,NULL);
char *input[1] =3D{"G729,PCMU"};
PQclea= r(res);
res=3DPQexecPrepared(conn,"codecs",1,(const char* cons= t*)input,NULL ,NULL,0);

//The second version( ANY($1::text[] )
<= br>char *query=3D"select codec_id,fs_name,pt from codec_defs where fs= _name =3D ANY($1::text[])";
//this gets prepared successfully
PG= result *res=3DPQprepare(conn,"codecs",query,1,NULL);

char = *input[2] =3D{"G729","PCMU"};
PQclear(res);
//the= below generate an erro:
res=3DPQexecPrepared(conn,"codecs",2,= (const char* const*)input,NULL ,NULL,0);

//PQexecPrepared() generate= s Error:
ERROR: bind message supplies 2 parameters, but prepared statem= ent "codecs" requires 1



=
On= Tuesday, November 24, 2020, 01:18:15 PM PST, Tom Lane <tgl@sss.pgh.pa.u= s> wrote:

=

Dave Greeko <= davegreeko@yahoo.com> writes:

> I tried both and I am g= etting syntax error.

> char *query= =3D"select codec_id,fs_name,pt from codec_defs where pt =3D ANY(string_to_a= rray(?, ','))";
> OR
> char *quer= y=3D"select codec_id,fs_name,pt from codec_defs where pt =3D ANY(?::text)";=

> PGresult *res=3DPQprepare(conn,"= codecs",query,1,NULL);


Well, you= r first problem is that "?" is not the parameter symbol
u= nderstood by libpq+backend.  Try "$1".  The other problem,
at least for the second version of that, is that you want to be=
passing a text array not a single text value --- so it n= eeds to
look more like "where pt =3D ANY($1::text[])".
        &n= bsp;   regards, tom lane
=
------=_Part_795291_779104159.1606258039510--