Home Explore Blog CI



postgresql

67th chunk of `doc/src/sgml/ecpg.sgml`
ba266d5fae58e0dfa878827997902d8b8d2d41cae987fbfd0000000100000fa0
 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>
      construct.  Subsequent lines are processed normally.
     </para>
     </listitem>
    </varlistentry>
   </variablelist>
   </para>

   <para>
    <literal>ifdef</literal>/<literal>ifndef</literal>...<literal>endif</literal>
    constructs can be nested, up to 127 levels deep.
   </para>

   <para>
    This example will compile exactly one of the three <literal>SET
    TIMEZONE</literal> commands:
<programlisting>
EXEC SQL ifdef TZVAR;
EXEC SQL SET TIMEZONE TO TZVAR;
EXEC SQL elif TZNAME;
EXEC SQL SET TIMEZONE TO TZNAME;
EXEC SQL else;
EXEC SQL SET TIMEZONE TO 'GMT';
EXEC SQL endif;
</programlisting>
   </para>

  </sect2>
 </sect1>

  <sect1 id="ecpg-process">
  <title>Processing Embedded SQL Programs</title>

  <para>
   Now that you have an idea how to form embedded SQL C programs, you
   probably want to know how to compile them.  Before compiling you
   run the file through the embedded <acronym>SQL</acronym>
   <acronym>C</acronym> preprocessor, which converts the
   <acronym>SQL</acronym> statements you used to special function
   calls.  After compiling, you must link with a special library that
   contains the needed functions. These functions fetch information
   from the arguments, perform the <acronym>SQL</acronym> command using
   the <application>libpq</application> interface, and put the result
   in the arguments specified for output.
  </para>

  <para>
   The preprocessor program is called <filename>ecpg</filename> and is
   included in a normal <productname>PostgreSQL</productname> installation.
   Embedded SQL programs are typically named with an extension
   <filename>.pgc</filename>.  If you have a program file called
   <filename>prog1.pgc</filename>, you can preprocess it by simply
   calling:
<programlisting>
ecpg prog1.pgc
</programlisting>
   This will create a file called <filename>prog1.c</filename>.  If
   your input files do not follow the suggested naming pattern, you
   can specify the output file explicitly using the
   <option>-o</option> option.
  </para>

  <para>
   The preprocessed file can be compiled normally, for example:
<programlisting>
cc -c prog1.c
</programlisting>
   The generated C source files include header files from the
   <productname>PostgreSQL</productname> installation, so if you installed
   <productname>PostgreSQL</productname> in a location that is not searched by
   default, you have to add an option such as
   <literal>-I/usr/local/pgsql/include</literal> to the compilation
   command line.
  </para>

  <para>
   To link an embedded SQL program, you need to include the
   <filename>libecpg</filename> library, like so:
<programlisting>
cc -o myprog prog1.o prog2.o ... -lecpg
</programlisting>
   Again, you might have to add an option like
   <literal>-L/usr/local/pgsql/lib</literal> to that command line.
  </para>

  <para>
   You can
   use <command>pg_config</command><indexterm><primary>pg_config</primary><secondary sortas="ecpg">with
   ecpg</secondary></indexterm>
   or

Title: ECPG Conditional Compilation Directives (Continued) and Processing Embedded SQL Programs
Summary
This section concludes the explanation of conditional compilation directives (`ifdef`, `ifndef`, `elif`, `else`, `endif`) in ECPG, noting their nesting capability. It then transitions to processing embedded SQL programs, detailing the use of the `ecpg` preprocessor to convert SQL statements into C function calls. The process involves preprocessing the `.pgc` file to create a `.c` file, compiling the generated C file with necessary include paths, and linking with the `libecpg` library, potentially requiring library path specification.