# 46.3.数据值

46.3.1. 数据类型映射

46.3.2. 空,无

46.3.3. 数组、列表

46.3.4. 复合类型

46.3.5. 设置返回函数

一般来说,PL/Python的目标是提供PostgreSQL和Python世界之间的“自然”映射。这将通知下面描述的数据映射规则。

# 46.3.1.数据类型映射

调用PL/Python函数时,其参数将从PostgreSQL数据类型转换为相应的Python类型:

  • PostgreSQL布尔值已转换为Python布尔.

  • PostgreSQL短整型智力转换为Python智力.PostgreSQL比基特老年人转化为长的在Python2和to中智力在Python 3中。

  • PostgreSQL 真实的双重的转换为Python浮动.

  • PostgreSQL 数字的已转换为Python十进制的.此类型是从cdecimal如果可以的话,打包。否则十进制的十进制的将使用标准库中的。cdecimal速度明显快于十进制的.然而,在Python 3.3及更高版本中,cdecimal已以名称集成到标准库中十进制的,因此不再有任何区别。

  • PostgreSQL 二进制数据已转换为Pythonstr在Python2和to中字节在Python 3中。在Python2中,字符串应该被视为一个字节序列,没有任何字符编码。

  • 所有其他数据类型,包括PostgreSQL字符串类型,都转换为Pythonstr.在Python 2中,该字符串将采用PostgreSQL server编码;在Python3中,它将是一个Unicode字符串,就像所有字符串一样。

  • 有关非标度数据类型,请参见下文。

    PL/Python函数返回时,其返回值将转换为函数声明的PostgreSQL返回数据类型,如下所示:

  • 当PostgreSQL返回类型为布尔值,返回值将根据Python规则。也就是说,0和空字符串为false,但值得注意的是“f”这是真的。

  • 当PostgreSQL返回类型为二进制数据,返回值将使用相应的Python内置程序转换为字符串(Python 2)或字节(Python 3),并将结果转换为二进制数据.

  • 对于所有其他PostgreSQL返回类型,使用Python内置函数将返回值转换为字符串str,并将结果传递给PostgreSQL数据类型的输入函数。(如果Python值是浮动,使用报告内置而不是str,以避免精度损失。)

    在将Python 2中的字符串传递给PostgreSQL时,它们必须在PostgreSQL服务器编码中。在当前服务器编码中无效的字符串将引发错误,但并非所有编码不匹配都能被检测到,因此,如果编码不正确,仍然会产生垃圾数据。Unicode字符串会自动转换为正确的编码,因此使用这些字符串更安全、更方便。在Python 3中,所有字符串都是Unicode字符串。

  • 有关非标度数据类型,请参见下文。

    注意,声明的PostgreSQL返回类型和实际返回对象的Python数据类型之间的逻辑不匹配没有标记;该值在任何情况下都将被转换。

# 46.3.2.空,无

如果一个SQL空值传递给函数时,参数值将显示为没有一个在Python中。例如,函数定义皮马克斯如所示第46.2节将返回空输入的错误答案。我们可以加上严格的对函数定义进行修改,使PostgreSQL做一些更合理的事情:如果传递了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空值,请返回没有一个.无论函数是否严格,都可以这样做。

# 46.3.3.数组、列表

SQL数组值作为Python列表传递到PL/Python中。要从PL/Python函数中返回SQL数组值,请返回Python列表:

CREATE FUNCTION return_arr()
  RETURNS int[]
AS $$
return [1, 2, 3, 4, 5]
$$ LANGUAGE plpythonu;

SELECT return_arr();
 return_arr  
### 46.3.4. Composite Types

 Composite-type arguments are passed to the function as Python mappings. The element names of the mapping are the attribute names of the composite type. If an attribute in the passed row has the null value, it has the value `None` in the mapping. Here is an example:

创建表employee(姓名文本、薪资整数、年龄整数);

CREATE函数overpaid(e employee)返回布尔值为$$if e[“薪水”"]>200000:如果(e)返回True[“年龄”"]<30)和(e)[“薪水”"]>100000):返回真返回假$$LANGUAGE plpythonu;

 There are multiple ways to return row or composite types from a Python function. The following examples assume we have:

创建名为_value AS(name text,value integer)的类型;

 A composite result can be returned as a:

Sequence type (a tuple or list, but not a set because it is not indexable)

 Returned sequence objects must have the same number of items as the composite result type has fields. The item with index 0 is assigned to the first field of the composite type, 1 to the second and so on. For example:

CREATE函数make_pair(name text,value integer)以$$return(name,value)的形式返回名为_的值

# 或者,作为列表:return[名字,价值,]

$$LANGUAGE plpythonu;

 To return an SQL null for any column, insert `None` at the corresponding position.

 When an array of composite types is returned, it cannot be returned as a list, because it is ambiguous whether the Python list represents a composite type, or another array dimension.

Mapping (dictionary)

 The value for each result type column is retrieved from the mapping with the column name as key. Example:

CREATE函数make_pair(name text,value integer)以$$return{“name”:name,“value”:value}$$LANGUAGE plpythonu的形式返回命名的_值;

 Any extra dictionary key/value pairs are ignored. Missing keys are treated as errors. To return an SQL null value for any column, insert `None` with the corresponding column name as the key.

Object (any object providing method `__getattr__`)

 This works the same as a mapping. Example:

CREATE函数make_pair(name text,value integer)以$$class name_value:def的形式返回命名的_值初始化(self,n,v):self。name=n self。value=v返回命名的_值(名称、值)

# 或者干脆

nv级:通过nv级。name=name-nv。value=返回值nv$$LANGUAGE plpythonu;

 Functions with `OUT` parameters are also supported. For example:

将函数multiout_simple(OUT i integer,OUT j integer)创建为$$return(1,2)$$LANGUAGE plpythonu;

从multiout_simple()中选择*;

 Output parameters of procedures are passed back the same way. For example:

将过程python_triple(INOUT a integer,INOUT b integer)创建为$$return(a3,b3) $$LANGUAGE plpythonu;

调用python_triple(5,10);

### 46.3.5. Set-Returning Functions

 A PL/Python function can also return sets of scalar or composite types. There are several ways to achieve this because the returned object is internally turned into an iterator. The following examples assume we have composite type:

创建类型问候语(如何发送文本、谁发送文本);

 A set result can be returned from a:

Sequence type (tuple, list, set)

CREATE函数greet(how text)返回一组问候语作为$$

# 返回包含列表作为复合类型的元组

# 所有其他组合也适用

报税表([怎么说,“世界”,], [如何“PostgreSQL”,], [怎么说,“PL,Python”"])$$LANGUAGE plpythonu;

Iterator (any object providing `__iter__` and `next` methods)

CREATE函数greet(how text)以$$class producer:def的形式返回问候语集初始化(self,how,who):self。how=如何自我。谁=谁的自我。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] )

回归制作人(如何,[“世界”、“PostgreSQL”、“PL"Python”",])$$LANGUAGE plpythonu;

Generator (`yield`)

CREATE函数greet(how text)将问候语集作为$$返回给[“世界”、“PostgreSQL”、“PL"Python”",]:产量(如何,谁)$$LANGUAGE plpythonu;

 Set-returning functions with `OUT` parameters (using `RETURNS SETOF record`) are also supported. For example:

创建函数multiout_simple_setof(n integer,OUT integer,OUT integer)以$$return的形式返回记录集[(1, 2)]*n$$LANGUAGE plpythonu;

从多输出\简单\集合(3)中选择*;