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 1khjgl-0005GW-9L for pgsql-general@arkaria.postgresql.org; Wed, 25 Nov 2020 01:33:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1khjgj-0006EB-VF for pgsql-general@arkaria.postgresql.org; Wed, 25 Nov 2020 01:33:41 +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 1khjgj-0006E4-KW for pgsql-general@lists.postgresql.org; Wed, 25 Nov 2020 01:33:41 +0000 Received: from sonic309-15.consmr.mail.bf2.yahoo.com ([74.6.129.125]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1khjgf-0000v1-6n for pgsql-general@postgresql.org; Wed, 25 Nov 2020 01:33:41 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1606268014; bh=lZXdG4G0Ak2mKfv6YAe7n9YqGGpS9K8No5Xvl63eVfs=; h=Date:From:To:Cc:In-Reply-To:References:Subject:From:Subject; b=eXLqC9igrrQAODRA5mFRnbtqeboHBvyXSrXwG1OY+lrXTZmiseHZkc00qptqg8dnc27nKjCIIQ0aRuKF7E0t+ditXsdWas72Uofe7hF4cmnE31D/emiJCacP7D8Hxt0Gz6lK3OZkV7LTzgI8AXSpWG7n+BVWia6FTGIy1MRVdfgMB6X4z2MsS658PD8MBxaVjO5GD3a/6mXFfvfI7Yl3cP4BXp4FwLDE9hCU/1fEIiwnqUvdP4tTK/D/y5g+MrnmrrWqWof/PSBI5WdDAXDDz7ThfKoYbLlVoikVx8QaViSa2uLy0RjlTkU8HXk34pdW1ivlibI/62SApG9WHxbv7A== X-SONIC-DKIM-SIGN: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1606268014; bh=q/TgEzHu3LsscbGrvr68GdBsXgZmMeIwUhZ2Cnel8EM=; h=Date:From:To:Subject:From:Subject; b=fQgyVRDp2a8GuY5WzZ7M9jNyQ6jTMyNxD/Rl4yIsm2IBQjbIGQG8wEgll+oJlo2yIjiBVdRlH3VGYse/0koh5u41UyMrsN5Cy83Wu7/rO9krQGSWlitS2ukH2/t65sjLwLhHpNjs79xhLvYL5EH3ykRjQnYE1dTcJpOsps/B3rJcY7ZbM86NxjeMQ3TtfZ0S1+I6KNYa/zC3yf/3Uc6ErbvtISUbpZVv58ifPoF90hrumKf56lCJM8T9Wm9lFxNc5A+GVAB9pfKwntD/AaHM3EmOZbceqMhE0P3shmd5J90U9LcETLnBD5a8jGz1Xh0/BevxF9a1vgIWQDL2oOco5g== X-YMail-OSG: CRfLk6gVM1lhs3P0LZ1.0I.ThpP0eRHgbyjfMO55jH.0JpVml.P9IXM4q84gfJV qzL.790dGcGTDFtDb_3_xwl0S9P50sS0CiyACXghJKI_QxhXKxKt8CWK2gVOvq43zrJGh7DPzBOa lpfXszn0..zy_O_JsdxKKeCyRykx9tmibfQuiIds941n5hlB3DYNKuqHlYWJ6fm5IgFZ_.V0Es8n AvypYwct0vzO8IcCgw07mPlPK9IpngjKiJp6N9OBm4rI6uJrR06BH.CmcyxcOyHaokVex.vzgJep Q7VQRoPXVZ72Nh4V4xWUUG5BSRWj1yXKt7BG8KrHR9iZoXuFsgMNRUBVwnAb9BpLavqX8i71GOJd pX_GFALosMt1yeTQaVtlEBi26.KEyuUnSSQqT_QOZCSIR52AoXdT7RHqM0bcs.BWKFTi5F8HgEXH 6.KmB8oxlWM778AWsCRCNsJoBw_ONYTL47DD_i_ArflVbnSZzKRXHl5c4fovjlV.4in_vx6dtGc5 JKABiwOxlPrLKUSPwjgM3NBUZT.esKthlS1G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8YhrVsmWl87duBdCzCZmiwyDtWX6tp0YZ jms.396oODbWsXCEtap7Kz4E9as1Su6kVeoM3JeloPgqF79O_grXvPhnQc6r2Svl5kb9LFHreJSy ZXPPfAa8g96ndMh15mxh3nNnOY.sbO.swA2ME0W6D3P3tEMxe6kEskzzROS_xwL_nJrej2xR5R43 JabJimO.mcE1_vFcE0GWpQM78EVF9s8ePKKyeZBFsGx5XpwSK7UsKcCFC59_aI2iyx.3Ix45VOGN N3M8tJiXlmTT8zpy12LYl49T7hA.fp.I62c2wQ.uWyl4TnH8TxFPl0PlhQCeqsTE0PCu1IEP1A4u rH7501Hihh5G8IPSYXOt5XRHwaP0GLW40J.rqEemGqU434ZvBxP_fzUZLaLf42duYd2h4ZZ.iZU8 mbbaJqlBdpbJinC5_yeBjMMPUsScixKTB4CvN59clxNYY_BUmfGRcNzHOrM6IdOG6A0nF16Qn8Up _gsR_Qn7ptp_syjYPpWyuQ.GhxKExK66IsOByaWIQphxFhsH9q5TMiGVurJobkiIXxmQgFHXxxI4 EmMz_CecNlwwz_7Z86wQZIa_U.5vCArKIbh0TbHfcT.nVEIdhQV4YYb9.CBuze4GAwtOb_8b8ZEQ uqsoZfnjuk.9T13WInSowZ7i49giltdVSIT.grvRPMicSjGGlpKVXG6TlzYvJXA_XsxoHDnKtywx HZqVVdoEDvBAE9qxcTIHVxbS7Oo34FuhToXLT2lm2eIwjfxUg.f6dXTJnp9WH2bxoPVTQNQa0fcj Q51x8mlCY1Kg6g8CqNe9bdlIo9YiT3pKcp0pNT78f2BvWQsZawYxLKj9_ozBriIzotlaxNhqMNnt gStUpuIhZA.UGSc_vj8rBXWt8BkJaqEkA.FubnF37SHwcidOmtmGliU3wqZfs1UIatcGMPS8- Received: from sonic.gate.mail.ne1.yahoo.com by sonic309.consmr.mail.bf2.yahoo.com with HTTP; Wed, 25 Nov 2020 01:33:34 +0000 Date: Wed, 25 Nov 2020 01:33:31 +0000 (UTC) From: Dave Greeko To: "David G. Johnston" Cc: pgsql-general Message-ID: <460944945.821038.1606268011494@mail.yahoo.com> In-Reply-To: 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> <1047012595.795292.1606258039511@mail.yahoo.com> Subject: Re: libpq Prepared Statement with dynamic IN operator MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_821037_1923748489.1606268011492" 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: 9182 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk ------=_Part_821037_1923748489.1606268011492 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Many thanks for the clarification David. I wish there is a way without tou= ching my input array but at least based on your feedback I can avoid the ca= ll to string_to_array call in the query. I tried it and it worked but I sti= ll have to permute the C array. Here is what I have working so far: char *query=3D"select codec_id,fs_name,pt from codec_defs where fs_name =3D= ANY($1::text[])"; PGresult *res=3DPQprepare(conn,"codecs",query,1,NULL); char *input[1] =3D{ "{G722,PCMU}" }; //this will be dynamic at run-time res=3DPQexecPrepared(conn,"codecs",1,(const char* const*)input,NULL ,NULL,0= ); Dave. On Tuesday, November 24, 2020, 04:13:06 PM PST, David G. Johnston wrote: =20 =20 The convention here is to inline or bottom-post, not top-post. On Tue, Nov 24, 2020 at 3:47 PM Dave Greeko wrote: I would really like to just pass an array of filters of type (const char* c= onst*) to PQexecPrepared's paramValues[] parameter instead of making it som= e sort of csv string.=20 //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 I don't use the C API myself but a quick observation is that you specified = nParams=3D1 during prepare and nParams=3D2 during execute, so there is no w= ay it is going to work.=C2=A0 nParams=3D1 is correct for both - you must on= ly pass a single value to the backend, that value must be of type "text arr= ay" (however one does that here).=C2=A0 Since you've chosen not to provide = a data type OID that would mean: "If paramTypes is NULL, or any particular = element in the array is zero, the server assigns a data type to the paramet= er symbol in the same way it would do for an untyped literal string.".=C2= =A0 As the server will never assign an untyped literal string to be an arra= y the best you can do without an OID here is supply a literal that can be c= ast directly to "text[]" (https://www.postgresql.org/docs/current/arrays.ht= ml#ARRAYS-INPUT) - and then write the cast into the sql query, all of which= is probably only a nominal difference from using "csv" and the string_to_a= rray function.=C2=A0 Even if you do specify whatever oid is "text[]" all yo= u really avoid is the cast in the query - the API is expecting text in the = values argument, not an "array object" (or the equivalent in C).=C2=A0 That= said, if you can import a client-side header that provides a helper functi= on for this array-to-text conversion (idk?) then passing the data becomes e= asier and you just need to decide whether to add a cast in the SQL or provi= de the OID. David J. =20 ------=_Part_821037_1923748489.1606268011492 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Many thanks for the clarification David. I wish there = is a way without touching my input array but at least based on your feedbac= k I can avoid the call to string_to_array call in the query. I tried it and= it worked but I still have to permute the C array. Here is what I have wor= king so far:


char *query=3D"select codec_id,fs_name,pt from= codec_defs where fs_name =3D ANY($1::text[])";
PGresult *res=3DPQ= prepare(conn,"codecs",query,1,NULL);

char *input[1] =3D{ &= quot;{G722,PCMU}" }; //this will be dynamic at run-time
res=3DPQexe= cPrepared(conn,"codecs",1,(const char* const*)input,NULL ,NULL,0)= ;

Dave.

=
On Tuesday, November 24, 2020= , 04:13:06 PM PST, David G. Johnston <david.g.johnston@gmail.com> wro= te:


The convention here is to inline or bottom-post, n= ot top-post.

On Tue, Nov 24, = 2020 at 3:47 PM Dave Greeko <davegreeko@yahoo.com> wrote:
<= /div>
I would really like to jus= t pass an array of filters of type (const char* const*) to PQexecPrepared's= paramValues[] parameter instead of making it some sort of csv string.
//The second version( ANY($1::text[] )
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",que= ry,1,NULL);

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

//P= QexecPrepared() generates Error:
ERROR: bind message sup= plies 2 parameters, but prepared statement "codecs" requires 1

I don't use th= e C API myself but a quick observation is that you specified nParams=3D1 du= ring prepare and nParams=3D2 during execute, so there is no way it is going= to work.  nParams=3D1 is correct for both - you must only pass a sing= le value to the backend, that value must be of type "text array" (however o= ne does that here).  Since you've chosen not to provide a data type OI= D that would mean: "If paramTypes is NULL, or any particular element in the= array is zero, the server assigns a data type to the parameter symbol in t= he same way it would do for an untyped literal string.".  As the serve= r will never assign an untyped literal string to be an array the best you c= an do without an OID here is supply a literal that can be cast directly to = "text[]" (https://www.p= ostgresql.org/docs/current/arrays.html#ARRAYS-INPUT) - and then write t= he cast into the sql query, all of which is probably only a nominal differe= nce from using "csv" and the string_to_array function.  Even if you do= specify whatever oid is "text[]" all you really avoid is the cast in the q= uery - the API is expecting text in the values argument, not an "array obje= ct" (or the equivalent in C).  That said, if you can import a client-s= ide header that provides a helper function for this array-to-text conversio= n (idk?) then passing the data becomes easier and you just need to decide w= hether to add a cast in the SQL or provide the OID.

David J.

------=_Part_821037_1923748489.1606268011492--