INFORMIX DataBlade Developers Kit Tutorial

List of Exercises

Exercise 5: Creating Opaque Data Types, continued


6

Generate the source code and SQL registration scripts.

See " Generating Code" for detailed instructions.

BladeSmith generates the basic C code and SQL scripts necessary for your DataBlade module to run:

  • The source code is saved in the Circle\src\C directory.
  • The SQL scripts are saved in the Circle\scripts directory.

The BladeSmith portion of this exercise is complete; you can exit BladeSmith.


7

Add your source code to the templates generated by BladeSmith.

BladeSmith generates four source code files for the Circle DataBlade module:

  • Circ.c. For the Circ opaque type support routines
  • Pnt.c. For the Pnt opaque type support routines
  • udr.c. For the user-defined routines, Distance() and Contains()
  • support.c. For tracing and BladeSmith utility functions (do not edit this file)

Look at the source code for the Circ and Pnt data type support routines in the Circ.c and Pnt.c files. You do not need to modify these files. BladeSmith creates complete support routines; the generated code manipulates the data types that compose the Pnt and Circ opaque types.

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.

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()

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 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 *Distance
    (
    Pnt       * Pnt1,
    Pnt       * Pnt2, 
    MI_FPARAM * Gen_fparam
    );
    
    

Implement the Distance() function. This function implements the same functionality as the rciDistance() function in the RowCircle DataBlade module exercise.

To implement the Distance() function

    1. Following the comment containing Your_Code ... BEGIN, find the comment below 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. Replace the statement Gen_RetVal = 0; with the following code:

            *Gen_RetVal = 
                  sqrt((Pnt1->x - Pnt2->x) * (Pnt1->x - Pnt2->x) + 
                  (Pnt1->y - Pnt2->y) * (Pnt1->y - Pnt2->y) ); 
    
    

Implement the Contains() function. This function implements the same functionality as the rciContains() function in the RowCircle DataBlade module exercise.

To 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, 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.

Check your source code against the final version of udr.c.

Save the changes to udr.c.


Copyright © 1998, Informix Software, Inc. All rights reserved.