Postgres:带有可选“WHERE”参数的函数
Postgres: Function with optional “WHERE” parameters
我在 postgres 中创建了下一个函数:
create or replace FUNCTION public.myFunction(language integer, app_type integer, user uuid, level integer default null, status integer default null) RETURNS SETOF json
LANGUAGE plpgsql STABLE STRICT SECURITY DEFINER
AS $_$
begin
return query
select json_build_object(
'rowId', p.id,
'projectId', p.short_id,
'name', p.name,
'level', pd.level, )
from public.project p join types t on p.type = t.type
join public.project_data pd on pd.project = p.id
where p.language = and ( IS NULL OR p.status = ) and p.type =
and ( IS NULL OR pd.level = ) ;
end;
$_$;
目标是创建 运行 带有选项 where 参数的函数。因此,如果状态已通过且级别已通过两个参数的 运行。否则,运行 带有状态或级别(如果通过)或 运行 根本没有这些参数。
但是,这些子句不起作用 ( IS NULL OR pd.level = )
、( IS NULL OR p.status = )
这个想法来自
不得不说状态和级别是“枚举表”。
我找到了使用相同方法的此类 postgres 函数的其他示例,但看起来这种方法对我不起作用。
我的结果总是空节点
任何建议..
要么尝试删除 STRICT
子句,要么在参数列表中禁止 NULL
值。
我在 postgres 中创建了下一个函数:
create or replace FUNCTION public.myFunction(language integer, app_type integer, user uuid, level integer default null, status integer default null) RETURNS SETOF json
LANGUAGE plpgsql STABLE STRICT SECURITY DEFINER
AS $_$
begin
return query
select json_build_object(
'rowId', p.id,
'projectId', p.short_id,
'name', p.name,
'level', pd.level, )
from public.project p join types t on p.type = t.type
join public.project_data pd on pd.project = p.id
where p.language = and ( IS NULL OR p.status = ) and p.type =
and ( IS NULL OR pd.level = ) ;
end;
$_$;
目标是创建 运行 带有选项 where 参数的函数。因此,如果状态已通过且级别已通过两个参数的 运行。否则,运行 带有状态或级别(如果通过)或 运行 根本没有这些参数。
但是,这些子句不起作用 ( IS NULL OR pd.level = )
、( IS NULL OR p.status = )
这个想法来自
不得不说状态和级别是“枚举表”。
我找到了使用相同方法的此类 postgres 函数的其他示例,但看起来这种方法对我不起作用。
我的结果总是空节点
任何建议..
要么尝试删除 STRICT
子句,要么在参数列表中禁止 NULL
值。