bool_plperl 是一个 contrib 模块,为 PL/Perl 提供了一个布尔 转换。
bool_plperl 在 PostgreSQL 14 中引入。
模式
bool_plperl 将被安装到当前模式,或者使用 CREATE EXTENSION ... SCHEMA ... 指定的模式。
变更历史
- PostgreSQL 14
- 添加(提交 36058a3c)
示例
用法示例
postgres=# CREATE EXTENSION bool_plperl CASCADE; NOTICE: installing required extension "plperl" CREATE EXTENSION postgres=# CREATE FUNCTION hello_bool(bool) RETURNS TEXT TRANSFORM FOR TYPE bool LANGUAGE plperl AS $$ my $with_world = shift; elog(INFO, "with_world: '$with_world'");
return sprintf('hello%s', $with_world ? ' world' : ''); $$; CREATE FUNCTION postgres=# SELECT hello_bool(true), hello_bool(false);
INFO: with_world: '1'
INFO: with_world: '' hello_bool | hello_bool -------------+------------ hello world | hello (1 row)
如果不使用转换,等效函数会将 FALSE 值作为字面量 'f' 传递,(除非由函数显式转换)否则不会按常规方式解释为布尔值。
postgres=# CREATE OR REPLACE FUNCTION hello_nobool(bool)
RETURNS TEXT
LANGUAGE plperl
AS $$
my $with_world = shift;
elog(INFO, "with_world: '$with_world'");
return sprintf('hello%s', $with_world ? ' world' : '');
$$;
CREATE FUNCTION
postgres=# SELECT hello_nobool(true), hello_nobool(false);
INFO: with_world: 't'
INFO: with_world: 'f'
hello_nobool | hello_nobool
--------------+--------------
hello world | hello world
(1 row)
参考资料
- PostgreSQL 文档: PL/Perl 函数和参数
