gtpa3m0uApplication Requester User's Guide

Removing a Telephone Directory Entry

The program listed in Figure 23 removes an employee entry from the corporate telephone directory.

The format of the parameters is:

rc /R last_name/first_name[/employee_number]

where:

rc
A routing code. This code is used to route messages to the application and is not used in the example of parsing the entry.

R
Remove action code.

last_name
The last name of the person to be removed.

first_name
The first name of the person to be removed.

employee_number
An optional parameter that indicates a specific employee number. If this parameter is included, only the person with this last name, first name, and employee number is removed. If this parameter is omitted, all people with the same last and first names are removed.

Notes:

  1. The / character separates the different parameters.

  2. The [ and ] characters denote an optional parameter.

For example, to remove an entry for Robert Durr from the PHONE_DIRECTORY table, you would type:

     rc /R DURR/ROBERT/1

Figure 23. TPF Program to Remove a Specific Entry in the PHONE_DIRECTORY Table

#include <tpfeq.h>                /* Include libraries                */
#include <tpfapi.h>
#include <tpfarapi.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
 
/**********************************************************************/
/*                                                                    */
/*  Declare the internal and external functions                       */
/*                                                                    */
/**********************************************************************/
static void Check();
#pragma map(qxp2_delete,"QXP2")
extern void qxp2_delete();
 
EXEC SQL INCLUDE SQLCA;           /* Include the SQLCA                */
 
 
  /********************************************************************/
  /* Check:                                                           */
  /*    This function verifies the SQLSTATE returned.  If it is not 0,*/
  /* a message is printed indicating what the SQLSTATE returned was.  */
  /*                                                                  */
  /* A customer implementing a function like this may want to include */
  /* a much more robust error handling and recovery.                  */
  /********************************************************************/
 
static void Check()
{
  if (memcmp(sqlca.sqlstate,"00000",sizeof(sqlca.sqlstate)) != 0) {
    printf("FAILED\n %d %.5s\n",sqlca.sqlcode,sqlca.sqlstate);
    exit(5);
  }
}
 
 
 
 /********************************************************************/
 /*  This function will delete a entry from the database.            */
 /*                                                                  */
 /********************************************************************/
void qxp2_delete()
{
  short int num_input;             /* The number of variables sscanf */
                                   /* has correctly set up.  This is */
                                   /* used to check the validity of  */
                                   /* the parameters.                */
  struct mi0mi *blk;               /* Pointer to the input message   */
 
 
 /********************************************************************/
 /* Declare all the variables that SQL needs to know about.          */
 /********************************************************************/
 
 
  EXEC SQL BEGIN DECLARE SECTION;
 
 /********************************************************************/
 /*  Set up a structure for the directory record.  This structure    */
 /*  is set up in the same order as the CREATE TABLE parameters      */
 /*  were when the table was created on DB2.                         */
 /********************************************************************/
    struct {
      char last_name[18];
      char first_name[9];
      char middle_initial[2];
      char country_code[5];
      char area_code[6];
      char phone_number[13];
      short int employee_number;
      char timestamp[27];
    } dir_record;
 
    char buf[16];                /* This will be used to point to   */
                                  /* the database to connect to.     */
  EXEC SQL END DECLARE SECTION;
 
 
 
 /********************************************************************/
 /* Issue a connect with the name of the database to connect         */
 /* to.  When done, the check function will check the return code,   */
 /* and if invalid, exit.                                            */
 /********************************************************************/
 
  strcpy(buf, &ecbptr()->ebx000);
  EXEC SQL
    CONNECT TO :buf;
  Check();
 
 
 
/********************************************************************/
 /* Parse the message block.  The first parameter is unused, and     */
 /* the second was already parsed.  The next two must be the         */
 /* last name/first name combination.  The employee number is        */
 /* optional.                                                        */
 /*                                                                  */
 /********************************************************************/
  blk = ecbptr()->ce1cr0;
  num_input = sscanf(blk->mi0acc,
     "%*s /%*1c %17[^/]/%8[^/]/%hd",
                dir_record.last_name,
                        dir_record.first_name,
                               &dir_record.employee_number);
 
 
  switch (num_input)
  {
    /*****************************************************************/
    /*  We only have the last name and first name.  The delete will  */
    /*  be based on just this information.                           */
    /*****************************************************************/
 
     case 2:
     {
        EXEC SQL
          DELETE FROM TPFNET.PHONE_DIRECTORY
          WHERE LAST_NAME = :dir_record.last_name AND
                FIRST_NAME = :dir_record.first_name;
 
        Check();
        break;
     }
    /*****************************************************************/
    /*  We have the last name and first name and employee number.    */
    /*  This will guarantee a unique identification.                 */
    /*****************************************************************/
     case 3:
     {
        EXEC SQL
          DELETE FROM TPFNET.PHONE_DIRECTORY
          WHERE LAST_NAME = :dir_record.last_name AND
                FIRST_NAME = :dir_record.first_name AND
                EMPLOYEE_NUMBER = :dir_record.employee_number;
 
        Check();
        break;
     }
     default:
     {
        printf("The input for the delete was invalid. Please check.");
        exit(0);
     }
  }
 
 
 /********************************************************************/
 /*  Commit the work so others may continue.                         */
 /********************************************************************/
 
  EXEC SQL
        COMMIT;
  Check();
 
 /********************************************************************/
 /*  Tell the user that we have completed.                           */
 /********************************************************************/
 
  printf("Employee %s %s was removed from the database.",
                    dir_record.first_name,
                        dir_record.last_name);
  exit(0);
}