PostgreSQL编程与安全:从函数到动态SQL及认证
1. 函数类型与返回值
在PostgreSQL中,PL/pgSQL和SQL函数可以返回不同类型的值。一些开发者将返回单行单列标量变量的函数称为标量函数,其返回类型可以是基本类型、复合类型、域类型、伪类型或域数据类型。
例如,下面的函数返回某个账户的JSON表示:
CREATE OR REPLACE FUNCTION car_portal_app.get_account_in_json (account_id INT) RETURNS JSON AS
$$
SELECT row_to_json(account) FROM car_portal_app.account WHERE
account_id = $1;
$$
LANGUAGE SQL;
测试代码如下:
WITH inserted_account AS (
INSERT into car_portal_app.account VALUES (DEFAULT,
'first_name', 'last_name', '[email protected]','some_pass')
RETURNING account_id
) SELECT car_portal_app.get_account_in_json (account_id) FROM inserted_account;
结果示例:
"{"acc