The following user function, get_results( ), demonstrates the mi_get_result( ) row-retrieval loop, controlled with the mi_get_result( ) function. It also demonstrates the use of the mi_result_command_name( ) function to get the name of the current statement and the mi_result_row_count( ) function to get the number of rows affected by this statement.
/*
* FUNCTION: get_results( )
* PURPOSE: Get results of current statement.
* Obtain the kind of statement and the number of
* rows affected.
* Return the number of rows affected.
*
* CALLED BY: send_statement( ), see page Example: The send_statement( ) Function.
*/
#include "mi.h"
mi_integer get_results(MI_CONNECTION *conn)
{
mi_integer count;
mi_integer result;
char cmd[25];
while ( (result = mi_get_result(conn) )
!= MI_NO_MORE_RESULTS )
{
switch( result )
{
case MI_ERROR:
mi_db_error_raise(conn, MI_EXCEPTION,
"Could not get statement results (mi_get_result)\n");
case MI_DDL:
count = 0;
break;
case MI_DML:
count = handle_dml(conn);
break;
case MI_ROWS:
count = get_data(conn);
break;
default:
mi_db_error_raise(conn, MI_EXCEPTION,
"Unknown statement results (mi_get_result)\n");
} /* end switch */
} /* end while */
return ( count );
}
When a query returns rows of data, the mi_get_result( ) loop in get_results( ) executes three times:
The get_results( ) function executes the MI_ROWS case of the switch statement. This function then calls another user function, get_data( ), to iterate over all query rows. For the implementation of the get_data( ) function, see Example: The get_data( ) Function.
The get_data( ) function has handled the rows in the cursor so no more query rows remain to be processed. The get_results( ) function executes the MI_DML case of the switch statement, which calls the handle_dml( ) function to obtain the name and number of statements from the current statement. For the implementation of the handle_dml( ) function, see Figure 43.
The MI_NO_MORE_RESULTS value from mi_get_result( ) causes the mi_get_result( ) loop to terminate.