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

Part 1 The Basic for Developing Application> Chapter 7 Shunsaku File> 7.4 Operating Multiple Shunsaku Files in One Transaction

7.4.3 Sample Code for C API

The following code is the sample code for operating multiple Shunsaku Files in one transaction using C API.

/* This program assumes that the following variables have been declared      */
/* personData : The information of employees to be added*/
/* query      :Search expression                                */
/* returnQuery:Return expression                            */
/* updateData :Update data                            */
/* pResultId  :Structure array holding record ID        */
/* pUpdateId  : Structure array for setting the record IDs to be updated     */

/* Allocate a connection handle */
ret = ShunAllocHandle(NULL, &ConH);
if(ret != SHUN_SUCCESS) {
   error("ShunAllocHandle ", ConH);
   return;
}
/* Establish a connection by specifying the host name. port number and Shunsaku File */
ret = ShunConnect(ConH, "DirSvr1", 33101, "fileA");
if(ret != SHUN_SUCCESS) {
   error("ShunConnect ", ConH);
   return;
}
/* Allocate a data manipulation handle for adding data */
ret = ShunAllocHandle(ConH, &insertStmtH);
if(ret != SHUN_SUCCESS) {
   error("ShunAllocHandle ", ConH);
   return;
}
/* Turn auto commit off */
ret = ShunSetConnectAttr(ConH, SHUN_ATTR_AUTOCOMMIT, SHUN_FALSE);
if(ret != SHUN_SUCCESS) {
   error("ShunSetConnectAttr ", ConH);
   return;
}
/* Add data */
ret = ShunInsert(InsertStmtH, 1, personData);
if(ret != SHUN_SUCCESS) {
   error("ShunInsert ", insertStmtH);
   return;
}
/* Release the data manipulation handle for adding data */
ret = ShunFreeHandle(insertStmtH);
if(ret != SHUN_SUCCESS) {
   error("ShunFreeHandle ", insertStmtH);
   return;
}
/* Switch Shunsaku File to fileD */ 
ret = ShunSetConnectAttr(ConH, SHUN_ATTR_SHUNSAKU_FILE, (int)" fileD");
if(ret != SHUN_SUCCESS) {
   error("ShunSetConnectAttr ", ConH);
   return;
}
/* Allocate a data manipulation handle for searching data */
ret = ShunAllocHandle(ConH, &searchStmtH);
if(ret != SHUN_SUCCESS) {
   error("ShunAllocHandle ", ConH);
   return;
}
/* Execute search */
ret = ShunSearch(searchStmtH, 1, NULL, 0, 30, query, returnQuery,
                 NULL, &Hit_Cnt, &Return_Cnt, &Returnable_Cnt,
                 &Rec_Id_Out, &Data, &First_Pos, &Last_Pos);
if(ret != SHUN_SUCCESS) {
   error("ShunSearch ", searchStmtH);
   return;
}
/* Retain record ID */
for(i=0; i< Return_Cnt; i++) {
    pResultId[i] = Rec_Id_Out[i];
}
/* Release the data manipulation handle for searching data */
ret = ShunFreeHandle(searchStmtH);
if(ret != SHUN_SUCCESS) {
   error("ShunFreeHandle ", searchStmtH);
   return;
}
/* Allocate a data manipulation handle for updating data */
ret = ShunAllocHandle(ConH, &updateStmtH);
if(ret != SHUN_SUCCESS) {
   error("ShunAllocHandle ", ConH);
   return;
}
/* Update the 0th data */
pUpdateId[0] = pResultId[0];
/* Update data */
ret = ShunUpdateRecId(updateStmtH, 1, pUpdateId,updateData);
if(ret != SHUN_SUCCESS) {
  error("ShunUpdateRecId", updateStmtH);
  return;
}
/* Release the data manipulation handle for updating data */
ret = ShunFreeHandle(updateStmtH);
if(ret != SHUN_SUCCESS) {
   error("ShunFreeHandle ", updateStmtH);
   return;
}
/*  End the transactions */
ret = ShunCommit(ConH);
if(ret != SHUN_SUCCESS) {
   error("ShunCommit ", ConH);
   return;
}
/* Close the connection */
ret = ShunDisconnect(ConH);
if(ret != SHUN_SUCCESS) {
   error("ShunDisconnect", ConH);
   return;

}
/* Release the connection handle */
ret = ShunFreeHandle(ConH);
if(ret != SHUN_SUCCESS) {
   error("ShunAllocHandle ", ConH);
   return;
}

/* Error processing */
void error(char* func, SHUNHANDLE handle) {
  printf( "%s:abnormal end\n", func );
  ShunGetErrorMessage(handle, &Err_Level, &Err_Msg);
  printf("Error level = %d\n", Err_Level);
  printf("Error message = %s\n", Err_Msg);
  if(ConH != NULL)
  {
    ret = ShunRollback(ConH);
  }
  ret = ShunFreeHandle(ConH); 
  return;
}

Contents Index PreviousNext

All Rights Reserved, Copyright(C) FUJITSU LIMITED 2006