Adding source code to the templates generated by BladeSmith
About this task
- rciDistance()
- rciContains()
These functions are all in the udr.c file. Do not modify the other generated functions that begin with a Gen_ prefix.
To implement the rciDistance() and rciContains() functions:
Procedure
- Double-click rciDistance() in the Class view and scroll to the beginning of the udr.c file.
- Add the following statement to the #include statements
to include the C math library:
#include <math.h>Because the rciContains() function calls the rciDistance() function, you must have a definition of the rciDistance() function in the udr.c file before the code for the rciContains() function.
- Add the following rciDistance() function
prototype directly after the #include statements in udr.c:
UDREXPORT mi_double_precision *rciDistance ( MI_ROW * point1, MI_ROW * point2, MI_FPARAM * Gen_fparam ); - Implement the rciDistance() function:
- Implement the rciContains() function
by replacing the code between the comment containing
Your_Code ... BEGINand the comment containingYour_Code ... ENDwith the following code fragment:{ mi_double_precision * dist; mi_integer retlen; MI_DATUM center, radius; /* ** Fetch values from rows */ mi_value( circle, 0, ¢er, &retlen ); mi_value( circle, 1, &radius, &retlen ); /* ** Computes the distance between ** the center of the circle and ** the point. */ dist = rciDistance( point, center, Gen_fparam ); /* Is the distance within the radius? */ if ((*dist - *(mi_double_precision *)radius) <= 0) { Gen_RetVal = 1; } else { Gen_RetVal = 0; } }BladeSmith makes the return value an mi_integer type, which is the closest available C data type to the SQL Boolean return type you defined. This mismatch between the C and SQL type systems is known as impedance. The C code in user-defined routines must map the arguments of the routine and return values between the two type systems.
The elements of the rciCircle row data type, the radius and center of a circle, are extracted by using the mi_value() statement and are stored in variables of type MI_DATUM. See the HCL® Informix® DataBlade® API Programmer's Guide for information about handling row types in C.
To determine whether the point is contained within the circle, the rciContains() function first uses the rciDistance() function to calculate the distance between the center of the circle and the point. Then the rciContains() function subtracts the radius from the distance. If the result is negative, the point is contained by the circle; if the result is positive, the point is outside the circle.
In the subtraction, the radius is cast from an MI_DATUM type to an mi_double_precision type.
- Check your source code against the final version of udr.c.
- Save the changes to udr.c.