To create a row type, you create a row structure (MI_ROW) that holds the row type. The mi_row_create( ) function is the constructor function for the row structure (MI_ROW). To create a row type with mi_row_create( ), you must provide the following information to the function:
You create a new row descriptor for a row type with the mi_row_desc_create( ) function. The mi_row_desc_create( ) is the constructor function for a row descriptor. You provide this function with the type identifier of the row type for which you want the row descriptor. If you do not know the type identifier for your row type, use the mi_type_typename( ) function or mi_typestring_to_id( ) to create a type identifier based on the type name. The type name for a row type is its text representation. For more information, see Row-Type Text Representation.
To provide values for the columns (or fields) of a row structure, you pass information for the columns in several parallel arrays:
These column-value arrays are similar to the column arrays in the row descriptor (see Figure 23). They have an element for each column in the row descriptor. The column-value arrays are different from the column arrays in the row descriptor, in the following ways:
Column arrays describe the column data type.
The DataBlade API does not provide accessor functions for these column-value arrays. For each column, your DataBlade API module must declare, allocate, and assign values to these arrays.
All of the column-value arrays have zero-based indexes. Figure 24 shows how the information at index position 1 of these arrays holds the column-value information for the second column of a row.
The following sections provide additional information about each of the column-value arrays.
The column-value array, col_values, is the third argument of the mi_row_create( ) function. Each element of the column-value array is a pointer to an MI_DATUM structure that holds the value for each column. The format of this value depends on whether the MI_DATUM value is passed by reference or by value:
For more information on the passing mechanism for an MI_DATUM value, see Contents of an MI_DATUM Structure.
The column-value null array, col_nulls, is the fourth argument of the mi_row_create( ) function. Each element of the column-value null array is either:
Suppose you have the row type that the following SQL statement creates:
CREATE ROW TYPE rowtype_t ( id INTEGER, name CHAR(20) );
The following code shows how to use the mi_row_create( ) function to create a new row type of type rowtype_t:
/* * Create a row structure for the 'rowtype_t' row type */ MI_CONNECTION *conn; MI_ROW_DESC *rowdesc; MI_ROW *row; MI_DATUM *values; mi_boolean *nulls; mi_integer num_cols; /* Allocate a row descriptor for the 'rowtype_t' row type */ rowdesc = mi_row_desc_create( mi_typestring_to_id(conn, "rowtype_t")); /* Assume number of columns is known */ num_cols = 2; /* Allocate the 'col_values' and 'col_nulls' arrays */ values = mi_alloc(num_cols *sizeof(MI_DATUM)); nulls = mi_alloc(num_cols *sizeof(mi_boolean)); /* Populate the 'col_values' and 'col_nulls' arrays */ /* Initialize value for field 1: 'id' */ values[0] = 1; nulls[0] = MI_FALSE; /* Initialize value for field 2: 'name' */ values[1] = mi_string_to_lvarchar("Dexter"); nulls[1] = MI_FALSE; /* Create row structure for 'name_t' */ row = mi_row_create(conn, rowdesc, values, nulls);
When this code completes, the row variable points to a row structure that contains the following field values.
If the preceding code fragment were part of a client LIBMI application, it would require changes to the way the values are addressed in the values array. For example, the INTEGER value would require the following cast to create a copy of the column value:
mi_integer col_val; ... /* Initialize value for field 1: 'id' */ col_val = 1; values[0] = &col_val; nulls[0] = MI_FALSE;
This different kind of addressing is required because in client LIBMI applications, mi_row_create( ) passes values for all data types by reference. Therefore, the contents of the MI_DATUM structure is always a pointer to the actual value, never the value itself.