/******************************************************************************
**
** Source File Name = mrspcli3.sqc
**
** Licensed Materials - Property of IBM
**
** (C) COPYRIGHT International Business Machines Corp. 1995, 1999
** 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 a stored procedures that return a result set.
**
**   - This embedded SQL program calls the CLI function defined in clicall.c, which in
**     turns calls the stored procedure on the server defined in mrspcli2.sqc.
**   - Note: mrspcli3.sqc is NOT built in the makefile using the ALL
**           command. To build this example you must copy util.c and util.h
**           from the .../SAMPLES/C subdirectory. Once this is done you can
**           use the make command: make mrspcli3
**
** For more information about these samples see the README file.
**
** For more information on programming in CLI see the:
**     - "Building CLI Applications" section of the Application Building Guide, and the
**     - CLI Guide and Reference.
**
** For more information on the SQL language see the SQL Reference.
**
*******************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <sql.h> /* :rk.1:erk. */
#include <sqlda.h>
#include <sqlca.h>
#include <string.h>

#ifdef DB268K
/* Need to include ASLM for 68K applications */
#include <LibraryManager.h>
#endif

#define  CHECKERR(CE_STR)   if (sql_error (CE_STR, &sqlca) != 0) return 1;


int sql_error (char eString[], struct sqlca *caPointer);
int callcli( char *, double*, short* );

int main(int argc, char *argv[]) {

   EXEC SQL BEGIN DECLARE SECTION;
     char database[9];
     char userid[9];
     char passwd[19];
     /* Declare a Local Variable for Holding the Procedure's Name */
     char procname[255] = "mrspsrv2";

     /* Declare Local Variables for Holding Returned Data */
     double sal     = 0.0;  /* :rk.3:erk. */
     short  salind  = 0;
   EXEC SQL END DECLARE SECTION;


   /* Declare the output SQLDA */
   struct sqlda *inout_sqlda = (struct sqlda *)
                                malloc(SQLDASIZE(1)); /* :rk.2:erk. */

   /* Declare the SQLCA */
   struct sqlca sqlca;

#ifdef DB268K
   /* Before making any API calls for 68K environment,
      need to initial the Library Manager */
        InitLibraryManager(0,kCurrentZone,kNormalMemory);
        atexit(CleanupLibraryManager);
#endif

   if (argc != 4) {
      printf ("\nUSAGE: mrspcli3 remote_database userid passwd\n\n");
      return 1;
   }
   strcpy (database, argv[1]);
   strcpy (userid, argv[2]);
   strcpy (passwd, argv[3]);
   /* Connect to Remote Database */
   printf("CONNECT TO Remote Database.\n");
   EXEC SQL CONNECT TO :database USER :userid USING :passwd;
   CHECKERR ("CONNECT TO RSAMPLE");

   printf("Use CLI to invoke the Server Procedure named outsrv\n");
   salind = -1;                  /* Sal has no input, so set to null */
   /*
   * Don't execute as embedded, call CLI function instead
   *
   * EXEC SQL CALL :procname (:sal :salind);
   * CHECKERR ("CALL WITH HOST VARIABLES");
   */
   callcli( procname, &sal, &salind);

   /* Disconnect from Remote Database */
   EXEC SQL CONNECT RESET; /* :rk.7:erk. */
   CHECKERR ("CONNECT RESET");
   return 0;
}


int sql_error (char eString[], struct sqlca *caPointer) {
   char eBuffer[1024];
   char sBuffer[1024];
   char message[1024];
   char messToken[1024];
   short rc, Erc;
   int status=0;

   if (caPointer->sqlcode != 0 && caPointer->sqlcode != 100) {
      strcpy(message, "");

      sprintf (messToken, "--- error report ---\n");
      strcat(message, messToken);

      sprintf (messToken, "ERROR occurred : %s.\nSQLCODE : %ld\n", eString,
         caPointer->sqlcode);
      strcat(message, messToken);

      /**********************\
      * GET SQLSTATE MESSAGE *
      \**********************/
      rc = sqlogstt (sBuffer, 1024, 80, caPointer->SQLSTATE);

      /******************************\
      * GET ERROR MESSAGE API called *
      \******************************/
      Erc = sqlaintp (eBuffer, 1024, 80, caPointer);

      /* return code is the length of the eBuffer string */
      if (Erc > 0)
      {   sprintf (messToken, "%s", eBuffer);
          strcat(message, messToken);          
      }

      if (caPointer->sqlcode < 0)
      {   if (rc == 0)
          {   sprintf (messToken, "\n%s", sBuffer);
              strcat(message, messToken);              
          }
          sprintf (messToken, "--- end error report ---\n");
          strcat(message, messToken);

          printf("%s", message);
          return 1;
      } 
      else 
      {   /* errorCode is just a Warning message */
	  if (rc == 0)
          {   sprintf (messToken, "\n%s", sBuffer);
              strcat(message, messToken);
          }
          sprintf (messToken, "--- end error report ---\n");
          strcat(message, messToken);

          sprintf (messToken, "WARNING - CONTINUING PROGRAM WITH WARNINGS!\n");
          strcat(message, messToken);

          printf("%s", message);
          return 0;
      } /* endif */
   } /* endif */

   return 0;
}