/* **************************************** *
 *                                          *
 * Connect to an DRDA DB/2 database         *
 * Invocation of procedure from PROLOG:     *
 *                                          *
 * db2_rc = ConnectDatasource()             *
 * Note: The CONNECTSTRING must be changed  *
 * or can be deleted from the snippet, but  *
 * then it must be supplied before the pro- *
 * cedure is called.                        *
 *                                          *
 * **************************************** */

ConnectDatasource:

	/* Connect to DRDA Datasource  */
	say 'Connect ********'
	
	token_drda = 0
	
	/* DRDA Connection */
	DBI.InterfaceType    = 'DRDA'
	DBI.ConnectString    = 'HOST=hostname;PORT=portno;DATABASE=database-name;UID=userid;PWD=password;PLAN=TCEXPRESS;PACKAGE=TCELF000;CONSTOK=6841446351614450;'
	rc = CALLDB('CONNECT', 'DBI', 'DQ2')
	
	IF rc = 0 THEN token_drda = DQ2.Token
	
	IF rc <> 0 THEN DO
		SAY 'CONNECT DRDA rc('rc') NativeErrorCode('DQ2.NativeErrorCode')'
		SAY ' State('DQ2.State') ErrorMessage('DQ2.ErrorMessage')'
		RETURN '12, CONNECT ERROR'
	END

RETURN 0

/* **************************************** *
 *                                          *
 * Execute a statement for a DB/2 database  *
 * The procedure can be called with         *
 * db2_rc = ExecuteDatasource()             *
 *                                          *
 * Before the procdeure is called           *
 * you may provide the SQL statement in     *
 * DBI.SQL_Command                          *
 *                                          * 
 * The code in the snippet must be changed  *
 * to replace "local-variable" with the name*
 * of the variable that should contain the  *
 * result.                                  *  
 * **************************************** */
ExecuteDatasource:

SAY 'EXECUTE ********'

	/* Execute SQL Command DRDA*/
	DBI.Token       = token_drda
	DBI.MaxRecords  = 1
	DBI.SQL_Command = 'select * from db2admin.kategorie where nr = ' katval
	drda_rc = CALLDB('EXECUTE', 'DBI', 'DQ2')

	IF drda_rc <> 0 THEN DO
		SAY 'EXECUTE DRDA rc('drda_rc') NativeErrorCode('DQ2.NativeErrorCode')'
		SAY ' State('DQ2.State') ErrorMessage('DQ2.ErrorMessage')'
		RETURN '12, EXECUTE ERROR'
	END  

	/* Display Results */
	IF drda_rc = 0 THEN DO
		DBI.Token = token_drda
		DBI.RequestId = 0
		rc = CALLDB('FETCH', 'DBI', 'SQL')
	
		/*  
		 * If returncode is ZERO we save 
		 * the value from the  resultset, 
		 * it is the 3rd field 	
		 */
	
		IF rc = 0  THEN local-variable =  SQL.V.Value.3    
	
		/*  
		 * If returncode is 16 (END_OF_FILE) 
		 * we save the error message 	
		 */	

		IF rc = 16 THEN local-variable = '*** Invalid data ' katval  
	END	

RETURN 0
  
/* **************************************** *
 *                                          *
 * Disconnect from a DB/2 database          *
 *                                          *
 * Invocation of procedure from EPILOG:     *
 *                                          *
 * db2_rc = DisconnectDatasource()          * 
 *                                          *
 * **************************************** */
DisconnectDatasource:

	IF token_drda > 0 THEN DO
		DBI.Token = token_drda
		rc = CALLDB('DISCONNECT', 'DBI', 'DBO')
	END
	
	IF rc <> 0 THEN DO
		SAY 'DISCONNECT DRDA rc('rc') NativeErrorCode('DBO.NativeErrorCode')'
		SAY ' State('DBO.State') ErrorMessage('DBO.ErrorMessage')'
		RETURN '12, DISCONNECT ERROR'
	END 	

RETURN 0  
