SET PATH *LIBL ; 

CREATE OR REPLACE FUNCTION CISTOOLS.GET_DATABASE_OBJECTS ( 
	INSCHEMA VARCHAR(128) ) 
	RETURNS TABLE ( 
	SQL_NAME VARCHAR(128) , 
	SYSTEM_NAME VARCHAR(128) , 
	SQL_OBJECT_TYPE VARCHAR(128) , 
	OBJECT_OWNER VARCHAR(128) , 
	OBJECT_DESCRIPTION VARCHAR(128) )   
	LANGUAGE SQL 
	SPECIFIC CISTOOLS.GET_DATABASE_OBJECTS 
	NOT DETERMINISTIC 
	READS SQL DATA 
	CALLED ON NULL INPUT 
	SET OPTION  ALWBLK = *ALLREAD , 
	ALWCPYDTA = *OPTIMIZE , 
	COMMIT = *NONE , 
	DECRESULT = (31, 31, 00) , 
	DYNDFTCOL = *NO , 
	DYNUSRPRF = *USER , 
	SRTSEQ = *HEX   
	RETURN WITH AITV AS ( 
SELECT F . DBXLFI , F . DBXFIL , F . DBXATR , F . DBXREL , F . DBXOWN , F . DBXTXT , T . DBXATR TATR 
FROM QSYS . QADBXFIL F 
LEFT OUTER JOIN QSYS . QADBXFIL T 
ON ( F . DBXALB = T . DBXLB2 AND 
( F . DBXAFL = T . DBXLFI OR 
F . DBXAFL = T . DBXFIL ) ) 
WHERE F . DBXLB2 = INSCHEMA AND 
F . DBXATR IN ( 'AL' , 'IX' , 'LF' , 'MQ' , 'PF' , 'TB' , 'VW' ) AND 
F . DBXREL = 'Y' 
) SELECT QSYS2 . DELIMIT_NAME ( AITV . DBXLFI ) "Name" , AITV . DBXFIL "System Name" , 
CASE 
WHEN AITV . DBXATR = 'AL' THEN 
CASE 
WHEN AITV . TATR IN ( 'TB' , 'MQ' , 'PF' ) THEN 'Alias (Table)' 
WHEN AITV . TATR IN ( 'VW' , 'LF' ) THEN 'Alias (View)' 
ELSE 'Alias' 
END 
WHEN AITV . DBXATR = 'IX' THEN 'Index' 
WHEN AITV . DBXATR IN ( 'TB' , 'PF' ) THEN 'Table' 
WHEN AITV . DBXATR = 'MQ' THEN 'Table (Materialized Query)' 
WHEN AITV . DBXATR IN ( 'VW' , 'LF' ) THEN 'View' 
ELSE AITV . DBXATR 
END "Type" , AITV . DBXOWN "Owner" , 
CASE 
WHEN AITV . DBXTXT IS NULL THEN '' 
ELSE TRIM ( AITV . DBXTXT ) 
END "Text" 
FROM AITV 
UNION ALL 
SELECT QSYS2 . DELIMIT_NAME ( RELNAME ) "Name" , '' "System Name" , 
CASE TYPE 
WHEN 'CHECK' THEN 'Check Constraint' 
WHEN 'UNIQUE' THEN 'Unique Key Constraint' 
WHEN 'PRIMARY KEY' THEN 'Primary Key Constraint' 
WHEN 'FOREIGN KEY' THEN 'Foreign Key Constraint' 
ELSE TYPE 
END "Type" , '' "Owner" , 
CASE 
WHEN LABEL IS NULL THEN '' 
ELSE TRIM ( LABEL ) 
END "Text" 
FROM QSYS2 . SYSCST 
WHERE CDBNAME = INSCHEMA 
UNION ALL 
SELECT QSYS2 . DELIMIT_NAME ( RTNNAME ) "Name" , QSYS2 . DELIMIT_NAME ( SPECNAME) "System Name" , 
CASE RTNTYPE 
WHEN 'FUNCTION' THEN 'Function' 
WHEN 'PROCEDURE' THEN 'Procedure' 
ELSE RTNTYPE 
END "Type" , '' "Owner" , 
CASE 
WHEN LABEL IS NULL THEN '' 
ELSE TRIM ( LABEL ) 
END "Text" 
FROM QSYS2 . SYSROUTINE 
WHERE RTNSCHEMA = INSCHEMA AND 
ORIGIN <> 'S' 
UNION ALL 
SELECT QSYS2 . DELIMIT_NAME ( TYPENAME ) "Name" , SYSTNAME "System Name" , 
CASE METATYPE 
WHEN 'A' THEN 'Array Type' 
WHEN 'T' THEN 'Distinct Type' 
ELSE 'Type' 
END "Type" , '' "Owner" , 
CASE 
WHEN LABEL IS NULL THEN '' 
ELSE TRIM ( LABEL ) 
END "Text" 
FROM QSYS2 . SYSTYPES 
WHERE TYPESCHEMA = INSCHEMA AND 
METATYPE <> 'S' 
UNION ALL 
SELECT NAME "Name" , SYS_NAME "System Name" , 'SQL Package' "Type" , OWNER "Owner" , 
CASE 
WHEN LABEL IS NULL THEN '' 
ELSE TRIM ( LABEL ) 
END "Text" 
FROM QSYS2 . SYSPACKAGE 
WHERE COLLID = INSCHEMA 
UNION ALL 
SELECT QSYS2 . DELIMIT_NAME ( TRIGNAME ) "Name" , '' "System Name" , 'Trigger' "Type" , '' "Owner" , 
CASE 
WHEN LABEL IS NULL THEN '' 
ELSE TRIM ( LABEL ) 
END "Text" 
FROM QSYS2 . SYSTRIGGERS 
WHERE TRIGSCHEMA = INSCHEMA 
UNION ALL 
SELECT QSYS2 . DELIMIT_NAME ( SEQNAME ) "Name" , '' "System Name" , 'Sequence' "Type" , '' "Owner" , 
CASE 
WHEN LABEL IS NULL THEN '' 
ELSE TRIM ( LABEL ) 
END "Text" 
FROM QSYS2 . SYSSEQUENCES 
WHERE SEQSCHEMA = INSCHEMA 
UNION ALL 
SELECT QSYS2 . DELIMIT_NAME ( VARNAME ) "Name" , SYSVNAME "System Name" , 'Variable' "Type" , OWNER "Owner" , 
CASE 
WHEN LABEL IS NULL THEN '' 
ELSE TRIM ( LABEL ) 
END "Text" 
FROM QSYS2 . SYSVARIABLES 
WHERE VARSCHEMA = INSCHEMA 
UNION ALL 
SELECT OBJNAME "Name" , OBJNAME "System Name" , 
CASE OBJTYPE 
WHEN '*JRN' THEN 'Journal' 
WHEN '*JRNRCV' THEN 'Journal Receiver' 
ELSE OBJTYPE 
END "Type" , OBJOWNER "Owner" , 
CASE 
WHEN OBJTEXT IS NULL THEN '' 
ELSE TRIM ( OBJTEXT ) 
END "Text" 
FROM TABLE ( 
QSYS2 . OBJECT_STATISTICS ( INSCHEMA , '*JRN *JRNRCV' ) 
) OS 
UNION ALL 
SELECT QSYS2 . DELIMIT_NAME ( NAME ) "Name" , '' "System Name" , 
CASE CONTROLTYP 
WHEN 'M' THEN 'Column Mask' 
WHEN 'R' THEN 'Row Permission' 
ELSE CONTROLTYP 
END "Type" , CREATOR "Owner" , 
CASE 
WHEN LABEL IS NULL THEN '' 
ELSE TRIM ( LABEL ) 
END "Text" 
FROM QSYS2 . SYSCONTROLS 
WHERE SCHEMA = INSCHEMA 
UNION ALL 
SELECT QSYS2 . DELIMIT_NAME ( XSRNAME ) "Name" , SYSXNAME "System Name" , 'XML Schema' "Type" , OWNER "Owner" , 
CASE 
WHEN LABEL IS NULL THEN '' 
ELSE TRIM ( LABEL ) 
END "Text" 
FROM QSYS2 . XSROBJECTS 
WHERE XSRSCHEMA = INSCHEMA 
ORDER BY 3 ASC , 1 ASC  ; 

GRANT ALTER , EXECUTE   
ON SPECIFIC FUNCTION CISTOOLS.GET_DATABASE_OBJECTS 
TO AMAPICS WITH GRANT OPTION ; 

GRANT EXECUTE   
ON SPECIFIC FUNCTION CISTOOLS.GET_DATABASE_OBJECTS 
TO PUBLIC ; 

