SET PATH *LIBL ; 

CREATE OR REPLACE FUNCTION RESTAPI.EXECUTE_SQL2 ( 
	INAPPVER VARCHAR(10) DEFAULT  'v1'  , 
	INAPPNAME VARCHAR(128) DEFAULT  ''  , 
	INAPPFUNC VARCHAR(128) DEFAULT  ''  , 
	INSUBSTITUTE VARCHAR(10240) DEFAULT  NULL  ) 
	RETURNS CLOB(2147483647)   
	LANGUAGE SQL 
	SPECIFIC RESTAPI.EXECUTE_SQL2 
	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) , 
	DYNDFTCOL = *NO , 
	DYNUSRPRF = *OWNER , 
	SRTSEQ = *HEX   
	BEGIN 
DECLARE SQL_STATEMENT VARCHAR ( 32000 ) DEFAULT NULL ; 
DECLARE SQL_PARAMS VARCHAR ( 10240 ) CCSID 1208 DEFAULT NULL ; 
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 
( 
VALUES 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 SQL_STATEMENT FORMAT JSON 
) ) ; 
END ; 
-- Get SQL template 
SET SQL_STATEMENT = ( 
SELECT SQLSTMT 
FROM RESTAPI . SQLSTMTS 
WHERE ( APPVER , APPNAME , APPFUNC ) = ( INAPPVER , 
INAPPNAME , INAPPFUNC ) ) ; 
-- Normalize substitute JSON into array form 
IF INSUBSTITUTE IS NOT NULL 
AND TRIM ( INSUBSTITUTE ) <> '' THEN 
IF CISTOOLS . JSON_TYPE ( INSUBSTITUTE ) = 'array' THEN 
SET SQL_PARAMS = INSUBSTITUTE ; 
ELSE 
SET SQL_PARAMS = JSON_ARRAY ( INSUBSTITUTE FORMAT JSON ) ; 
END IF ; 
END IF ; 
-- Substitute parameters if we have both template and subs 
IF SQL_STATEMENT IS NOT NULL 
AND SQL_PARAMS IS NOT NULL THEN 
FOR SELECT APPPARAM 
FROM TABLE ( 
RESTAPI . GET_PARAM_PLACEHOLDERS ( 
INAPPVER , INAPPNAME , INAPPFUNC ) 
) 
DO 
-- Figure out type of this param 
SET SQL_STATEMENT = REPLACE ( 
SQL_STATEMENT , '{{' || TRIM ( APPPARAM ) || '}}' , 
IFNULL ( 
JSON_QUERY ( SQL_PARAMS , 
'$[*].' || TRIM ( APPPARAM ) ) , '{{' || 
TRIM ( APPPARAM ) || '}}' ) ) ; 
END FOR ; 
END IF ; 
-- Execute dynamically 
SET CISTOOLS . SQL_STATEMENT = SQL_STATEMENT ; 
SET SQL_STATEMENT = 'Values ((' || SQL_STATEMENT || ')) Into ?' ; 
PREPARE SQL_ FROM SQL_STATEMENT ; 
EXECUTE SQL_ USING CISTOOLS . SQL_CLOB ; 
-- After Execute Sql_ Using Cistools.Sql_Clob; 
-- Detect if Cistools.Sql_Clob is already JSON object with success/data/result
IF LEFT ( LTRIM ( CISTOOLS . SQL_CLOB ) , 1 ) = '{' 
AND ( LOCATE ( '"success"' , CISTOOLS . SQL_CLOB ) > 0 ) 
AND ( LOCATE ( '"data"' , CISTOOLS . SQL_CLOB ) > 0 
OR LOCATE ( '"result"' , CISTOOLS . SQL_CLOB ) > 0 
OR LOCATE ( '"SQLInfo"' , CISTOOLS . SQL_CLOB ) > 0 ) THEN 
IF LOCATE ( '"SQLInfo"' , CISTOOLS . SQL_CLOB ) > 0 THEN 
RETURN 
JSON_OBJECT ( 
'SQLStatement' : SQL_STATEMENT , 'substitute' : SQL_PARAMS , 
'error' : CISTOOLS . SQL_CLOB FORMAT JSON 
) ; 
END IF ; 
-- Return raw JSON as is 
RETURN CISTOOLS . SQL_CLOB ; 
ELSE 
-- Wrap in standard envelope 
RETURN JSON_OBJECT ( 
'success' : 'true' , 'data' : CISTOOLS . SQL_CLOB FORMAT JSON 
) ; 
END IF ; 
END  ; 

GRANT ALTER , EXECUTE   
ON SPECIFIC FUNCTION RESTAPI.EXECUTE_SQL2 
TO AMAPICS WITH GRANT OPTION ; 

GRANT EXECUTE   
ON SPECIFIC FUNCTION RESTAPI.EXECUTE_SQL2 
TO PUBLIC ; 

