INFORMIX
DataBlade API Programmer's Manual
Chapter 4: Using the MI_FPARAM Structure
Home Contents Index Master Index New Book

Creating an Iterator Function

To have a DataBlade function return more than one row of values in an SQL statement, you must write it as an iterator function. The database server automatically calls an iterator function once for each row of return values. This row can contain column values from a database table or return values from some other external function.

Tip: For end users to be able to use an iterator function within an SQL statement, you must register the iterator function with the CREATE FUNCTION statement. The CREATE FUNCTION must include the ITERATOR routine modifier to tell the database server that it must call the function several times. For more information on how to register iterator functions, see "Extending INFORMIX-Universal Server: User-Defined Routines."

The database server uses the function-parameter structure, MI_FPARAM, to tell a DataBlade iterator function about which iteration operation to perform next. To have a DataBlade function return more than one value (or row), you must define it as an iterator function. An iterator function can return more than one value from one of the following conditions:

The group of values that one of these conditions returns is called an active set. The database server calls an iterator function to access the active set in the following cases.
What Does the Iterator Function Do? When Is the Iterator Function Called? fp_request Field of MI_FPARAM

Initializes the active set

The first time that the DataBlade function is called

SET_INIT

Returns one value/row from the active set

For each row in the active set

SET_RETONE

Shuts down the active set

After the last row is returned from the active set

SET_END

The DataBlade iterator function must record enough information to return values one at a time on demand. Declare the iterator function so that its return value matches one of the return values of the active set. For example, to return an active set of integer values, declare the iterator function to return an integer.

The following code fragment is a sample implementation of an iterator function called get_orders(), which generates an active set of order numbers for a given customer number and then iterates through the active set to return each order number:

The database server calls this get_orders() iterator function at the following execution points:

Initializing the Active Set of Values

An active set is the row values that one of the following SQL statements generates:

The first time that the database server calls the iterator function, the database server passes it an MI_FPARAM structure with the fp_request field set to SET_INIT and the fp_funcstate field set to zero. To initialize the active set, some function must perform the following tasks:

Each subsequent call to the iterator function uses the same MI_FPARAM structure, so each iteration can reuse the cached memory.

Returning a Value from the Active Set

Once the initialization function has saved the active set in the MI_FPARAM structure, the database server can call the iterator function repeatedly to return the values from the active set. Each time the database server calls the iterator function to return a value, the fp_request field of the MI_FPARAM structure is set to SET_RETONE.

When SET_RETONE is set, the function can call a value-return function to return the next value in the active set. When this value-return function has returned the last value in the active set, it calls the mi_fp_setisdone() function to set the fp_isdone field of the MI_FPARAM structure to MI_TRUE. This value indicates to the calling function that the last value has been returned.

Shutting Down the Active Set

When the value-return function (see the previous section) notifies the iterator function that the active set is exhausted, the database server calls the iterator function one last time with the fp_request field of the MI_FPARAM structure set to SET_END. This fp_request value indicates that the iterator function should perform any tasks needed to shut down the active set. The shutdown function should release any memory that the active set uses.

Calling an Iterator Function from an SQL Statement

When you write a DataBlade function whose state information you wish to save, you must provide an extra argument at the end of the argument list for the function parameter. When you use this function in an SQL statement, you omit this function-parameter argument.

For more information on how to register an iterator function for use by end users, see Extending INFORMIX-Universal Server: User-Defined Routines.




DataBlade API Programmer's Manual, version 9.1
Copyright © 1998, Informix Software, Inc. All rights reserved.