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.3 Advanced Applications

J.3.3 Delete Data Using Direct Access Key

The following is a sample program to delete data with the direct access key using the C APIs.

mark1Data to be Deleted

<course>
  <id>0001</id>
  <name>Business negotiation</name>
  <instructor>
    <first-name>Max</first-name>
    <last-name>cameron</last-name>
  </instructor>
  <capacity>40</capacity>
  <current-auditors>30</current-auditors>
</course>

mark1Defined Direct Access Key

key /course/id/text()

mark1An Example of Using the APIs

The following is a sample program using the C APIs.

#include <stdio.h>
#include <string.h>

#include "libshun.h"

/* Use the direct access to delete the data */
int main()

{
  /* Handle variables */
  SHUNHCON  connectionHandle;
  SHUNHSTMT statementHandle;

  /* Working variables */
  int status;
  int i;

  /* Input parameters */
  char *hostName;
  int portNo;
  char *ShunFileName;
  char *keyName;
  SHUNKEY keyInfo[1];

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


  /* Initialize variables */
  connectionHandle = NULL;
  statementHandle = NULL;

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

  /* Establish a connection by specifying the host name, port number and Shunsaku File name */
  hostName = "DirSvr1";
  portNo = 33101;
  ShunFileName = "shunsakuFile1";
  status = ShunConnect( connectionHandle, hostName, portNo, ShunFileName );
  if ( status != SHUN_SUCCESS ) {
    errorHandle = (SHUNHANDLE)connectionHandle;
    goto ErrorEnd;
  }

  /* Allocate data manipulation handle */
  status = ShunAllocHandle( connectionHandle, &statementHandle );
  if ( status != SHUN_SUCCESS ) {
    errorHandle = (SHUNHANDLE)connectionHandle;
    goto ErrorEnd;
  }

  /* Set direct access key */
  keyName = "key";
  keyInfo[0].Key = "0001";
  keyInfo[0].Key_Len = strlen( keyInfo[0].Key );

  /* Call the API and delete data directly */
  status = ShunDeleteKey( statementHandle,
                          keyName,
                          SHUN_KEY_COMPLETE_MATCH,
                          1,
                          keyInfo );
  if ( status != SHUN_SUCCESS ) {
    errorHandle = (SHUNHANDLE)statementHandle;
    goto ErrorEnd;
  }
  printf( "Deletion complete\n" );

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

  /* Close connection  */
  status = ShunDisconnect( connectionHandle );
  if ( status != SHUN_SUCCESS ) {
    errorHandle = (SHUNHANDLE)connectionHandle;
    goto ErrorEnd;
  }

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

  return 0;



ErrorEnd: /* Error processing if an error occurs */

  /* Get 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 obtaining error message : %d\n", status);
  }

  /* Release the data manipulation handle */
  if ( statementHandle != NULL ) {
    status = ShunFreeHandle( statementHandle );
    if ( status != SHUN_SUCCESS ) {
      printf("Error releasing data manipulation handle : %d\n", status);
    }
  }

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

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

  return 1;
}

mark1Execution Results

Deletion complete

Contents Index PreviousNext

All Rights Reserved, Copyright(C) FUJITSU LIMITED 2006