Execute a built-in cast function

The execution of a built-in cast (system or MI_SYSTEM_CAST) function is different from the execution of a user-defined cast function. A user-defined cast function takes only one argument, the data value to be converted, but a built-in cast function takes three arguments.

A built-in cast function takes the following three arguments:
  • The source value to be cast.
  • The length or the maxlength of the target type. If this parameter is NULL, then the casting function obtains the length from the return type information in FPARAM. It is best to either pass this value by getting the length or the maxlength value from the target type or to use the mi_fp_setretlen(fparam,0,length) function.
  • Target type precision value. For the DATETIME, INTERVAL, DECIMAL, or MONEY data types, the total precision value also includes the scale value. It is recommended that you set this target type precision value as NULL, and then use the mi_fp_setretprec() and the mi_fp_setretscale() functions to set the return value's precision and scale.

The FPARAM information for the value argument comes from the source type for the cast. The length and precision fields are both integers so their precision is always 0. The following code fragment is an example of a system cast.

                                    if (cast_status == MI_SYSTEM_CAST)
                       {
                              mi_integer     precision
=
mi_type_precision(tdTarget);
                              mi_integer        length =
mi_type_maxlength(tdTarget);
                  mi_integer scale = mi_type_scale(tdTarget);
                                          fd_fparam =
mi_fparam_get(conn,fd);
                      ....
                     .....
                     mi_setretlen(fd_fparam,0,length);
                     mi_setretprec(fd_fparam,0,precision);
                     mi_setretscale(fd_fparam,0,scale);
                    retValue = mi_routine_exec(conn, fd, &error,
 value,
NULL,NULL);