Files
tis-no-violations/Database/Функции/application_get.sql

42 lines
1.3 KiB
PL/PgSQL

CREATE OR REPLACE FUNCTION nn_db.application_get(
in_pk_user nn_db.user.pk_user%TYPE)
RETURNS TABLE(
pk_application nn_db.application.pk_application%TYPE
, car_number nn_db.application.car_number%TYPE
, description nn_db.application.description%TYPE
, fk_status nn_db.application.fk_status%TYPE
, title nn_db.status.title%TYPE
, fk_user nn_db.application.fk_user%TYPE
, username nn_db.user.username%TYPE
, pk_image nn_db.image.pk_image%TYPE
, file_name nn_db.image.file_name%TYPE
, file_ext nn_db.image.file_ext%TYPE)
AS $$
BEGIN
RETURN QUERY SELECT
nn_db.application.pk_application
, nn_db.application.car_number
, nn_db.application.description
, nn_db.application.fk_status
, nn_db.status.title
, nn_db.application.fk_user
, nn_db.user.username
, nn_db.image.pk_image
, nn_db.image.file_name
, nn_db.image.file_ext
FROM nn_db.application
INNER JOIN nn_db.status
ON nn_db.status.pk_status = nn_db.application.fk_status
INNER JOIN nn_db.user
ON nn_db.user.pk_user = nn_db.application.fk_user
LEFT JOIN nn_db.image
ON nn_db.image.fk_application = nn_db.application.pk_application
WHERE
CASE WHEN in_pk_user IS NULL
THEN nn_db.application.fk_user IN (SELECT nn_db.user.pk_user FROM nn_db.user)
ELSE
nn_db.application.fk_user = in_pk_user
END
ORDER BY nn_db.application.pk_application;
END;
$$ LANGUAGE plpgsql