1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-07 19:06:32 +03:00

psql: Add more meta-commands able to use the extended protocol

Currently, only unnamed prepared statement are supported by psql with
the meta-command \bind.  With only this command, it is not possible to
test named statement creation, execution or close through the extended
protocol.

This commit introduces three additional commands:
* \parse creates a prepared statement using the extended protocol,
acting as a wrapper of libpq's PQsendPrepare().
* \bind_named binds and executes an existing prepared statement using
the extended protocol, for PQsendQueryPrepared().
* \close closes an existing prepared statement using the extended
protocol, for PQsendClosePrepared().

This is going to be useful to add regression tests for the extended
query protocol, and I have some plans for that on separate threads.
Note that \bind relies on PQsendQueryParams().

The code of psql is refactored so as bind_flag is replaced by an enum in
_psqlSettings that tracks the type of libpq routine to execute, based on
the meta-command involved, with the default being PQsendQuery().  This
refactoring piece has been written by me, while Anthonin has implemented
the rest.

Author: Anthonin Bonnefoy, Michael Paquier
Reviewed-by: Aleksander Alekseev, Jelte Fennema-Nio
Discussion: https://postgr.es/m/CAO6_XqpSq0Q0kQcVLCbtagY94V2GxNP3zCnR6WnOM8WqXPK4nw@mail.gmail.com
This commit is contained in:
Michael Paquier
2024-08-22 16:25:57 +09:00
parent a36aa223ec
commit d55322b0da
9 changed files with 369 additions and 19 deletions

View File

@@ -45,8 +45,28 @@ SELECT 1 as one, 2 as two \g (format=csv csv_fieldsep='\t')
SELECT 1 as one, 2 as two \gx (title='foo bar')
\g
-- \bind (extended query protocol)
-- \parse (extended query protocol)
\parse
SELECT 1 \parse ''
SELECT 2 \parse stmt1
SELECT $1 \parse stmt2
SELECT $1, $2 \parse stmt3
-- \bind_named (extended query protocol)
\bind_named
\bind_named '' \g
\bind_named stmt1 \g
\bind_named stmt2 'foo' \g
\bind_named stmt3 'foo' 'bar' \g
-- \close (extended query protocol)
\close
\close ''
\close stmt2
\close stmt2
SELECT name, statement FROM pg_prepared_statements ORDER BY name;
-- \bind (extended query protocol)
SELECT 1 \bind \g
SELECT $1 \bind 'foo' \g
SELECT $1, $2 \bind 'foo' 'bar' \g
@@ -58,6 +78,9 @@ SELECT foo \bind \g
SELECT 1 \; SELECT 2 \bind \g
-- bind error
SELECT $1, $2 \bind 'foo' \g
-- bind_named error
\bind_named stmt2 'baz' \g
\bind_named stmt3 'baz' \g
-- \gset
@@ -990,9 +1013,11 @@ select \if false \\ (bogus \else \\ 42 \endif \\ forty_two;
\pset fieldsep | `nosuchcommand` :foo :'foo' :"foo"
\a
SELECT $1 \bind 1 \g
\bind_named stmt1 1 2 \g
\C arg1
\c arg1 arg2 arg3 arg4
\cd arg1
\close stmt1
\conninfo
\copy arg1 arg2 arg3 arg4 arg5 arg6
\copyright
@@ -1020,6 +1045,7 @@ select \if false \\ (bogus \else \\ 42 \endif \\ forty_two;
\lo_list
\o arg1
\p
SELECT 1 \parse
\password arg1
\prompt arg1 arg2
\pset arg1 arg2