/*******************************************************************************
**
** Source File Name = client.c  1.2
**
** 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 :
**       an example showing how to use CLIENT APIs in order to:
**          - set a client
**          - and query a client
**
**    APIs USED :
**       SET CLIENT                     sqlesetc
**       QUERY CLIENT                   sqleqryc
**
**    STRUCTURES USED :
**       sqle_conn_setting
**       sqlca
**
**    OTHER FUNCTIONS DECLARED :
**       'C' COMPILER LIBRARY :
**          stdio.h  -  printf
**
**       internal :
**
**       external :
**          check_error :     Checks for SQLCODE error, and prints out any
**          [in util.c]          related information available.
**                               This procedure is located in the util.c file.
**
**    EXTERNAL DEPENDENCIES :
**       - Ensure existence of database for precompile purposes.
**       - 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>
#ifndef DB2MAC
#include <malloc.h>
#endif
#include <sqlenv.h>
#include "util.h"

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


#define  CHECKERR(CE_STR)   check_error (CE_STR, &sqlca);

#define NUMSETTINGS 5

int printQuery (struct sqle_conn_setting *);

int main (void) {
   int rc;
   struct sqlca sqlca;
   struct sqle_conn_setting connSetting[NUMSETTINGS];
   short temp_val1, temp_val2, temp_val3, temp_val4, temp_val5;
   connSetting[0].type = SQL_CONNECT_TYPE;
   connSetting[1].type = SQL_RULES;
   connSetting[2].type = SQL_DISCONNECT;
   connSetting[3].type = SQL_SYNCPOINT;
   connSetting[4].type = SQL_MAX_NETBIOS_CONNECTIONS;
   printf ("QUERY CLIENT**************************************************\n");
   
#ifdef DB268K
   /* Before making any API calls for 68K environment,
      need to initial the Library Manager */
	InitLibraryManager(0,kCurrentZone,kNormalMemory);
	atexit(CleanupLibraryManager);
#endif

   /*************************\
   * QUERY CLIENT API called *
   \*************************/
   sqleqryc (&connSetting[0], NUMSETTINGS, &sqlca);

   CHECKERR ("QUERY CLIENT");

   rc = printQuery(&connSetting[0]);

   temp_val1 = connSetting[0].value;
   temp_val2 = connSetting[1].value;
   temp_val3 = connSetting[2].value;
   temp_val4 = connSetting[3].value;
   temp_val5 = connSetting[4].value;

   connSetting[0].value = SQL_CONNECT_2;
   connSetting[1].value = SQL_RULES_STD;
   connSetting[2].value = SQL_DISCONNECT_COND;
   connSetting[3].value = SQL_SYNC_TWOPHASE;
   connSetting[4].value = 254;

   printf ("SET CLIENT****************************************************\n");
   printf ("connect type        = SQL_CONNECT_2\n");
   printf ("rules               = SQL_RULES_STD\n");
   printf ("disconnect          = SQL_DISCONNECT_COND\n");
   printf ("syncpoint           = SQL_SYNC_TWOPHASE\n");
   printf ("max netbios conn.   = 254\n");
   /***********************\
   * SET CLIENT API called *
   \***********************/
   sqlesetc (&connSetting[0], NUMSETTINGS, &sqlca);
   CHECKERR ("SET CLIENT");

   printf ("QUERY CLIENT**************************************************\n");
   /*************************\
   * QUERY CLIENT API called *
   \*************************/
   sqleqryc (&connSetting[0], NUMSETTINGS, &sqlca);
   CHECKERR ("QUERY CLIENT");

   rc = printQuery(&connSetting[0]);

   connSetting[0].value = temp_val1;
   connSetting[1].value = temp_val2;
   connSetting[2].value = temp_val3;
   connSetting[3].value = temp_val4;
   connSetting[4].value = temp_val5;

   printf ("SET CLIENT back to original settings *************************\n");
   /***********************\
   * SET CLIENT API called *
   \***********************/
   sqlesetc (&connSetting[0], NUMSETTINGS, &sqlca);
   CHECKERR ("SET CLIENT");

   return 0;
}

int printQuery (struct sqle_conn_setting *connP) {
   printf ("\n\n\n");
   printf ("SQL CONNECTION TYPE\n");
   printf ("===================\n");
   switch (connP->value){
      case SQL_CONNECT_1:
         printf ("Enforces the rules for Remote Unit of Work (RUOW) from "
            "previous releases\n  TYPE = SQL_1\n\n");
         break;
      case SQL_CONNECT_2:
         printf ("Supports the multiple databases per unit of work semantics "
            "DUOW\n  TYPE = SQL_2\n\n");
         break;
      default:
         printf ("undefined value = %d\n\n", connP->value);
   } /* endswitch */

   connP++;
   printf ("SQL RULES\n");
   printf ("=========\n");
   switch (connP->value) {
      case SQL_RULES_DB2:
         printf ("Enables the SQL CONNECT statement to switch the current "
            "connection to an\nestablished (dormant) connection\n"
            "  TYPE = SQL_DB2\n\n");
         break;
      case SQL_RULES_STD:
         printf ("Permits the establishment of a new connection only through "
            "SQL CONNECT\nstatement.  Under SQL_STD, the SQL SET "
            "CONNECTION statement is used to switch\nthe current connection "
            "to a dormant connection.\n  TYPE = SQL_STD\n\n");
         break;
      default:
         printf ("undefined value = %d\n\n", connP->value);
   } /* endswitch */

   connP++;
   printf ("SQL DISCONNECT\n");
   printf ("==============\n");
   switch (connP->value) {
      case SQL_DISCONNECT_EXPL:
         printf ("Breaks those connections that have been explicitly marked "
            "for release at commit\nby the SQL RELEASE statement\n"
            "  TYPE = SQL_EXPLICIT\n\n");
         break;
      case SQL_DISCONNECT_COND:
         printf("Breaks those connections that have no open WITH HOLD "
            "cursors at commit,\nand those that have been marked for release "
            "by the SQL RELEASE statement\n  TYPE = SQL_CONDITIONAL\n\n");
         break;
      case SQL_DISCONNECT_AUTO:
         printf("Breaks all connections at commit\n  TYPE = SQL_AUTOMATIC\n\n");
         break;
      default:
         printf ("undefined value = %d\n\n", connP->value);
   } /* endswitch */

   connP++;
   printf ("SQL SYNCPOINT\n");
   printf ("=============\n");
   switch (connP->value) {
      case SQL_SYNC_TWOPHASE:
         printf ("Requires a Transaction Manager (TM) to coordinate two-"
            "phase commits among\ndatabases that support this protocol\n"
            "  TYPE = SQL_TWOPHASE\n\n");
         break;
      case SQL_SYNC_ONEPHASE:
         printf ("Uses one-phase commits to commit the work done by each"
            "database in multiple\ndatabase transactions.  Enforces single"
            "updater, multiple read behaviour\n  TYPE = SQL_ONEPHASE\n\n");
         break;
      case SQL_SYNC_NONE:
         printf ("Does not enforce two-phase commits, or singer updater, "
            "multiple\nread behaviour.\n  TYPE = SQL_NONE\n\n");
         break;
      default:
         printf ("undefined value = %d\n\n", connP->value);
   } /* endswitch */

   connP++;
   printf ("SQL MAX NETBIOS CONNECTIONS\n");
   printf ("===========================\n");
   printf ("This specifies the maximum number of concurrent connections that "
      "can be\nmade using the NETBIOS adapter in an application\n");
   printf ("  CONNECTIONS = %d\n", connP->value);

   printf ("\n\n\n");

   return 0;
}