/* **************************************** *
 *                                          *
 * Connect to a tcACCESS datasource via     *
 * a host manager                           *
 * Invocation of procedure from PROLOG:     *
 *                                          *
 * tcaC_rc = ConnectDatasource()            *
 * Note: The CONNECT parms must be changed  *
 * or can be deleted from the snippet, but  *
 * then they must be supplied before the    *
 * procedure is called.                     *
 *                                          *
 * **************************************** */
ConnectDatasource:

	say 'Connect ************'
	
	token_tca = 0
	
	/* tcACCESS Connection */
	DBI.InterfaceType    = 'TCACCESS'
	DBI.IP_Name          = '00'
	DBI.UserId           = 'username'
	DBI.Password         = 'password'
	
	/* set the hostname of the manager system */
	DBI.HostName         = 'hostname'
	DBI.Port             = managerport
	
	DBI.Local_CCSID      = 1252
	DBI.Host_CCSID       = 1141
	
	rc = CALLDB('CONNECT', 'DBI', 'DQ1')
	
	IF rc = 0 THEN  token_tca = DQ1.Token
	
	IF rc <> 0 THEN  DO
		SAY '**** Error during TCACCESS Connect ****'
		SAY 'CONNECT TCA rc('rc') HostRC('D2X(DQ1.HostRC)') CommRC('D2X(DQ1.CommRC)') ServerRC('D2X(DQ1.ServerRC)') ErrorMessage('DQ1.ErrorMessage')'
		RETURN '12, TCACCESS CONNECT ERROR'
	END
	
RETURN 0

/* **************************************** *
 *                                          *
 * Execute a SQL statement in tcACCESS      *

 * The procedure can be called with         *
 * tcac_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 "localvariable" with the name*
 * of the variable that should contain the  *
 * result.                                  *  
 * **************************************** */
ExecuteDatasource:

	say 'Execute *********'
	
	/* Execute SQL Command tcACCESS*/
	DBI.Token       = token_tca
	
	/* Please set the logical name of the managers tcACCESS subsystem */
	DBI.TCA_Name    = 'tcaccess-name'
	DBI.KeepSession = 'Y'
	DBI.Describe    = 'N'
	
	/* Please change the SQL statement here */
	DBI.SQL_Command = 'select * from demo.kategorie where katnr = ' katval
	
	rc = CALLDB('EXECUTE', 'DBI', 'DQ1')
	
	IF rc <> 0 THEN  DO
		SAY '**** Error during tcACCESS Execute ****'
		SAY 'EXECUTE TCA rc('tcac_rc') HostRC('D2X(DQ1.HostRC)') CommRC('D2X(DQ1.CommRC)') ServerRC('D2X(DQ1.ServerRC)') ErrorMessage('DQ1.ErrorMessage')'
		RETURN '12, TCACCECC EXECUTE ERROR'
	END 
	
	/* Display Results */
	IF rc = 0 THEN DO
		
		IFieldCount = DQ1.FieldCount
		DBI.Token = token_tca
		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  localvariable =  SQL.V.Value.3
	
		/*  
		 * If returncode is 16 (END_OF_FILE) 
		 * we save the error message 	
		 */	    
	
		IF rc = 16  THEN  localvariable = '*** Invalid CAT-Nr : ' katval 
	
	END
    
RETURN 0


/* **************************************** *
 *                                          *
 * Disconnect from tcACCESS                 *
 * Invocation of procedure from EPILOG:     *
 *                                          *
 * tcac_rc = DisconnectDatasource()         * 
 *                                          *
 * **************************************** */
DisconnectDatasource:
	
	say 'Disconnect *********'
	
	IF token_tca > 0 THEN DO
		DBI.Token = token_tca
		rc = CALLDB('DISCONNECT', 'DBI', 'DBO')
	END
	
	IF rc <> 0 THEN  DO
		SAY '**** Error during tcACCESS Disconnect ****'
		SAY 'DISCONNECT TCA rc('rc') HostRC('DBO.HostRC') CommRC('DBO.CommRC') ServerRC('DBO.ServerRC')'
		RETURN '12, TCACCESS DISCONNNECT ERROR'
	END 
RETURN 0  
