Home Explore Blog CI



postgresql

56th chunk of `doc/src/sgml/ecpg.sgml`
7384a4eba8aa64bb0245056240b9a09722827e8aaa1ee3510000000100000fa1
 </para>
      </listitem>
     </varlistentry>

     <varlistentry id="ecpg-whenever-goto">
      <term><literal>GOTO <replaceable>label</replaceable></literal></term>
      <term><literal>GO TO <replaceable>label</replaceable></literal></term>
      <listitem>
       <para>
        Jump to the specified label (using a C <literal>goto</literal>
        statement).
       </para>
      </listitem>
     </varlistentry>

     <varlistentry id="ecpg-whenever-sqlprint">
      <term><literal>SQLPRINT</literal></term>
      <listitem>
       <para>
        Print a message to standard error.  This is useful for simple
        programs or during prototyping.  The details of the message
        cannot be configured.
       </para>
      </listitem>
     </varlistentry>

     <varlistentry id="ecpg-whenever-stop">
      <term><literal>STOP</literal></term>
      <listitem>
       <para>
        Call <literal>exit(1)</literal>, which will terminate the
        program.
       </para>
      </listitem>
     </varlistentry>

     <varlistentry id="ecpg-whenever-do-break">
      <term><literal>DO BREAK</literal></term>
      <listitem>
       <para>
        Execute the C statement <literal>break</literal>.  This should
        only be used in loops or <literal>switch</literal> statements.
       </para>
      </listitem>
     </varlistentry>

     <varlistentry id="ecpg-whenever-do-continue">
      <term><literal>DO CONTINUE</literal></term>
      <listitem>
       <para>
        Execute the C statement <literal>continue</literal>.  This should
        only be used in loops statements.  if executed, will cause the flow
        of control to return to the top of the loop.
       </para>
      </listitem>
     </varlistentry>

     <varlistentry id="ecpg-whenever-call">
      <term><literal>CALL <replaceable>name</replaceable> (<replaceable>args</replaceable>)</literal></term>
      <term><literal>DO <replaceable>name</replaceable> (<replaceable>args</replaceable>)</literal></term>
      <listitem>
       <para>
        Call the specified C functions with the specified arguments.  (This
        use is different from the meaning of <literal>CALL</literal>
        and <literal>DO</literal> in the normal PostgreSQL grammar.)
       </para>
      </listitem>
     </varlistentry>
    </variablelist>

    The SQL standard only provides for the actions
    <literal>CONTINUE</literal> and <literal>GOTO</literal> (and
    <literal>GO TO</literal>).
   </para>

   <para>
    Here is an example that you might want to use in a simple program.
    It prints a simple message when a warning occurs and aborts the
    program when an error happens:
<programlisting>
EXEC SQL WHENEVER SQLWARNING SQLPRINT;
EXEC SQL WHENEVER SQLERROR STOP;
</programlisting>
   </para>

   <para>
    The statement <literal>EXEC SQL WHENEVER</literal> is a directive
    of the SQL preprocessor, not a C statement.  The error or warning
    actions that it sets apply to all embedded SQL statements that
    appear below the point where the handler is set, unless a
    different action was set for the same condition between the first
    <literal>EXEC SQL WHENEVER</literal> and the SQL statement causing
    the condition, regardless of the flow of control in the C program.
    So neither of the two following C program excerpts will have the
    desired effect:
<programlisting>
/*
 * WRONG
 */
int main(int argc, char *argv[])
{
    ...
    if (verbose) {
        EXEC SQL WHENEVER SQLWARNING SQLPRINT;
    }
    ...
    EXEC SQL SELECT ...;
    ...
}
</programlisting>

<programlisting>
/*
 * WRONG
 */
int main(int argc, char *argv[])
{
    ...
    set_error_handler();
    ...
    EXEC SQL SELECT ...;
    ...
}

static void set_error_handler(void)
{
    EXEC SQL WHENEVER SQLERROR STOP;
}
</programlisting>
   </para>
  </sect2>

  <sect2 id="ecpg-sqlca">
   <title>sqlca</title>

   <para>
    For more powerful error handling, the embedded SQL interface
    provides a global variable with the

Title: ecpg WHENEVER Action Options and Usage
Summary
This section details the various actions that can be specified with the WHENEVER command in ecpg, including CONTINUE, GOTO, SQLPRINT, STOP, DO BREAK, DO CONTINUE, and CALL. It emphasizes that the WHENEVER directive is a preprocessor instruction that applies to all subsequent SQL statements, regardless of program flow, until overridden. The section also provides examples of correct and incorrect usage of WHENEVER within C code.