#include <stdio.h>
#include <string.h>
#include "libshun.h"
/* Transaction processing sample */
int main()
{
/* Handle variables */
SHUNHCON connectionHandle;
SHUNHSTMT statementHandle_search;
SHUNHSTMT statementHandle_update;
/* Working 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;
/* Update data */
SHUNRECID courseRecID[1];
SHUNDATA courseData[1];
SHUNRECID studentRecID[1];
SHUNDATA studentData[1];
/* Error variables */
SHUNHANDLE errorHandle;
int errorLevel;
char *errorMessage;
/* Initialize variables */
connectionHandle = NULL;
statementHandle_search = NULL;
statementHandle_update = NULL;
/* Allocate connection handle */
status = ShunAllocHandle( NULL, &connectionHandle );
if ( status != SHUN_SUCCESS ) {
errorHandle = (SHUNHANDLE)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_search );
if ( status != SHUN_SUCCESS ) {
errorHandle = (SHUNHANDLE)connectionHandle;
goto ErrorEnd;
}
/* Specify the number of search records and the search expression, etc.*/
startNo = 1;
returnRequestCount = 10;
queryForm = "/course/name == 'Business negotiation'";
returnForm = "/";
sortForm = NULL;
/* Call API, search data, and search for course information */
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;
}
if ( hitCount > 0 ) {
/* Save the update data */
courseRecID[0] = recID[0];
}
else {
printf( "None of data is hit in the course information.\n" );
goto ErrorEnd2;
}
/* Specify the number of search records and the search expression, etc. */
startNo = 1;
returnRequestCount = 10;
queryForm = "/student/e-mail == 'mary\\.tompson@def\\.com'";
returnForm = "/";
sortForm = NULL;
/* Call API, search data, and search for student information */
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;
}
if ( hitCount > 0 ) {
/* Save update data */
studentRecID[0] = recID[0];
}
else {
printf( "None of data is hit in the student information\n" );
goto ErrorEnd2;
}
/* Turn off auto-commit mode */
status = ShunSetConnectAttr( connectionHandle, SHUN_ATTR_AUTOCOMMIT, SHUN_FALSE );
if ( status != SHUN_SUCCESS ) {
errorHandle = (SHUNHANDLE)connectionHandle;
goto ErrorEnd;
}
/* Allocate data manipulation handle */
status = ShunAllocHandle( connectionHandle, &statementHandle_update );
if ( status != SHUN_SUCCESS ) {
errorHandle = (SHUNHANDLE)connectionHandle;
goto ErrorEnd;
}
/* Create course information data */
courseData[0].Data =
"<course>"
" <name>Business negotiation</name>"
" <instructor>"
" <first-name>Max</first-name>"
" <last-name>cameron</last-name>"
" </instructor>"
" <capacity>40</capacity>"
" <current-auditors>31</current-auditors>"
"</course>";
courseData[0].Data_Len = strlen( courseData[0].Data );
/* Update information (update data by adding the student */
status = ShunUpdateRecId(
statementHandle_update,
1,
courseRecID,
courseData );
if ( status != SHUN_SUCCESS ) {
errorHandle = (SHUNHANDLE)statementHandle_update;
goto ErrorEnd;
}
printf( "Finished updating course information:\n" );
/* Create student data */
studentData[0].Data =
"<student>"
" <first-name>Mary</first-name>"
" <last-name>Tompson</last-name>"
" <e-mail>mary.tompson@def.com</e-mail>"
" <course>Chinese language</course>"
" <course>Business negotiation</course>"
"</student>";
studentData[0].Data_Len = strlen( studentData[0].Data );
/* Add student */
status = ShunUpdateRecId(
statementHandle_update,
1,
studentRecID,
studentData );
if ( status != SHUN_SUCCESS ) {
errorHandle = (SHUNHANDLE)statementHandle_update;
goto ErrorEnd;
}
printf( "Finished updating student information\n" );
/* Commit the updates made so far */
status = ShunCommit( connectionHandle );
if ( status != SHUN_SUCCESS ) {
errorHandle = (SHUNHANDLE)connectionHandle;
goto ErrorEnd;
}
/* Release the data manipulation handle */
status = ShunFreeHandle( statementHandle_search );
if ( status != SHUN_SUCCESS ) {
errorHandle = (SHUNHANDLE)statementHandle_search;
goto ErrorEnd;
}
/* Release the data manipulation handle */
status = ShunFreeHandle( statementHandle_update );
if ( status != SHUN_SUCCESS ) {
errorHandle = (SHUNHANDLE)statementHandle_update;
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 = (SHUNHANDLE)connectionHandle;
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_TRANSACTION:
printf("Error level = SHUN_ERROR_TRANSACTION\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);
}
if ( connectionHandle != NULL ) {
status = ShunRollback( connectionHandle );
if ( status != SHUN_SUCCESS ) {
printf( " Failed to roll back transaction \n" );
}
}
ErrorEnd2:
/* Release the data manipulation handle */
if ( statementHandle_update != NULL ) {
status = ShunFreeHandle( statementHandle_update );
if ( status != SHUN_SUCCESS ) {
printf("Error releasing data manipulation handle : %d\n", status);
}
}
/* Release the data manipulation handle */
if ( statementHandle_search != NULL ) {
status = ShunFreeHandle( statementHandle_search );
if ( status != SHUN_SUCCESS ) {
printf("Error releasing data manipulation handle : %d\n", status);
}
}
/* Release the connection handle */
if ( connectionHandle != NULL) {
int connection_dead;
status = ShunGetConnectAttr( connectionHandle, SHUN_ATTR_CONNECTION_DEAD, &connection_dead );
if ( status != SHUN_SUCCESS ) {
printf("Error obtaining connection handle attribute : %d\n", status);
}
if ( connection_dead == SHUN_FALSE ) {
status = ShunDisconnect( connectionHandle );
if ( status != SHUN_SUCCESS ) {
printf("Connection close error : %d\n", status);
}
}
status = ShunFreeHandle( connectionHandle );
if ( status != SHUN_SUCCESS ) {
printf("Connection handle release error : %d\n", status);
}
}
return 1;
) |