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.1 Adding Data

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

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"

/* Add data */
int main()
{
  /* Handle variables */
  SHUNHCON  connectionHandle;
  SHUNHSTMT statementHandle;

  /* Work variables */
  int status;
  
  /* Input parameters */
  char *hostName;
  int portNo;
  char *ShunFileName;
  
  SHUNDATA inputData[1];
  
  /* Error variables */
  SHUNHANDLE errorHandle;
  int errorLevel;
  char *errorMessage;


  /* Variable initialization */
  connectionHandle = NULL;
  statementHandle = 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 );
  if ( status != SHUN_SUCCESS ) {
    errorHandle = (SHUNHANDLE)connectionHandle;
    goto ErrorEnd;
  }

  /* Create data to be added */
  inputData[0].Data =
    "<document>"
    "    <base>"
    "        <name>Hotel 9</name>"
    "        <city>Adelaide</city>"
    "        <address>Clearview</address>"
    "        <detail>http://xxxxx.com.au</detail>"
    "        <price>300</price>"
    "    </base>"
    "    <information>"
    "        <date>2006/07/18</date>"
    "    </information>"
    "    <note>En-suite bathroom and toilet, five minutes walk to train station XX</note>"
    "</document>";
  inputData[0].Data_Len = strlen( inputData[0].Data );

  /* Call API and add data */
  status = ShunInsert(
    statementHandle,
    1,
    inputData );
  if ( status != SHUN_SUCCESS ) {
    errorHandle = (SHUNHANDLE)statementHandle;
    goto ErrorEnd;
  }
  
  printf( "Addition complete\n" );
  
  /* Release data manipulation handle */
  status = ShunFreeHandle( statementHandle );
  if ( status != SHUN_SUCCESS ) {
    errorHandle = (SHUNHANDLE)statementHandle;
    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 != NULL ) {
    status = ShunFreeHandle( statementHandle );
    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

Addition complete

Contents Index PreviousNext

All Rights Reserved, Copyright(C) FUJITSU LIMITED 2006