codekingpro/portable-devtools
115k
1<?xml version="1.0" encoding="UTF-8" standalone="no"?>2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>36.9. Preprocessor Directives</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="ecpg-errors.html" title="36.8. Error Handling" /><link rel="next" href="ecpg-process.html" title="36.10. Processing Embedded SQL Programs" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">36.9. Preprocessor Directives</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="ecpg-errors.html" title="36.8. Error Handling">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="ecpg.html" title="Chapter 36. ECPG — Embedded SQL in C">Up</a></td><th width="60%" align="center">Chapter 36. <span class="application">ECPG</span> — Embedded <acronym class="acronym">SQL</acronym> in C</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 16.3 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="ecpg-process.html" title="36.10. Processing Embedded SQL Programs">Next</a></td></tr></table><hr /></div><div class="sect1" id="ECPG-PREPROC"><div class="titlepage"><div><div><h2 class="title" style="clear: both">36.9. Preprocessor Directives <a href="#ECPG-PREPROC" class="id_link">#</a></h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="ecpg-preproc.html#ECPG-INCLUDE">36.9.1. Including Files</a></span></dt><dt><span class="sect2"><a href="ecpg-preproc.html#ECPG-DEFINE">36.9.2. The define and undef Directives</a></span></dt><dt><span class="sect2"><a href="ecpg-preproc.html#ECPG-IFDEF">36.9.3. ifdef, ifndef, elif, else, and endif Directives</a></span></dt></dl></div><p>3 Several preprocessor directives are available that modify how4 the <code class="command">ecpg</code> preprocessor parses and processes a5 file.6 </p><div class="sect2" id="ECPG-INCLUDE"><div class="titlepage"><div><div><h3 class="title">36.9.1. Including Files <a href="#ECPG-INCLUDE" class="id_link">#</a></h3></div></div></div><p>7 To include an external file into your embedded SQL program, use:8</p><pre class="programlisting">9EXEC SQL INCLUDE <em class="replaceable"><code>filename</code></em>;10EXEC SQL INCLUDE <<em class="replaceable"><code>filename</code></em>>;11EXEC SQL INCLUDE "<em class="replaceable"><code>filename</code></em>";12</pre><p>13 The embedded SQL preprocessor will look for a file named14 <code class="literal"><em class="replaceable"><code>filename</code></em>.h</code>,15 preprocess it, and include it in the resulting C output. Thus,16 embedded SQL statements in the included file are handled correctly.17 </p><p>18 The <code class="command">ecpg</code> preprocessor will search a file at19 several directories in following order:20 21 </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">current directory</li><li class="listitem"><code class="filename">/usr/local/include</code></li><li class="listitem">PostgreSQL include directory, defined at build time (e.g., <code class="filename">/usr/local/pgsql/include</code>)</li><li class="listitem"><code class="filename">/usr/include</code></li></ul></div><p>22 23 But when <code class="literal">EXEC SQL INCLUDE24 "<em class="replaceable"><code>filename</code></em>"</code> is used, only the25 current directory is searched.26 </p><p>27 In each directory, the preprocessor will first look for the file28 name as given, and if not found will append <code class="literal">.h</code>29 to the file name and try again (unless the specified file name30 already has that suffix).31 </p><p>32 Note that <code class="command">EXEC SQL INCLUDE</code> is <span class="emphasis"><em>not</em></span> the same as:33</p><pre class="programlisting">34#include <<em class="replaceable"><code>filename</code></em>.h>35</pre><p>36 because this file would not be subject to SQL command preprocessing.37 Naturally, you can continue to use the C38 <code class="literal">#include</code> directive to include other header39 files.40 </p><div class="note"><h3 class="title">Note</h3><p>41 The include file name is case-sensitive, even though the rest of42 the <code class="literal">EXEC SQL INCLUDE</code> command follows the normal43 SQL case-sensitivity rules.44 </p></div></div><div class="sect2" id="ECPG-DEFINE"><div class="titlepage"><div><div><h3 class="title">36.9.2. The define and undef Directives <a href="#ECPG-DEFINE" class="id_link">#</a></h3></div></div></div><p>45 Similar to the directive <code class="literal">#define</code> that is known from C,46 embedded SQL has a similar concept:47</p><pre class="programlisting">48EXEC SQL DEFINE <em class="replaceable"><code>name</code></em>;49EXEC SQL DEFINE <em class="replaceable"><code>name</code></em> <em class="replaceable"><code>value</code></em>;50</pre><p>51 So you can define a name:52</p><pre class="programlisting">53EXEC SQL DEFINE HAVE_FEATURE;54</pre><p>55 And you can also define constants:56</p><pre class="programlisting">57EXEC SQL DEFINE MYNUMBER 12;58EXEC SQL DEFINE MYSTRING 'abc';59</pre><p>60 Use <code class="literal">undef</code> to remove a previous definition:61</p><pre class="programlisting">62EXEC SQL UNDEF MYNUMBER;63</pre><p>64 </p><p>65 Of course you can continue to use the C versions <code class="literal">#define</code>66 and <code class="literal">#undef</code> in your embedded SQL program. The difference67 is where your defined values get evaluated. If you use <code class="literal">EXEC SQL68 DEFINE</code> then the <code class="command">ecpg</code> preprocessor evaluates the defines and substitutes69 the values. For example if you write:70</p><pre class="programlisting">71EXEC SQL DEFINE MYNUMBER 12;72...73EXEC SQL UPDATE Tbl SET col = MYNUMBER;74</pre><p>75 then <code class="command">ecpg</code> will already do the substitution and your C compiler will never76 see any name or identifier <code class="literal">MYNUMBER</code>. Note that you cannot use77 <code class="literal">#define</code> for a constant that you are going to use in an78 embedded SQL query because in this case the embedded SQL precompiler is not79 able to see this declaration.80 </p><p>81 If multiple input files are named on the <code class="command">ecpg</code>82 preprocessor's command line, the effects of <code class="literal">EXEC SQL83 DEFINE</code> and <code class="literal">EXEC SQL UNDEF</code> do not carry84 across files: each file starts with only the symbols defined85 by <code class="option">-D</code> switches on the command line.86 </p></div><div class="sect2" id="ECPG-IFDEF"><div class="titlepage"><div><div><h3 class="title">36.9.3. ifdef, ifndef, elif, else, and endif Directives <a href="#ECPG-IFDEF" class="id_link">#</a></h3></div></div></div><p>87 You can use the following directives to compile code sections conditionally:88 89 </p><div class="variablelist"><dl class="variablelist"><dt id="ECPG-IFDEF-IFDEF"><span class="term"><code class="literal">EXEC SQL ifdef <em class="replaceable"><code>name</code></em>;</code></span> <a href="#ECPG-IFDEF-IFDEF" class="id_link">#</a></dt><dd><p>90 Checks a <em class="replaceable"><code>name</code></em> and processes subsequent lines if91 <em class="replaceable"><code>name</code></em> has been defined via <code class="literal">EXEC SQL define92 <em class="replaceable"><code>name</code></em></code>.93 </p></dd><dt id="ECPG-IFDEF-IFNDEF"><span class="term"><code class="literal">EXEC SQL ifndef <em class="replaceable"><code>name</code></em>;</code></span> <a href="#ECPG-IFDEF-IFNDEF" class="id_link">#</a></dt><dd><p>94 Checks a <em class="replaceable"><code>name</code></em> and processes subsequent lines if95 <em class="replaceable"><code>name</code></em> has <span class="emphasis"><em>not</em></span> been defined via96 <code class="literal">EXEC SQL define <em class="replaceable"><code>name</code></em></code>.97 </p></dd><dt id="ECPG-IFDEF-ELIF"><span class="term"><code class="literal">EXEC SQL elif <em class="replaceable"><code>name</code></em>;</code></span> <a href="#ECPG-IFDEF-ELIF" class="id_link">#</a></dt><dd><p>98 Begins an optional alternative section after an99 <code class="literal">EXEC SQL ifdef <em class="replaceable"><code>name</code></em></code> or100 <code class="literal">EXEC SQL ifndef <em class="replaceable"><code>name</code></em></code>101 directive. Any number of <code class="literal">elif</code> sections can appear.102 Lines following an <code class="literal">elif</code> will be processed103 if <em class="replaceable"><code>name</code></em> has been104 defined <span class="emphasis"><em>and</em></span> no previous section of the same105 <code class="literal">ifdef</code>/<code class="literal">ifndef</code>...<code class="literal">endif</code>106 construct has been processed.107 </p></dd><dt id="ECPG-IFDEF-ELSE"><span class="term"><code class="literal">EXEC SQL else;</code></span> <a href="#ECPG-IFDEF-ELSE" class="id_link">#</a></dt><dd><p>108 Begins an optional, final alternative section after an109 <code class="literal">EXEC SQL ifdef <em class="replaceable"><code>name</code></em></code> or110 <code class="literal">EXEC SQL ifndef <em class="replaceable"><code>name</code></em></code>111 directive. Subsequent lines will be processed if no previous section112 of the same113 <code class="literal">ifdef</code>/<code class="literal">ifndef</code>...<code class="literal">endif</code>114 construct has been processed.115 </p></dd><dt id="ECPG-IFDEF-ENDIF"><span class="term"><code class="literal">EXEC SQL endif;</code></span> <a href="#ECPG-IFDEF-ENDIF" class="id_link">#</a></dt><dd><p>116 Ends an117 <code class="literal">ifdef</code>/<code class="literal">ifndef</code>...<code class="literal">endif</code>118 construct. Subsequent lines are processed normally.119 </p></dd></dl></div><p>120 </p><p>121 <code class="literal">ifdef</code>/<code class="literal">ifndef</code>...<code class="literal">endif</code>122 constructs can be nested, up to 127 levels deep.123 </p><p>124 This example will compile exactly one of the three <code class="literal">SET125 TIMEZONE</code> commands:126</p><pre class="programlisting">127EXEC SQL ifdef TZVAR;128EXEC SQL SET TIMEZONE TO TZVAR;129EXEC SQL elif TZNAME;130EXEC SQL SET TIMEZONE TO TZNAME;131EXEC SQL else;132EXEC SQL SET TIMEZONE TO 'GMT';133EXEC SQL endif;134</pre><p>135 </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ecpg-errors.html" title="36.8. Error Handling">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ecpg.html" title="Chapter 36. ECPG — Embedded SQL in C">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ecpg-process.html" title="36.10. Processing Embedded SQL Programs">Next</a></td></tr><tr><td width="40%" align="left" valign="top">36.8. Error Handling </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 16.3 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 36.10. Processing Embedded SQL Programs</td></tr></table></div></body></html>