SET PATH *LIBL ; 

CREATE OR REPLACE FUNCTION CISTOOLS.GENERATE_JSON ( 
	SQLSTMT VARCHAR(32000) , 
	CAMELCASELABELS SMALLINT DEFAULT  0  ) 
	RETURNS CLOB(2147483647)   
	LANGUAGE SQL 
	SPECIFIC CISTOOLS.GENERATE_JSON 
	NOT DETERMINISTIC 
	MODIFIES SQL DATA 
	CALLED ON NULL INPUT 
	NO EXTERNAL ACTION 
	NOT FENCED 
	CONCURRENT ACCESS RESOLUTION DEFAULT 
	SET OPTION  ALWBLK = *ALLREAD , 
	ALWCPYDTA = *OPTIMIZE , 
	COMMIT = *NONE , 
	DBGVIEW = *SOURCE , 
	DECRESULT = (31, 31, 00) , 
	DYNDFTCOL = *NO , 
	DYNUSRPRF = *OWNER , 
	LANGID = ENU , 
	OPTLOB = *YES , 
	SQLCURRULE = *STD , 
	SRTSEQ = *LANGIDSHR   
	BEGIN 
DECLARE COLUMN_LIST CLOB ( 2 M ) DEFAULT ' ' ; 
DECLARE JSON_SQL CLOB ( 3 M ) DEFAULT ' ' ; 
DECLARE JSON_DATA CLOB ( 2 G ) DEFAULT '' ; 
DECLARE ERROR_JSON VARCHAR ( 32000 ) DEFAULT '' ; 
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION 
BEGIN 
DECLARE V_SQLSTATE CHAR ( 5 ) ; 
DECLARE V_DB2_RETURN_CODE INT ; 
DECLARE V_DB2_MESSAGE_ID CHAR ( 7 ) ; 
DECLARE V_ERROR_MESSAGE CLOB ( 2048 ) ; 
GET DIAGNOSTICS CONDITION 1 
V_DB2_RETURN_CODE = DB2_RETURNED_SQLCODE , 
V_SQLSTATE = RETURNED_SQLSTATE , 
V_ERROR_MESSAGE = MESSAGE_TEXT , 
V_DB2_MESSAGE_ID = DB2_MESSAGE_ID ; 
RETURN 
JSON_OBJECT ( 
'success' VALUE 'false' , 'SQLInfo' VALUE JSON_OBJECT ( 
'SQLState' VALUE V_SQLSTATE , 
'SQLCode' VALUE V_DB2_RETURN_CODE , 
'messageID' VALUE V_DB2_MESSAGE_ID , 
'message' VALUE V_ERROR_MESSAGE , 
'currentTimestamp' VALUE CURRENT_TIMESTAMP , 
'SQLStatement' VALUE CISTOOLS . STRIP_SPACES ( SQLSTMT ) 
) 
) ; 
END ; 
SET COLUMN_LIST = ( 
SELECT CAST ( 
LISTAGG ( 
'''' || 
CAST ( 
CASE 
CAMELCASELABELS 
WHEN 
1 
THEN 
CISTOOLS . GET_CAMEL_CASE ( 
CASE 
WHEN 
LOCATE ( '_' , COLUMNNAME ) > 0 
THEN TRIM ( COLUMNNAME ) 
WHEN 
COLUMNLABEL IS NOT NULL 
AND TRIM ( COLUMNLABEL ) <> '' 
THEN TRIM ( COLUMNLABEL ) 
ELSE TRIM ( COLUMNNAME ) 
END ) 
ELSE TRIM ( COLUMNNAME ) 
END || ''':' || 
CASE 
WHEN 
COLUMNCCSID = 65535 
THEN 
'Cast(RTrim(' || 
DELIMIT_NAME ( TRIM ( COLUMNNAME ) ) 
|| ') As Varchar(' || 
COLUMNLENGTH || ') Ccsid 37)' 
WHEN 
SQLTYPE IN ( 'SQL_DECIMAL' , 
'SQL_NUMERIC' , 'SQL_INTERGER' , 
'SQL_SMALLINT' , 'SQL_BIGINT' , 
'SQL_DOUBLE' , 'SQL_FLOAT' , 
'SQL_REAL' ) 
THEN 
DELIMIT_NAME ( TRIM ( COLUMNNAME ) ) || 
' Format Json' 
ELSE 
'RTrim(' || 
DELIMIT_NAME ( TRIM ( COLUMNNAME ) ) || 
')' 
END AS CLOB ( 1 M ) ) , ', ' ) 
WITHIN GROUP ( ORDER BY ORDINALPOS ) AS CLOB ( 2 M ) ) 
FROM TABLE ( 
CISTOOLS . GET_DESCRIPTOR ( SQLSTMT ) 
) ) ; 
SET JSON_SQL = 
'Values (Select Json_Object(Key ''success''  Value ''true'', ' 
|| 'Key ''data'' Value ' || 'Json_ArrayAgg(Json_Object(' || 
TRIM ( COLUMN_LIST ) || ') Format Json ))  From Table(' || 
SQLSTMT || ')) into ? ' ; 
SET CISTOOLS . SQL_STATEMENT = JSON_SQL ;  -- global variable sql_statement for
PREPARE JSONSTMT FROM JSON_SQL ; 
EXECUTE JSONSTMT USING JSON_DATA ; 
RETURN JSON_DATA ; 
END  ; 

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

GRANT EXECUTE   
ON SPECIFIC FUNCTION CISTOOLS.GENERATE_JSON 
TO PUBLIC ; 

