From: Dave Greeko <davegreeko@yahoo.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: David G. Johnston <david.g.johnston@gmail.com>
Cc: pgsql-general <pgsql-general@postgresql.org>
Subject: Re: libpq Prepared Statement with dynamic IN operator
Date: Tue, 24 Nov 2020 22:47:19 +0000 (UTC)
Message-ID: <1047012595.795292.1606258039511@mail.yahoo.com> (raw)
In-Reply-To: <310537.1606252691@sss.pgh.pa.us>
References: <160624463958.2014.4816164987028270278@malur.postgresql.org>
<418457940.732204.1606245243551@mail.yahoo.com>
<CAKFQuwZatW8rvCq6FbWnEkqqWzYNKWyt24mUpTSQNExUtruyrQ@mail.gmail.com>
<486897627.767571.1606252454106@mail.yahoo.com>
<310537.1606252691@sss.pgh.pa.us>
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 prepared 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_to_array delimiter when calling PQexecPrepared(). I would really like to just pass an array of filters of type (const char* const*) to PQexecPrepared's paramValues[] parameter instead of making it some sort of csv string.
Here is a sample working code:
char *query="select codec_id,fs_name,pt from codec_defs where fs_name = ANY(string_to_array($1, ','))";
PGresult *res=PQprepare(conn,"codecs",query,1,NULL);
char *input[1] ={"G729,PCMU"};
PQclear(res);
res=PQexecPrepared(conn,"codecs",1,(const char* const*)input,NULL ,NULL,0);
//The second version( ANY($1::text[] )
char *query="select codec_id,fs_name,pt from codec_defs where fs_name = ANY($1::text[])";
//this gets prepared successfully
PGresult *res=PQprepare(conn,"codecs",query,1,NULL);
char *input[2] ={"G729","PCMU"};
PQclear(res);
//the below generate an erro:
res=PQexecPrepared(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 <tgl@sss.pgh.pa.us> wrote:
Dave Greeko <davegreeko@yahoo.com> writes:
> I tried both and I am getting syntax error.
> char *query="select codec_id,fs_name,pt from codec_defs where pt = ANY(string_to_array(?, ','))";
> OR
> char *query="select codec_id,fs_name,pt from codec_defs where pt = ANY(?::text)";
> PGresult *res=PQprepare(conn,"codecs",query,1,NULL);
Well, your first problem is that "?" is not the parameter symbol
understood 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 needs to
look more like "where pt = ANY($1::text[])".
regards, tom lane
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: davegreeko@yahoo.com, tgl@sss.pgh.pa.us, david.g.johnston@gmail.com
Subject: Re: libpq Prepared Statement with dynamic IN operator
In-Reply-To: <1047012595.795292.1606258039511@mail.yahoo.com>
References: <160624463958.2014.4816164987028270278@malur.postgresql.org> <418457940.732204.1606245243551@mail.yahoo.com> <CAKFQuwZatW8rvCq6FbWnEkqqWzYNKWyt24mUpTSQNExUtruyrQ@mail.gmail.com> <486897627.767571.1606252454106@mail.yahoo.com> <310537.1606252691@sss.pgh.pa.us> <1047012595.795292.1606258039511@mail.yahoo.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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