¾�ΥС�������ʸ�� �� 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 9.6 | 9.5 | 9.4 | 9.3 | 9.2 | 9.1 | 9.0 | 8.4 | 8.3 | 8.2 | 8.1 | 8.0 | 7.4 | 7.3 | 7.2

41.1. PL/Python�ؿ�

PL/Python�Ǻ������줿�ؿ���ɸ���CREATE FUNCTION��ʸ���������ޤ���

CREATE FUNCTION funcname (argument-list)
  RETURNS return-type
AS $$
  # PL/Python function body
$$ LANGUAGE plpythonu;

�ؿ����Τ�ñ�ʤ�Python������ץȤǤ��� �ؿ����ƤӽФ����ȡ�������args[]��������ǤȤ��ơ��ޤ���̾���դ��ΰ������̾���ѿ��Ȥ���Python������ץȤ��Ϥ���ޤ��� ��̤ϡ�Python�����ɤ����̾����ˡ��return�ޤ���yield�ʷ�̥��å�ʸ�ξ��ˤ��֤�����ΤǤ���

���Ȥ��С�2�Ĥ����������礭�ʿ����֤��ؿ��ϰʲ��Τ褦��������뤳�Ȥ��Ǥ��ޤ���

CREATE FUNCTION pymax (a integer, b integer)
  RETURNS integer
AS $$
  if a > b:
    return a
  return b
$$ LANGUAGE plpythonu;

�ؿ���������ΤȤ����󶡤��줿Python�Υ����ɤ�Python�δؿ����Ѵ�����ޤ��� �㤨�о����ϰʲ��Τ褦�ˤʤ�ޤ���

def __plpython_procedure_pymax_23456():
  if a > b:
    return a
  return b

�����ǡ�23456��PostgreSQL�ˤ�������Ƥ�줿���δؿ���OID�Ǥ���

PostgreSQL�ؿ��ѥ�᡼����args�������Х�ꥹ�Ȥ������Ѳ�ǽ�Ǥ��� pymax����ˤ���ȡ�args[0]�ˤϺǽ�ΰ����Ȥ����Ϥ��줿��Τ��ޤޤ졢args[1]�ˤ�2���ܤΰ������ͤ��ޤޤ�ޤ��� �̤���ˡ�Ȥ��ơ������Τ褦��̾���դ��ѥ�᡼������Ѥ��뤳�Ȥ��Ǥ��ޤ��� ̾���դ��ѥ�᡼������Ѥ��뤳�Ȥ��̾�������������ޤ���

SQL��NULL�ͤ��ؿ����Ϥ����ȡ����ΰ����ͤ�Python.�Ǥ�None�Ȥʤ�ޤ��� ��δؿ�����Ǥϡ�NULL���ϤǤϴְ�ä���̤��֤���ޤ��� �ؿ������STRICT����Ϳ����PostgreSQL��NULL�ͤ��Ϥ��줿���ˤ��δؿ���ƤӽФ�������ưŪ��ñ��NULL��̤��֤��Ȥ������������Ū��ư����뤳�Ȥ��Ǥ��ޤ��� ¾�ˡ��ؿ����Τ�NULL���Ϥ򸡺����뤳�Ȥ�Ǥ��ޤ���

CREATE FUNCTION pymax (a integer, b integer)
  RETURNS integer
AS $$
  if (a is None) or (b is None):
    return None
  if a > b:
    return a
  return b
$$ LANGUAGE plpythonu;

��Ǽ������褦�ˡ�PL/Python�ؿ�����SQL NULL�ͤ��֤��ˤϡ�None�Ȥ����ͤ��֤��Ƥ��������� �ؿ���̩�Ȥ������Ǥ⸷̩�Ȥ��ʤ����Ǥ⡢�����Ԥ����Ȥ��Ǥ��ޤ���

ʣ�緿�ΰ�����Python�ΥޥåפȤ����Ϥ���ޤ��� �ޥåפ�����̾��ʣ�緿��°��̾�Ǥ��� �Ϥ��줿�Ԥ�°���ͤ�NULL�ξ�硢�ޥå׾�Ǥ�None�Ȥ����ͤȤʤ�ޤ��� �ʲ�����򼨤��ޤ���

CREATE TABLE employee (
  name text,
  salary integer,
  age integer
);

CREATE FUNCTION overpaid (e employee)
  RETURNS boolean
AS $$
  if e["salary"] > 200000:
    return True
  if (e["age"] < 30) and (e["salary"] > 100000):
    return True
  return False
$$ LANGUAGE plpythonu;

Python�ؿ�����Ԥޤ���ʣ�緿���֤���ˡ��ʣ��¸�ߤ��ޤ��� �ʲ�����Ǥ�

CREATE TYPE named_value AS (
  name   text,
  value  integer
);

������Ȥ��ޤ��� ʣ�緿�η�̤ϰʲ��Τ褦���֤���ޤ���

������ʥ��ץ�ޤ��ϥꥹ�ȡˤ�����set ���Բġʥ���ǥå��դ����Ǥ��ʤ������

�֤���������֥������Ȥϡ���̤�ʣ�緿�����ĥե�����ɤ�Ʊ�����ܿ���⤿�ʤ���Фʤ�ޤ��� 0�Ȥ�������ǥå����ι��ܤ�ʣ�緿�κǽ�Υե�����ɡ�1�����Υե�����ɡ������Ȥʤ�ޤ��� �ʲ�����򼨤��ޤ���

CREATE FUNCTION make_pair (name text, value integer)
  RETURNS named_value
AS $$
  return [ name, value ]
  # or alternatively, as tuple: return ( name, value )
$$ LANGUAGE plpythonu;

Ǥ�դ����SQL NULL�ͤ��֤��ˤϡ��б�������֤�None���������ޤ���

�ޥåסʼ����

��̷�������ͤϡ���̾�򥭡��Ȥ��ƻ��ĥޥåפ�����Ф���ޤ��� �ʲ�����򼨤��ޤ���

CREATE FUNCTION make_pair (name text, value integer)
  RETURNS named_value
AS $$
  return { "name": name, "value": value }
$$ LANGUAGE plpythonu;

;�פʼ���Υ������ͤ��Ȥ߹�碌��̵�뤵��ޤ��� ¸�ߤ��ʤ������ϥ��顼�Ȥ��ư����ޤ��� Ǥ�դ����SQL NULL���֤�����ˤϡ��б�������̾�򥭡��Ȥ���None���������Ƥ���������

���֥������ȡ�__getattr__�᥽�åɤ��󶡤���Ǥ�դΥ��֥������ȡ�

����ϥޥåפ�Ʊ���褦��ư��ޤ��� �ʲ�����򼨤��ޤ���

CREATE FUNCTION make_pair (name text, value integer)
  RETURNS named_value
AS $$
  class named_value:
    def __init__ (self, n, v):
      self.name = n
      self.value = v
  return named_value(name, value)

  # or simply
  class nv: pass
  nv.name = name
  nv.value = value
  return nv
$$ LANGUAGE plpythonu;

����ͤ��Ѱդ��Ƥ��ʤ���硢Python�ϥǥե���Ȥ�None���֤��ޤ��� PL/Python�ϡ�Python��None��SQL��NULL�ͤ��Ѵ����ޤ���

�ޤ���PL/Python�ؿ��ϥ�����ޤ���ʣ�緿�ν�����֤����Ȥ�Ǥ��ޤ��� �֤���륪�֥������Ȥ�����Ū�˥��ƥ졼�����Ѵ�����뤿�ᡢʣ���μ¸���ˡ������ޤ��� �ʲ�����Ǥϡ��ʲ���ʣ�緿��¸�ߤ��뤳�Ȥ��ꤷ�ޤ���

CREATE TYPE greeting AS (
  how text,
  who text
);

����Ȥ�����̤ϰʲ������֤���ޤ���

������ʥ��ץ롢�ꥹ�ȡ����åȡ�

CREATE FUNCTION greet (how text)
  RETURNS SETOF greeting
AS $$
  # return tuple containing lists as composite types
  # all other combinations work also
  return ( [ how, "World" ], [ how, "PostgreSQL" ], [ how, "PL/Python" ] )
$$ LANGUAGE plpythonu;

���ƥ졼����__iter__�᥽�åɤ�next�᥽�åɤ��󶡤���Ǥ�դΥ��֥������ȡ�

CREATE FUNCTION greet (how text)
  RETURNS SETOF greeting
AS $$
  class producer:
    def __init__ (self, how, who):
      self.how = how
      self.who = who
      self.ndx = -1

    def __iter__ (self):
      return self

    def next (self):
      self.ndx += 1
      if self.ndx == len(self.who):
        raise StopIteration
      return ( self.how, self.who[self.ndx] )

  return producer(how, [ "World", "PostgreSQL", "PL/Python" ])
$$ LANGUAGE plpythonu;

�����ͥ졼��(yield)

CREATE FUNCTION greet (how text)
  RETURNS SETOF greeting
AS $$
  for who in [ "World", "PostgreSQL", "PL/Python" ]:
    yield ( how, who )
$$ LANGUAGE plpythonu;

警告

�������Ǥϡ�Python bug #1483133�Τ��ᡢ������Python 2.4�ǥХå��ǡ�--with-pydebug���ץ�����դ�������/����ѥ��뤵�줿Python�ˤ��������̤��֤�����˥��ƥ졼������Ѥ������PostgreSQL�����Ф򥯥�å��夵���뤳�Ȥ��狼�äƤ��ޤ��� ̤�ѥå���Fedora 4�ˤϤ����Զ�礬����ޤ��� Python�����Ǥ�ѥå�Ŭ�ѺѤߤ�Fedora 4�ǤϤ�������ϵ�����ޤ���

�������Х��SD����ϡ��ؿ��ƤӽФ��֤Υǡ�����¸�Τ���˻��Ѥ��뤳�Ȥ��Ǥ��ޤ��� �����ѿ��ϥץ饤�١��Ȥ���Ū�ǡ����Ǥ��� �������Х��GD����ϡ���ͭ�ǡ����Ǥ��ꡢ���å����������Ƥ�Python�ؿ��ǻ��Ѥ��뤳�Ȥ��Ǥ��ޤ��� ���դ��ƻ��Ѥ��Ƥ���������

�ƴؿ��ϡ�Python���󥿥ץ꥿��Ǽ��Ȥμ¹ԴĶ������ꤷ�ޤ��� ���Τ��ᡢmyfunc�ˤ�륰�����Х�ǡ����ȴؿ��ΰ�����myfunc2������Ѥ��뤳�ȤϤǤ��ޤ��� �嵭�����������̤ꡢGD������Υǡ������㳰�Ǥ���