/*******************************************************************************
**
** Source File Name = d_dbconf.c  1.3
**
** 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 is an example of how APIs are implemented in order to
**       obtain Database Configuration Defaults, by using the
**       GET DATABASE CONFIGURATION DEFAULTS API.
**
**    APIs USED :
**       GET DATABASE CONFIGURATION DEFAULTS       sqlfddb()
**       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.
**       - a database by the name of "DBCONF" does not exist.
**
** 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 "util.h"

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


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

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

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

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

   printf ("this is sample program : d_dbconf.c\n");

   /*********************************************
    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: d_dbconf [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);
      CHECKERR ("attach to instance");
   }
   alloc_mem (itemList);
   printf ("Listing the default database configurations\n");
   /************************************************\
   * GET DATABASE CONFIGURATION DEFAULTS API called *
   \************************************************/
   sqlfddb ("SAMPLE", LISTNUMBER, itemList, &sqlca);
   CHECKERR("get database config defaults");

   print_info (itemList);

   for (idx=0; idx < LISTNUMBER; idx++) {
      free (itemList[idx].ptrvalue);
   } /* endfor */

   if (argc == 4)
   {  printf ("DETACHed FROM INSTANCE API called\n", instName);
      /*********************************\
      * DETACH FROM INSTANCE API called *
      \*********************************/
      sqledtin (&sqlca);
      CHECKERR ("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_DBTN_LOCKLIST;
   itemList[0].ptrvalue = (char *)malloc(sizeof(short));

   itemList[1].token = SQLF_DBTN_BUFF_PAGE;
   itemList[1].ptrvalue = (char *)malloc(sizeof(int));

   itemList[2].token = SQLF_DBTN_MAXFILOP;
   itemList[2].ptrvalue = (char *)malloc(sizeof(short));

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

   itemList[4].token = SQLF_DBTN_LOGPATH;
   itemList[4].ptrvalue = (char *)malloc(MAXLENGTH);

   return 0;
}

int print_info (struct sqlfupd *itemList) {
   printf ("\n");
   printf ("Max. storage for lost lists (4kb)             : %d\n",
      *(short *)itemList[0].ptrvalue);
   printf ("Buffer pool size (4kb)                        : %d\n",
      *(int *)itemList[1].ptrvalue);
   printf ("Max. DB files open per appl.                  : %d\n",
      *(short *)itemList[2].ptrvalue);
   printf ("percent log reclaimed before soft checkpoint  : %d\n",
      *(short *)itemList[3].ptrvalue);
   printf ("path [not changeable] : %s\n\n", itemList[4].ptrvalue);

   return 0;
}