Home Explore Blog CI



postgresql

66th chunk of `doc/src/sgml/ecpg.sgml`
78e0f6c3127be12604f7ce40ee2a622229d5d4addc63b0d10000000100000feb

</programlisting>
    Use <literal>undef</literal> to remove a previous definition:
<programlisting>
EXEC SQL UNDEF MYNUMBER;
</programlisting>
   </para>

   <para>
    Of course you can continue to use the C versions <literal>#define</literal>
    and <literal>#undef</literal> in your embedded SQL program. The difference
    is where your defined values get evaluated. If you use <literal>EXEC SQL
    DEFINE</literal> then the <command>ecpg</command> preprocessor evaluates the defines and substitutes
    the values. For example if you write:
<programlisting>
EXEC SQL DEFINE MYNUMBER 12;
...
EXEC SQL UPDATE Tbl SET col = MYNUMBER;
</programlisting>
    then <command>ecpg</command> will already do the substitution and your C compiler will never
    see any name or identifier <literal>MYNUMBER</literal>. Note that you cannot use
    <literal>#define</literal> for a constant that you are going to use in an
    embedded SQL query because in this case the embedded SQL precompiler is not
    able to see this declaration.
   </para>

   <para>
    If multiple input files are named on the <command>ecpg</command>
    preprocessor's command line, the effects of <literal>EXEC SQL
    DEFINE</literal> and <literal>EXEC SQL UNDEF</literal> do not carry
    across files: each file starts with only the symbols defined
    by <option>-D</option> switches on the command line.
   </para>
  </sect2>

  <sect2 id="ecpg-ifdef">
   <title>ifdef, ifndef, elif, else, and endif Directives</title>
   <para>
   You can use the following directives to compile code sections conditionally:

   <variablelist>
    <varlistentry id="ecpg-ifdef-ifdef">
     <term><literal>EXEC SQL ifdef <replaceable>name</replaceable>;</literal></term>
     <listitem>
     <para>
      Checks a <replaceable>name</replaceable> and processes subsequent lines if
      <replaceable>name</replaceable> has been defined via <literal>EXEC SQL define
      <replaceable>name</replaceable></literal>.
     </para>
     </listitem>
    </varlistentry>

    <varlistentry id="ecpg-ifdef-ifndef">
     <term><literal>EXEC SQL ifndef <replaceable>name</replaceable>;</literal></term>
     <listitem>
     <para>
      Checks a <replaceable>name</replaceable> and processes subsequent lines if
      <replaceable>name</replaceable> has <emphasis>not</emphasis> been defined via
      <literal>EXEC SQL define <replaceable>name</replaceable></literal>.
     </para>
     </listitem>
    </varlistentry>

    <varlistentry id="ecpg-ifdef-elif">
     <term><literal>EXEC SQL elif <replaceable>name</replaceable>;</literal></term>
     <listitem>
     <para>
      Begins an optional alternative section after an
      <literal>EXEC SQL ifdef <replaceable>name</replaceable></literal> or
      <literal>EXEC SQL ifndef <replaceable>name</replaceable></literal>
      directive.  Any number of <literal>elif</literal> sections can appear.
      Lines following an <literal>elif</literal> will be processed
      if <replaceable>name</replaceable> has been
      defined <emphasis>and</emphasis> no previous section of the same
      <literal>ifdef</literal>/<literal>ifndef</literal>...<literal>endif</literal>
      construct has been processed.
     </para>
     </listitem>
    </varlistentry>

    <varlistentry id="ecpg-ifdef-else">
     <term><literal>EXEC SQL else;</literal></term>
     <listitem>
     <para>
      Begins an optional, final alternative section after an
      <literal>EXEC SQL ifdef <replaceable>name</replaceable></literal> or
      <literal>EXEC SQL ifndef <replaceable>name</replaceable></literal>
      directive.  Subsequent lines will be processed if no previous section
      of the same
      <literal>ifdef</literal>/<literal>ifndef</literal>...<literal>endif</literal>
      construct has been processed.
     </para>
     </listitem>
    </varlistentry>

    <varlistentry id="ecpg-ifdef-endif">
     <term><literal>EXEC SQL endif;</literal></term>
     <listitem>
     <para>
      Ends an
      <literal>ifdef</literal>/<literal>ifndef</literal>...<literal>endif</literal>

Title: ECPG Preprocessor Directives: Define/Undefine and Conditional Compilation (ifdef, ifndef, elif, else, endif)
Summary
The document describes the `EXEC SQL DEFINE` and `EXEC SQL UNDEF` directives for defining and undefining variables in embedded SQL programs, contrasting them with C's `#define` and `#undef` in terms of evaluation timing and scope across multiple input files. It then details the conditional compilation directives `EXEC SQL ifdef`, `EXEC SQL ifndef`, `EXEC SQL elif`, `EXEC SQL else`, and `EXEC SQL endif`, explaining how they control which code sections are processed based on whether a name has been defined using `EXEC SQL DEFINE`.