gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
7.6KB

  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.2. PL/Tcl Functions and Arguments</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-overview.html" title="43.1. Overview" /><link rel="next" href="pltcl-data.html" title="43.3. Data Values 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.2. PL/Tcl Functions and Arguments</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="pltcl-overview.html" title="43.1. Overview">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-data.html" title="43.3. Data Values in PL/Tcl">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="PLTCL-FUNCTIONS"><div class="titlepage"><div><div><h2 class="title" style="clear: both">43.2. PL/Tcl Functions and Arguments</h2></div></div></div><p>
  3. To create a function in the <span class="application">PL/Tcl</span> language, use
  4. the standard <a class="xref" href="sql-createfunction.html" title="CREATE FUNCTION"><span class="refentrytitle">CREATE FUNCTION</span></a> syntax:
  5. </p><pre class="programlisting">
  6. CREATE FUNCTION <em class="replaceable"><code>funcname</code></em> (<em class="replaceable"><code>argument-types</code></em>) RETURNS <em class="replaceable"><code>return-type</code></em> AS $$
  7. # PL/Tcl function body
  8. $$ LANGUAGE pltcl;
  9. </pre><p>
  10. <span class="application">PL/TclU</span> is the same, except that the language has to be specified as
  11. <code class="literal">pltclu</code>.
  12. </p><p>
  13. The body of the function is simply a piece of Tcl script.
  14. When the function is called, the argument values are passed to the
  15. Tcl script as variables named <code class="literal">1</code>
  16. ... <code class="literal"><em class="replaceable"><code>n</code></em></code>. The result is
  17. returned from the Tcl code in the usual way, with
  18. a <code class="literal">return</code> statement. In a procedure, the return value
  19. from the Tcl code is ignored.
  20. </p><p>
  21. For example, a function
  22. returning the greater of two integer values could be defined as:
  23. </p><pre class="programlisting">
  24. CREATE FUNCTION tcl_max(integer, integer) RETURNS integer AS $$
  25. if {$1 &gt; $2} {return $1}
  26. return $2
  27. $$ LANGUAGE pltcl STRICT;
  28. </pre><p>
  29. Note the clause <code class="literal">STRICT</code>, which saves us from
  30. having to think about null input values: if a null value is passed, the
  31. function will not be called at all, but will just return a null
  32. result automatically.
  33. </p><p>
  34. In a nonstrict function,
  35. if the actual value of an argument is null, the corresponding
  36. <code class="literal">$<em class="replaceable"><code>n</code></em></code> variable will be set to an empty string.
  37. To detect whether a particular argument is null, use the function
  38. <code class="literal">argisnull</code>. For example, suppose that we wanted <code class="function">tcl_max</code>
  39. with one null and one nonnull argument to return the nonnull
  40. argument, rather than null:
  41. </p><pre class="programlisting">
  42. CREATE FUNCTION tcl_max(integer, integer) RETURNS integer AS $$
  43. if {[argisnull 1]} {
  44. if {[argisnull 2]} { return_null }
  45. return $2
  46. }
  47. if {[argisnull 2]} { return $1 }
  48. if {$1 &gt; $2} {return $1}
  49. return $2
  50. $$ LANGUAGE pltcl;
  51. </pre><p>
  52. </p><p>
  53. As shown above,
  54. to return a null value from a PL/Tcl function, execute
  55. <code class="literal">return_null</code>. This can be done whether the
  56. function is strict or not.
  57. </p><p>
  58. Composite-type arguments are passed to the function as Tcl
  59. arrays. The element names of the array are the attribute names
  60. of the composite type. If an attribute in the passed row has the
  61. null value, it will not appear in the array. Here is an example:
  62. </p><pre class="programlisting">
  63. CREATE TABLE employee (
  64. name text,
  65. salary integer,
  66. age integer
  67. );
  68. CREATE FUNCTION overpaid(employee) RETURNS boolean AS $$
  69. if {200000.0 &lt; $1(salary)} {
  70. return "t"
  71. }
  72. if {$1(age) &lt; 30 &amp;&amp; 100000.0 &lt; $1(salary)} {
  73. return "t"
  74. }
  75. return "f"
  76. $$ LANGUAGE pltcl;
  77. </pre><p>
  78. </p><p>
  79. PL/Tcl functions can return composite-type results, too. To do this,
  80. the Tcl code must return a list of column name/value pairs matching
  81. the expected result type. Any column names omitted from the list
  82. are returned as nulls, and an error is raised if there are unexpected
  83. column names. Here is an example:
  84. </p><pre class="programlisting">
  85. CREATE FUNCTION square_cube(in int, out squared int, out cubed int) AS $$
  86. return [list squared [expr {$1 * $1}] cubed [expr {$1 * $1 * $1}]]
  87. $$ LANGUAGE pltcl;
  88. </pre><p>
  89. </p><p>
  90. Output arguments of procedures are returned in the same way, for example:
  91. </p><pre class="programlisting">
  92. CREATE PROCEDURE tcl_triple(INOUT a integer, INOUT b integer) AS $$
  93. return [list a [expr {$1 * 3}] b [expr {$2 * 3}]]
  94. $$ LANGUAGE pltcl;
  95. CALL tcl_triple(5, 10);
  96. </pre><p>
  97. </p><div class="tip"><h3 class="title">Tip</h3><p>
  98. The result list can be made from an array representation of the
  99. desired tuple with the <code class="literal">array get</code> Tcl command. For example:
  100. </p><pre class="programlisting">
  101. CREATE FUNCTION raise_pay(employee, delta int) RETURNS employee AS $$
  102. set 1(salary) [expr {$1(salary) + $2}]
  103. return [array get 1]
  104. $$ LANGUAGE pltcl;
  105. </pre><p>
  106. </p></div><p>
  107. PL/Tcl functions can return sets. To do this, the Tcl code should
  108. call <code class="function">return_next</code> once per row to be returned,
  109. passing either the appropriate value when returning a scalar type,
  110. or a list of column name/value pairs when returning a composite type.
  111. Here is an example returning a scalar type:
  112. </p><pre class="programlisting">
  113. CREATE FUNCTION sequence(int, int) RETURNS SETOF int AS $$
  114. for {set i $1} {$i &lt; $2} {incr i} {
  115. return_next $i
  116. }
  117. $$ LANGUAGE pltcl;
  118. </pre><p>
  119. and here is one returning a composite type:
  120. </p><pre class="programlisting">
  121. CREATE FUNCTION table_of_squares(int, int) RETURNS TABLE (x int, x2 int) AS $$
  122. for {set i $1} {$i &lt; $2} {incr i} {
  123. return_next [list x $i x2 [expr {$i * $i}]]
  124. }
  125. $$ LANGUAGE pltcl;
  126. </pre><p>
  127. </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="pltcl-overview.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-data.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">43.1. Overview </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 43.3. Data Values in PL/Tcl</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1