/********** Start of Header ********************************************
**
** Source File Name = asynrlog.sqc 
**
** Licensed Materials - Property of IBM
**
** (C) COPYRIGHT International Business Machines Corp. 1999, 2000
** All Rights Reserved.
**
** US Government Users Restricted Rights - Use, duplication or
** disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
**
**
** PURPOSE: This sample program demonstrates the use of a sqlurlog
**          (asynhronous log read) API.
**
** An external function "check_error" is contained in the file "util.c"
** which must be compiled along with this file.  
**
**    EXTERNAL DEPENDENCIES :
**       - Ensure existence of database for precompile purposes.
**       - Precompile with the SQL precompiler (PREP in DB2)
**       - Bind to a database (BIND in DB2)
**       - Compile and link with the IBM Cset++ compiler (AIX and OS/2)
**         or the Microsoft Visual C++ compiler (Windows) 
**         or the compiler supported on your platform.
**
** For more information about these samples see the README file.
**
** For more information on programming in C, see the:
**   -  "Programming in C and C++" section of the Application Development Guide
** For more information on Building C Applications, see the:
**   -  "Building C Applications" section of the Application Building Guide.
**
** For more information on the SQL language see the SQL Reference.
**
*********** End of Header **********************************************
* example tags in SQLB0206 API	*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sqlutil.h"
#include "utilemb.h"
 
EXEC SQL INCLUDE SQLCA ;

 
int main(int argc, char *argv[])
{
   SQLU_RLOG_INFO readLogInfo ;
   struct sqlca   sqlca ;
   SQLU_LSN       startLSN ;
   SQLU_LSN       endLSN ;
   char          *logBuffer ;
   sqluint32      callerAction ;
   sqluint32      logBufferSize ;
   int            returnCode = 0 ;

   EXEC SQL BEGIN DECLARE SECTION;
      char userid[9];
      char passwd[19];
   EXEC SQL END DECLARE SECTION;

   printf( "Sample C program: ASYNRLOG\n" );
   
   if (argc == 1) {
      EXEC SQL CONNECT TO sample;
      EMB_SQL_CHECK("CONNECT TO SAMPLE");
   }
   else if (argc == 3) { 
      strcpy (userid, argv[1]);
      strcpy (passwd, argv[2]);
      EXEC SQL CONNECT TO sample USER :userid USING :passwd;
      EMB_SQL_CHECK("CONNECT TO SAMPLE");
   }
   else {
      printf ("\nUSAGE: asynrlog [userid passwd]\n\n");
      return 1;
   } /* endif */
    
   /* Initialize the sqlurlog() parameters to QUERY values */
   callerAction = SQLU_RLOG_QUERY ;
 
   memset( &startLSN, 0, sizeof( startLSN )) ;
   memset( &endLSN, 0, sizeof( endLSN )) ;
 
   logBuffer = NULL ;
   logBufferSize = 0  ;
 
   memset( &readLogInfo, '\0', sizeof( readLogInfo )) ;
   memset( &sqlca, '\0', sizeof( sqlca )) ;

   /* Query the database log for database SAMPLE */
   returnCode = sqlurlog( callerAction, &startLSN, &endLSN, logBuffer,
                          logBufferSize, &readLogInfo, &sqlca ) ; 
   if (( returnCode  == 0 ) && ( sqlca.sqlcode == 0 ))
   {
      /* Reset the sqlurlog() parameters to READ values */
      callerAction = SQLU_RLOG_READ ;
 
      /* Use a 50 * 4k buffer to store the returned log records	*/
      logBufferSize = 50 * 4096 ;
      logBuffer = ( char * ) malloc( logBufferSize ) ;
 
      memset( logBuffer, '\0', logBufferSize ) ;
 
      /* Read the log sequence number range as passed back from the
      ** QUERY of the SAMPLE database
      */
      memcpy( &startLSN, &readLogInfo.initialLSN, sizeof( startLSN ) ) ;
      memcpy( &endLSN, &readLogInfo.curActiveLSN, sizeof( endLSN ) ) ;

	  memset( &readLogInfo, '\0', sizeof( readLogInfo )) ;
 
      /* Read the database log for database SAMPLE */
      returnCode = sqlurlog( callerAction, &startLSN, &endLSN, logBuffer,
                             logBufferSize, &readLogInfo, &sqlca ) ;
    }
 
   /* Disconnect from the database */
   EXEC SQL CONNECT RESET ;
 
   return 0;
}