Editorial improvements for recent plpython doc updates.
This commit is contained in:
parent
90f53d8487
commit
425417d498
@ -1,4 +1,4 @@
|
|||||||
<!-- $PostgreSQL: pgsql/doc/src/sgml/plpython.sgml,v 1.34 2006/10/16 17:28:03 momjian Exp $ -->
|
<!-- $PostgreSQL: pgsql/doc/src/sgml/plpython.sgml,v 1.35 2006/10/21 18:33:05 tgl Exp $ -->
|
||||||
|
|
||||||
<chapter id="plpython">
|
<chapter id="plpython">
|
||||||
<title>PL/Python - Python Procedural Language</title>
|
<title>PL/Python - Python Procedural Language</title>
|
||||||
@ -61,11 +61,11 @@ $$ LANGUAGE plpythonu;
|
|||||||
|
|
||||||
<para>
|
<para>
|
||||||
The body of a function is simply a Python script. When the function
|
The body of a function is simply a Python script. When the function
|
||||||
is called, all unnamed arguments are passed as elements to the array
|
is called, its arguments are passed as elements of the array
|
||||||
<varname>args[]</varname> and named arguments as ordinary variables to the
|
<varname>args[]</varname>; named arguments are also passed as ordinary
|
||||||
Python script. The result is returned from the Python code in the usual way,
|
variables to the Python script. The result is returned from the Python code
|
||||||
with <literal>return</literal> or <literal>yield</literal> (in case of
|
in the usual way, with <literal>return</literal> or
|
||||||
a resultset statement).
|
<literal>yield</literal> (in case of a resultset statement).
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
<para>
|
<para>
|
||||||
@ -101,9 +101,9 @@ def __plpython_procedure_pymax_23456():
|
|||||||
the global <varname>args</varname> list. In the
|
the global <varname>args</varname> list. In the
|
||||||
<function>pymax</function> example, <varname>args[0]</varname> contains
|
<function>pymax</function> example, <varname>args[0]</varname> contains
|
||||||
whatever was passed in as the first argument and
|
whatever was passed in as the first argument and
|
||||||
<varname>args[1]</varname> contains the second argument's value. Alternatively,
|
<varname>args[1]</varname> contains the second argument's
|
||||||
one can use named parameters as shown in the example above. This greatly simplifies
|
value. Alternatively, one can use named parameters as shown in the example
|
||||||
the reading and writing of <application>PL/Python</application> code.
|
above. Use of named parameters is usually more readable.
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
<para>
|
<para>
|
||||||
@ -161,31 +161,27 @@ $$ LANGUAGE plpythonu;
|
|||||||
|
|
||||||
<para>
|
<para>
|
||||||
There are multiple ways to return row or composite types from a Python
|
There are multiple ways to return row or composite types from a Python
|
||||||
scripts. In following examples we assume to have:
|
function. The following examples assume we have:
|
||||||
|
|
||||||
<programlisting>
|
<programlisting>
|
||||||
CREATE TABLE named_value (
|
|
||||||
name text,
|
|
||||||
value integer
|
|
||||||
);
|
|
||||||
</programlisting>
|
|
||||||
or
|
|
||||||
<programlisting>
|
|
||||||
CREATE TYPE named_value AS (
|
CREATE TYPE named_value AS (
|
||||||
name text,
|
name text,
|
||||||
value integer
|
value integer
|
||||||
);
|
);
|
||||||
</programlisting>
|
</programlisting>
|
||||||
|
|
||||||
|
A composite result can be returned as a:
|
||||||
|
|
||||||
<variablelist>
|
<variablelist>
|
||||||
<varlistentry>
|
<varlistentry>
|
||||||
<term>Sequence types (tuple or list), but not <literal>set</literal> (because
|
<term>Sequence type (a tuple or list, but not a set because
|
||||||
it is not indexable)</term>
|
it is not indexable)</term>
|
||||||
<listitem>
|
<listitem>
|
||||||
<para>
|
<para>
|
||||||
Returned sequence objects must have the same number of items as
|
Returned sequence objects must have the same number of items as the
|
||||||
composite types have fields. Item with index 0 is assigned to the first field
|
composite result type has fields. The item with index 0 is assigned to
|
||||||
of the composite type, 1 to second and so on. For example:
|
the first field of the composite type, 1 to the second and so on. For
|
||||||
|
example:
|
||||||
|
|
||||||
<programlisting>
|
<programlisting>
|
||||||
CREATE FUNCTION make_pair (name text, value integer)
|
CREATE FUNCTION make_pair (name text, value integer)
|
||||||
@ -196,7 +192,7 @@ AS $$
|
|||||||
$$ LANGUAGE plpythonu;
|
$$ LANGUAGE plpythonu;
|
||||||
</programlisting>
|
</programlisting>
|
||||||
|
|
||||||
To return SQL null in any column, insert <symbol>None</symbol> at
|
To return a SQL null for any column, insert <symbol>None</symbol> at
|
||||||
the corresponding position.
|
the corresponding position.
|
||||||
</para>
|
</para>
|
||||||
</listitem>
|
</listitem>
|
||||||
@ -206,8 +202,8 @@ $$ LANGUAGE plpythonu;
|
|||||||
<term>Mapping (dictionary)</term>
|
<term>Mapping (dictionary)</term>
|
||||||
<listitem>
|
<listitem>
|
||||||
<para>
|
<para>
|
||||||
Value for a composite type's column is retrieved from the mapping with
|
The value for each result type column is retrieved from the mapping
|
||||||
the column name as key. Example:
|
with the column name as key. Example:
|
||||||
|
|
||||||
<programlisting>
|
<programlisting>
|
||||||
CREATE FUNCTION make_pair (name text, value integer)
|
CREATE FUNCTION make_pair (name text, value integer)
|
||||||
@ -217,8 +213,9 @@ AS $$
|
|||||||
$$ LANGUAGE plpythonu;
|
$$ LANGUAGE plpythonu;
|
||||||
</programlisting>
|
</programlisting>
|
||||||
|
|
||||||
Additional dictionary key/value pairs are ignored. Missing keys are
|
Any extra dictionary key/value pairs are ignored. Missing keys are
|
||||||
treated as errors, i.e. to return an SQL null value for any column, insert
|
treated as errors.
|
||||||
|
To return a SQL null value for any column, insert
|
||||||
<symbol>None</symbol> with the corresponding column name as the key.
|
<symbol>None</symbol> with the corresponding column name as the key.
|
||||||
</para>
|
</para>
|
||||||
</listitem>
|
</listitem>
|
||||||
@ -228,6 +225,7 @@ $$ LANGUAGE plpythonu;
|
|||||||
<term>Object (any object providing method <literal>__getattr__</literal>)</term>
|
<term>Object (any object providing method <literal>__getattr__</literal>)</term>
|
||||||
<listitem>
|
<listitem>
|
||||||
<para>
|
<para>
|
||||||
|
This works the same as a mapping.
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
<programlisting>
|
<programlisting>
|
||||||
@ -261,9 +259,9 @@ $$ LANGUAGE plpythonu;
|
|||||||
|
|
||||||
<para>
|
<para>
|
||||||
A <application>PL/Python</application> function can also return sets of
|
A <application>PL/Python</application> function can also return sets of
|
||||||
scalar or composite types. There are serveral ways to achieve this because
|
scalar or composite types. There are several ways to achieve this because
|
||||||
the returned object is internally turned into an iterator. For following
|
the returned object is internally turned into an iterator. The following
|
||||||
examples, let's assume to have composite type:
|
examples assume we have composite type:
|
||||||
|
|
||||||
<programlisting>
|
<programlisting>
|
||||||
CREATE TYPE greeting AS (
|
CREATE TYPE greeting AS (
|
||||||
@ -272,10 +270,11 @@ CREATE TYPE greeting AS (
|
|||||||
);
|
);
|
||||||
</programlisting>
|
</programlisting>
|
||||||
|
|
||||||
Currently known iterable types are:
|
A set result can be returned from a:
|
||||||
|
|
||||||
<variablelist>
|
<variablelist>
|
||||||
<varlistentry>
|
<varlistentry>
|
||||||
<term>Sequence types (tuple, list, set)</term>
|
<term>Sequence type (tuple, list, set)</term>
|
||||||
<listitem>
|
<listitem>
|
||||||
<para>
|
<para>
|
||||||
<programlisting>
|
<programlisting>
|
||||||
@ -341,9 +340,10 @@ $$ LANGUAGE plpythonu;
|
|||||||
<ulink url="http://sourceforge.net/tracker/index.php?func=detail&aid=1483133&group_id=5470&atid=105470">bug #1483133</ulink>,
|
<ulink url="http://sourceforge.net/tracker/index.php?func=detail&aid=1483133&group_id=5470&atid=105470">bug #1483133</ulink>,
|
||||||
some debug versions of Python 2.4
|
some debug versions of Python 2.4
|
||||||
(configured and compiled with option <literal>--with-pydebug</literal>)
|
(configured and compiled with option <literal>--with-pydebug</literal>)
|
||||||
are known to crash the <productname>PostgreSQL</productname> server.
|
are known to crash the <productname>PostgreSQL</productname> server
|
||||||
|
when using an iterator to return a set result.
|
||||||
Unpatched versions of Fedora 4 contain this bug.
|
Unpatched versions of Fedora 4 contain this bug.
|
||||||
It does not happen in production version of Python or on patched
|
It does not happen in production versions of Python or on patched
|
||||||
versions of Fedora 4.
|
versions of Fedora 4.
|
||||||
</para>
|
</para>
|
||||||
</warning>
|
</warning>
|
||||||
@ -351,9 +351,6 @@ $$ LANGUAGE plpythonu;
|
|||||||
</listitem>
|
</listitem>
|
||||||
</varlistentry>
|
</varlistentry>
|
||||||
</variablelist>
|
</variablelist>
|
||||||
|
|
||||||
Whenever new iterable types are added to Python language,
|
|
||||||
<application>PL/Python</application> is ready to use it.
|
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
<para>
|
<para>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user