Home Explore Blog CI



postgresql

7th chunk of `doc/src/sgml/pltcl.sgml`
590229dffa7c55288d7195f663b55151b51abe3be1b3acb80000000100000fa1
 the ID returned by
        <function>spi_prepare</function>.  If the query references parameters,
        a <replaceable>value-list</replaceable> must be supplied.  This
        is a Tcl list of actual values for the parameters.  The list must be
        the same length as the parameter type list previously given to
        <function>spi_prepare</function>.  Omit <replaceable>value-list</replaceable>
        if the query has no parameters.
       </para>
       <para>
        The optional value for <literal>-nulls</literal> is a string of spaces and
        <literal>'n'</literal> characters telling <function>spi_execp</function>
        which of the parameters are null values. If given, it must have exactly the
        same length as the <replaceable>value-list</replaceable>.  If it
        is not given, all the parameter values are nonnull.
       </para>
       <para>
        Except for the way in which the query and its parameters are specified,
        <function>spi_execp</function> works just like <function>spi_exec</function>.
        The <literal>-count</literal>, <literal>-array</literal>, and
        <replaceable>loop-body</replaceable> options are the same,
        and so is the result value.
       </para>
       <para>
        Here's an example of a PL/Tcl function using a prepared plan:

<programlisting>
CREATE FUNCTION t1_count(integer, integer) RETURNS integer AS $$
    if {![ info exists GD(plan) ]} {
        # prepare the saved plan on the first call
        set GD(plan) [ spi_prepare \
                "SELECT count(*) AS cnt FROM t1 WHERE num &gt;= \$1 AND num &lt;= \$2" \
                [ list int4 int4 ] ]
    }
    spi_execp -count 1 $GD(plan) [ list $1 $2 ]
    return $cnt
$$ LANGUAGE pltcl;
</programlisting>

        We need backslashes inside the query string given to
        <function>spi_prepare</function> to ensure that the
        <literal>$<replaceable>n</replaceable></literal> markers will be passed
        through to <function>spi_prepare</function> as-is, and not replaced by Tcl
        variable substitution.

       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term><function>subtransaction</function> <replaceable>command</replaceable></term>
      <listitem>
       <para>
        The Tcl script contained in <replaceable>command</replaceable> is
        executed within an SQL subtransaction.  If the script returns an
        error, that entire subtransaction is rolled back before returning the
        error out to the surrounding Tcl code.
        See <xref linkend="pltcl-subtransactions"/> for more details and an
        example.
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term><function>quote</function> <replaceable>string</replaceable></term>
      <listitem>
       <para>
        Doubles all occurrences of single quote and backslash characters
        in the given string.  This can be used to safely quote strings
        that are to be inserted into SQL commands given
        to <function>spi_exec</function> or
        <function>spi_prepare</function>.
        For example, think about an SQL command string like:

<programlisting>
"SELECT '$val' AS ret"
</programlisting>

        where the Tcl variable <literal>val</literal> actually contains
        <literal>doesn't</literal>. This would result
        in the final command string:

<programlisting>
SELECT 'doesn't' AS ret
</programlisting>

        which would cause a parse error during
        <function>spi_exec</function> or
        <function>spi_prepare</function>.
        To work properly, the submitted command should contain:

<programlisting>
SELECT 'doesn''t' AS ret
</programlisting>

        which can be formed in PL/Tcl using:

<programlisting>
"SELECT '[ quote $val ]' AS ret"
</programlisting>

        One advantage of <function>spi_execp</function> is that you don't
        have to quote parameter values like this, since the parameters are never
        parsed as

Title: PL/Tcl Database Access: spi_execp Details, Subtransactions, and Quote Function
Summary
This section elaborates on `spi_execp`, explaining its similarities to `spi_exec` and providing an example of its use in a PL/Tcl function. It also introduces the `subtransaction` command for executing Tcl scripts within SQL subtransactions and the `quote` function for safely quoting strings to be inserted into SQL commands, particularly useful for avoiding parse errors when using `spi_exec` or `spi_prepare` without prepared statements.