/******************************************************************************
**
** Source File Name = loadqry.sqc
**
** Licensed Materials - Property of IBM
**
** (C) COPYRIGHT International Business Machines Corp. 1995, 2000 
** All Rights Reserved.
**
** US Government Users Restricted Rights - Use, duplication or
** disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
**
**
**    PURPOSE :
**       This program is an example of how APIs are implemented in order to
**       query the current status of any LOAD being envoked on a table in
**       the database that the program is currently connected to.
**
**       This program needs the embedded SQL calls in order to connect to
**       an existing database, then to create a temporary table to work with.
**
**    APIs USED :
**       LOAD QUERY                 db2LoadQuery()
**
**    FUNCTIONS DECLARED :
**       'C' COMPILER LIBRARY :
**          stdio.h  -  printf
**          string.h -  strncpy
**
**       DBMS LIBRARY :
**          sql.h -  see "APIs USED" above
**
**       OTHER :
**          external :
**             check_error :     Checks for SQLCODE error, and prints out any
**             [in UTIL.C]       related information available.
**
**    EXTERNAL DEPENDANCIES :
**       - 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.
**
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sqlutil.h>
#include <db2ApiDf.h>
#include "utilemb.h"

EXEC SQL INCLUDE SQLCA;

int main (int argc, char *argv[]) {
   db2LoadQueryStruct       loadQueryParameters;
   db2LoadQueryOutputStruct loadQueryOutputStructure;

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

   if (argc != 5) {
      printf ("\nUSAGE: %s [userid passwd tablename msgfile]\n\n", argv[0]);
      return 1;
   } /* endif */

   strcpy (userid, argv[1]);
   strcpy (passwd, argv[2]);

   /* Initialize structures */
   memset(&loadQueryParameters, 0, sizeof(db2LoadQueryStruct));
   memset(&loadQueryOutputStructure, 0, sizeof(db2LoadQueryOutputStruct));

   /* Set up the tablename to query. */
   loadQueryParameters.iStringType = DB2LOADQUERY_TABLENAME;
   loadQueryParameters.piString = argv[3];

   /* Specify that we want all LOAD messages to be reported. */
   loadQueryParameters.iShowLoadMessages = DB2LOADQUERY_SHOW_ALL_MSGS;

   /* LOAD summary information goes here. */
   loadQueryParameters.poOutputStruct = &loadQueryOutputStructure;

   /* Set up the local message file. */
   loadQueryParameters.piLocalMessageFile = argv[4];
      
   EXEC SQL CONNECT TO sample USER :userid USING :passwd;
   EMB_SQL_CHECK("CONNECT TO SAMPLE");
   
   /***************/
   /* LOAD QUERY  */
   /***************/
   db2LoadQuery(db2Version610, &loadQueryParameters, &sqlca);

   if (SQLCODE == 3523)
     printf("No load is in progress; empty message file %s created.\n",
            argv[4]);
   else
   {
     printf("Load status has been written to %s.\n\n", argv[4]);

     printf("Number of rows read         = %ld\n",
            loadQueryOutputStructure.oRowsRead);

     printf("Number of rows skipped      = %ld\n",
            loadQueryOutputStructure.oRowsSkipped);

     printf("Number of rows loaded       = %ld\n",
            loadQueryOutputStructure.oRowsLoaded);

     printf("Number of rows rejected     = %ld\n",
            loadQueryOutputStructure.oRowsRejected);

     printf("Number of rows deleted      = %ld\n",
            loadQueryOutputStructure.oRowsDeleted);

     printf("Number of rows committed    = %ld\n",
            loadQueryOutputStructure.oRowsCommitted);

     printf("Number of warnings          = %ld\n",
            loadQueryOutputStructure.oWarningCount);
   }

   printf("\nCONNECT RESETting\n");
   EXEC SQL CONNECT RESET;
   EMB_SQL_CHECK("CONNECT RESET");

   return 0;

}