gooderp18绿色标准版
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

189 linhas
14KB

  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>43.5. Database Access from PL/Tcl</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="pltcl-global.html" title="43.4. Global Data in PL/Tcl" /><link rel="next" href="pltcl-trigger.html" title="43.6. Trigger Functions in PL/Tcl" /></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">43.5. Database Access from PL/Tcl</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="pltcl-global.html" title="43.4. Global Data in PL/Tcl">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="pltcl.html" title="Chapter 43. PL/Tcl - Tcl Procedural Language">Up</a></td><th width="60%" align="center">Chapter 43. PL/Tcl - Tcl Procedural Language</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="pltcl-trigger.html" title="43.6. Trigger Functions in PL/Tcl">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="PLTCL-DBACCESS"><div class="titlepage"><div><div><h2 class="title" style="clear: both">43.5. Database Access from PL/Tcl</h2></div></div></div><p>
  3. The following commands are available to access the database from
  4. the body of a PL/Tcl function:
  5. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal"><code class="function">spi_exec</code> ?<span class="optional">-count <em class="replaceable"><code>n</code></em></span>? ?<span class="optional">-array <em class="replaceable"><code>name</code></em></span>? <em class="replaceable"><code>command</code></em> ?<span class="optional"><em class="replaceable"><code>loop-body</code></em></span>?</code></span></dt><dd><p>
  6. Executes an SQL command given as a string. An error in the command
  7. causes an error to be raised. Otherwise, the return value of <code class="function">spi_exec</code>
  8. is the number of rows processed (selected, inserted, updated, or
  9. deleted) by the command, or zero if the command is a utility
  10. statement. In addition, if the command is a <code class="command">SELECT</code> statement, the
  11. values of the selected columns are placed in Tcl variables as
  12. described below.
  13. </p><p>
  14. The optional <code class="literal">-count</code> value tells
  15. <code class="function">spi_exec</code> the maximum number of rows
  16. to process in the command. The effect of this is comparable to
  17. setting up a query as a cursor and then saying <code class="literal">FETCH <em class="replaceable"><code>n</code></em></code>.
  18. </p><p>
  19. If the command is a <code class="command">SELECT</code> statement, the values of the
  20. result columns are placed into Tcl variables named after the columns.
  21. If the <code class="literal">-array</code> option is given, the column values are
  22. instead stored into elements of the named associative array, with the
  23. column names used as array indexes. In addition, the current row
  24. number within the result (counting from zero) is stored into the array
  25. element named <span class="quote">“<span class="quote"><code class="literal">.tupno</code></span>”</span>, unless that name is
  26. in use as a column name in the result.
  27. </p><p>
  28. If the command is a <code class="command">SELECT</code> statement and no <em class="replaceable"><code>loop-body</code></em>
  29. script is given, then only the first row of results are stored into
  30. Tcl variables or array elements; remaining rows, if any, are ignored.
  31. No storing occurs if the query returns no rows. (This case can be
  32. detected by checking the result of <code class="function">spi_exec</code>.)
  33. For example:
  34. </p><pre class="programlisting">
  35. spi_exec "SELECT count(*) AS cnt FROM pg_proc"
  36. </pre><p>
  37. will set the Tcl variable <code class="literal">$cnt</code> to the number of rows in
  38. the <code class="structname">pg_proc</code> system catalog.
  39. </p><p>
  40. If the optional <em class="replaceable"><code>loop-body</code></em> argument is given, it is
  41. a piece of Tcl script that is executed once for each row in the
  42. query result. (<em class="replaceable"><code>loop-body</code></em> is ignored if the given
  43. command is not a <code class="command">SELECT</code>.)
  44. The values of the current row's columns
  45. are stored into Tcl variables or array elements before each iteration.
  46. For example:
  47. </p><pre class="programlisting">
  48. spi_exec -array C "SELECT * FROM pg_class" {
  49. elog DEBUG "have table $C(relname)"
  50. }
  51. </pre><p>
  52. will print a log message for every row of <code class="literal">pg_class</code>. This
  53. feature works similarly to other Tcl looping constructs; in
  54. particular <code class="literal">continue</code> and <code class="literal">break</code> work in the
  55. usual way inside the loop body.
  56. </p><p>
  57. If a column of a query result is null, the target
  58. variable for it is <span class="quote">“<span class="quote">unset</span>”</span> rather than being set.
  59. </p></dd><dt><span class="term"><code class="function">spi_prepare</code> <em class="replaceable"><code>query</code></em> <em class="replaceable"><code>typelist</code></em></span></dt><dd><p>
  60. Prepares and saves a query plan for later execution. The
  61. saved plan will be retained for the life of the current
  62. session.<a id="id-1.8.9.9.2.1.2.2.1.1" class="indexterm"></a>
  63. </p><p>
  64. The query can use parameters, that is, placeholders for
  65. values to be supplied whenever the plan is actually executed.
  66. In the query string, refer to parameters
  67. by the symbols <code class="literal">$1</code> ... <code class="literal">$<em class="replaceable"><code>n</code></em></code>.
  68. If the query uses parameters, the names of the parameter types
  69. must be given as a Tcl list. (Write an empty list for
  70. <em class="replaceable"><code>typelist</code></em> if no parameters are used.)
  71. </p><p>
  72. The return value from <code class="function">spi_prepare</code> is a query ID
  73. to be used in subsequent calls to <code class="function">spi_execp</code>. See
  74. <code class="function">spi_execp</code> for an example.
  75. </p></dd><dt><span class="term"><code class="literal"><code class="function">spi_execp</code> ?<span class="optional">-count <em class="replaceable"><code>n</code></em></span>? ?<span class="optional">-array <em class="replaceable"><code>name</code></em></span>? ?<span class="optional">-nulls <em class="replaceable"><code>string</code></em></span>? <em class="replaceable"><code>queryid</code></em> ?<span class="optional"><em class="replaceable"><code>value-list</code></em></span>? ?<span class="optional"><em class="replaceable"><code>loop-body</code></em></span>?</code></span></dt><dd><p>
  76. Executes a query previously prepared with <code class="function">spi_prepare</code>.
  77. <em class="replaceable"><code>queryid</code></em> is the ID returned by
  78. <code class="function">spi_prepare</code>. If the query references parameters,
  79. a <em class="replaceable"><code>value-list</code></em> must be supplied. This
  80. is a Tcl list of actual values for the parameters. The list must be
  81. the same length as the parameter type list previously given to
  82. <code class="function">spi_prepare</code>. Omit <em class="replaceable"><code>value-list</code></em>
  83. if the query has no parameters.
  84. </p><p>
  85. The optional value for <code class="literal">-nulls</code> is a string of spaces and
  86. <code class="literal">'n'</code> characters telling <code class="function">spi_execp</code>
  87. which of the parameters are null values. If given, it must have exactly the
  88. same length as the <em class="replaceable"><code>value-list</code></em>. If it
  89. is not given, all the parameter values are nonnull.
  90. </p><p>
  91. Except for the way in which the query and its parameters are specified,
  92. <code class="function">spi_execp</code> works just like <code class="function">spi_exec</code>.
  93. The <code class="literal">-count</code>, <code class="literal">-array</code>, and
  94. <em class="replaceable"><code>loop-body</code></em> options are the same,
  95. and so is the result value.
  96. </p><p>
  97. Here's an example of a PL/Tcl function using a prepared plan:
  98. </p><pre class="programlisting">
  99. CREATE FUNCTION t1_count(integer, integer) RETURNS integer AS $$
  100. if {![ info exists GD(plan) ]} {
  101. # prepare the saved plan on the first call
  102. set GD(plan) [ spi_prepare \
  103. "SELECT count(*) AS cnt FROM t1 WHERE num &gt;= \$1 AND num &lt;= \$2" \
  104. [ list int4 int4 ] ]
  105. }
  106. spi_execp -count 1 $GD(plan) [ list $1 $2 ]
  107. return $cnt
  108. $$ LANGUAGE pltcl;
  109. </pre><p>
  110. We need backslashes inside the query string given to
  111. <code class="function">spi_prepare</code> to ensure that the
  112. <code class="literal">$<em class="replaceable"><code>n</code></em></code> markers will be passed
  113. through to <code class="function">spi_prepare</code> as-is, and not replaced by Tcl
  114. variable substitution.
  115. </p></dd><dt><span class="term"><code class="function">subtransaction</code> <em class="replaceable"><code>command</code></em></span></dt><dd><p>
  116. The Tcl script contained in <em class="replaceable"><code>command</code></em> is
  117. executed within a SQL subtransaction. If the script returns an
  118. error, that entire subtransaction is rolled back before returning the
  119. error out to the surrounding Tcl code.
  120. See <a class="xref" href="pltcl-subtransactions.html" title="43.9. Explicit Subtransactions in PL/Tcl">Section 43.9</a> for more details and an
  121. example.
  122. </p></dd><dt><span class="term"><code class="function">quote</code> <em class="replaceable"><code>string</code></em></span></dt><dd><p>
  123. Doubles all occurrences of single quote and backslash characters
  124. in the given string. This can be used to safely quote strings
  125. that are to be inserted into SQL commands given
  126. to <code class="function">spi_exec</code> or
  127. <code class="function">spi_prepare</code>.
  128. For example, think about an SQL command string like:
  129. </p><pre class="programlisting">
  130. "SELECT '$val' AS ret"
  131. </pre><p>
  132. where the Tcl variable <code class="literal">val</code> actually contains
  133. <code class="literal">doesn't</code>. This would result
  134. in the final command string:
  135. </p><pre class="programlisting">
  136. SELECT 'doesn't' AS ret
  137. </pre><p>
  138. which would cause a parse error during
  139. <code class="function">spi_exec</code> or
  140. <code class="function">spi_prepare</code>.
  141. To work properly, the submitted command should contain:
  142. </p><pre class="programlisting">
  143. SELECT 'doesn''t' AS ret
  144. </pre><p>
  145. which can be formed in PL/Tcl using:
  146. </p><pre class="programlisting">
  147. "SELECT '[ quote $val ]' AS ret"
  148. </pre><p>
  149. One advantage of <code class="function">spi_execp</code> is that you don't
  150. have to quote parameter values like this, since the parameters are never
  151. parsed as part of an SQL command string.
  152. </p></dd><dt><span class="term">
  153. <code class="function">elog</code> <em class="replaceable"><code>level</code></em> <em class="replaceable"><code>msg</code></em>
  154. <a id="id-1.8.9.9.2.1.6.1.4" class="indexterm"></a>
  155. </span></dt><dd><p>
  156. Emits a log or error message. Possible levels are
  157. <code class="literal">DEBUG</code>, <code class="literal">LOG</code>, <code class="literal">INFO</code>,
  158. <code class="literal">NOTICE</code>, <code class="literal">WARNING</code>, <code class="literal">ERROR</code>, and
  159. <code class="literal">FATAL</code>. <code class="literal">ERROR</code>
  160. raises an error condition; if this is not trapped by the surrounding
  161. Tcl code, the error propagates out to the calling query, causing
  162. the current transaction or subtransaction to be aborted. This
  163. is effectively the same as the Tcl <code class="literal">error</code> command.
  164. <code class="literal">FATAL</code> aborts the transaction and causes the current
  165. session to shut down. (There is probably no good reason to use
  166. this error level in PL/Tcl functions, but it's provided for
  167. completeness.) The other levels only generate messages of different
  168. priority levels.
  169. Whether messages of a particular priority are reported to the client,
  170. written to the server log, or both is controlled by the
  171. <a class="xref" href="runtime-config-logging.html#GUC-LOG-MIN-MESSAGES">log_min_messages</a> and
  172. <a class="xref" href="runtime-config-client.html#GUC-CLIENT-MIN-MESSAGES">client_min_messages</a> configuration
  173. variables. See <a class="xref" href="runtime-config.html" title="Chapter 19. Server Configuration">Chapter 19</a>
  174. and <a class="xref" href="pltcl-error-handling.html" title="43.8. Error Handling in PL/Tcl">Section 43.8</a>
  175. for more information.
  176. </p></dd></dl></div><p>
  177. </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="pltcl-global.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="pltcl.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="pltcl-trigger.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">43.4. Global Data in PL/Tcl </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 43.6. Trigger Functions in PL/Tcl</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1