SET PATH *LIBL ; 

CREATE OR REPLACE FUNCTION RESTAPI.GENERATE_DBML2 ( 
	INAPPVER VARCHAR(10) , 
	INAPPNAME VARCHAR(128) ) 
	RETURNS CLOB(2147483647)   
	LANGUAGE SQL 
	SPECIFIC RESTAPI.GENERATE_DBML2 
	NOT DETERMINISTIC 
	READS SQL DATA 
	CALLED ON NULL INPUT 
	SET OPTION  ALWBLK = *ALLREAD , 
	ALWCPYDTA = *OPTIMIZE , 
	COMMIT = *NONE , 
	DBGVIEW = *SOURCE , 
	DECRESULT = (31, 31, 00) , 
	DYNDFTCOL = *NO , 
	DYNUSRPRF = *OWNER , 
	SRTSEQ = *HEX   
	BEGIN 
DECLARE V_RESULT CLOB ( 2 G ) ; 
SELECT XMLSERIALIZE ( CONTENT XMLELEMENT ( NAME "dbml" , XMLATTRIBUTES ( '/' 
AS "restUriPathTemplate" , '1.0' AS "version" ) , XMLAGG ( XMLELEMENT ( 
NAME "program" , XMLATTRIBUTES ( '' AS "errormessage" , '*NONE' AS 
"httpheaders" , '500' AS "httpstatusonfailure" , '200' AS 
"httpstatusonsuccess" , 'get' || UPPER ( LEFT ( Q . APPFUNC , 1 ) ) || 
SUBSTR ( Q . APPFUNC , 2 ) || 'Input' AS "inputWrapperIdentifier" , 
'get' || UPPER ( LEFT ( Q . APPFUNC , 1 ) ) || SUBSTR ( Q . APPFUNC , 2 ) AS 
"name" , 'get' || UPPER ( LEFT ( Q . APPFUNC , 1 ) ) || SUBSTR ( Q . APPFUNC , 2) 
|| 'Result' AS "outputWrapperIdentifier" , 'media-resource' AS 
"querytype" , '*/*' AS "restConsumes" , 'POST' AS 
"restHttpRequestMethod" , 'application/json' AS "restProduces" , 
'/v1/' || Q . APPNAME || '/' || Q . APPFUNC AS "restUriPathTemplate" , 
'*ERRORS' AS "returnsqlstate" , 'true' AS "treatwarningaserror" , 
'*TRAILING' AS "trimmode" , 'false' AS "wrapInputParams" , 'true' AS 
"wrapOutputParam" ) 
, XMLELEMENT ( NAME "sql" , XMLATTRIBUTES ( 
'Values Restapi.Execute_Sql(''' || Q . APPVER || ''', ''' || 
Q . APPNAME || ''', ''' || Q . APPFUNC || 
''', Cast(Json_Object(' || Q . APPPARMS || 
' ) As Varchar(10240) CCSID 37))' AS "statement" ) 
, DATATAGS ) ) ORDER BY Q . APPFUNC ) ) AS CLOB ( 2 G ) ) 
INTO V_RESULT 
FROM 
( 
SELECT S . APPVER , S . APPNAME , S . APPFUNC , 
LISTAGG ( 
'''' || P . APPPARAM || ''':' || '?' , ', ' ) 
WITHIN GROUP ( ORDER BY P . PARAMPOS ) AS APPPARMS , 
XMLAGG ( XMLELEMENT ( NAME "data" , 
XMLATTRIBUTES ( COALESCE ( P . CCSID , 1208 ) AS "ccsid" , 
P . APPPARAM AS "name" , 
CASE 
WHEN P . IS_REQUIRED = 1 THEN 'false' 
ELSE 'true' 
END AS "nullable" , 
CASE 
WHEN P . TYPENAME IN ( 'CLOB' ) THEN 2147483647 
WHEN 
P . TYPENAME IN ( 'VARCHAR' , 'CHAR' ) 
THEN COALESCE ( P . LENGTH , 256 ) 
WHEN 
P . TYPENAME IN ( 'DECIMAL' , 'NUMERIC' ) 
THEN COALESCE ( P . LENGTH , 31 ) 
WHEN 
P . TYPENAME IN ( 'INT' , 
'INTEGER' , 'BIGINT' , 'SMALLINT' ) THEN 10 
ELSE 256 
END AS "precision" , 
CASE 
WHEN P . LOCATION = 'PATH' THEN P . RESTNAME 
END AS "restInPathParam" , 
CASE 
WHEN P . LOCATION = 'QUERY' THEN P . RESTNAME 
END AS "restInQueryParam" , 
CASE 
WHEN P . LOCATION = 'FORM' THEN P . RESTNAME 
END AS "restInFormParam" , P . TYPENAME AS "type" , 
'input' AS "usage" ) ) ORDER BY P . PARAMPOS ) AS DATATAGS 
FROM RESTAPI . SQLSTMTS S 
JOIN TABLE ( 
RESTAPI . GET_PARAM_PLACEHOLDERS2 ( ) 
) P 
ON ( S . APPVER , S . APPNAME , S . APPFUNC ) = ( P . APPVER , 
P . APPNAME , P . APPFUNC ) 
WHERE ( S . APPVER , S . APPNAME ) = ( INAPPVER , INAPPNAME ) 
GROUP BY S . APPVER , S . APPNAME , S . APPFUNC 
) AS Q ; 
RETURN CISTOOLS . STRIP_SPACES ( V_RESULT ) ; 
END  ; 

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

GRANT EXECUTE   
ON SPECIFIC FUNCTION RESTAPI.GENERATE_DBML2 
TO PUBLIC ; 

