ページの先頭行へ戻る
Interstage Shunsaku Data Manager V9.0.6 アプリケーション開発ガイド
FUJITSU Software

J.2.2 データを削除する

データを削除する場合の、C APIの使用例を示します。


削除するデータの検索条件

「2006年7月18日に神奈川で宿泊可能なホテルのうち、ホテル9のデータを削除したい。」

年月日(2006年7月18日)および、場所(神奈川)を条件に検索を行い、

ホテル名『ホテル9』と一致したデータを削除します。


APIの使用例


以下にC APIを使用したプログラミング例を示します。

#include <stdio.h>

#include "libshun.h"

/* データを削除します */
int main()
{
  /* ハンドル変数群 */
  SHUNHCON  connectionHandle;
  SHUNHSTMT statementHandle_search;
  SHUNHSTMT statementHandle_del;

  /* 作業用変数群 */
  int status;

  /* 入力パラメタ群 */
  char *hostName;
  int portNo;
  char *ShunFileName;
  int startNo;
  int returnRequestCount;
  char *queryForm;
  char *returnForm;
  char *sortForm;

  /* 出力パラメタ群*/
  int hitCount;
  int returnCount;
  int returnableCount;
  SHUNRECID *recID;
  SHUNDATA  *dataInfo;
  SHUNPOS *firstPosition, *lastPosition;

  /* エラー用変数群 */
  SHUNHANDLE errorHandle;
  int errorLevel;
  char *errorMessage;


  /* 変数の初期化 */
  connectionHandle = NULL;
  statementHandle_search = NULL;
  statementHandle_del = NULL;

  /* コネクションハンドルの割当て */
  status = ShunAllocHandle( NULL, &connectionHandle );
  if ( status != SHUN_SUCCESS ) {
    errorHandle = (SHUNHANDLE)connectionHandle;
    goto ErrorEnd;
  }

  /* ホスト名・ポート番号、Shunsakuファイル名を指定してコネクションを確立 */
  hostName = "DirSvr1";
  portNo = 23101;
  ShunFileName = "shunsakuFile1";
  status = ShunConnect( connectionHandle, hostName, portNo, ShunFileName );
  if ( status != SHUN_SUCCESS ) {
    errorHandle = (SHUNHANDLE)connectionHandle;
    goto ErrorEnd;
  }

  /* データ操作ハンドルの割当て */
  status = ShunAllocHandle( connectionHandle, &statementHandle_search );
  if ( status != SHUN_SUCCESS ) {
    errorHandle = (SHUNHANDLE)connectionHandle;
    goto ErrorEnd;
  }

  /* 検索件数、検索式などの指定*/
  startNo = 1;
  returnRequestCount = 30;
  queryForm = "/document/base/prefecture == '神奈川' "
              "AND /document/information/date == '2006年07月18日' "
              "AND /document/base/name == 'ホテル9'";
  returnForm = "/document/base/name, /document/base/price";
  sortForm = NULL;
  
  /* API呼出し データ検索 */
  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;
  }

  /* ヒット件数の取出し */
  printf( "ヒット件数                = %d\n", hitCount );
  printf( "応答件数                  = %d\n", returnCount );


  /* データ操作ハンドルの割当て */
  status = ShunAllocHandle( connectionHandle, &statementHandle_del );
  if ( status != SHUN_SUCCESS ) {
    errorHandle = (SHUNHANDLE)connectionHandle;
    goto ErrorEnd;
  }

  /* IDを基にデータすべてを削除 */
  if ( hitCount > 0 ) {
    status = ShunDeleteRecId(
      statementHandle_del,
      returnCount,
      recID);
    if ( status != SHUN_SUCCESS ) {
      errorHandle = (SHUNHANDLE)statementHandle_del;
      goto ErrorEnd;
    }
  }

  printf( "削除成功\n" );

  /* データ操作ハンドルの解放 */
  status = ShunFreeHandle( statementHandle_del );
  if ( status != SHUN_SUCCESS ) {
    errorHandle = (SHUNHANDLE)statementHandle_del;    goto ErrorEnd;
  }

  /* データ操作ハンドルの解放 */
  status = ShunFreeHandle( statementHandle_search );
  if ( status != SHUN_SUCCESS ) {
    errorHandle = (SHUNHANDLE)statementHandle_search;
    goto ErrorEnd;
  }

  /* コネクションの切断 */
  status = ShunDisconnect( connectionHandle );
  if ( status != SHUN_SUCCESS ) {
    errorHandle = (SHUNHANDLE)connectionHandle; 
    goto ErrorEnd; 
  } 
 
  /* コネクションハンドルの解放 */
  status = ShunFreeHandle( connectionHandle );
  if ( status != SHUN_SUCCESS ) {
    errorHandle = (SHUNHANDLE)connectionHandle;
    goto ErrorEnd;
  }

  return 0;



ErrorEnd: /* エラーが発生した際のエラー処理 */

  /* エラー情報の取得 */
  status = ShunGetErrorMessage(errorHandle,
                               &errorLevel, &errorMessage);

  if ( status == SHUN_SUCCESS ) {
    switch ( errorLevel ) {
    case SHUN_ERROR_CONNECTION:
      printf("エラーレベル              = SHUN_ERROR_CONNECTION\n");
      break;
    case SHUN_ERROR_DATA:
      printf("エラーレベル              = SHUN_ERROR_DATA\n");
      break;
    }
    printf("エラーメッセージ          = %s\n", errorMessage);
  }
  else {
    printf("エラーメッセージ取得エラー : %d\n", status);
  }

  /* データ操作ハンドルの解放 */
  if ( statementHandle_del != NULL ) {
    status = ShunFreeHandle( statementHandle_del );
    if ( status != SHUN_SUCCESS ) {
      printf("データ操作ハンドル解放エラー : %d\n", status);
    }
  }

  /* データ操作ハンドルの解放 */
  if ( statementHandle_search != NULL ) {
    status = ShunFreeHandle( statementHandle_search );
    if ( status != SHUN_SUCCESS ) {
      printf("データ操作ハンドル解放エラー : %d\n", status);
    }
  }

  /* コネクションの切断 */
  if ( connectionHandle != NULL) {
    status = ShunDisconnect( connectionHandle );
    if ( status != SHUN_SUCCESS ) {
      printf("コネクション切断エラー : %d\n", status);
     }
  }

  /* コネクションハンドルの解放 */
  if ( connectionHandle != NULL) {
    status = ShunFreeHandle( connectionHandle );
    if ( status != SHUN_SUCCESS ) {
      printf("コネクションハンドル解放エラー : %d\n", status);
    }
  }

  return 1;
}

実行結果


ヒット件数                = 1
応答件数                  = 1
削除成功