From 5b66de3433e2110b38a2b32aaaa0b9cdac8aacdb Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Tue, 15 Nov 2022 13:50:27 +0100 Subject: psql: Add command to use extended query protocol This adds a new psql command \bind that sets query parameters and causes the next query to be sent using the extended query protocol. Example: SELECT $1, $2 \bind 'foo' 'bar' \g This may be useful for psql scripting, but one of the main purposes is also to be able to test various aspects of the extended query protocol from psql and to write tests more easily. Reviewed-by: Corey Huinker Discussion: https://www.postgresql.org/message-id/flat/e8dd1cd5-0e04-3598-0518-a605159fe314@enterprisedb.com --- src/bin/psql/common.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'src/bin/psql/common.c') diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index 864f195992f..b989d792aa7 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1220,6 +1220,16 @@ sendquery_cleanup: pset.gsavepopt = NULL; } + /* clean up after \bind */ + if (pset.bind_flag) + { + for (i = 0; i < pset.bind_nparams; i++) + free(pset.bind_params[i]); + free(pset.bind_params); + pset.bind_params = NULL; + pset.bind_flag = false; + } + /* reset \gset trigger */ if (pset.gset_prefix) { @@ -1397,7 +1407,10 @@ ExecQueryAndProcessResults(const char *query, if (timing) INSTR_TIME_SET_CURRENT(before); - success = PQsendQuery(pset.db, query); + if (pset.bind_flag) + success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char * const *) pset.bind_params, NULL, NULL, 0); + else + success = PQsendQuery(pset.db, query); if (!success) { -- cgit v1.2.3