Tohle mi navrhla ChatGPT, napada nekoho neco lepsiho?
CREATE OR REPLACE FUNCTION get_devices_with_kpi()
RETURNS TABLE (
device_id INTEGER,
device_name VARCHAR(64),
kpi_id INTEGER,
kpi_name VARCHAR(64)
) AS
$$
BEGIN
BEGIN
RETURN QUERY
SELECT d.id AS device_id, d.name AS device_name,
k.id AS kpi_id, k.name AS kpi_name
FROM device d
LEFT JOIN kpi k ON d.id = k.id; -- Adjust the join condition as needed
EXCEPTION
WHEN SQLSTATE '08006' THEN -- Exception for network errors
-- Return left joined nulls as if "kpi" is empty
RETURN QUERY
SELECT d.id AS device_id, d.name AS device_name,
NULL AS kpi_id, NULL AS kpi_name
FROM device d;
END;
END;
$$ LANGUAGE plpgsql;