/*******************************************************************************
**
** Source File Name = dcscat.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
**       access DB/2.  The order of the program is as follows:
**          - create/catalog a DCS database
**          - list a directory of DCS databases (showing what was created)
**          - uncatalog the DCS database
**
**    STRUCTURES USED :
**       "sql_dir_entry" :
**          unsigned short struct_id;
**          unsigned short release;
**          unsigned short codepage;
**          unsigned char  comment[SQL_CMT_SZ];
**          unsigned char  ldb[SQL_DBNAME_SZ];
**          unsigned char  tdb[SQL_LONG_NAME_SZ];
**          unsigned char  ar[SQL_AR_SZ];
**          unsigned char  parm[SQL_PARAMETER_SZ];
**
**    APIs USED :
**       ADD DCS DIRECTORY ENTRY                      sqlegdad()
**       SCAN DCS DIRECTORY                           sqlegdsc()
**       GET DCS DIRECTORY ENTRIES                    sqlegdgt()
**       CLOSE DCS DIRECTORY SCAN                     sqlegdcl()
**       GET DCS DIRECTORY ENTRY                      sqlegdge()
**       DELETE DCS DIRECTORY ENTRY                   sqlegdel()
**
**    FUNCTIONS USED :
**       'C' COMPILER LIBRARY :
**          stdio.h  -  printf
**          string.h -  fgets, strncpy
**
**       DBMS LIBRARY :
**          sqlenv.h -  see "APIs USED" above
**
**       OTHER :
**          internal :
**             list_dcs :         Displays a directory of databases
**
**          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.
**       - 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 <sqlenv.h>
#include "utilapi.h"



int list_dcs (struct sqlca);

int main (void) {
   int rc;
   struct sqlca sqlca;
   struct sql_dir_entry dcsEntry;


   printf ("this is sample program 'dcscat.c'\n");
   strcpy(dcsEntry.comment, "this is a dcs database");
   strcpy(dcsEntry.ldb, "dcsnm");
   strcpy(dcsEntry.tdb, "targetnm");
   strcpy(dcsEntry.ar, "arName");
   strcpy(dcsEntry.parm, "");
   dcsEntry.struct_id = SQL_DCS_STR_ID;

   printf ("cataloging the DCS database : '%s'...\n",dcsEntry.tdb);
   /*********************************\
   * CATALOG DCS DATABASE API called *
   \*********************************/
   sqlegdad (&dcsEntry, &sqlca);
   API_SQL_CHECK("cataloging the database");
   printf ("database '%s' has been catalogued\n",dcsEntry.tdb);

   printf ("now listing all databases...\n");
   rc = list_dcs (sqlca);

   printf ("now uncataloging the database that was created : '%s'\n",
      dcsEntry.tdb);
   /***********************************\
   * UNCATALOG DCS DATABASE API called *
   \***********************************/
   sqlegdel (&dcsEntry, &sqlca);
   API_SQL_CHECK("uncataloging database");

   printf ("list all databases [after uncataloged DCS]\n");
   rc = list_dcs (sqlca);

   return 0;
}


/*******************************************************************************
** Procedure : list_dcs
**
** Purpose :   This procedure displays the data contained in each database
**             entries by retreiving the "sql_dir_entry" structure.
**
*******************************************************************************/
int list_dcs (struct sqlca sqlca) {
   short count, index;
   struct sql_dir_entry *dcsPointer;

   /************************************\
   * OPEN DCS DIRECTORY SCAN API called *
   \************************************/
   sqlegdsc (&count, &sqlca);
   if (sqlca.sqlcode == 1312) {
     printf ("--- DCS directory is empty ---\n");
     return 0;
   } else {
      API_SQL_CHECK("opening the database directory scan API");
   } /* endif */

   dcsPointer = (struct sql_dir_entry * )
      malloc (count * (sizeof(struct sql_dir_entry)));
   /**************************************\
   * GET DCS DIRECTORY ENTRIES API called *
   \**************************************/
   sqlegdgt (&count, dcsPointer, &sqlca);
   API_SQL_CHECK("getting dcs database entries API");

   printf ("number of dcs databases : %d\n",count);
   for (index = 0; index < count; index++) {        
   /* printing out the node information on to the screen */
      printf ("Local Database Name :   %.8s\n", dcsPointer[index].ldb);
      printf ("Target Database Name :  %.18s\n", dcsPointer[index].tdb);
      printf ("App. Requestor Name :   %.32s\n", dcsPointer[index].ar);
      printf ("DCS parameters :        %.50s\n", dcsPointer[index].parm);
      printf ("Comment :               %.30s\n", dcsPointer[index].comment);
      printf ("DCS Release Level :     0x%x\n", dcsPointer[index].release);
      printf ("\n");
   } /* endfor */

   if (count > 0)
   /* use the first sql_dir_entry from the previous call.
      Normally just the Target Database Name field would be set prior
      to the call. */
   {
   /**************************************************\
   * GET DCS DIRECTORY ENTRY FOR DATABASE API called *
   \**************************************************/
      sqlegdge (dcsPointer, &sqlca);
   }

   free (dcsPointer);
   /*************************************\
   * CLOSE DCS DIRECTORY SCAN API called *
   \*************************************/
   sqlegdcl (&sqlca);
   API_SQL_CHECK("closing the node directory scan");

   return 0;
}