The pseudocode below illustrates a typical algorithm for retrieving result sets using a RecordSet object. getEmployeeDetails() is a method in the component that returns a single result set as a RecordSet object. The algorithm executes three nested loops, as follows:
The outermost loop iterates through RecordSet objects. Each object contains rows from one result set. After rows have been retrieved, the example calls the RecordSet.NextRecordSet method. This method returns the next RecordSet object. The outermost loop terminates when RecordSet.NextRecordSet has set the RecordSet.EOF property to true.
The middle loop iterates through the rows in a result set, calling the RecordSet.MoveNext method until the RecordSet.EOF property tests as true. Inside the loop, the RecordSet.Fields property provides a Fields object that allows access to the row’s columns.
The innermost loop iterates through the columns in a row, using the Fields.Item property to retrieve the Field object that represents each column.
Here is the algorithm:
Integer employee_id RecordSet = proxycomponent.getEmployeeDetails(employee_id) DO // Position the row pointer before the first row. RecordSet.MoveFirst()
// Iterate through all the rows. WHILE RecordSet.EOF = FALSE // Fields object represents the current row. Fields = RecordSet.Fields // Iterate through columns. FOR i = 0 TO i = (Fields.Count - 1) Field = Fields.Item(i) ... retrieve Field properties to process column data as desired ... END FOR // Move to the next row. RecordSet.MoveNext() END WHILE // Move to the next result set, if any. RecordSet = RecordSet.NextRecordSet() WHILE RecordSet.EOF = FALSE
The logic in this example executes correctly if a method has not returned result sets. In this case, the RecordSet.EOF property is always false.
Some scripting languages may allow or require variations on this algorithm, for example:
You can replace the WHILE loop logic that iterates through rows with a FOR loop that indexes from 1 to RecordSet.Count.
Some scripting languages provide a FOR EACH loop syntax that allows iterations over an ActiveX collection. You can use this construct to iterate through the Field objects in a Fields collection. For example, in Microsoft Visual Basic, you can use code similar to this:
’Get the collection of fields from record set
Set flds = recset.Fields For Each fld in flds ’Process each Field as desired Next fld
Copyright © 2005. Sybase Inc. All rights reserved. |