/* **************************************** *
 *                                          *
 * Connect to an ODBC datasource            *
 * Invocation of procedure from PROLOG:     *
 *                                          *
 * ODBC_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:
	say 'Connect **********'
	token_odbc = 0

	DBI.InterfaceType    = 'ODBC'
	
	/* Please change the name of the dsn here */
	DBI.ConnectString    = 'DSN=dsn-name;'
	rc = CALLDB('CONNECT', 'DBI', 'DQ3')

	IF rc <> 0 THEN DO
    	SAY 'CONNECT ODBC rc('rc') NativeErrorCode('DQ3.NativeErrorCode')' 
	    SAY 'State('DQ3.State') ErrorMessage('DQ3.ErrorMessage')'
    	RETURN '12,ODBC Connect Error'
    
	END    

	IF rc = 0 THEN token_odbc = DQ3.Token

RETURN 0

/* **************************************** *
 *                                          *
 * Execute a Statement for an ODBC DSN      *
 * The procedure can be called with         *
 * odbc_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 ODBC*/
	DBI.Token       = token_odbc
	DBI.MaxRecords  = 10
	
	/* Please change the statement here */
	DBI.SQL_Command = 'select * from CATEGORY where CATEGORYNR = ' katval
	
	odbc_rc = CALLDB('EXECUTE', 'DBI', 'DQ3')
	
	IF odbc_rc <> 0 THEN  DO
		SAY '**** Error during ODBC Execute ****'
		SAY 'EXECUTE ODBC rc('odbc_rc') NativeErrorCode('DQ3.NativeErrorCode') State('DQ3.State') ErrorMessage('DQ3.ErrorMessage')'
		RETURN '12, ODBC EXECUTE ERROR'
	END 
	
	/* Display Results */
	IF odbc_rc = 0 THEN DO
		DBI.Token      = token_odbc
		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 data' katval  
	END	
	
	RETURN 0

/* **************************************** *
 *                                          *
 * Disconnect from an ODBC datasource       *
 *                                          *
 * Invocation of procedure from EPILOG:     *
 *                                          *
 * odbc_rc = DisconnectDatasource()         * 
 *                                          *
 * **************************************** */
DisconnectDatasource:

	/* Disconnect from Datasource */
	
	say 'Disconnect ********'
	
	IF token_odbc > 0 THEN DO
		DBI.Token = token_odbc
		rc = CALLDB('DISCONNECT', 'DBI', 'DBO')
	
	
		IF rc <> 0 THEN DO
			SAY 'DISCONNECT ODBC rc('rc') NativeErrorCode('DBO.NativeErrorCode')'
			SAY ' State('DBO.State') ErrorMessage('DBO.ErrorMessage')'
			RETURN '12, ODBC Disconnect Error'
		END
	
	END

RETURN 0

