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.

74 linhas
6.5KB

  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>44.6. PL/Perl Triggers</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="plperl-trusted.html" title="44.5. Trusted and Untrusted PL/Perl" /><link rel="next" href="plperl-event-triggers.html" title="44.7. PL/Perl Event Triggers" /></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">44.6. PL/Perl Triggers</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="plperl-trusted.html" title="44.5. Trusted and Untrusted PL/Perl">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="plperl.html" title="Chapter 44. PL/Perl - Perl Procedural Language">Up</a></td><th width="60%" align="center">Chapter 44. PL/Perl - Perl 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="plperl-event-triggers.html" title="44.7. PL/Perl Event Triggers">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="PLPERL-TRIGGERS"><div class="titlepage"><div><div><h2 class="title" style="clear: both">44.6. PL/Perl Triggers</h2></div></div></div><p>
  3. PL/Perl can be used to write trigger functions. In a trigger function,
  4. the hash reference <code class="varname">$_TD</code> contains information about the
  5. current trigger event. <code class="varname">$_TD</code> is a global variable,
  6. which gets a separate local value for each invocation of the trigger.
  7. The fields of the <code class="varname">$_TD</code> hash reference are:
  8. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">$_TD-&gt;{new}{foo}</code></span></dt><dd><p>
  9. <code class="literal">NEW</code> value of column <code class="literal">foo</code>
  10. </p></dd><dt><span class="term"><code class="literal">$_TD-&gt;{old}{foo}</code></span></dt><dd><p>
  11. <code class="literal">OLD</code> value of column <code class="literal">foo</code>
  12. </p></dd><dt><span class="term"><code class="literal">$_TD-&gt;{name}</code></span></dt><dd><p>
  13. Name of the trigger being called
  14. </p></dd><dt><span class="term"><code class="literal">$_TD-&gt;{event}</code></span></dt><dd><p>
  15. Trigger event: <code class="literal">INSERT</code>, <code class="literal">UPDATE</code>,
  16. <code class="literal">DELETE</code>, <code class="literal">TRUNCATE</code>, or <code class="literal">UNKNOWN</code>
  17. </p></dd><dt><span class="term"><code class="literal">$_TD-&gt;{when}</code></span></dt><dd><p>
  18. When the trigger was called: <code class="literal">BEFORE</code>,
  19. <code class="literal">AFTER</code>, <code class="literal">INSTEAD OF</code>, or
  20. <code class="literal">UNKNOWN</code>
  21. </p></dd><dt><span class="term"><code class="literal">$_TD-&gt;{level}</code></span></dt><dd><p>
  22. The trigger level: <code class="literal">ROW</code>, <code class="literal">STATEMENT</code>, or <code class="literal">UNKNOWN</code>
  23. </p></dd><dt><span class="term"><code class="literal">$_TD-&gt;{relid}</code></span></dt><dd><p>
  24. OID of the table on which the trigger fired
  25. </p></dd><dt><span class="term"><code class="literal">$_TD-&gt;{table_name}</code></span></dt><dd><p>
  26. Name of the table on which the trigger fired
  27. </p></dd><dt><span class="term"><code class="literal">$_TD-&gt;{relname}</code></span></dt><dd><p>
  28. Name of the table on which the trigger fired. This has been deprecated,
  29. and could be removed in a future release.
  30. Please use $_TD-&gt;{table_name} instead.
  31. </p></dd><dt><span class="term"><code class="literal">$_TD-&gt;{table_schema}</code></span></dt><dd><p>
  32. Name of the schema in which the table on which the trigger fired, is
  33. </p></dd><dt><span class="term"><code class="literal">$_TD-&gt;{argc}</code></span></dt><dd><p>
  34. Number of arguments of the trigger function
  35. </p></dd><dt><span class="term"><code class="literal">@{$_TD-&gt;{args}}</code></span></dt><dd><p>
  36. Arguments of the trigger function. Does not exist if <code class="literal">$_TD-&gt;{argc}</code> is 0.
  37. </p></dd></dl></div><p>
  38. </p><p>
  39. Row-level triggers can return one of the following:
  40. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">return;</code></span></dt><dd><p>
  41. Execute the operation
  42. </p></dd><dt><span class="term"><code class="literal">"SKIP"</code></span></dt><dd><p>
  43. Don't execute the operation
  44. </p></dd><dt><span class="term"><code class="literal">"MODIFY"</code></span></dt><dd><p>
  45. Indicates that the <code class="literal">NEW</code> row was modified by
  46. the trigger function
  47. </p></dd></dl></div><p>
  48. </p><p>
  49. Here is an example of a trigger function, illustrating some of the
  50. above:
  51. </p><pre class="programlisting">
  52. CREATE TABLE test (
  53. i int,
  54. v varchar
  55. );
  56. CREATE OR REPLACE FUNCTION valid_id() RETURNS trigger AS $$
  57. if (($_TD-&gt;{new}{i} &gt;= 100) || ($_TD-&gt;{new}{i} &lt;= 0)) {
  58. return "SKIP"; # skip INSERT/UPDATE command
  59. } elsif ($_TD-&gt;{new}{v} ne "immortal") {
  60. $_TD-&gt;{new}{v} .= "(modified by trigger)";
  61. return "MODIFY"; # modify row and execute INSERT/UPDATE command
  62. } else {
  63. return; # execute INSERT/UPDATE command
  64. }
  65. $$ LANGUAGE plperl;
  66. CREATE TRIGGER test_valid_id_trig
  67. BEFORE INSERT OR UPDATE ON test
  68. FOR EACH ROW EXECUTE FUNCTION valid_id();
  69. </pre><p>
  70. </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="plperl-trusted.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="plperl.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="plperl-event-triggers.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">44.5. Trusted and Untrusted PL/Perl </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 44.7. PL/Perl Event Triggers</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1