3.3 Generic Coding Example
The following example shows how to apply the LANSA Open functions and is not directed to any one language. It is supplied to illustrate the steps required, using familiar constructs.
Note that this is a simple example and detailed error handling has been omitted. Your own application programs should check the return codes from the LANSA Open functions and provide for the appropriate handling of any errors.
/* SESSION DEFINITION SECTION */
/* Get a session ID */
iSession = LceGetSessionId()
if not iSession
return FALSE
endif
/* Specify LANSA partition to be used as DEM */
if not LceUsePartition ( iSession, "DEM" )
return FALSE
endif
/* Specify the language of the partition as French */
if not LceUseLanguage ( iSession, "FRA" )
return FALSE
endif
/* Set the execution priority on host to 20 */
if not LceUsePriority ( iSession, 20 )
return FALSE
endif
/* Define the files and fields to be used */
if not LceUseFile ( iSession, "PSLMST,DEPTAB" )
return FALSE
endif
if not LceUseField ( iSession, "EMPNO,SURNAME,GIVENAME,SALARY" )
return FALSE
endif
/* OPEN SESSION SECTION */
if not LceOpenSession ( iSession ) . . .
Error ("Could not open a session.")
return FALSE
endif
/* DATA ACCESS SECTION */
/* Fetch an employee record */
/* Set the value of the key field, EMPNO */
if not LceSetFieldValue ( iSession, "EMPNO", "A1001" )
return FALSE
endif
/* Fetch the record from the host */
If not LceFetch ( iSession, "SURNAME, GIVENAME, SALARY", "PSLMST", "EMPNO" )
Error ("Employee record not found.")
return FALSE
else
/* Get the value of the SURNAME field */
if not LceGetFieldValueX ( iSession, "SURNAME", strSurname , lFlags)
return FALSE
endif
/* Get the value of the SALARY field */
if not LceGetFieldValueX ( iSession, "SALARY", strSalary , lFlags)
return FALSE
endif
/* Convert the SALARY field from a string to a float */
LceASCIIToFloat ( strSalary, nSalary )
endif
/* Select all employee records ( No keys ) */
/* Using *RECEIVEIMMED and specifying a buffer with LceReceiveNextX should result in
better performance */
if not LceSetSelectOptions(iSessionId, "*RECEIVEIMMED")
return FALSE
endif
/* Specify the records to be selected from the host */
if not LceRequestSelect ( iSession, "EMPNO, SURNAME, SALARY", "PSLMST", "", FALSE )
return FALSE
else
/* Transfer the selected records to the PC */
Do While ( LceReceiveNextX( iSessionId, strBuff, lSize,
fldData, iTotalFields ) = LceTrue )
strEmpNo = CopyString( strBuff, fldData(0).fieldLen )
iStartPos = fldData(0).fieldLen
strSurname = CopyString( strBuff, iStartPos, fldData(1).fieldLen )
iStartPos = iStartPos + fldData(1).fieldLen
strSalary = CopyString( strBuff, iStartPos, fldData(2).fieldLen )
End While
endif
/* END SESSION SECTION */
LceEndSession ( iSession, FALSE )
/* iSession is now invalid and the data dictionary has been removed from memory. */
/* Alternative method for performing the above query */
/* Specify the records to be selected from the host */
if not LceRequestSelect ( iSession, "EMPNO, SURNAME, SALARY", "PSLMST", "", FALSE )
return FALSE
else
/* Transfer the selected records to the PC */
if not LceReceiveSelect ( iSession, "PSLMST" )
return FALSE
else
/* Determine how many records were transferred */
LceGetRecordCount ( iSession, "PSLMST", nRecordCount )
/* Loop through all transferred records */
For ( I = 1 to nRecordCount )
/* Get a record from the data transferred */
LceGetSelect ( iSession, "PSLMST", I, "EMPNO,SURNAME" )
LceGetFieldValueX (iSession, "EMPNO", strEmpno , lFlags)
LceGetFieldValueX (iSession, "SURNAME", strSurname , lFlags)
Next
endif
endif
/* END SESSION SECTION */
LceEndSession ( iSession, FALSE )
/* iSession is now invalid and the data dictionary has been removed from memory. */