0

我有一堆用于简单插入的函数(或过程),例如: insert_user( email );

它们中的大多数都返回inserted_id,无论是用于其他过程还是驱动程序。而有些则没有,比如插入到没有 id 的多对多表中。

PG 存储过程的参数似乎以 SQL 标准方式处理,而不像 PG 函数。它们不是可选的/可默认的,因此每次不需要 id 时都会以结尾的 null 结尾,例如:

call insert_user('test@email', null);

那感觉很笨拙。如果我可以重载它,那就太棒了,但令人惊讶的是,你不能使用 out 参数,即使它们是签名的必需部分。我可以为每个插入创建第二个过程,例如:

call insert_user( 'test@email' );
call insert_user_return_id( 'test@email', out_id);

但后来我有双倍的过程来测试和维护。我可以切换到函数,它不需要 out_id 的额外参数,如果我不想要它可以忽略 out_id。但是“插入”函数必须以 SELECT ex 开头:

select insert_user( 'test@email' );
select insert_user( 'test@email' ) into out_id;
select id from insert_user( 'test@email' );

关于“选择插入”的一些东西让人感觉不舒服。尤其是返回 void 的多对多插入。任何想法或建议将不胜感激!

4

0 回答 0