sql - Conditionally perform SQL statements in a raw postgresql function?
问题描述
I'm not exactly clear on whether or not it's possible to conditionally perform SQL statements using raw SQL
functions in Postgres, or if you need to use plpgsql
with IF
statements for that.
Basically, what I'm trying to do is this:
CREATE OR REPLACE FUNCTION public.usp_reply_to_convo(convo_id BIGINT, person_id INTEGER, body TEXT)
RETURNS TABLE(convo_reply_id BIGINT, person_id integer)
LANGUAGE sql
AS $function$
-- SELECT timeout_until FROM person WHERE person_id = $1;
-- IF timeout_until > now() RETURN timeout_until
-- ELSE do the below
INSERT INTO convo_reply (convo_id, person_id, body) VALUES ($1, $2, $3);
RETURNING convo_reply_id, person_id;
$function$;
Where the user can reply to a conversation if their timeout_until
value is less than now()
. And if it isn't, then the query isn't executed, and instead their timeout value is returned.
Is this sort of thing possible with raw SQL, or is plpgsql
necessary?
Also, is there a way to handle the fact that this function can return two things? Either it returns the timeout timestamp, or the conversation reply id. Not sure how to handle these competing concerns.
解决方案
I think you can do what you want with CTEs. You can phrase this something like this:
with p as (
select timeout_until
from person
where person_id = $1
),
i as (
insert into convo_reply (convo_id, person_id, body)
select $1, $2, $3
where (select timeout_until from p) <= now()
returning convo_reply_id, person_id
)
select ?
from p left join
i
on 1=1;
推荐阅读
- xml - 将 XML 文件保存到 HDFS
- vb.net - 空条件运算符不起作用(显示“调用不是 xxx 的成员”)
- sql - 如果行不符合要求,如何分配值 0
- web - 有关 Microsoft Power BI Embedded 的一般问题
- compilation - 混合编译过程的库路径
- javascript - 填写表单字段后如何使弹出窗口出现?
- webdriver-io - Appium - Android - 错误:生成 appium ENOENT
- autodesk-viewer - 何时加载自定义全景扩展
- python - 如何打印从今天起接下来的 3 个日期?
- c# - 无法使用正则表达式模式进行多次匹配。模式视为单个匹配