/*******************************************************************************
**
** Source File Name = dbcat.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 :
**       an example showing how to use DATABASE CATALOG APIs in order to:
**          - catalog a database
**          - list a directory of databases (showing what was created)
**          - uncatalog the database
**
**    APIs USED :
**       CATALOG DATABASE                       sqlecadb()
**       OPEN DATABASE DIRECTORY SCAN           sqledosd()
**       GET NEXT DATABASE DIRECTORY ENTRY      sqledgne()
**       CLOSE DATABASE DIRECTORY SCAN          sqledcls()
**       UNCATALOG DATABASE                     sqleuncd()
**
**    STRUCTURES USED :
**       sqledinfo
**       sqlca
**
**    OTHER FUNCTIONS USED :
**       'C' COMPILER LIBRARY :
**          stdio.h  -  printf
**
**       internal :
**          list_db :         Displays a directory of databases
**
**       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 <sqlenv.h>
#include "utilapi.h"


#if ( defined(DB2OS2) | defined(DB2WIN))
   #define DRIVE_SZ 2
#else
   /* The "drive" field of the SQLEDINFO is truncated to 50 characters for
      display purposes in this program.  The actual length of this field is
      215 */
   #define DRIVE_SZ 50
#endif
#define DB_PATH NULL
#define DB_NODE "db2test"  
#define DB_TYPE SQL_REMOTE 

int list_db (struct sqlca);

int main (void) {
   struct sqlca sqlca;


   printf ("this is sample program 'dbcat.c'\n");

   printf ("cataloging the database...");
   /*****************************\
   * CATALOG DATABASE API called *
   \*****************************/
   sqlecadb ("newdata", "newalias", DB_TYPE, DB_NODE, DB_PATH,
      "the new database : newalias", SQL_AUTHENTICATION_SERVER,
      NULL, &sqlca);
   /* Ignore warning SQL1100W */
   if (SQLCODE != 1100) API_SQL_CHECK("cataloguing the database");

   printf ("Listing all databases...\n");
   printf ("========================\n");
   list_db (sqlca);

   printf ("UNCATALOGing the database that was created\n");

   /*******************************\
   * UNCATALOG DATABASE API called *
   \*******************************/
   sqleuncd ("newalias", &sqlca);
   API_SQL_CHECK("uncataloging database");

   printf ("Listing all databases [after uncatalogued database]\n");
   printf ("===================================================\n");
   list_db (sqlca);

   return 0;
}


/*******************************************************************************
** Procedure : list_db
**
** Purpose :   This procedure displays the data contained in each database
**             entries by retreiving the "sqldinfo" structure.
**
*******************************************************************************/
int list_db (struct sqlca sqlca) {
   unsigned short index;
   unsigned short dbHandle;
   unsigned short dbCount;
   struct sqledinfo *dbBuffer;

   /*****************************************\
   * OPEN DATABASE DIRECTORY SCAN API called *
   \*****************************************/
   sqledosd ("\0", &dbHandle, &dbCount, &sqlca);
   if (sqlca.sqlcode == SQLE_RC_NODBDIR) {
      printf ("--- Database directory is empty ---\n");
      return 1;
   } else {
      API_SQL_CHECK("opening the database directory scan API");
   } /* endif */

   for (index = 0; index < dbCount; index++) {
      /************************************\
      * GET NEXT DATABASE ENTRY API called *
      \************************************/
      sqledgne (dbHandle, &dbBuffer, &sqlca);
      API_SQL_CHECK("getting next database entry API");

      /* printing out the node information on to the screen */
      printf ("alias name :            %.8s\n", dbBuffer->alias);
      printf ("database name :         %.8s\n", dbBuffer->dbname);
      printf ("database drive :        %.*s\n",DRIVE_SZ, dbBuffer->drive);
      printf ("database directory :    %.8s\n", dbBuffer->intname);
      printf ("node name :             %.8s\n", dbBuffer->nodename);
      printf ("database release type : %.20s\n", dbBuffer->dbtype);
      printf ("database comment :      %.30s\n", dbBuffer->comment);
      printf ("database entry type :   %d\n", dbBuffer->type);

      switch (dbBuffer->authentication) {
      case SQL_AUTHENTICATION_SERVER:
         printf ("authentication :        SERVER\n");
         break;
      case SQL_AUTHENTICATION_CLIENT:
         printf ("authentication :        CLIENT\n");
         break;
      case SQL_AUTHENTICATION_DCS:
         printf ("authentication :        DCS\n");
         break;
      default:
        break;
      } /* endswitch */
      printf ("\n");
   } /* endfor */
   /******************************************\
   * CLOSE DATABASE DIRECTORY SCAN API called *
   \******************************************/
   sqledcls (dbHandle, &sqlca);
   API_SQL_CHECK("closing the node directory scan");
   return 0;
}