gooderp18绿色标准版
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

106 lines
9.3KB

  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>CREATE TRANSFORM</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="sql-createtstemplate.html" title="CREATE TEXT SEARCH TEMPLATE" /><link rel="next" href="sql-createtrigger.html" title="CREATE TRIGGER" /></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">CREATE TRANSFORM</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-createtstemplate.html" title="CREATE TEXT SEARCH TEMPLATE">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><th width="60%" align="center">SQL Commands</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="sql-createtrigger.html" title="CREATE TRIGGER">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="SQL-CREATETRANSFORM"><div class="titlepage"></div><a id="id-1.9.3.92.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">CREATE TRANSFORM</span></h2><p>CREATE TRANSFORM — define a new transform</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
  3. CREATE [ OR REPLACE ] TRANSFORM FOR <em class="replaceable"><code>type_name</code></em> LANGUAGE <em class="replaceable"><code>lang_name</code></em> (
  4. FROM SQL WITH FUNCTION <em class="replaceable"><code>from_sql_function_name</code></em> [ (<em class="replaceable"><code>argument_type</code></em> [, ...]) ],
  5. TO SQL WITH FUNCTION <em class="replaceable"><code>to_sql_function_name</code></em> [ (<em class="replaceable"><code>argument_type</code></em> [, ...]) ]
  6. );
  7. </pre></div><div class="refsect1" id="SQL-CREATETRANSFORM-DESCRIPTION"><h2>Description</h2><p>
  8. <code class="command">CREATE TRANSFORM</code> defines a new transform.
  9. <code class="command">CREATE OR REPLACE TRANSFORM</code> will either create a new
  10. transform, or replace an existing definition.
  11. </p><p>
  12. A transform specifies how to adapt a data type to a procedural language.
  13. For example, when writing a function in PL/Python using
  14. the <code class="type">hstore</code> type, PL/Python has no prior knowledge how to
  15. present <code class="type">hstore</code> values in the Python environment. Language
  16. implementations usually default to using the text representation, but that
  17. is inconvenient when, for example, an associative array or a list would be
  18. more appropriate.
  19. </p><p>
  20. A transform specifies two functions:
  21. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  22. A <span class="quote">“<span class="quote">from SQL</span>”</span> function that converts the type from the SQL
  23. environment to the language. This function will be invoked on the
  24. arguments of a function written in the language.
  25. </p></li><li class="listitem"><p>
  26. A <span class="quote">“<span class="quote">to SQL</span>”</span> function that converts the type from the
  27. language to the SQL environment. This function will be invoked on the
  28. return value of a function written in the language.
  29. </p></li></ul></div><p>
  30. It is not necessary to provide both of these functions. If one is not
  31. specified, the language-specific default behavior will be used if
  32. necessary. (To prevent a transformation in a certain direction from
  33. happening at all, you could also write a transform function that always
  34. errors out.)
  35. </p><p>
  36. To be able to create a transform, you must own and
  37. have <code class="literal">USAGE</code> privilege on the type, have
  38. <code class="literal">USAGE</code> privilege on the language, and own and
  39. have <code class="literal">EXECUTE</code> privilege on the from-SQL and to-SQL
  40. functions, if specified.
  41. </p></div><div class="refsect1" id="id-1.9.3.92.6"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><em class="replaceable"><code>type_name</code></em></span></dt><dd><p>
  42. The name of the data type of the transform.
  43. </p></dd><dt><span class="term"><em class="replaceable"><code>lang_name</code></em></span></dt><dd><p>
  44. The name of the language of the transform.
  45. </p></dd><dt><span class="term"><code class="literal"><em class="replaceable"><code>from_sql_function_name</code></em>[(<em class="replaceable"><code>argument_type</code></em> [, ...])]</code></span></dt><dd><p>
  46. The name of the function for converting the type from the SQL
  47. environment to the language. It must take one argument of
  48. type <code class="type">internal</code> and return type <code class="type">internal</code>. The
  49. actual argument will be of the type for the transform, and the function
  50. should be coded as if it were. (But it is not allowed to declare an
  51. SQL-level function returning <code class="type">internal</code> without at
  52. least one argument of type <code class="type">internal</code>.) The actual return
  53. value will be something specific to the language implementation.
  54. If no argument list is specified, the function name must be unique in
  55. its schema.
  56. </p></dd><dt><span class="term"><code class="literal"><em class="replaceable"><code>to_sql_function_name</code></em>[(<em class="replaceable"><code>argument_type</code></em> [, ...])]</code></span></dt><dd><p>
  57. The name of the function for converting the type from the language to
  58. the SQL environment. It must take one argument of type
  59. <code class="type">internal</code> and return the type that is the type for the
  60. transform. The actual argument value will be something specific to the
  61. language implementation.
  62. If no argument list is specified, the function name must be unique in
  63. its schema.
  64. </p></dd></dl></div></div><div class="refsect1" id="SQL-CREATETRANSFORM-NOTES"><h2>Notes</h2><p>
  65. Use <a class="xref" href="sql-droptransform.html" title="DROP TRANSFORM"><span class="refentrytitle">DROP TRANSFORM</span></a> to remove transforms.
  66. </p></div><div class="refsect1" id="SQL-CREATETRANSFORM-EXAMPLES"><h2>Examples</h2><p>
  67. To create a transform for type <code class="type">hstore</code> and language
  68. <code class="literal">plpythonu</code>, first set up the type and the language:
  69. </p><pre class="programlisting">
  70. CREATE TYPE hstore ...;
  71. CREATE EXTENSION plpythonu;
  72. </pre><p>
  73. Then create the necessary functions:
  74. </p><pre class="programlisting">
  75. CREATE FUNCTION hstore_to_plpython(val internal) RETURNS internal
  76. LANGUAGE C STRICT IMMUTABLE
  77. AS ...;
  78. CREATE FUNCTION plpython_to_hstore(val internal) RETURNS hstore
  79. LANGUAGE C STRICT IMMUTABLE
  80. AS ...;
  81. </pre><p>
  82. And finally create the transform to connect them all together:
  83. </p><pre class="programlisting">
  84. CREATE TRANSFORM FOR hstore LANGUAGE plpythonu (
  85. FROM SQL WITH FUNCTION hstore_to_plpython(internal),
  86. TO SQL WITH FUNCTION plpython_to_hstore(internal)
  87. );
  88. </pre><p>
  89. In practice, these commands would be wrapped up in an extension.
  90. </p><p>
  91. The <code class="filename">contrib</code> section contains a number of extensions
  92. that provide transforms, which can serve as real-world examples.
  93. </p></div><div class="refsect1" id="SQL-CREATETRANSFORM-COMPAT"><h2>Compatibility</h2><p>
  94. This form of <code class="command">CREATE TRANSFORM</code> is a
  95. <span class="productname">PostgreSQL</span> extension. There is a <code class="command">CREATE
  96. TRANSFORM</code> command in the <acronym class="acronym">SQL</acronym> standard, but it
  97. is for adapting data types to client languages. That usage is not supported
  98. by <span class="productname">PostgreSQL</span>.
  99. </p></div><div class="refsect1" id="SQL-CREATETRANSFORM-SEEALSO"><h2>See Also</h2><p>
  100. <a class="xref" href="sql-createfunction.html" title="CREATE FUNCTION"><span class="refentrytitle">CREATE FUNCTION</span></a>,
  101. <a class="xref" href="sql-createlanguage.html" title="CREATE LANGUAGE"><span class="refentrytitle">CREATE LANGUAGE</span></a>,
  102. <a class="xref" href="sql-createtype.html" title="CREATE TYPE"><span class="refentrytitle">CREATE TYPE</span></a>,
  103. <a class="xref" href="sql-droptransform.html" title="DROP TRANSFORM"><span class="refentrytitle">DROP TRANSFORM</span></a>
  104. </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="sql-createtstemplate.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sql-commands.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sql-createtrigger.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">CREATE TEXT SEARCH TEMPLATE </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> CREATE TRIGGER</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1