Obtain the data pointer
The mi_get_vardata() and mi_get_vardata_align() functions obtain the actual data pointer from the varying-length descriptor. Through this data pointer, you can directly access the varying-length data.
mi_lvarchar *new_lvarch;
char *var_ptr;
...
/* Get the data pointer of the varying-length structure */
var_ptr = mi_get_vardata(new_lvarch);
mi_lvarchar *new_lvarch;
mi_integer var_len, i;
mi_char one_char;
mi_char *var_ptr;
var_ptr = mi_get_vardata(new_lvarch);
var_len = mi_get_varlen(new_lvarch);
for ( i=0; i<var_len; i++ )
{
one_char = var_ptr[i];
/* process the character as needed */
...
}
The database server passes text data to a UDR as an mi_lvarchar structure. Figure 1 shows the implementation of a user-defined function named initial_cap(), which ensures that the first letter of a character string is uppercase and that subsequent letters are lowercase.
The initial_cap() function uses mi_get_vardata() to obtain each character from the data portion of the varying-length structure. This data portion contains the character value that the function receives as an argument. The function checks each letter to ensure that it has the correct case. If the case is incorrect, initial_cap() uses the data pointer to update the appropriate letter. The function then returns a new mi_lvarchar structure that holds the result.
The mi_get_vardata_align() function obtains the number of bytes that the data-length field specifies.