I'm wondering if I'm handling CASE WHEN appropriately below. I have optional parameters and want to make sure this is the best way to handle them.
Postgres version 9.3.14.
CREATE FUNCTION sp_contacts_get_all_by_id (user_id integer DEFAULT NULL::integer, vendor_id integer DEFAULT NULL::integer, filter_contact_type integer DEFAULT NULL::integer)
RETURNS TABLE(...)
LANGUAGE plpgsql
AS $$
BEGIN
IF($1 IS NULL AND $2 IS NULL) THEN
RAISE EXCEPTION 'userId or vendorId is required!';
END IF;
RETURN QUERY
SELECT
*
FROM
myapp_contacts
JOIN myapp_contacts_assoc ON
myapp_contacts_assoc.contact_id=myapp_contacts.contact_id
WHERE
(CASE WHEN $1 IS NOT NULL THEN myapp_contacts_assoc.user_id=$1 END) AND
(CASE WHEN $2 IS NOT NULL THEN myapp_contacts_assoc.vendor_id=$2 END) AND
myapp_contacts.expired_at IS NULL AND
(CASE WHEN $3 IS NOT NULL THEN myapp_contacts.type=$3 END);
END;
$$
CASEexpressions have a default ofELSE NULL. So when a parameter is null, the CASE expression goes to the default ELSE and the result (of the expression) is NULL=UNKNOWN. And only rows that the WHERE evaluates to TRUE go to the result of a query. – ypercubeᵀᴹ Nov 26 '16 at 21:39myapp_contacts.expired_at IS NULLin the first place or why you removed it). – ypercubeᵀᴹ Nov 26 '16 at 21:40