gooderp18绿色标准版
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

228 行
10KB

  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.13. C++ Applications</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-lo.html" title="35.12. Large Objects" /><link rel="next" href="ecpg-sql-commands.html" title="35.14. Embedded SQL Commands" /></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.13. <acronym xmlns="http://www.w3.org/1999/xhtml" class="acronym">C++</acronym> Applications</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="ecpg-lo.html" title="35.12. Large Objects">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-sql-commands.html" title="35.14. Embedded SQL Commands">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="ECPG-CPP"><div class="titlepage"><div><div><h2 class="title" style="clear: both">35.13. <acronym class="acronym">C++</acronym> Applications</h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="ecpg-cpp.html#ECPG-CPP-SCOPE">35.13.1. Scope for Host Variables</a></span></dt><dt><span class="sect2"><a href="ecpg-cpp.html#ECPG-CPP-AND-C">35.13.2. C++ Application Development with External C Module</a></span></dt></dl></div><p>
  3. ECPG has some limited support for C++ applications. This section
  4. describes some caveats.
  5. </p><p>
  6. The <code class="command">ecpg</code> preprocessor takes an input file
  7. written in C (or something like C) and embedded SQL commands,
  8. converts the embedded SQL commands into C language chunks, and
  9. finally generates a <code class="filename">.c</code> file. The header file
  10. declarations of the library functions used by the C language chunks
  11. that <code class="command">ecpg</code> generates are wrapped
  12. in <code class="literal">extern "C" { ... }</code> blocks when used under
  13. C++, so they should work seamlessly in C++.
  14. </p><p>
  15. In general, however, the <code class="command">ecpg</code> preprocessor only
  16. understands C; it does not handle the special syntax and reserved
  17. words of the C++ language. So, some embedded SQL code written in
  18. C++ application code that uses complicated features specific to C++
  19. might fail to be preprocessed correctly or might not work as
  20. expected.
  21. </p><p>
  22. A safe way to use the embedded SQL code in a C++ application is
  23. hiding the ECPG calls in a C module, which the C++ application code
  24. calls into to access the database, and linking that together with
  25. the rest of the C++ code. See <a class="xref" href="ecpg-cpp.html#ECPG-CPP-AND-C" title="35.13.2. C++ Application Development with External C Module">Section 35.13.2</a>
  26. about that.
  27. </p><div class="sect2" id="ECPG-CPP-SCOPE"><div class="titlepage"><div><div><h3 class="title">35.13.1. Scope for Host Variables</h3></div></div></div><p>
  28. The <code class="command">ecpg</code> preprocessor understands the scope of
  29. variables in C. In the C language, this is rather simple because
  30. the scopes of variables is based on their code blocks. In C++,
  31. however, the class member variables are referenced in a different
  32. code block from the declared position, so
  33. the <code class="command">ecpg</code> preprocessor will not understand the
  34. scope of the class member variables.
  35. </p><p>
  36. For example, in the following case, the <code class="command">ecpg</code>
  37. preprocessor cannot find any declaration for the
  38. variable <code class="literal">dbname</code> in the <code class="literal">test</code>
  39. method, so an error will occur.
  40. </p><pre class="programlisting">
  41. class TestCpp
  42. {
  43. EXEC SQL BEGIN DECLARE SECTION;
  44. char dbname[1024];
  45. EXEC SQL END DECLARE SECTION;
  46. public:
  47. TestCpp();
  48. void test();
  49. ~TestCpp();
  50. };
  51. TestCpp::TestCpp()
  52. {
  53. EXEC SQL CONNECT TO testdb1;
  54. EXEC SQL SELECT pg_catalog.set_config('search_path', '', false); EXEC SQL COMMIT;
  55. }
  56. void Test::test()
  57. {
  58. EXEC SQL SELECT current_database() INTO :dbname;
  59. printf("current_database = %s\n", dbname);
  60. }
  61. TestCpp::~TestCpp()
  62. {
  63. EXEC SQL DISCONNECT ALL;
  64. }
  65. </pre><p>
  66. This code will result in an error like this:
  67. </p><pre class="screen">
  68. <strong class="userinput"><code>ecpg test_cpp.pgc</code></strong>
  69. test_cpp.pgc:28: ERROR: variable "dbname" is not declared
  70. </pre><p>
  71. </p><p>
  72. To avoid this scope issue, the <code class="literal">test</code> method
  73. could be modified to use a local variable as intermediate storage.
  74. But this approach is only a poor workaround, because it uglifies
  75. the code and reduces performance.
  76. </p><pre class="programlisting">
  77. void TestCpp::test()
  78. {
  79. EXEC SQL BEGIN DECLARE SECTION;
  80. char tmp[1024];
  81. EXEC SQL END DECLARE SECTION;
  82. EXEC SQL SELECT current_database() INTO :tmp;
  83. strlcpy(dbname, tmp, sizeof(tmp));
  84. printf("current_database = %s\n", dbname);
  85. }
  86. </pre><p>
  87. </p></div><div class="sect2" id="ECPG-CPP-AND-C"><div class="titlepage"><div><div><h3 class="title">35.13.2. C++ Application Development with External C Module</h3></div></div></div><p>
  88. If you understand these technical limitations of
  89. the <code class="command">ecpg</code> preprocessor in C++, you might come to
  90. the conclusion that linking C objects and C++ objects at the link
  91. stage to enable C++ applications to use ECPG features could be
  92. better than writing some embedded SQL commands in C++ code
  93. directly. This section describes a way to separate some embedded
  94. SQL commands from C++ application code with a simple example. In
  95. this example, the application is implemented in C++, while C and
  96. ECPG is used to connect to the PostgreSQL server.
  97. </p><p>
  98. Three kinds of files have to be created: a C file
  99. (<code class="filename">*.pgc</code>), a header file, and a C++ file:
  100. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="filename">test_mod.pgc</code></span></dt><dd><p>
  101. A sub-routine module to execute SQL commands embedded in C.
  102. It is going to be converted
  103. into <code class="filename">test_mod.c</code> by the preprocessor.
  104. </p><pre class="programlisting">
  105. #include "test_mod.h"
  106. #include &lt;stdio.h&gt;
  107. void
  108. db_connect()
  109. {
  110. EXEC SQL CONNECT TO testdb1;
  111. EXEC SQL SELECT pg_catalog.set_config('search_path', '', false); EXEC SQL COMMIT;
  112. }
  113. void
  114. db_test()
  115. {
  116. EXEC SQL BEGIN DECLARE SECTION;
  117. char dbname[1024];
  118. EXEC SQL END DECLARE SECTION;
  119. EXEC SQL SELECT current_database() INTO :dbname;
  120. printf("current_database = %s\n", dbname);
  121. }
  122. void
  123. db_disconnect()
  124. {
  125. EXEC SQL DISCONNECT ALL;
  126. }
  127. </pre><p>
  128. </p></dd><dt><span class="term"><code class="filename">test_mod.h</code></span></dt><dd><p>
  129. A header file with declarations of the functions in the C
  130. module (<code class="filename">test_mod.pgc</code>). It is included by
  131. <code class="filename">test_cpp.cpp</code>. This file has to have an
  132. <code class="literal">extern "C"</code> block around the declarations,
  133. because it will be linked from the C++ module.
  134. </p><pre class="programlisting">
  135. #ifdef __cplusplus
  136. extern "C" {
  137. #endif
  138. void db_connect();
  139. void db_test();
  140. void db_disconnect();
  141. #ifdef __cplusplus
  142. }
  143. #endif
  144. </pre><p>
  145. </p></dd><dt><span class="term"><code class="filename">test_cpp.cpp</code></span></dt><dd><p>
  146. The main code for the application, including
  147. the <code class="function">main</code> routine, and in this example a
  148. C++ class.
  149. </p><pre class="programlisting">
  150. #include "test_mod.h"
  151. class TestCpp
  152. {
  153. public:
  154. TestCpp();
  155. void test();
  156. ~TestCpp();
  157. };
  158. TestCpp::TestCpp()
  159. {
  160. db_connect();
  161. }
  162. void
  163. TestCpp::test()
  164. {
  165. db_test();
  166. }
  167. TestCpp::~TestCpp()
  168. {
  169. db_disconnect();
  170. }
  171. int
  172. main(void)
  173. {
  174. TestCpp *t = new TestCpp();
  175. t-&gt;test();
  176. return 0;
  177. }
  178. </pre><p>
  179. </p></dd></dl></div><p>
  180. </p><p>
  181. To build the application, proceed as follows. Convert
  182. <code class="filename">test_mod.pgc</code> into <code class="filename">test_mod.c</code> by
  183. running <code class="command">ecpg</code>, and generate
  184. <code class="filename">test_mod.o</code> by compiling
  185. <code class="filename">test_mod.c</code> with the C compiler:
  186. </p><pre class="programlisting">
  187. ecpg -o test_mod.c test_mod.pgc
  188. cc -c test_mod.c -o test_mod.o
  189. </pre><p>
  190. </p><p>
  191. Next, generate <code class="filename">test_cpp.o</code> by compiling
  192. <code class="filename">test_cpp.cpp</code> with the C++ compiler:
  193. </p><pre class="programlisting">
  194. c++ -c test_cpp.cpp -o test_cpp.o
  195. </pre><p>
  196. </p><p>
  197. Finally, link these object files, <code class="filename">test_cpp.o</code>
  198. and <code class="filename">test_mod.o</code>, into one executable, using the C++
  199. compiler driver:
  200. </p><pre class="programlisting">
  201. c++ test_cpp.o test_mod.o -lecpg -o test_cpp
  202. </pre><p>
  203. </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ecpg-lo.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-sql-commands.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">35.12. Large Objects </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 35.14. Embedded SQL Commands</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1