/******************************************************************************
**
** Source File Name = utilapi.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 :
**    - contains functions that use administrative APIs:
**
**          DbCreate - create and catalog a database
**          DbDrop   - drop and uncatalog a database
**
** For more information about these samples see the README file.
**
** For more information on programming in CLI see the:
**     - "Building CLI Applications" section of the Application Building Guide, and the
**     - CLI Guide and Reference.
**
** 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 "utilapi.h"

/******************************************************************************
**     DbCreate - create and catalog a database
**                    using administrative APIs
******************************************************************************/
int  DbCreate( char remoteNodeName[],
               char user[],
               char pswd[],
               char dbName[],
               char dbAlias[] )
{   int rc = 0;

    struct sqlca sqlca;
    struct sqledbdesc dbDesc;
    struct sqledbcountryinfo dbCountryInfo;	


    printf("\n\nCreating the database %s...\n", dbAlias);
      
    if (strlen (remoteNodeName) > 0)
    {   /* attach to the remote instance */
        sqleatin (remoteNodeName, user, pswd, &sqlca);
        if (sqlca.sqlcode != 0)
        {   printf("\nError while attaching to the remote node %s.\n",
                   remoteNodeName);
            return(1);	    
        }	
    }

    /* prepare the structures needed to create a database */   
    strcpy(dbDesc.sqldbdid, SQLE_DBDESC_2);
    dbDesc.sqldbccp = 0;
    dbDesc.sqldbcss = SQL_CS_USER ;
    dbDesc.sqldbsgp = 0;
    dbDesc.sqldbnsg = 10;
    dbDesc.sqltsext = -1;
    dbDesc.sqlcatts = NULL;
    dbDesc.sqlusrts = NULL;
    dbDesc.sqltmpts = NULL;
    
    strcpy( dbCountryInfo.sqldbcodeset, "IBM-850") ;
    strcpy( dbCountryInfo.sqldblocale, "En_US") ;	
		
    /* create and catalog the database */
    sqlecrea( dbName,
              dbAlias,
              "",
              &dbDesc,
              &dbCountryInfo,
              '\0',
              NULL,
              &sqlca
            ) ;
    if (sqlca.sqlcode != 0)
    {   printf("\nError while creating the database %s.\n", dbAlias);
        return(1);	    
    }	 
    printf("The database %s created.\n", dbAlias);    


    if (strlen (remoteNodeName) > 0)
    {   /* detach from the remote instance */
        sqledtin( &sqlca);
        if (sqlca.sqlcode != 0)
        {   printf("\nError while detaching from the remote node %s.\n",
                   remoteNodeName);
            return(1);	    
        }	
    }
    
   
    return (0);
}

/******************************************************************************
**     DbDrop - drop and uncatalog a database
**                   using administrative APIs
******************************************************************************/
int  DbDrop( char remoteNodeName[],
             char user[],
             char pswd[],
             char dbAlias[] )
{   int rc = 0;

    struct sqlca sqlca;

    printf("\nDropping the database %s...\n", dbAlias);
    
    if (strlen (remoteNodeName) > 0)
    {   /* attach to the remote instance */
        sqleatin (remoteNodeName, user, pswd, &sqlca);
        if (sqlca.sqlcode != 0)
        {   printf("\nError while attaching to the remote node %s.\n",
                   remoteNodeName);
            return(1);	    
        }	
    }

    /* drop and uncatalog the database */    
    sqledrpd( dbAlias, &sqlca);
    if (sqlca.sqlcode != 0)
    {   printf("\nError while dropping the database %s.\n", dbAlias);
        return(1);	    
    }	    
    printf("The database %s was dropped.\n", dbAlias);

    if (strlen (remoteNodeName) > 0)
    {   /* detach from the remote instance */
        sqledtin( &sqlca);
        if (sqlca.sqlcode != 0)
        {   printf("\nError while detaching from the remote node %s.\n",
                   remoteNodeName);
            return(1);	    
        }	
    }
    
    return (0);
}