SET PATH *LIBL ; 

CREATE OR REPLACE FUNCTION SAILPOINT.ES_V2_GET_ALL_MAPICS_ENVIRONMENTS ( ) 
	RETURNS CLOB(2147483647)   
	LANGUAGE SQL 
	SPECIFIC SAILPOINT.V2_ESALLENVS 
	NOT DETERMINISTIC 
	MODIFIES SQL DATA 
	CALLED ON NULL INPUT 
	NOT FENCED 
	SET OPTION  ALWBLK = *ALLREAD , 
	ALWCPYDTA = *OPTIMIZE , 
	COMMIT = *NONE , 
	DBGVIEW = *SOURCE , 
	DECRESULT = (31, 31, 00) , 
	DLYPRP = *NO , 
	DYNDFTCOL = *NO , 
	DYNUSRPRF = *OWNER , 
	SRTSEQ = *HEX   
	BEGIN 
RETURN 
( 
SELECT JSON_OBJECT ( 
'data' : 
JSON_ARRAYAGG ( 
JSON_OBJECT ( 
'envId' VALUE ENVID , 'envDsc' VALUE ENDSC , 
'esInstalled' VALUE ES_INSTALLED 
) 
) , 
'errors' : JSON_ARRAY ( ) 
) 
FROM TABLE ( 
SELECT ENVID , ENDSC , 
CASE 
WHEN FILECOUNT > 0 THEN 'true' 
ELSE 'false' 
END AS ES_INSTALLED 
FROM TABLE ( 
SELECT DISTINCT ENVID , ENDSC , 
COUNT ( CESLIB ) OVER ( 
PARTITION BY ENVID , ENDSC 
) AS FILECOUNT 
FROM TABLE ( 
SELECT LIDES || FIDES AS ENVID , 
'CESLIB' || 
CASE LIDES || FIDES 
WHEN 'MM' THEN '' 
ELSE FIDES 
END AS CESLIB , ENDSC 
FROM QGPL . MMLIST 
WHERE RCDCD = 'M3' 
AND ACREC = 'A' 
) 
JOIN TABLE ( 
QSYS2 . OBJECT_STATISTICS ( 
CESLIB , '*FILE' ) 
) 
ON 1 = 1 
) 
) ) ; 
END  ; 

GRANT ALTER , EXECUTE   
ON SPECIFIC FUNCTION SAILPOINT.V2_ESALLENVS 
TO AMAPICS WITH GRANT OPTION ; 

GRANT EXECUTE   
ON SPECIFIC FUNCTION SAILPOINT.V2_ESALLENVS 
TO PUBLIC ; 

