/******************************************************************************
**
** Source File Name = bindfile.sqc  1.7
**
** 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 program demonstrates the use of embedded SQL compiled to
**       run without BINDING the application to a database.
**       To create this program, the PREP stage, the COMPILE, and the LINK
**       are all that are necessary.  No BIND is required.  Thus no BIND file
**       is needed.
**
**    STRUCTURES USED :
**       sqlopt
**       sqlca
**
**    APIs USED :
**       BIND                       sqlabndx()
**
**    FUNCTIONS DECLARED :
**       'C' COMPILER LIBRARY :
**          stdio.h  -  printf
**          string.h -  strncpy
**
**       DBMS LIBRARY :
**          sqlenv.h -  see "APIs USED" above
**
**       OTHER :
**          external :
**             check_error :     Checks for SQLCODE error, and prints out any
**             [in util.c]       related information available.
**
**    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.
**
*******************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sqlenv.h> /* :rk.1:erk. */
#include "util.h"

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

EXEC SQL INCLUDE SQLCA;  /* :rk.2:erk. */

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

int main(int argc, char *argv[]) {
   struct sqlopt *sqloptStructPointer;
   struct sqloptions *optionsPointer;

   EXEC SQL BEGIN DECLARE SECTION;
      char statement[256]; /* :rk.3:erk. */
      char userid[9];
      char passwd[19];
      char db[9];
   EXEC SQL END DECLARE SECTION;
   SQL_API_RC rc = 0;

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

   printf( "\nSample C program:  BINDFILE \n");
      
   if (argc == 2) {
      EXEC SQL CONNECT TO sample;     /* :rk.5:erk. */
      CHECKERR ("CONNECT TO SAMPLE"); /* :rk.6:erk. */
   }
   else if (argc == 4) {
      strcpy (userid, argv[2]);
      strcpy (passwd, argv[3]);
      EXEC SQL CONNECT TO sample USER :userid USING :passwd; /* :rk.3:erk. */
          CHECKERR ("CONNECT TO SAMPLE");
   }
   else {
      printf ("\nUSAGE: bindfile <program_name>.bnd [userid password]\n\n");
      return 1;
   } /* endif */

   sqloptStructPointer = (struct sqlopt *) malloc
      (sizeof (struct sqlopt)
         + (2 * sizeof (struct sqloptions)));
   sqloptStructPointer->header.allocated = 2;
   sqloptStructPointer->header.used = 2;
   optionsPointer = sqloptStructPointer->option;

   /* nullifying the options for the BIND API */
   optionsPointer = sqloptStructPointer->option;
   optionsPointer->type = SQL_NO_OPT;
   optionsPointer->val = 0;
   optionsPointer++;
   optionsPointer->type = SQL_NO_OPT;
   optionsPointer->val = 0;

   printf ("\n--->binding the application :%s: to database sample\n",
      argv[1]);
   /*****************\
   * BIND API called *
   \*****************/
   sqlabndx (argv[1], "message.pb2", sqloptStructPointer, &sqlca);
   CHECKERR ("binding the application");

   printf ("connect reset\n");
   EXEC SQL CONNECT RESET;  /* :rk.10:erk. */
   CHECKERR ("CONNECT RESET");
   return 0;
}
/* end of program : bindfile.sqc */