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.3 Updating Data

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

mark1An Example of Using the APIs

The following is a sample program using the C APIs.

#include <stdio.h>

#include "libshun.h"

/* Obtain the records that correspond to the specified search conditions
 * then edit the records and overwrite the data.
 */
int main()
{
  /* Handle variables */
  SHUNHCON  connectionHandle;
  SHUNHSTMT statementHandle_search;
  SHUNHSTMT statementHandle_update;

  /* 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_update = 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 == 'Sydney' "
              "AND /information/date == '2006/07/18'}";
  returnForm = "/";
  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 );

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

  /* Display data that was found */
  if ( hitCount > 0 ) {

    /* Here, data processing is performed on dataInfo */

    status = ShunUpdateRecId(
      statementHandle_update,
      returnCount,
      recID,
      dataInfo );
    if ( status != SHUN_SUCCESS ) {
      errorHandle = (SHUNHANDLE)statementHandle_update;
      goto ErrorEnd;
    }
  }


  printf( "Update complete\n" );

  /* Release data manipulation handle */
  status = ShunFreeHandle( statementHandle_update );
  if ( status != SHUN_SUCCESS ) {
    errorHandle = (SHUNHANDLE)statementHandle_update;
    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_update != NULL ) {
    status = ShunFreeHandle( statementHandle_update );
    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                = 3
Number of responses           = 3
Update complete

Contents Index PreviousNext

All Rights Reserved, Copyright(C) FUJITSU LIMITED 2006