gooderp18绿色标准版
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

113 lignes
9.5KB

  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>35.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 V1.79.1" /><link rel="prev" href="ecpg-errors.html" title="35.8. Error Handling" /><link rel="next" href="ecpg-process.html" title="35.10. Processing Embedded SQL Programs" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">35.9. Preprocessor Directives</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="ecpg-errors.html" title="35.8. Error Handling">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="ecpg.html" title="Chapter 35. ECPG - Embedded SQL in C">Up</a></td><th width="60%" align="center">Chapter 35. <span xmlns="http://www.w3.org/1999/xhtml" class="application">ECPG</span> - Embedded <acronym xmlns="http://www.w3.org/1999/xhtml" class="acronym">SQL</acronym> in C</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="ecpg-process.html" title="35.10. Processing Embedded SQL Programs">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="ECPG-PREPROC"><div class="titlepage"><div><div><h2 class="title" style="clear: both">35.9. Preprocessor Directives</h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="ecpg-preproc.html#ECPG-INCLUDE">35.9.1. Including Files</a></span></dt><dt><span class="sect2"><a href="ecpg-preproc.html#ECPG-DEFINE">35.9.2. The define and undef Directives</a></span></dt><dt><span class="sect2"><a href="ecpg-preproc.html#ECPG-IFDEF">35.9.3. ifdef, ifndef, else, elif, and endif Directives</a></span></dt></dl></div><p>
  3. Several preprocessor directives are available that modify how
  4. the <code class="command">ecpg</code> preprocessor parses and processes a
  5. file.
  6. </p><div class="sect2" id="ECPG-INCLUDE"><div class="titlepage"><div><div><h3 class="title">35.9.1. Including Files</h3></div></div></div><p>
  7. To include an external file into your embedded SQL program, use:
  8. </p><pre class="programlisting">
  9. EXEC SQL INCLUDE <em class="replaceable"><code>filename</code></em>;
  10. EXEC SQL INCLUDE &lt;<em class="replaceable"><code>filename</code></em>&gt;;
  11. EXEC SQL INCLUDE "<em class="replaceable"><code>filename</code></em>";
  12. </pre><p>
  13. The embedded SQL preprocessor will look for a file named
  14. <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 at
  19. several directories in following order:
  20. </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>
  21. But when <code class="literal">EXEC SQL INCLUDE
  22. "<em class="replaceable"><code>filename</code></em>"</code> is used, only the
  23. current directory is searched.
  24. </p><p>
  25. In each directory, the preprocessor will first look for the file
  26. name as given, and if not found will append <code class="literal">.h</code>
  27. to the file name and try again (unless the specified file name
  28. already has that suffix).
  29. </p><p>
  30. Note that <code class="command">EXEC SQL INCLUDE</code> is <span class="emphasis"><em>not</em></span> the same as:
  31. </p><pre class="programlisting">
  32. #include &lt;<em class="replaceable"><code>filename</code></em>.h&gt;
  33. </pre><p>
  34. because this file would not be subject to SQL command preprocessing.
  35. Naturally, you can continue to use the C
  36. <code class="literal">#include</code> directive to include other header
  37. files.
  38. </p><div class="note"><h3 class="title">Note</h3><p>
  39. The include file name is case-sensitive, even though the rest of
  40. the <code class="literal">EXEC SQL INCLUDE</code> command follows the normal
  41. SQL case-sensitivity rules.
  42. </p></div></div><div class="sect2" id="ECPG-DEFINE"><div class="titlepage"><div><div><h3 class="title">35.9.2. The define and undef Directives</h3></div></div></div><p>
  43. Similar to the directive <code class="literal">#define</code> that is known from C,
  44. embedded SQL has a similar concept:
  45. </p><pre class="programlisting">
  46. EXEC SQL DEFINE <em class="replaceable"><code>name</code></em>;
  47. EXEC SQL DEFINE <em class="replaceable"><code>name</code></em> <em class="replaceable"><code>value</code></em>;
  48. </pre><p>
  49. So you can define a name:
  50. </p><pre class="programlisting">
  51. EXEC SQL DEFINE HAVE_FEATURE;
  52. </pre><p>
  53. And you can also define constants:
  54. </p><pre class="programlisting">
  55. EXEC SQL DEFINE MYNUMBER 12;
  56. EXEC SQL DEFINE MYSTRING 'abc';
  57. </pre><p>
  58. Use <code class="literal">undef</code> to remove a previous definition:
  59. </p><pre class="programlisting">
  60. EXEC SQL UNDEF MYNUMBER;
  61. </pre><p>
  62. </p><p>
  63. Of course you can continue to use the C versions <code class="literal">#define</code>
  64. and <code class="literal">#undef</code> in your embedded SQL program. The difference
  65. is where your defined values get evaluated. If you use <code class="literal">EXEC SQL
  66. DEFINE</code> then the <code class="command">ecpg</code> preprocessor evaluates the defines and substitutes
  67. the values. For example if you write:
  68. </p><pre class="programlisting">
  69. EXEC SQL DEFINE MYNUMBER 12;
  70. ...
  71. EXEC SQL UPDATE Tbl SET col = MYNUMBER;
  72. </pre><p>
  73. then <code class="command">ecpg</code> will already do the substitution and your C compiler will never
  74. see any name or identifier <code class="literal">MYNUMBER</code>. Note that you cannot use
  75. <code class="literal">#define</code> for a constant that you are going to use in an
  76. embedded SQL query because in this case the embedded SQL precompiler is not
  77. able to see this declaration.
  78. </p></div><div class="sect2" id="ECPG-IFDEF"><div class="titlepage"><div><div><h3 class="title">35.9.3. ifdef, ifndef, else, elif, and endif Directives</h3></div></div></div><p>
  79. You can use the following directives to compile code sections conditionally:
  80. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">EXEC SQL ifdef <em class="replaceable"><code>name</code></em>;</code></span></dt><dd><p>
  81. Checks a <em class="replaceable"><code>name</code></em> and processes subsequent lines if
  82. <em class="replaceable"><code>name</code></em> has been created with <code class="literal">EXEC SQL define
  83. <em class="replaceable"><code>name</code></em></code>.
  84. </p></dd><dt><span class="term"><code class="literal">EXEC SQL ifndef <em class="replaceable"><code>name</code></em>;</code></span></dt><dd><p>
  85. Checks a <em class="replaceable"><code>name</code></em> and processes subsequent lines if
  86. <em class="replaceable"><code>name</code></em> has <span class="emphasis"><em>not</em></span> been created with
  87. <code class="literal">EXEC SQL define <em class="replaceable"><code>name</code></em></code>.
  88. </p></dd><dt><span class="term"><code class="literal">EXEC SQL else;</code></span></dt><dd><p>
  89. Starts processing an alternative section to a section introduced by
  90. either <code class="literal">EXEC SQL ifdef <em class="replaceable"><code>name</code></em></code> or
  91. <code class="literal">EXEC SQL ifndef <em class="replaceable"><code>name</code></em></code>.
  92. </p></dd><dt><span class="term"><code class="literal">EXEC SQL elif <em class="replaceable"><code>name</code></em>;</code></span></dt><dd><p>
  93. Checks <em class="replaceable"><code>name</code></em> and starts an alternative section if
  94. <em class="replaceable"><code>name</code></em> has been created with <code class="literal">EXEC SQL define
  95. <em class="replaceable"><code>name</code></em></code>.
  96. </p></dd><dt><span class="term"><code class="literal">EXEC SQL endif;</code></span></dt><dd><p>
  97. Ends an alternative section.
  98. </p></dd></dl></div><p>
  99. </p><p>
  100. Example:
  101. </p><pre class="programlisting">
  102. EXEC SQL ifndef TZVAR;
  103. EXEC SQL SET TIMEZONE TO 'GMT';
  104. EXEC SQL elif TZNAME;
  105. EXEC SQL SET TIMEZONE TO TZNAME;
  106. EXEC SQL else;
  107. EXEC SQL SET TIMEZONE TO TZVAR;
  108. EXEC SQL endif;
  109. </pre><p>
  110. </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">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ecpg.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ecpg-process.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">35.8. Error Handling </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 35.10. Processing Embedded SQL Programs</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1