An iterator function is a user-defined function that returns to its calling SQL statement several times, each time returning a value. The database server gathers these returned values together in an active set. To access a value in the active set, you must obtain it from a database cursor. Therefore, an iterator function is a cursor function because it must be associated with a cursor when it is executed.
The database server might execute an iterator function many times. It groups these iterations into the iterator-status values and puts the iterator status for a given iteration in the MI_FPARAM structure. Within an iterator function, you examine the MI_FPARAM structure for an iterator status to determine which actions the iterator function must take.
To specify the different points at which the database server calls an iterator function, the iterator-status flag (of type MI_SETREQUEST) supports the constants in Table 102.
For example, to return an active set of integer values, declare the iterator function to return the mi_integer data type.
The MI_FPARAM structure holds the iterator status, the iterator-completion flag, and the user-state pointer.
This function returns the iterator-status constant (SET_INIT, SET_RETONE, or SET_END) that the database server has set for the distinct groups of iterations of the iterator function.
Iterator-Status Value | More Information |
---|---|
SET_INIT | Initializing the Iterations |
SET_RETONE | Returning One Active-Set Item |
SET_END | Releasing Iteration Resources |
Omit the MI_FPARAM parameter from the parameter list when you register the iterator function. For more information, see Registering a C UDR.
The Fibonacci series is a list of numbers for which each value is the sum of the previous two. For example, the Fibonacci series up to a stop value of 20 is as follows:
0, 1, 1, 2, 3, 5, 8, 13
Figure 89 is an implementation of an iterator function named fibGen( ). This function builds an active set that contains a Fibonacci series of numbers up to a specified stop value.
typedef struct fibState1 /* function-state structure */ { mi_integer fib_prec1; /* second most recent number in series */ mi_integer fib_prec2; /* most recent number in series */ mi_integer fib_ncomputed; /* number computed */ mi_integer fib_endval; /* stop value */ }fibState; /* fibGen( ): an iterator function to return the Fibonacci series. * This function takes a stop value as a parameter and returns the * Fibonacci series up to this stop value. * * Three states of iterator status: * SET_INIT : Allocate the defined user-state structure. * SET_RETONE : Compute the next number in the series. * SET_END : Free the user-allocated user-state structure. */ mi_integer fibgen(stop_val,fparam) mi_integer stop_val; MI_FPARAM *fparam; { mi_integer next; fibState *fibstate = NULL; switch( mi_fp_request(fparam) ) { case SET_INIT: next = fibGen_init(stop_val, fparam); break; case SET_RETONE: next = fibGen_retone(fparam); fibstate = (fibState *)mi_fp_funcstate(fparam); if ( next > fibstate->fib_endval ) { mi_fp_setisdone(fparam, 1); next = 0; /* return value ignored */ } break; case SET_END: next = fibGen_end(fparam); break; } return (next); }
The database server calls this fibGen( ) iterator function at the following execution points:
At this point, the database server has set the iterator status to SET_INIT and fibGen( ) calls the fibGen_init( ) function (see Figure 90).
As long as the number is less than the stop value, the database server sets the iterator status to SET_RETONE and fibGen( ) calls the fibGen_retone( ) function (see Figure 91).
At this point, the database server has set the iterator status to SET_END and fibGen( ) calls the fibGen_end( ) function (see Figure 92).
When the iterator function reaches the last item, call the mi_fp_setisdone( ) function to set the iterator-completion flag of the MI_FPARAM structure to one (1). This flag indicates to the database server that it has reached the end condition for the iterator function. The database server no longer needs to continue calling the iterator function with the SET_RETONE iterator-status value. Instead, it calls the iterator function one more time, with the SET_END status value.
In Figure 89, the fibGen( ) iterator function determines if it has reached an end condition after it calls fibGen_retone( ). It makes this determination as follows:
The fibGen( ) function calls the mi_fp_setisdone( ) function to set the iterator-completion flag to 1. The function then exits with a return value of zero (0). However, this last return value of 0 is not returned as part of the active set. The database server calls the next iteration of fibGen( ) with an iterator-status value of SET_END.
The function returns this next number in the Fibonacci series to the active set. The database server calls the next iteration of fibGen( ) with an iterator-status value of SET_RETONE.