Copying a row structure
To create a copy of a row structure, you must:
- Create a new row descriptor that describes the row type.
- Copy the row values from the old row structure into the col_values and col_nulls arrays to be used for the new row structure.
- Create the new row structure with the values in the col_values and col_nulls arrays.
The following code fragment copies a row structure:
MI_CONNECTION *conn;
MI_ROW_DESC *rowdesc, *new_rowdesc;
mi_integer num_cols, i, len;
MI_DATUM *values;
mi_boolean *nulls;
MI_ROW *new_row;
...
/* Allocate a new row descriptor for the 'name_t' row type */
new_rowdesc = mi_row_desc_create(
mi_typestring_to_id(conn, "name_t"));
/* Determine number of columns needed */
num_cols = mi_column_count(new_rowdesc);
/* 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 */
for ( i=0; i < num_cols; i++ )
{
nulls[i] = MI_FALSE; /* assume non-NULL value */
/* Put field value from original row type ('rowdesc')
* into 'values' array for new row type ('new_rowdesc'
*/
switch ( mi_value(rowdesc, i, &values[i], &len) )
{
case MI_ERROR:
/* Unable to get field value. Raise an error */
break;
case MI_NULL_VALUE:
/* Field value is an SQL NULL value. Set 'nulls'
* array for new row type ('new_rowdesc')
*/
nulls[i] = MI_TRUE;
break;
case MI_NORMAL_VALUE:
/* No action needed: mi_value() call has already
* copied field value into 'values' array
*/
break;
case MI_COLLECTION_VALUE:
/* Need to add code to handle collection */
break;
case MI_ROW_VALUE:
/* Need to add code to handle nested rows */
break;
default:
/* Handle error */
break;
} /* end switch */
} /* end for */
/* Create new row type with values copied from old row type */
new_row = mi_row_create(conn, new_rowdesc, values, nulls);
/* Deallocate memory for 'values' and 'nulls' arrays */
mi_free(values);
mi_free(nulls);
After this code fragment executes, the new_row row structure contains a copy of the values in the row row structure.