/*******************************************************************************
**
** Source File Name = dmscont.c  1.1
**
** Licensed Materials - Property of IBM
**
** (C) COPYRIGHT International Business Machines Corp. 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
**       CREATE a DATABASE with more than 1 DMS container, then DROP the
**       DATABASE.
**
**    APIs USED :
**       INSTANCE ATTACH                  sqleatin()
**       CREATE DATABASE                  sqlecrea()
**       DROP DATABASE                    sqledrpd()
**
**    STRUCTURES USED :
**       sqlca
**       sqledbdesc
**       SQLETSCDESC
**       SQLETSDESC
**       sqledbcountryinfo
**
**    OTHER FUNCTIONS USED :
**       'C' COMPILER LIBRARY :
**          stdio.h  -  printf
**
**       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 :
**       - Compiling and linking 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 "DMSCONT" 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"

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

int main (int argc, char* argv[]) {
   struct sqlca sqlca;
   struct sqledbdesc db_desc;
   struct SQLETSCDESC * tscdesc ;
   struct SQLETSDESC * tsdesc ;
   struct sqledbcountryinfo dbCountryInfo ;

   char * new_container ;
   int container_count = 3 ;
  
   char userid[9];
   char passwd[19];
   char nodename[9];

   if (argc == 4) {
      strcpy (userid, argv[1]);
      strcpy (passwd, argv[2]);
      strcpy (nodename, argv[3]);

       
      /*************************/
      /* ATTACH API called     */
      /*************************/
      sqleatin (nodename, userid, passwd, &sqlca);
   }

   else if (argc != 1) {
      printf ("\nUSAGE: dmscont [userid passwd remote_nodename]\n");
      return 1;
   }
 
   tsdesc = malloc( SQLETSDESC_SIZE( container_count ) ) ;
   memset( tsdesc, '\0', SQLETSDESC_SIZE( container_count ) ) ;

   strcpy(tsdesc->sqltsdid, SQLE_DBTSDESC_1);   
   tsdesc->sqlextnt = -1 ;
   tsdesc->sqlprftc = -1 ;
   tsdesc->sqlpovhd = -1 ;
   tsdesc->sqltrfrt = -1 ;
   tsdesc->sqltstyp = SQL_TBS_TYP_DMS ;
   tsdesc->sqlccnt = container_count ;

   new_container = ( char * ) &( tsdesc->containr[0] ) ;
   tscdesc = ( struct SQLETSCDESC * ) new_container ;

   tscdesc->sqlctype = SQL_TBSC_TYP_FILE ;
   tscdesc->sqlcsize = 100 ;
   tscdesc->sqlclen = 12 ;
   strcpy( tscdesc->sqlcontr, "tsfile1.tmp" ) ;

   new_container += sizeof( struct SQLETSCDESC ) ;
   tscdesc = ( struct SQLETSCDESC * ) new_container ;

   tscdesc->sqlctype = SQL_TBSC_TYP_FILE ;
   tscdesc->sqlcsize = 100 ;
   tscdesc->sqlclen = 12 ;
   strcpy( tscdesc->sqlcontr, "tsfile2.tmp" ) ;

   new_container += sizeof( struct SQLETSCDESC ) ;
   tscdesc = ( struct SQLETSCDESC * ) new_container ;

   tscdesc->sqlctype = SQL_TBSC_TYP_FILE ;
   tscdesc->sqlcsize = 100 ;
   tscdesc->sqlclen = 12 ;
   strcpy( tscdesc->sqlcontr, "tsfile3.tmp" ) ;

   strcpy(db_desc.sqldbdid, SQLE_DBDESC_2);
   db_desc.sqldbccp = 0;
   db_desc.sqldbcss = SQL_CS_USER ;
   db_desc.sqldbsgp = 0;
   db_desc.sqldbnsg = 10;
   db_desc.sqltsext = -1;
   db_desc.sqlcatts = NULL;
   db_desc.sqlusrts = NULL;
   db_desc.sqltmpts = tsdesc ;

   strcpy( dbCountryInfo.sqldbcodeset, "IBM-850") ;
   strcpy( dbCountryInfo.sqldblocale, "En_US") ;

   printf ( "CREATing the temporary database DMSCONT...\n" ) ;
   printf ( "please wait... this will take a while...\n" ) ;
   /****************************\
   * CREATE DATABASE API called *
   \****************************/
   sqlecrea( "DMSCONT",
             "DMSCONT",
             "",
             &db_desc,
             &dbCountryInfo,
             '\0',
             NULL,
             &sqlca
           ) ;
   CHECKERR("creating the database");
   printf ( "database DMSCONT with more than 1 DMS container\n" ) ;
   printf ( "created successfully.\n" ) ;

   printf ("DROPping the database DMSCONT\n");
   /**************************\
   * DROP DATABASE API called *
   \**************************/
   sqledrpd ("DMSCONT", &sqlca);
   CHECKERR("dropping the database");

   free( tsdesc ) ;
      
   return 0;
}