=> CREATE OR REPLACE FUNCTION get_catalog(author_name text, book_title text, in_stock boolean)
RETURNS TABLE(book_id integer, display_name text, onhand_qty integer)
AS $$
DECLARE
title_cond text := '';
author_cond text := '';
qty_cond text := '';
BEGIN
IF book_title != '' THEN
title_cond := format(' AND cv.title ILIKE %L', '%'||coalesce(book_title,'')||'%');
END IF;
IF author_name != '' THEN
author_cond := format(' AND cv.authors ILIKE %L', '%'||coalesce(author_name,'')||'%');
END IF;
IF in_stock THEN
qty_cond := ' AND cv.onhand_qty > 0';
END IF;
RETURN QUERY EXECUTE '
SELECT cv.book_id,
cv.display_name,
cv.onhand_qty
FROM catalog_v cv
WHERE true'
|| title_cond || author_cond || qty_cond || '
ORDER BY display_name';
END;
$$ STABLE LANGUAGE plpgsql;