/*******************************************************************************
**
** Source File Name = dbmconf.c 
**
** 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
**       CREATE a DATABASE and then GET the CONFIGURATION information from
**       it, then UPDATE the CONFIGURATION, RESET the CONFIGURATION, then
**       DROP the DATABASE.
**       This program only can only be run locally.
**       It can not be run on a remote node.
**
**    APIs USED :
**       GET DATABASE MANAGER CONFIGURATION       sqlfxsys()
**       UPDATE DATABASE MANAGER CONFIGURATION    sqlfusys()
**       RESET DATABASE MANAGER CONFIGURATION     sqlfrsys()
**       ATTACH TO INSTANCE                       sqleatin()
**       DETACH FROM INSTANCE                     sqledtin()
**
**
**    STRUCTURES USED :
**       sqlfupd
**       sqlca
**
**    OTHER FUNCTIONS USED :
**       'C' COMPILER LIBRARY :
**          stdio.h  -  printf
**
**       internal :
**          print_info :      Displays a directory of databases
**          alloc_mem :       Allocates the proper amount of memory for the
**                            GET DATABASE CONFIGURATION API to fill with
**                            data.
**
**       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>
#include <sqlutil.h>
#include <sqlenv.h>
#include "utilapi.h"

#define  LISTNUMBER 4

int alloc_mem (struct sqlfupd *);
int print_info (struct sqlfupd *);

int main (int argc, char *argv[]) {
   struct sqlfupd itemList[LISTNUMBER];
   struct sqlca sqlca;
   char option;

   char instName[SQL_INSTNAME_SZ + 1];

   /*********************************************
    If no arguments are passed, assume local instance, otherwise
      the first argument is the instance name
      the second argument is the user name
      the third argument is the password
   *********************************************/
   if (argc !=1 && argc != 4) {
      printf ("\nUSAGE: dbmconf [instance/node_name user_name password]\n\n");
      return 1;
   }

   if (argc == 4)
   {  printf ("ATTACHed TO INSTANCE API called for instance :%s:\n", argv[1]);
      /*******************************\
      * ATTACH TO INSTANCE API called *
      \*******************************/
      sqleatin (argv[1], argv[2], argv[3], &sqlca);
      API_SQL_CHECK("attach to instance");
   }

   alloc_mem (itemList);

   printf ("getting the default Database Manager Configuration\n");
   /***********************************************\
   * GET DATABASE MANAGER CONFIGURATION API called *
   \***********************************************/
   sqlfxsys (LISTNUMBER, itemList, &sqlca);
   API_SQL_CHECK("get database manager config");

   printf ("listing the database configuration\n");
   print_info (itemList);

   printf ("                     *****************************\n");
   printf ("                     *** IMPORTANT INFORMATION ***\n");
   printf ("                     *****************************\n");
   printf ("    In the following steps of the this program, an UPDATE and a RESET database\n");
   printf ("manager configuration APIs will be called, changing the current database\n");
   printf ("manager configuration to be reset to the DEFAULT values.\n\n");
   printf ("Do you wish to continue? (y/n)  <--the default is 'NO'\n");

   option = getc(stdin);
   if (option != 'y' && option != 'Y') return 0;   /* else continue */


   /* Altering values of the default Database Manager Configuration */
   *(int *)itemList[2].ptrvalue=2500;    /* change # Agents */
   *(short *)itemList[3].ptrvalue=15;    /* change # active DB */

   printf ("UPDATing the Database Manager Configuration\n");
   /**************************************************\
   * UPDATE DATABASE MANAGER CONFIGURATION API called *
   \**************************************************/
   sqlfusys (LISTNUMBER, itemList, &sqlca);
   API_SQL_CHECK("updating the database configuration");

   printf ("listing the UPDATEd Database Manager Configuration\n");
   /***********************************************\
   * GET DATABASE MANAGER CONFIGURATION API called *
   \***********************************************/
   sqlfxsys (LISTNUMBER, itemList, &sqlca);
   API_SQL_CHECK("get database config");
   print_info (itemList);

   printf ("RESETting the Database Manager Configuration\n");
   /*************************************************\
   * RESET DATABASE MANAGER CONFIGURATION API called *
   \*************************************************/
   sqlfrsys (&sqlca);
   API_SQL_CHECK("resetting the database manager config");

   printf ("printing the RESETed Database Manager Configuration\n");
   /***********************************************\
   * GET DATABASE MANAGER CONFIGURATION API called *
   \***********************************************/
   sqlfxsys (LISTNUMBER, itemList, &sqlca);
   API_SQL_CHECK("get database config");
   print_info (itemList);

   if (argc == 4)
   {  printf ("DETACHed FROM INSTANCE API called\n", instName);
      /*********************************\
      * DETACH FROM INSTANCE API called *
      \*********************************/
      sqledtin (&sqlca);
      API_SQL_CHECK("detach from instance");
   }

   return 0;

}

/*******************************************************************************
* this procedure allocates the required memory to each section of the array.
* Each member of the array must be appointed to a specific field in regards
* to the output of the GET DATABASE CONFIGURATION API call
*******************************************************************************/
int alloc_mem (struct sqlfupd *itemList) {
   itemList[0].token = SQLF_KTN_SVCENAME;
   itemList[0].ptrvalue = (char *)malloc(SQL_SERVICE_NAME_SZ + 1);

   itemList[1].token = SQLF_KTN_TPNAME;
   itemList[1].ptrvalue = (char *)malloc(SQL_TPNAME_SZ + 1);

   itemList[2].token = SQLF_KTN_MAXAGENTS;
   itemList[2].ptrvalue = (char *)malloc(sizeof(int));

   itemList[3].token = SQLF_KTN_NUMDB;
   itemList[3].ptrvalue = (char *)malloc(sizeof(short));

   return (0);
}

int print_info (struct sqlfupd *itemList) {
   printf ("Database Manager Service Name         : %s\n",
      itemList[0].ptrvalue);
   printf ("Transaction program name              : %s\n",
      itemList[1].ptrvalue);
   printf ("Max. number of Agents                 : %d\n",
      *(int *)itemList[2].ptrvalue);
   printf ("Number of concurrent active DB allowed: %d\n",
      *(short *)itemList[3].ptrvalue);
   printf ("\n");

   return (0);
}