/******************************************************************************
**
** Source File Name = tabscont.sqc  1.4
**
** 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 TABLESPACE CONTAINER APIs
**       in order to:
**          - obtain tablespace container information using lower level
**            service APIs (OPEN, FETCH, CLOSE)
**          - obtain tablespace container information using a higher level
**            API (QUERY).
**
**    APIs USED :
**        TABLESPACE CONTAINER QUERY             sqlbtcq
**        OPEN TABLESPACE CONTAINER QUERY        sqlbotcq
**        FETCH TABLESPACE CONTAINER QUERY       sqlbftcq
**        CLOSE TABLESPACE CONTAINER QUERY       sqlbctcq
**        FREE MEMORY                            sqlefmem
**
**    STRUCTURES USED :
**       SQLB_TBSCONTQRY_DATA
**       sqlca
**
**    OTHER FUNCTIONS DECLARED :
**       'C' COMPILER LIBRARY :
**          stdio.h  -  printf
**
**       internal :
**          tabspaceContInfo : Displays information on the tablespace 
**                             container that is passed through.
**
**       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 DEPENDANCIES :
**       - 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 <malloc.h>
#include <sqlutil.h>
#include <sqlenv.h>
#include "util.h"

EXEC SQL INCLUDE SQLCA;

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

void tabspaceContInfo (struct SQLB_TBSCONTQRY_DATA *, unsigned long);

int main (int argc, char *argv[]) {
   struct sqlca sqlca;
   struct SQLB_TBSCONTQRY_DATA *dataP;
   unsigned long numTSC, tableSpaceID, nCont, index;
   EXEC SQL BEGIN DECLARE SECTION;
      char dbname[9];
      char userid[9];
      char passwd[19];
   EXEC SQL END DECLARE SECTION;

   if (argc != 4) {
      printf ("\nUSAGE: tabscont databasename userid passwd\n\n");
      return 1;
   } /* endif */


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

   strcpy(dbname, argv[1]);
   strcpy(userid, argv[2]);
   strcpy(passwd, argv[3]);

   EXEC SQL CONNECT TO :dbname USER :userid USING :passwd;
   CHECKERR ("CONNECT TO DATABASE");

   printf ("Using the following APIs:\n   OPEN TABLESPACE CONTAINER QUERY\n"
      "   FETCH TABLESPACE CONTAINER QUERY\n   CLOSE TABLESPACE QUERY\n");
   printf ("=========================\n");
   /********************************************\
   * OPEN TABLESPACE CONTAINER QUERY API called *
   \********************************************/
   sqlbotcq (&sqlca, SQLB_ALL_TABLESPACES, &numTSC);
   CHECKERR ("OPEN TABLESPACE CONTAINER QUERY");
   dataP = (struct SQLB_TBSCONTQRY_DATA *) malloc (numTSC *
      sizeof (struct SQLB_TBSCONTQRY_DATA));
   /*********************************************\
   * FETCH TABLESPACE CONTAINER QUERY API called *
   \*********************************************/
   sqlbftcq (&sqlca, numTSC , dataP, &nCont);
   CHECKERR ("FETCH TABLESPACE CONTAINER QUERY");

   tableSpaceID = dataP->tbsID;
   tabspaceContInfo (dataP, numTSC);

   /*********************************************\
   * CLOSE TABLESPACE CONTAINER QUERY API called *
   \*********************************************/
   sqlbctcq (&sqlca);
   CHECKERR ("CLOSE TABLESPACE CONTAINER QUERY");

   printf ("\nUsing the TABLESPACE CONTAINER QUERY API\n");
   printf ("========================================\n");
   /***************************************\
   * TABLESPACE CONTAINER QUERY API called *
   \***************************************/
   sqlbtcq (&sqlca, tableSpaceID, &numTSC, &(dataP));
   CHECKERR ("TABLESPACE CONTAINER QUERY");
   tabspaceContInfo (dataP, numTSC);

   /************************\
   * FREE MEMORY API called *
   \************************/
   sqlefmem (&sqlca, dataP);
   CHECKERR ("FREE MEMORY");

   EXEC SQL CONNECT RESET;
   CHECKERR ("CONNECT RESET");

   return 0;
}

void tabspaceContInfo (struct SQLB_TBSCONTQRY_DATA *dataP, unsigned long num) {
   unsigned long idx;
   for (idx = 0; idx < num; idx++, dataP++) {
      printf ("     Tablespace Containers for Tablespace %d\n\n", dataP->tbsID);
      printf ("Container ID    = %ld\n", dataP->id);
      printf ("Name            = %s\n", dataP->name);
      printf ("Type            = ");
      switch (dataP->contType) {
         case SQLB_CONT_PATH:
            printf ("path\n");
            break;
         case SQLB_CONT_DISK:
            printf ("disk\n");
            break;
         case SQLB_CONT_FILE:
            printf ("file\n");
            break;
         default:
            printf ("unknown\n");
            break;
      } /* endswitch */

      printf ("\n");
   } /* endfor */
}