On this page
46.10. Util 功能
plpy
模块还提供功能
plpy.debug(msg, **kwargs) |
plpy.log(msg, **kwargs) |
plpy.info(msg, **kwargs) |
plpy.notice(msg, **kwargs) |
plpy.warning(msg, **kwargs) |
plpy.error(msg, **kwargs) |
plpy.fatal(msg, **kwargs) |
plpy.error
和plpy.fatal
实际上引发了一个 Python 异常,如果未被捕获,则会传播到调用查询,从而导致当前事务或子事务中止。 raise plpy.Error(msg)
和raise plpy.Fatal(msg)
等效于分别调用plpy.error(msg)
和plpy.fatal(msg)
,但是raise
形式不允许传递关键字参数。其他功能仅生成不同优先级的消息。由log_min_messages和client_min_messages配置变量控制是否向 Client 端报告特定优先级的消息,向服务器日志写入消息还是向服务器日志写入消息。有关更多信息,请参见Chapter 19。
msg
*参数作为位置参数给出。为了向后兼容,可以给出多个位置参数。在这种情况下,位置参数 Tuples 的字符串表示形式将成为报告给 Client 端的消息。
接受以下仅关键字参数:
detail |
hint |
sqlstate |
schema_name |
table_name |
column_name |
datatype_name |
constraint_name |
作为仅关键字参数传递的对象的字符串表示形式用于丰富报告给 Client 端的消息。例如:
CREATE FUNCTION raise_custom_exception() RETURNS void AS $$
plpy.error("custom exception message",
detail="some info about exception",
hint="hint for users")
$$ LANGUAGE plpythonu;
=# SELECT raise_custom_exception();
ERROR: plpy.Error: custom exception message
DETAIL: some info about exception
HINT: hint for users
CONTEXT: Traceback (most recent call last):
PL/Python function "raise_custom_exception", line 4, in <module>
hint="hint for users")
PL/Python function "raise_custom_exception"
另一套 Util 功能是plpy.quote_literal(string)
,plpy.quote_nullable(string)
和plpy.quote_ident(string)
。它们等效于Section 9.4中描述的内置报价功能。在构造临时查询时,它们很有用。来自Example 43.1的动态 SQL 的 PL/Python 等效项为:
plpy.execute("UPDATE tbl SET %s = %s WHERE key = %s" % (
plpy.quote_ident(colname),
plpy.quote_nullable(newvalue),
plpy.quote_literal(keyvalue)))