1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-27 12:41:57 +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

@ -917,6 +917,36 @@ INSERT INTO tbl1 VALUES ($1, $2) \bind 'first value' 'second value' \g
</listitem>
</varlistentry>
<varlistentry id="app-psql-meta-command-bind-named">
<term><literal>\bind_named</literal> <replaceable class="parameter">statement_name</replaceable> [ <replaceable class="parameter">parameter</replaceable> ] ... </term>
<listitem>
<para>
<literal>\bind_named</literal> is equivalent to <literal>\bind</literal>,
except that it takes the name of an existing prepared statement as
first parameter. An empty string denotes the unnamed prepared
statement.
</para>
<para>
Example:
<programlisting>
INSERT INTO tbls1 VALUES ($1, $2) \parse stmt1
\bind_named stmt1 'first value' 'second value' \g
</programlisting>
</para>
<para>
This command causes the extended query protocol (see
<xref linkend="protocol-query-concepts"/>) to be used, unlike normal
<application>psql</application> operation, which uses the simple
query protocol. So this command can be useful to test the extended
query protocol from <application>psql</application>.
</para>
</listitem>
</varlistentry>
<varlistentry id="app-psql-meta-command-c-lc">
<term><literal>\c</literal> or <literal>\connect [ -reuse-previous=<replaceable class="parameter">on|off</replaceable> ] [ <replaceable class="parameter">dbname</replaceable> [ <replaceable class="parameter">username</replaceable> ] [ <replaceable class="parameter">host</replaceable> ] [ <replaceable class="parameter">port</replaceable> ] | <replaceable class="parameter">conninfo</replaceable> ]</literal></term>
<listitem>
@ -1038,6 +1068,35 @@ INSERT INTO tbl1 VALUES ($1, $2) \bind 'first value' 'second value' \g
</listitem>
</varlistentry>
<varlistentry id="app-psql-meta-command-close">
<term><literal>\close</literal> <replaceable class="parameter">prepared_statement_name</replaceable></term>
<listitem>
<para>
Closes the specified prepared statement. An empty string denotes the
unnamed prepared statement. If no prepared statement exists with this
name, the operation is a no-op.
</para>
<para>
Example:
<programlisting>
SELECT $1 \parse stmt1
\close stmt1
</programlisting>
</para>
<para>
This command causes the extended query protocol to be used,
unlike normal <application>psql</application> operation, which
uses the simple query protocol. So this command can be useful
to test the extended query protocol from
<application>psql</application>.
</para>
</listitem>
</varlistentry>
<varlistentry id="app-psql-meta-commands-copy">
<term><literal>\copy { <replaceable class="parameter">table</replaceable> [ ( <replaceable class="parameter">column_list</replaceable> ) ] }
<literal>from</literal>
@ -2780,6 +2839,37 @@ lo_import 152801
</listitem>
</varlistentry>
<varlistentry id="app-psql-meta-command-parse">
<term><literal>\parse <replaceable class="parameter">statement_name</replaceable></literal></term>
<listitem>
<para>
Creates a prepared statement from the current query buffer, based on
the name of a destination prepared-statement object. An empty string
denotes the unnamed prepared statement.
</para>
<para>
Example:
<programlisting>
SELECT $1 \parse stmt1
</programlisting>
</para>
<para>
This command causes the extended query protocol to be used, unlike
normal <application>psql</application> operation, which uses the
simple query protocol. A
<xref linkend="protocol-message-formats-Parse"/>
message will be issued by this command so it can be useful to
test the extended query protocol from
<application>psql</application>. This command affects only the next
query executed; all subsequent queries will use the simple query
protocol by default.
</para>
</listitem>
</varlistentry>
<varlistentry id="app-psql-meta-command-password">
<term><literal>\password [ <replaceable class="parameter">username</replaceable> ]</literal></term>
<listitem>