SET PATH *LIBL ; 

CREATE OR REPLACE FUNCTION RESTAPI.GENERATE_DBML3 ( 
	INAPPVER VARCHAR(10) , 
	INAPPNAME VARCHAR(128) ) 
	RETURNS CLOB(2147483647)   
	LANGUAGE SQL 
	SPECIFIC RESTAPI.GENERATE_DBML3 
	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 ( 
'/v1' AS "restUriPathTemplate" , '1.0' AS "version" ) , XMLAGG ( 
XMLELEMENT ( NAME "program" , XMLATTRIBUTES ( '' AS "errormessage" , 
'*NONE' AS "httpheaders" , '500' AS "httpstatusonfailure" , CHAR ( 
E . SUCCESS_CODE ) AS "httpstatusonsuccess" , 'get' || 
UCASE ( SUBSTR ( S . APPFUNC , 1 , 1 ) ) || SUBSTR ( S . APPFUNC , 2 ) || 
'Input' AS "inputWrapperIdentifier" , 'get' || 
UCASE ( SUBSTR ( S . APPFUNC , 1 , 1 ) ) || SUBSTR ( S . APPFUNC , 2 ) AS 
"name" , 'get' || UCASE ( SUBSTR ( S . APPFUNC , 1 , 1 ) ) || 
SUBSTR ( S . APPFUNC , 2 ) || 'Result' AS "outputWrapperIdentifier" , 
'media-resource' AS "querytype" , E . CONSUMES AS "restConsumes" , 
E . HTTP_METHOD AS "restHttpRequestMethod" , E . PRODUCES AS 
"restProduces" , E . PATH_TEMPLATE AS "restUriPathTemplate" , '*ERRORS' 
AS "returnsqlstate" , 'true' AS "treatwarningaserror" , '*TRAILING' 
AS "trimmode" , 'false' AS "wrapInputParams" , 'true' AS 
"wrapOutputParam" ) , 
/* <sql ...> with statement built inline */ 
XMLELEMENT ( NAME "sql" , XMLATTRIBUTES ( 
/* LISTAGG returned as CLOB to avoid 32k select-list limit */ 
'Values Restapi.Execute_Sql(''' || S . APPVER || ''',''' || S . APPNAME 
|| ''',''' || S . APPFUNC || ''', Cast(Json_Object(' || 
COALESCE ( 
( 
SELECT CAST ( 
LISTAGG ( 
'''' || P . APPPARAM || ''':' || '?' , ', ' ) 
WITHIN GROUP ( ORDER BY P . PARAMPOS ) AS CLOB ( 64 K ) 
CCSID 1208 ) 
FROM TABLE ( 
RESTAPI . GET_PARAM_PLACEHOLDERS3 ( 
S . APPVER , S . APPNAME , S . APPFUNC ) 
) AS P ) , CAST ( '' AS CLOB ( 1 K ) ) ) || 
' ) As Varchar(10240) CCSID 37))' AS "statement" ) , 
/* <data .../> elements built via correlated XMLAGG */ 
XMLPARSE ( DOCUMENT XMLSERIALIZE ( CONTENT 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 = '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 ( 'INTEGER' , 'INT' , 'SMALLINT' , 'BIGINT' ) 
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 CLOB ( 1 M ) CCSID 1208 ) ) ) ) ORDER BY 
S . APPFUNC ) ) AS CLOB ( 2 G ) ) 
INTO V_RESULT 
FROM 
RESTAPI . SQLSTMTS S 
JOIN RESTAPI . ENDPOINTS E 
ON ( E . APPVER , E . APPNAME , E . APPFUNC ) = ( S . APPVER , 
S . APPNAME , S . APPFUNC ) 
WHERE ( S . APPVER , S . APPNAME ) = ( INAPPVER , INAPPNAME ) ; 
RETURN V_RESULT ; 
END  ; 

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

GRANT EXECUTE   
ON SPECIFIC FUNCTION RESTAPI.GENERATE_DBML3 
TO PUBLIC ; 

