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.

336 lignes
24KB

  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>33.9. Functions Associated with the COPY Command</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="libpq-notify.html" title="33.8. Asynchronous Notification" /><link rel="next" href="libpq-control.html" title="33.10. Control Functions" /></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">33.9. Functions Associated with the <code xmlns="http://www.w3.org/1999/xhtml" class="command">COPY</code> Command</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="libpq-notify.html" title="33.8. Asynchronous Notification">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="libpq.html" title="Chapter 33. libpq - C Library">Up</a></td><th width="60%" align="center">Chapter 33. <span xmlns="http://www.w3.org/1999/xhtml" class="application">libpq</span> - C Library</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="libpq-control.html" title="33.10. Control Functions">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="LIBPQ-COPY"><div class="titlepage"><div><div><h2 class="title" style="clear: both">33.9. Functions Associated with the <code class="command">COPY</code> Command</h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="libpq-copy.html#LIBPQ-COPY-SEND">33.9.1. Functions for Sending <code class="command">COPY</code> Data</a></span></dt><dt><span class="sect2"><a href="libpq-copy.html#LIBPQ-COPY-RECEIVE">33.9.2. Functions for Receiving <code class="command">COPY</code> Data</a></span></dt><dt><span class="sect2"><a href="libpq-copy.html#LIBPQ-COPY-DEPRECATED">33.9.3. Obsolete Functions for <code class="command">COPY</code></a></span></dt></dl></div><a id="id-1.7.3.16.2" class="indexterm"></a><p>
  3. The <code class="command">COPY</code> command in
  4. <span class="productname">PostgreSQL</span> has options to read from or write
  5. to the network connection used by <span class="application">libpq</span>.
  6. The functions described in this section allow applications to take
  7. advantage of this capability by supplying or consuming copied data.
  8. </p><p>
  9. The overall process is that the application first issues the SQL
  10. <code class="command">COPY</code> command via <code class="function">PQexec</code> or one
  11. of the equivalent functions. The response to this (if there is no
  12. error in the command) will be a <code class="structname">PGresult</code> object bearing
  13. a status code of <code class="literal">PGRES_COPY_OUT</code> or
  14. <code class="literal">PGRES_COPY_IN</code> (depending on the specified copy
  15. direction). The application should then use the functions of this
  16. section to receive or transmit data rows. When the data transfer is
  17. complete, another <code class="structname">PGresult</code> object is returned to indicate
  18. success or failure of the transfer. Its status will be
  19. <code class="literal">PGRES_COMMAND_OK</code> for success or
  20. <code class="literal">PGRES_FATAL_ERROR</code> if some problem was encountered.
  21. At this point further SQL commands can be issued via
  22. <code class="function">PQexec</code>. (It is not possible to execute other SQL
  23. commands using the same connection while the <code class="command">COPY</code>
  24. operation is in progress.)
  25. </p><p>
  26. If a <code class="command">COPY</code> command is issued via
  27. <code class="function">PQexec</code> in a string that could contain additional
  28. commands, the application must continue fetching results via
  29. <code class="function">PQgetResult</code> after completing the <code class="command">COPY</code>
  30. sequence. Only when <code class="function">PQgetResult</code> returns
  31. <code class="symbol">NULL</code> is it certain that the <code class="function">PQexec</code>
  32. command string is done and it is safe to issue more commands.
  33. </p><p>
  34. The functions of this section should be executed only after obtaining
  35. a result status of <code class="literal">PGRES_COPY_OUT</code> or
  36. <code class="literal">PGRES_COPY_IN</code> from <code class="function">PQexec</code> or
  37. <code class="function">PQgetResult</code>.
  38. </p><p>
  39. A <code class="structname">PGresult</code> object bearing one of these status values
  40. carries some additional data about the <code class="command">COPY</code> operation
  41. that is starting. This additional data is available using functions
  42. that are also used in connection with query results:
  43. </p><div class="variablelist"><dl class="variablelist"><dt id="LIBPQ-PQNFIELDS-1"><span class="term">
  44. <code class="function">PQnfields</code>
  45. <a id="id-1.7.3.16.7.3.1.1.2" class="indexterm"></a>
  46. </span></dt><dd><p>
  47. Returns the number of columns (fields) to be copied.
  48. </p></dd><dt id="LIBPQ-PQBINARYTUPLES-1"><span class="term">
  49. <code class="function">PQbinaryTuples</code>
  50. <a id="id-1.7.3.16.7.3.2.1.2" class="indexterm"></a>
  51. </span></dt><dd><p>
  52. 0 indicates the overall copy format is textual (rows separated by
  53. newlines, columns separated by separator characters, etc). 1
  54. indicates the overall copy format is binary. See <a class="xref" href="sql-copy.html" title="COPY"><span class="refentrytitle">COPY</span></a> for more information.
  55. </p></dd><dt id="LIBPQ-PQFFORMAT-1"><span class="term">
  56. <code class="function">PQfformat</code>
  57. <a id="id-1.7.3.16.7.3.3.1.2" class="indexterm"></a>
  58. </span></dt><dd><p>
  59. Returns the format code (0 for text, 1 for binary) associated with
  60. each column of the copy operation. The per-column format codes
  61. will always be zero when the overall copy format is textual, but
  62. the binary format can support both text and binary columns.
  63. (However, as of the current implementation of <code class="command">COPY</code>,
  64. only binary columns appear in a binary copy; so the per-column
  65. formats always match the overall format at present.)
  66. </p></dd></dl></div><p>
  67. </p><div class="note"><h3 class="title">Note</h3><p>
  68. These additional data values are only available when using protocol
  69. 3.0. When using protocol 2.0, all these functions will return 0.
  70. </p></div><div class="sect2" id="LIBPQ-COPY-SEND"><div class="titlepage"><div><div><h3 class="title">33.9.1. Functions for Sending <code class="command">COPY</code> Data</h3></div></div></div><p>
  71. These functions are used to send data during <code class="literal">COPY FROM
  72. STDIN</code>. They will fail if called when the connection is not in
  73. <code class="literal">COPY_IN</code> state.
  74. </p><div class="variablelist"><dl class="variablelist"><dt id="LIBPQ-PQPUTCOPYDATA"><span class="term">
  75. <code class="function">PQputCopyData</code>
  76. <a id="id-1.7.3.16.9.3.1.1.2" class="indexterm"></a>
  77. </span></dt><dd><p>
  78. Sends data to the server during <code class="literal">COPY_IN</code> state.
  79. </p><pre class="synopsis">
  80. int PQputCopyData(PGconn *conn,
  81. const char *buffer,
  82. int nbytes);
  83. </pre><p>
  84. </p><p>
  85. Transmits the <code class="command">COPY</code> data in the specified
  86. <em class="parameter"><code>buffer</code></em>, of length <em class="parameter"><code>nbytes</code></em>, to the server.
  87. The result is 1 if the data was queued, zero if it was not queued
  88. because of full buffers (this will only happen in nonblocking mode),
  89. or -1 if an error occurred.
  90. (Use <code class="function">PQerrorMessage</code> to retrieve details if
  91. the return value is -1. If the value is zero, wait for write-ready
  92. and try again.)
  93. </p><p>
  94. The application can divide the <code class="command">COPY</code> data stream
  95. into buffer loads of any convenient size. Buffer-load boundaries
  96. have no semantic significance when sending. The contents of the
  97. data stream must match the data format expected by the
  98. <code class="command">COPY</code> command; see <a class="xref" href="sql-copy.html" title="COPY"><span class="refentrytitle">COPY</span></a> for details.
  99. </p></dd><dt id="LIBPQ-PQPUTCOPYEND"><span class="term">
  100. <code class="function">PQputCopyEnd</code>
  101. <a id="id-1.7.3.16.9.3.2.1.2" class="indexterm"></a>
  102. </span></dt><dd><p>
  103. Sends end-of-data indication to the server during <code class="literal">COPY_IN</code> state.
  104. </p><pre class="synopsis">
  105. int PQputCopyEnd(PGconn *conn,
  106. const char *errormsg);
  107. </pre><p>
  108. </p><p>
  109. Ends the <code class="literal">COPY_IN</code> operation successfully if
  110. <em class="parameter"><code>errormsg</code></em> is <code class="symbol">NULL</code>. If
  111. <em class="parameter"><code>errormsg</code></em> is not <code class="symbol">NULL</code> then the
  112. <code class="command">COPY</code> is forced to fail, with the string pointed to by
  113. <em class="parameter"><code>errormsg</code></em> used as the error message. (One should not
  114. assume that this exact error message will come back from the server,
  115. however, as the server might have already failed the
  116. <code class="command">COPY</code> for its own reasons. Also note that the option
  117. to force failure does not work when using pre-3.0-protocol
  118. connections.)
  119. </p><p>
  120. The result is 1 if the termination message was sent; or in
  121. nonblocking mode, this may only indicate that the termination
  122. message was successfully queued. (In nonblocking mode, to be
  123. certain that the data has been sent, you should next wait for
  124. write-ready and call <code class="function">PQflush</code>, repeating until it
  125. returns zero.) Zero indicates that the function could not queue
  126. the termination message because of full buffers; this will only
  127. happen in nonblocking mode. (In this case, wait for
  128. write-ready and try the <code class="function">PQputCopyEnd</code> call
  129. again.) If a hard error occurs, -1 is returned; you can use
  130. <code class="function">PQerrorMessage</code> to retrieve details.
  131. </p><p>
  132. After successfully calling <code class="function">PQputCopyEnd</code>, call
  133. <code class="function">PQgetResult</code> to obtain the final result status of the
  134. <code class="command">COPY</code> command. One can wait for this result to be
  135. available in the usual way. Then return to normal operation.
  136. </p></dd></dl></div></div><div class="sect2" id="LIBPQ-COPY-RECEIVE"><div class="titlepage"><div><div><h3 class="title">33.9.2. Functions for Receiving <code class="command">COPY</code> Data</h3></div></div></div><p>
  137. These functions are used to receive data during <code class="literal">COPY TO
  138. STDOUT</code>. They will fail if called when the connection is not in
  139. <code class="literal">COPY_OUT</code> state.
  140. </p><div class="variablelist"><dl class="variablelist"><dt id="LIBPQ-PQGETCOPYDATA"><span class="term">
  141. <code class="function">PQgetCopyData</code>
  142. <a id="id-1.7.3.16.10.3.1.1.2" class="indexterm"></a>
  143. </span></dt><dd><p>
  144. Receives data from the server during <code class="literal">COPY_OUT</code> state.
  145. </p><pre class="synopsis">
  146. int PQgetCopyData(PGconn *conn,
  147. char **buffer,
  148. int async);
  149. </pre><p>
  150. </p><p>
  151. Attempts to obtain another row of data from the server during a
  152. <code class="command">COPY</code>. Data is always returned one data row at
  153. a time; if only a partial row is available, it is not returned.
  154. Successful return of a data row involves allocating a chunk of
  155. memory to hold the data. The <em class="parameter"><code>buffer</code></em> parameter must
  156. be non-<code class="symbol">NULL</code>. <em class="parameter"><code>*buffer</code></em> is set to
  157. point to the allocated memory, or to <code class="symbol">NULL</code> in cases
  158. where no buffer is returned. A non-<code class="symbol">NULL</code> result
  159. buffer should be freed using <code class="function">PQfreemem</code> when no longer
  160. needed.
  161. </p><p>
  162. When a row is successfully returned, the return value is the number
  163. of data bytes in the row (this will always be greater than zero).
  164. The returned string is always null-terminated, though this is
  165. probably only useful for textual <code class="command">COPY</code>. A result
  166. of zero indicates that the <code class="command">COPY</code> is still in
  167. progress, but no row is yet available (this is only possible when
  168. <em class="parameter"><code>async</code></em> is true). A result of -1 indicates that the
  169. <code class="command">COPY</code> is done. A result of -2 indicates that an
  170. error occurred (consult <code class="function">PQerrorMessage</code> for the reason).
  171. </p><p>
  172. When <em class="parameter"><code>async</code></em> is true (not zero),
  173. <code class="function">PQgetCopyData</code> will not block waiting for input; it
  174. will return zero if the <code class="command">COPY</code> is still in progress
  175. but no complete row is available. (In this case wait for read-ready
  176. and then call <code class="function">PQconsumeInput</code> before calling
  177. <code class="function">PQgetCopyData</code> again.) When <em class="parameter"><code>async</code></em> is
  178. false (zero), <code class="function">PQgetCopyData</code> will block until data is
  179. available or the operation completes.
  180. </p><p>
  181. After <code class="function">PQgetCopyData</code> returns -1, call
  182. <code class="function">PQgetResult</code> to obtain the final result status of the
  183. <code class="command">COPY</code> command. One can wait for this result to be
  184. available in the usual way. Then return to normal operation.
  185. </p></dd></dl></div></div><div class="sect2" id="LIBPQ-COPY-DEPRECATED"><div class="titlepage"><div><div><h3 class="title">33.9.3. Obsolete Functions for <code class="command">COPY</code></h3></div></div></div><p>
  186. These functions represent older methods of handling <code class="command">COPY</code>.
  187. Although they still work, they are deprecated due to poor error handling,
  188. inconvenient methods of detecting end-of-data, and lack of support for binary
  189. or nonblocking transfers.
  190. </p><div class="variablelist"><dl class="variablelist"><dt id="LIBPQ-PQGETLINE"><span class="term">
  191. <code class="function">PQgetline</code>
  192. <a id="id-1.7.3.16.11.3.1.1.2" class="indexterm"></a>
  193. </span></dt><dd><p>
  194. Reads a newline-terminated line of characters (transmitted
  195. by the server) into a buffer string of size <em class="parameter"><code>length</code></em>.
  196. </p><pre class="synopsis">
  197. int PQgetline(PGconn *conn,
  198. char *buffer,
  199. int length);
  200. </pre><p>
  201. </p><p>
  202. This function copies up to <em class="parameter"><code>length</code></em>-1 characters into
  203. the buffer and converts the terminating newline into a zero byte.
  204. <code class="function">PQgetline</code> returns <code class="symbol">EOF</code> at the
  205. end of input, 0 if the entire line has been read, and 1 if the
  206. buffer is full but the terminating newline has not yet been read.
  207. </p><p>
  208. Note that the application must check to see if a new line consists
  209. of the two characters <code class="literal">\.</code>, which indicates
  210. that the server has finished sending the results of the
  211. <code class="command">COPY</code> command. If the application might receive
  212. lines that are more than <em class="parameter"><code>length</code></em>-1 characters long,
  213. care is needed to be sure it recognizes the <code class="literal">\.</code>
  214. line correctly (and does not, for example, mistake the end of a
  215. long data line for a terminator line).
  216. </p></dd><dt id="LIBPQ-PQGETLINEASYNC"><span class="term">
  217. <code class="function">PQgetlineAsync</code>
  218. <a id="id-1.7.3.16.11.3.2.1.2" class="indexterm"></a>
  219. </span></dt><dd><p>
  220. Reads a row of <code class="command">COPY</code> data (transmitted by the
  221. server) into a buffer without blocking.
  222. </p><pre class="synopsis">
  223. int PQgetlineAsync(PGconn *conn,
  224. char *buffer,
  225. int bufsize);
  226. </pre><p>
  227. </p><p>
  228. This function is similar to <code class="function">PQgetline</code>, but it can be used
  229. by applications
  230. that must read <code class="command">COPY</code> data asynchronously, that is, without blocking.
  231. Having issued the <code class="command">COPY</code> command and gotten a <code class="literal">PGRES_COPY_OUT</code>
  232. response, the
  233. application should call <code class="function">PQconsumeInput</code> and
  234. <code class="function">PQgetlineAsync</code> until the
  235. end-of-data signal is detected.
  236. </p><p>
  237. Unlike <code class="function">PQgetline</code>, this function takes
  238. responsibility for detecting end-of-data.
  239. </p><p>
  240. On each call, <code class="function">PQgetlineAsync</code> will return data if a
  241. complete data row is available in <span class="application">libpq</span>'s input buffer.
  242. Otherwise, no data is returned until the rest of the row arrives.
  243. The function returns -1 if the end-of-copy-data marker has been recognized,
  244. or 0 if no data is available, or a positive number giving the number of
  245. bytes of data returned. If -1 is returned, the caller must next call
  246. <code class="function">PQendcopy</code>, and then return to normal processing.
  247. </p><p>
  248. The data returned will not extend beyond a data-row boundary. If possible
  249. a whole row will be returned at one time. But if the buffer offered by
  250. the caller is too small to hold a row sent by the server, then a partial
  251. data row will be returned. With textual data this can be detected by testing
  252. whether the last returned byte is <code class="literal">\n</code> or not. (In a binary
  253. <code class="command">COPY</code>, actual parsing of the <code class="command">COPY</code> data format will be needed to make the
  254. equivalent determination.)
  255. The returned string is not null-terminated. (If you want to add a
  256. terminating null, be sure to pass a <em class="parameter"><code>bufsize</code></em> one smaller
  257. than the room actually available.)
  258. </p></dd><dt id="LIBPQ-PQPUTLINE"><span class="term">
  259. <code class="function">PQputline</code>
  260. <a id="id-1.7.3.16.11.3.3.1.2" class="indexterm"></a>
  261. </span></dt><dd><p>
  262. Sends a null-terminated string to the server. Returns 0 if
  263. OK and <code class="symbol">EOF</code> if unable to send the string.
  264. </p><pre class="synopsis">
  265. int PQputline(PGconn *conn,
  266. const char *string);
  267. </pre><p>
  268. </p><p>
  269. The <code class="command">COPY</code> data stream sent by a series of calls
  270. to <code class="function">PQputline</code> has the same format as that
  271. returned by <code class="function">PQgetlineAsync</code>, except that
  272. applications are not obliged to send exactly one data row per
  273. <code class="function">PQputline</code> call; it is okay to send a partial
  274. line or multiple lines per call.
  275. </p><div class="note"><h3 class="title">Note</h3><p>
  276. Before <span class="productname">PostgreSQL</span> protocol 3.0, it was necessary
  277. for the application to explicitly send the two characters
  278. <code class="literal">\.</code> as a final line to indicate to the server that it had
  279. finished sending <code class="command">COPY</code> data. While this still works, it is deprecated and the
  280. special meaning of <code class="literal">\.</code> can be expected to be removed in a
  281. future release. It is sufficient to call <code class="function">PQendcopy</code> after
  282. having sent the actual data.
  283. </p></div></dd><dt id="LIBPQ-PQPUTNBYTES"><span class="term">
  284. <code class="function">PQputnbytes</code>
  285. <a id="id-1.7.3.16.11.3.4.1.2" class="indexterm"></a>
  286. </span></dt><dd><p>
  287. Sends a non-null-terminated string to the server. Returns
  288. 0 if OK and <code class="symbol">EOF</code> if unable to send the string.
  289. </p><pre class="synopsis">
  290. int PQputnbytes(PGconn *conn,
  291. const char *buffer,
  292. int nbytes);
  293. </pre><p>
  294. </p><p>
  295. This is exactly like <code class="function">PQputline</code>, except that the data
  296. buffer need not be null-terminated since the number of bytes to send is
  297. specified directly. Use this procedure when sending binary data.
  298. </p></dd><dt id="LIBPQ-PQENDCOPY"><span class="term">
  299. <code class="function">PQendcopy</code>
  300. <a id="id-1.7.3.16.11.3.5.1.2" class="indexterm"></a>
  301. </span></dt><dd><p>
  302. Synchronizes with the server.
  303. </p><pre class="synopsis">
  304. int PQendcopy(PGconn *conn);
  305. </pre><p>
  306. This function waits until the server has finished the copying.
  307. It should either be issued when the last string has been sent
  308. to the server using <code class="function">PQputline</code> or when the
  309. last string has been received from the server using
  310. <code class="function">PQgetline</code>. It must be issued or the server
  311. will get <span class="quote">“<span class="quote">out of sync</span>”</span> with the client. Upon return
  312. from this function, the server is ready to receive the next SQL
  313. command. The return value is 0 on successful completion,
  314. nonzero otherwise. (Use <code class="function">PQerrorMessage</code> to
  315. retrieve details if the return value is nonzero.)
  316. </p><p>
  317. When using <code class="function">PQgetResult</code>, the application should
  318. respond to a <code class="literal">PGRES_COPY_OUT</code> result by executing
  319. <code class="function">PQgetline</code> repeatedly, followed by
  320. <code class="function">PQendcopy</code> after the terminator line is seen.
  321. It should then return to the <code class="function">PQgetResult</code> loop
  322. until <code class="function">PQgetResult</code> returns a null pointer.
  323. Similarly a <code class="literal">PGRES_COPY_IN</code> result is processed
  324. by a series of <code class="function">PQputline</code> calls followed by
  325. <code class="function">PQendcopy</code>, then return to the
  326. <code class="function">PQgetResult</code> loop. This arrangement will
  327. ensure that a <code class="command">COPY</code> command embedded in a series
  328. of <acronym class="acronym">SQL</acronym> commands will be executed correctly.
  329. </p><p>
  330. Older applications are likely to submit a <code class="command">COPY</code>
  331. via <code class="function">PQexec</code> and assume that the transaction
  332. is done after <code class="function">PQendcopy</code>. This will work
  333. correctly only if the <code class="command">COPY</code> is the only
  334. <acronym class="acronym">SQL</acronym> command in the command string.
  335. </p></dd></dl></div></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="libpq-notify.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="libpq.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="libpq-control.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">33.8. Asynchronous Notification </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 33.10. Control Functions</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1