Adding your source code to the code generated by BladeSmith

About this task

For each of the support routine types you selected in the New Opaque Type wizard, BladeSmith generates the following functions for the Pnt and Circ data types in the Pnt.c and Circ.c source code files.
Support routine category Routines for Pnt Routines for Circ
Text input and output functions PntInput(), PntOutput() CircInput(), CircOutput()
Binary send and receive functions PntSend(), PntReceive() CircSend(), CircReceive()
Type compare functions PntCompare(), PntEqual(), PntNotEqual() CircCompare(), CircEqual(), CircNotEqual()

Look at the source code for each of these routines. You can see them in Visual C++ under the Source Files node in the File view. You do not need to modify these routines. BladeSmith creates complete support routines to manipulate the data types that compose the Pnt and Circ opaque types.

You must implement the following functions for the Circle DataBlade® module, which you can see under the Globals node in the Class view:
  • Distance()
  • Contains()

These functions are in the udr.c source code file.

To implement the Distance() and Contains() functions:

Procedure

  1. Double-click Distance() in the Class view and scroll to the beginning of the udr.c file.
  2. Add the following statement to the #include statements at the beginning of the udr.c file to include the C math library:
    #include <math.h>

    Because the Contains() function calls the Distance() function, you must have a definition of the Distance() function in the udr.c file before the code for the Contains() function.

  3. Add the following Distance() function prototype directly after the #include statements in udr.c:
    UDREXPORT mi_double_precision *Distance 
    ( Pnt       * Pnt1,
      Pnt       * Pnt2,
      MI_FPARAM * Gen_fparam 
    );
  4. Implement the Distance() function:
    1. Find the comment that begins after Your_Code ... BEGIN and remove it and the mi_db_error_raise() statement that follows it:
       /*             
       ** TO DO: Remove this comment and call to             
       ** mi_db_error_raise after implementing             
       ** this function.             
       */              
       mi_db_error_raise( Gen_Con, MI_EXCEPTION,              
           "Function Distance has not been implemented." );
    2. Find the comment TO DO: Compute and store your value into Gen_RetVal. Add the following code after the comment:
      *Gen_RetVal =           
            sqrt((Pnt1->x - Pnt2->x) * (Pnt1->x - Pnt2->x) +           
            (Pnt1->y - Pnt2->y) * (Pnt1->y - Pnt2->y) );

      This statement calculates the distance between two points by using the Pythagorean formula.

  5. Implement the Contains() function:
    1. Between the comments /* Your_Declarations ... BEGIN */ and /* Your_Declarations ... END */, add the following declaration:
      double * dist;
    2. Replace the code between the comment /* Your_Code ... BEGIN*/ and the comment /*Your_Code ... END*/ with the following code:
       /*              
       ** Computes the distance between             
       ** the center of the circle and             
       ** the point.             
       */             
       dist = Distance( Pnt1, &Circ1->center, Gen_fparam );                     
                          
       /* Is the distance within the radius? */              
       if( (*dist - Circ1->radius) <= 0 )              
       {                   
          Gen_RetVal = 1;             
       }              
       else              
       {              
           Gen_RetVal = 0;              
       }

      BladeSmith makes the return value of Contains() an mi_integer value, which is the closest available C data type to the SQL Boolean return type you defined.

      To determine whether the point is contained within the circle, the Contains() function first uses the Distance() function to calculate the distance between the center of the circle and the point. Then the Contains() 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.

  6. Check your source code against the final version of udr.c.
  7. Save the changes to udr.c.