Interstage Shunsaku Data Manager Application Development Guide - Microsoft(R) Windows(R) 2000/ Microsoft(R) Windows Server(TM) 2003 - - UNIX -
Contents Index PreviousNext

Appendix J Sample C Programs> J.2 Updating Data

J.2.2 Deleting Data

The following example shows how the C APIs are used to delete data.

mark1Search Conditions

'I want to delete the data for Hotel 9 from all data relating to hotels in Adelaide that have a vacancy on 2006/07/18.'

Perform a search using the date (2006/07/18) and location (Adelaide) as conditions, and then delete the data where the hotel name matches "Hotel 9".

mark1An Example of Using the APIs

The following is a sample program using the C APIs.

#include <stdio.h>

#include "libshun.h"

/* Delete data */
int main()
{
  /* Handle variables */
  SHUNHCON  connectionHandle;
  SHUNHSTMT statementHandle_search;
  SHUNHSTMT statementHandle_del;

  /* Work variables */
  int status;

  /* Input parameters */
  char *hostName;
  int portNo;
  char *ShunFileName;
  int startNo;
  int returnRequestCount;
  char *queryForm;
  char *returnForm;
  char *sortForm;

  /* Output parameters */
  int hitCount;
  int returnCount;
  int returnableCount;
  SHUNRECID *recID;
  SHUNDATA  *dataInfo;
  SHUNPOS *firstPosition, *lastPosition;

  /* Error variables */
  SHUNHANDLE errorHandle;
  int errorLevel;
  char *errorMessage;


  /* Variable initialization */
  connectionHandle = NULL;
  statementHandle_search = NULL;
  statementHandle_del = NULL;

  /* Connection handle allocation */
  status = ShunAllocHandle( NULL, &connectionHandle );
  if ( status != SHUN_SUCCESS ) {
    errorHandle = (SHUNHANDLE)connectionHandle;
    goto ErrorEnd;
  }

  /* Specify host name, port number and Shunsaku file name and establish connection */
  hostName = "DirSvr1";
  portNo = 33101;
  ShunFileName = "shunsakuFile1";
  status = ShunConnect( connectionHandle, hostName, portNo, ShunFileName );
  if ( status != SHUN_SUCCESS ) {
    errorHandle = (SHUNHANDLE)connectionHandle;
    goto ErrorEnd;
  }

  /* Allocation of data manipulation handles */
  status = ShunAllocHandle( connectionHandle, &statementHandle_search );
  if ( status != SHUN_SUCCESS ) {
    errorHandle = (SHUNHANDLE)connectionHandle;
    goto ErrorEnd;
  }

  /* Specify number of items to find, search expression, etc. */
  startNo = 1;
  returnRequestCount = 30;
  queryForm = "/document/base/city == 'Adelaide' "
              "AND /document/information/date == '2006/07/18' "
              "AND /document/base/name == 'Hotel 9'";
  returnForm = "/document/base/name, /document/base/price";
  sortForm = NULL;
  
  /* Call API and perform data search */
  status = ShunSearch(
    statementHandle_search,
    startNo,
    NULL,
    0,
    returnRequestCount,
    queryForm,
    returnForm,
    sortForm,
    &hitCount,
    &returnCount,
    &returnableCount,
    &recID,
    &dataInfo,
    &firstPosition,
    &lastPosition );
  if ( status != SHUN_SUCCESS ) {
    errorHandle = (SHUNHANDLE)statementHandle_search;
    goto ErrorEnd;
  }

  /* Extract number of hits */
  printf( "Number of hits                = %d\n", hitCount );
  printf( "Number of responses                  = %d\n", returnCount );


  /* Allocation of data manipulation handles */
  status = ShunAllocHandle( connectionHandle, &statementHandle_del );
  if ( status != SHUN_SUCCESS ) {
    errorHandle = (SHUNHANDLE)connectionHandle;
    goto ErrorEnd;
  }

  /* Delete all data based on ID */
  if ( hitCount > 0 ) {
    status = ShunDeleteRecId(
      statementHandle_del,
      returnCount,
      recID);
    if ( status != SHUN_SUCCESS ) {
      errorHandle = (SHUNHANDLE)statementHandle_del;
      goto ErrorEnd;
    }
  }

  printf( "Deletion complete\n" );

  /* Release data manipulation handle */
  status = ShunFreeHandle( statementHandle_del );
  if ( status != SHUN_SUCCESS ) {
    errorHandle = (SHUNHANDLE)statementHandle_del;
    goto ErrorEnd;
  }

  /* Release data manipulation handle */
  status = ShunFreeHandle( statementHandle_search );
  if ( status != SHUN_SUCCESS ) {
    errorHandle = (SHUNHANDLE)statementHandle_search;
    goto ErrorEnd;
  }

  /* Disconnect */
  status = ShunDisconnect( connectionHandle );
  if ( status != SHUN_SUCCESS ) {
    errorHandle = (SHUNHANDLE)connectionHandle; 
    goto ErrorEnd; 
  } 
 
  /* Release connection handle */
  status = ShunFreeHandle( connectionHandle );
  if ( status != SHUN_SUCCESS ) {
    errorHandle = (SHUNHANDLE)connectionHandle;
    goto ErrorEnd;
  }

  return 0;



ErrorEnd: /* Processing to perform in the event of an error */

  /* Obtain error information */
  status = ShunGetErrorMessage(errorHandle,
                               &errorLevel, &errorMessage);

  if ( status == SHUN_SUCCESS ) {
    switch ( errorLevel ) {
    case SHUN_ERROR_CONNECTION:
      printf("Error level              = SHUN_ERROR_CONNECTION\n");
      break;
    case SHUN_ERROR_DATA:
      printf("Error level              = SHUN_ERROR_DATA\n");
      break;
    }
    printf("Error message          = %s\n", errorMessage);}
  else {
    printf("Error message acquisition error: %d\n", status);
  }

  /* Release data manipulation handle */
  if ( statementHandle_del != NULL ) {
    status = ShunFreeHandle( statementHandle_del );
    if ( status != SHUN_SUCCESS ) {
      printf("Data manipulation handle release error: %d\n", status);
    }
  }

  /* Release data manipulation handle */
  if ( statementHandle_search != NULL ) {
    status = ShunFreeHandle( statementHandle_search );
    if ( status != SHUN_SUCCESS ) {
      printf("Data manipulation handle release error: %d\n", status);
    }
  }

  /* Disconnect */
  if ( connectionHandle != NULL) {
    status = ShunDisconnect( connectionHandle );
    if ( status != SHUN_SUCCESS ) {
      printf("Disconnection error: %d\n", status);
     }
  }

  /* Release connection handle */
  if ( connectionHandle != NULL) {
    status = ShunFreeHandle( connectionHandle );
    if ( status != SHUN_SUCCESS ) {
      printf("Connection handle release error: %d\n", status);
    }
  }

  return 1;
}

mark1Execution Results

Number of hits                = 1
Number of responses           = 1
Deletion complete

Contents Index PreviousNext

All Rights Reserved, Copyright(C) FUJITSU LIMITED 2006