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

115 行
9.4KB

  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.6. Trigger Functions in 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-dbaccess.html" title="43.5. Database Access from PL/Tcl" /><link rel="next" href="pltcl-event-trigger.html" title="43.7. Event 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.6. Trigger Functions in PL/Tcl</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="pltcl-dbaccess.html" title="43.5. Database Access from 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-event-trigger.html" title="43.7. Event Trigger Functions in PL/Tcl">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="PLTCL-TRIGGER"><div class="titlepage"><div><div><h2 class="title" style="clear: both">43.6. Trigger Functions in PL/Tcl</h2></div></div></div><a id="id-1.8.9.10.2" class="indexterm"></a><p>
  3. Trigger functions can be written in PL/Tcl.
  4. <span class="productname">PostgreSQL</span> requires that a function that is to be called
  5. as a trigger must be declared as a function with no arguments
  6. and a return type of <code class="literal">trigger</code>.
  7. </p><p>
  8. The information from the trigger manager is passed to the function body
  9. in the following variables:
  10. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="varname">$TG_name</code></span></dt><dd><p>
  11. The name of the trigger from the <code class="command">CREATE TRIGGER</code> statement.
  12. </p></dd><dt><span class="term"><code class="varname">$TG_relid</code></span></dt><dd><p>
  13. The object ID of the table that caused the trigger function
  14. to be invoked.
  15. </p></dd><dt><span class="term"><code class="varname">$TG_table_name</code></span></dt><dd><p>
  16. The name of the table that caused the trigger function
  17. to be invoked.
  18. </p></dd><dt><span class="term"><code class="varname">$TG_table_schema</code></span></dt><dd><p>
  19. The schema of the table that caused the trigger function
  20. to be invoked.
  21. </p></dd><dt><span class="term"><code class="varname">$TG_relatts</code></span></dt><dd><p>
  22. A Tcl list of the table column names, prefixed with an empty list
  23. element. So looking up a column name in the list with <span class="application">Tcl</span>'s
  24. <code class="function">lsearch</code> command returns the element's number starting
  25. with 1 for the first column, the same way the columns are customarily
  26. numbered in <span class="productname">PostgreSQL</span>. (Empty list
  27. elements also appear in the positions of columns that have been
  28. dropped, so that the attribute numbering is correct for columns
  29. to their right.)
  30. </p></dd><dt><span class="term"><code class="varname">$TG_when</code></span></dt><dd><p>
  31. The string <code class="literal">BEFORE</code>, <code class="literal">AFTER</code>, or
  32. <code class="literal">INSTEAD OF</code>, depending on the type of trigger event.
  33. </p></dd><dt><span class="term"><code class="varname">$TG_level</code></span></dt><dd><p>
  34. The string <code class="literal">ROW</code> or <code class="literal">STATEMENT</code> depending on the
  35. type of trigger event.
  36. </p></dd><dt><span class="term"><code class="varname">$TG_op</code></span></dt><dd><p>
  37. The string <code class="literal">INSERT</code>, <code class="literal">UPDATE</code>,
  38. <code class="literal">DELETE</code>, or <code class="literal">TRUNCATE</code> depending on the type of
  39. trigger event.
  40. </p></dd><dt><span class="term"><code class="varname">$NEW</code></span></dt><dd><p>
  41. An associative array containing the values of the new table
  42. row for <code class="command">INSERT</code> or <code class="command">UPDATE</code> actions, or
  43. empty for <code class="command">DELETE</code>. The array is indexed by column
  44. name. Columns that are null will not appear in the array.
  45. This is not set for statement-level triggers.
  46. </p></dd><dt><span class="term"><code class="varname">$OLD</code></span></dt><dd><p>
  47. An associative array containing the values of the old table
  48. row for <code class="command">UPDATE</code> or <code class="command">DELETE</code> actions, or
  49. empty for <code class="command">INSERT</code>. The array is indexed by column
  50. name. Columns that are null will not appear in the array.
  51. This is not set for statement-level triggers.
  52. </p></dd><dt><span class="term"><code class="varname">$args</code></span></dt><dd><p>
  53. A Tcl list of the arguments to the function as given in the
  54. <code class="command">CREATE TRIGGER</code> statement. These arguments are also accessible as
  55. <code class="literal">$1</code> ... <code class="literal">$<em class="replaceable"><code>n</code></em></code> in the function body.
  56. </p></dd></dl></div><p>
  57. </p><p>
  58. The return value from a trigger function can be one of the strings
  59. <code class="literal">OK</code> or <code class="literal">SKIP</code>, or a list of column name/value pairs.
  60. If the return value is <code class="literal">OK</code>,
  61. the operation (<code class="command">INSERT</code>/<code class="command">UPDATE</code>/<code class="command">DELETE</code>)
  62. that fired the trigger will proceed
  63. normally. <code class="literal">SKIP</code> tells the trigger manager to silently suppress
  64. the operation for this row. If a list is returned, it tells PL/Tcl to
  65. return a modified row to the trigger manager; the contents of the
  66. modified row are specified by the column names and values in the list.
  67. Any columns not mentioned in the list are set to null.
  68. Returning a modified row is only meaningful
  69. for row-level <code class="literal">BEFORE</code> <code class="command">INSERT</code> or <code class="command">UPDATE</code>
  70. triggers, for which the modified row will be inserted instead of the one
  71. given in <code class="varname">$NEW</code>; or for row-level <code class="literal">INSTEAD OF</code>
  72. <code class="command">INSERT</code> or <code class="command">UPDATE</code> triggers where the returned row
  73. is used as the source data for <code class="command">INSERT RETURNING</code> or
  74. <code class="command">UPDATE RETURNING</code> clauses.
  75. In row-level <code class="literal">BEFORE</code> <code class="command">DELETE</code> or <code class="literal">INSTEAD
  76. OF</code> <code class="command">DELETE</code> triggers, returning a modified row has the same
  77. effect as returning <code class="literal">OK</code>, that is the operation proceeds.
  78. The trigger return value is ignored for all other types of triggers.
  79. </p><div class="tip"><h3 class="title">Tip</h3><p>
  80. The result list can be made from an array representation of the
  81. modified tuple with the <code class="literal">array get</code> Tcl command.
  82. </p></div><p>
  83. Here's a little example trigger function that forces an integer value
  84. in a table to keep track of the number of updates that are performed on the
  85. row. For new rows inserted, the value is initialized to 0 and then
  86. incremented on every update operation.
  87. </p><pre class="programlisting">
  88. CREATE FUNCTION trigfunc_modcount() RETURNS trigger AS $$
  89. switch $TG_op {
  90. INSERT {
  91. set NEW($1) 0
  92. }
  93. UPDATE {
  94. set NEW($1) $OLD($1)
  95. incr NEW($1)
  96. }
  97. default {
  98. return OK
  99. }
  100. }
  101. return [array get NEW]
  102. $$ LANGUAGE pltcl;
  103. CREATE TABLE mytab (num integer, description text, modcnt integer);
  104. CREATE TRIGGER trig_mytab_modcount BEFORE INSERT OR UPDATE ON mytab
  105. FOR EACH ROW EXECUTE FUNCTION trigfunc_modcount('modcnt');
  106. </pre><p>
  107. Notice that the trigger function itself does not know the column
  108. name; that's supplied from the trigger arguments. This lets the
  109. trigger function be reused with different tables.
  110. </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="pltcl-dbaccess.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-event-trigger.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">43.5. Database Access from 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.7. Event Trigger Functions in PL/Tcl</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1