/******************************************************************************
**
** Source File Name = tabscont.sqc
**
** 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 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 "utilemb.h"

EXEC SQL INCLUDE SQLCA;


void tabspaceContInfo (struct SQLB_TBSCONTQRY_DATA *, sqluint32);

int main (int argc, char *argv[]) {
   struct sqlca sqlca;
   struct SQLB_TBSCONTQRY_DATA *dataP;
   sqluint32    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;
   EMB_SQL_CHECK("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);
   EMB_SQL_CHECK("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);
   EMB_SQL_CHECK("FETCH TABLESPACE CONTAINER QUERY");

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

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

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

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

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

   return 0;
}

void tabspaceContInfo (struct SQLB_TBSCONTQRY_DATA *dataP, sqluint32 num) {
   sqluint32 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 */
}