1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-02 09:02:37 +03:00

Allow the types of parameters to PREPARE to be inferred. If a parameter's

data type is unspecified or is declared to be "unknown", the type will
be inferred from the context in which the parameter is used. This was
already possible for protocol-level prepared statements.
This commit is contained in:
Neil Conway
2006-01-15 22:18:47 +00:00
parent f7ea931287
commit 106a3695f5
6 changed files with 95 additions and 60 deletions

View File

@ -1,5 +1,5 @@
<!--
$PostgreSQL: pgsql/doc/src/sgml/ref/deallocate.sgml,v 1.7 2004/09/30 04:23:27 neilc Exp $
$PostgreSQL: pgsql/doc/src/sgml/ref/deallocate.sgml,v 1.8 2006/01/15 22:18:46 neilc Exp $
PostgreSQL documentation
-->
@ -25,7 +25,7 @@ PostgreSQL documentation
<refsynopsisdiv>
<synopsis>
DEALLOCATE [ PREPARE ] <replaceable class="parameter">plan_name</replaceable>
DEALLOCATE [ PREPARE ] <replaceable class="parameter">name</replaceable>
</synopsis>
</refsynopsisdiv>
@ -58,7 +58,7 @@ DEALLOCATE [ PREPARE ] <replaceable class="parameter">plan_name</replaceable>
</varlistentry>
<varlistentry>
<term><replaceable class="parameter">plan_name</replaceable></term>
<term><replaceable class="parameter">name</replaceable></term>
<listitem>
<para>
The name of the prepared statement to deallocate.

View File

@ -1,5 +1,5 @@
<!--
$PostgreSQL: pgsql/doc/src/sgml/ref/execute.sgml,v 1.12 2004/09/30 04:23:27 neilc Exp $
$PostgreSQL: pgsql/doc/src/sgml/ref/execute.sgml,v 1.13 2006/01/15 22:18:46 neilc Exp $
PostgreSQL documentation
-->
@ -25,7 +25,7 @@ PostgreSQL documentation
<refsynopsisdiv>
<synopsis>
EXECUTE <replaceable class="PARAMETER">plan_name</replaceable> [ (<replaceable class="PARAMETER">parameter</replaceable> [, ...] ) ]
EXECUTE <replaceable class="PARAMETER">name</replaceable> [ (<replaceable class="PARAMETER">parameter</replaceable> [, ...] ) ]
</synopsis>
</refsynopsisdiv>
@ -60,7 +60,7 @@ EXECUTE <replaceable class="PARAMETER">plan_name</replaceable> [ (<replaceable c
<variablelist>
<varlistentry>
<term><replaceable class="PARAMETER">plan_name</replaceable></term>
<term><replaceable class="PARAMETER">name</replaceable></term>
<listitem>
<para>
The name of the prepared statement to execute.
@ -73,10 +73,9 @@ EXECUTE <replaceable class="PARAMETER">plan_name</replaceable> [ (<replaceable c
<listitem>
<para>
The actual value of a parameter to the prepared statement. This
must be an expression yielding a value of a type compatible with
the data type specified for this parameter position in the
<command>PREPARE</command> command that created the prepared
statement.
must be an expression yielding a value that is compatible with
the data type of this parameter, as was determined when the
prepared statement was created.
</para>
</listitem>
</varlistentry>

View File

@ -1,5 +1,5 @@
<!--
$PostgreSQL: pgsql/doc/src/sgml/ref/prepare.sgml,v 1.17 2006/01/08 07:00:25 neilc Exp $
$PostgreSQL: pgsql/doc/src/sgml/ref/prepare.sgml,v 1.18 2006/01/15 22:18:46 neilc Exp $
PostgreSQL documentation
-->
@ -25,7 +25,7 @@ PostgreSQL documentation
<refsynopsisdiv>
<synopsis>
PREPARE <replaceable class="PARAMETER">plan_name</replaceable> [ (<replaceable class="PARAMETER">datatype</replaceable> [, ...] ) ] AS <replaceable class="PARAMETER">statement</replaceable>
PREPARE <replaceable class="PARAMETER">name</replaceable> [ (<replaceable class="PARAMETER">datatype</replaceable> [, ...] ) ] AS <replaceable class="PARAMETER">statement</replaceable>
</synopsis>
</refsynopsisdiv>
@ -45,13 +45,15 @@ PREPARE <replaceable class="PARAMETER">plan_name</replaceable> [ (<replaceable c
<para>
Prepared statements can take parameters: values that are
substituted into the statement when it is executed. To include
parameters in a prepared statement, supply a list of data types in
the <command>PREPARE</command> statement, and, in the statement to
be prepared itself, refer to the parameters by position using
<literal>$1</literal>, <literal>$2</literal>, etc. When executing
the statement, specify the actual values for these parameters in
the <command>EXECUTE</command> statement. Refer to <xref
substituted into the statement when it is executed. When creating
the prepared statement, refer to parameters by position, using
<literal>$1</>, <literal>$2</>, etc. A corresponding list of
parameter data types can optionally be specified. When a
parameter's data type is not specified or is declared as
<literal>unknown</literal>, the type is inferred from the context
in which the parameter is used (if possible). When executing the
statement, specify the actual values for these parameters in the
<command>EXECUTE</command> statement. Refer to <xref
linkend="sql-execute" endterm="sql-execute-title"> for more
information about that.
</para>
@ -84,7 +86,7 @@ PREPARE <replaceable class="PARAMETER">plan_name</replaceable> [ (<replaceable c
<variablelist>
<varlistentry>
<term><replaceable class="PARAMETER">plan_name</replaceable></term>
<term><replaceable class="PARAMETER">name</replaceable></term>
<listitem>
<para>
An arbitrary name given to this particular prepared
@ -99,8 +101,11 @@ PREPARE <replaceable class="PARAMETER">plan_name</replaceable> [ (<replaceable c
<term><replaceable class="PARAMETER">datatype</replaceable></term>
<listitem>
<para>
The data type of a parameter to the prepared statement. To
refer to the parameters in the prepared statement itself, use
The data type of a parameter to the prepared statement. If the
data type of a particular parameter is unspecified or is
specified as <literal>unknown</literal>, it will be inferred
from the context in which the parameter is used. To refer to the
parameters in the prepared statement itself, use
<literal>$1</literal>, <literal>$2</literal>, etc.
</para>
</listitem>
@ -155,8 +160,8 @@ PREPARE <replaceable class="PARAMETER">plan_name</replaceable> [ (<replaceable c
<refsect1 id="sql-prepare-examples">
<title id="sql-prepare-examples-title">Examples</title>
<para>
Create a prepared query for an <command>INSERT</command> statement,
and then execute it:
Create a prepared statement for an <command>INSERT</command>
statement, and then execute it:
<programlisting>
PREPARE fooplan (int, text, bool, numeric) AS
INSERT INTO foo VALUES($1, $2, $3, $4);
@ -165,14 +170,17 @@ EXECUTE fooplan(1, 'Hunter Valley', 't', 200.00);
</para>
<para>
Create a prepared query for a <command>SELECT</command> statement,
and then execute it:
Create a prepared statement for a <command>SELECT</command>
statement, and then execute it:
<programlisting>
PREPARE usrrptplan (int, date) AS
PREPARE usrrptplan (int) AS
SELECT * FROM users u, logs l WHERE u.usrid=$1 AND u.usrid=l.usrid
AND l.date = $2;
EXECUTE usrrptplan(1, current_date);
</programlisting>
Note that the data type of the second parameter is not specified,
so it is inferred from the context in which <literal>$2</> is used.
</para>
</refsect1>
<refsect1>