INFORMIX
Extending Informix-Universal Server: Data Types
Chapter 5: Creating an Opaque Data Type
Home Contents Index Master Index New Book

A Fixed-Length Opaque Type: circle

The circle data type is an example of a fixed-length opaque type. This data type includes an (x,y) coordinate, to represent the center of the circle, and a radius value. This section briefly shows how to create the circle data type.

Creating the Fixed-Length Internal Structures

Figure 5-2 shows the internal data structures for the circle data type.

Figure 5-2
Internal Data Structures for the circle Opaque Data Type

The internal representation for circle requires three double values: two double values for the center (x and y in the point_t structure) and one for the radius. Because each double is 8 bytes, the total internal length for the circle_t structure is 24 bytes.

Writing the circle Support Functions

The following table shows possible function signatures for the support functions of the circle data type.

Support Function Function Signature

input

output

receive

send

The actual code for the circle data type would be written in the C language, compiled, and put in the circle.so shared library.

Suppose the input and output functions of the circle data type define the external format that Figure 5-3 shows.

Figure 5-3
External Format of the circle Opaque Data Type

The input support function, circle_input(), would accept a string of the form in Figure 5-3, parse the string and store each value in the circle_t structure. The output support function, circle_output(), would perform the inverse operation: it would take the values from the circle_t structure and build a string of the form in Figure 5-3.

Registering the circle Opaque Type

Figure 5-4 shows the SQL statements that register the circle data type and its input, output, send, and receive support functions in the database.

Figure 5-4
Registering the circle Opaque Data Type

The CREATE OPAQUE TYPE statement in Figure 5-4 registers the circle data type, which has the following characteristics:

The CREATE FUNCTION statements in Figure 5-4 register the following support functions:

The CREATE CAST statements create the appropriate casts for the support functions. For more information on these casts, see page 5-17.

Granting Privileges on the circle Data Type

The CREATE OPAQUE TYPE statement (see Figure 5-4) creates the circle data type with the Usage privilege granted to the owner (the person who ran the CREATE OPAQUE TYPE statement). The following GRANT statement allows all users of the database to use the circle data type in SQL statements:

The CREATE FUNCTION statements (also in Figure 5-4) register the support functions for the circle data type with the Execute privilege granted to the owner (the person who ran the CREATE FUNCTION statements). The following GRANT statements grant the Execute privilege to all users of the database on the support statements that are defined as explicit casts:

Creating SQL-Invoked Functions for the circle Data Type

The circle data type has two end-user functions:

    The actual code for the radius() function is written in the C language, compiled, and put in the circle.so shared library.

    The actual code for the area() function is written in the SPL language, so it is provided directly in the CREATE FUNCTION statement.

The following SQL statements register the end-user functions for the circle data type with the database:

Providing Indexes for the circle Data Type

Suppose that you decide that end users can define generic B-tree indexes on the circle data type. To generate a lexicographical sequence, the B-tree index compares the area of two circle values to determines the order of the values. Because the operator-class functions of the B-tree index do not know about the circle data type, you must write the following functions so that the B-tree index can order the values correctly:

You must first write the B-tree strategy functions in C. The following table shows the meaning and possible function signatures for the strategy functions that handle the circle data type.
B-Tree Strategy Function Meaning for circle Data Type Function Signature

lessthan()

area(circle1) < area(circle2)

lessthanorequal()

area(circle1) <= area(circle2)

equal()

area(circle1) = area(circle2)

greaterthan()

area(circle1) > area(circle2)

greaterthanorequal()

area(circle1) >= area(circle2)

These functions might already have been defined when you provided support for the relational operators on your opaque type. (For more information, see "Comparing Data".) In this case, the lessthan(), lessthanorequal(), equal(), greaterthan(), and greaterthanorequal() functions have already been written, compiled, stored in a shared library, and registered in the database. Therefore, the generic B-tree index can already use them as its strategy functions.

However, if you had not yet defined the relational operators for the circle data type, you would need to do so to provide support for B-tree indexes on circle columns and on user-defined functions that return the circle data type. Once you have generated the code for these functions and stored their compiled versions in a shared library, you must register them in the database.

Figure 5-5 shows the CREATE FUNCTION statements that register the B-tree strategy functions for the circle data type with the database.

Figure 5-5
Registering the
B-Tree Strategy Functions for the circle Opaque Data Type

The compare() function is the support function for the generic B-tree index. You might have already defined this function to provide support for the comparison operations in a SELECT statement (such as the ORDER BY clause or the BETWEEN operator) on your opaque type. (For more information, see "Comparing Data".) In this case, the compare() function has already been written, compiled, stored in a shared library, and registered in the database. Therefore, the generic B-tree index can use it as its support function for the circle data type.

However, if you had not yet defined the compare() function for the circle data type, you would need to do so to provide support for B-tree indexes on circle columns and user-defined functions that return the circle data type. Once you have generated the code for this function and stored its compiled version in a shared library, you must register it in the database with the following CREATE FUNCTION statement:

With the strategy and support functions registered, you are ready to define generic B-tree indexes on the circle data type. The code fragment in Figure 5-6 creates a generic B-tree index called circle_ix on the circle_col column.

Tip: To index the values of the area of the circle data type, you could also create a functional index on the area() function.

Using the circle Opaque Data Type

Figure 5-6 shows the SQL statements that create a table called circle_tab that has a column of type circle and insert several rows into this table.

Figure 5-6
Creating a Column of the circle Opaque Data Type

For information about the CREATE INDEX statement in Figure 5-6, see "Providing Indexes for the circle Data Type".

In the INFORMIX-ESQL/C Programmer's Manual, the chapter on opaque types provides examples of how to access the circle opaque type with a fixed binary host variable.




Extending Informix-Universal Server: Data Types, version 9.1
Copyright © 1998, Informix Software, Inc. All rights reserved.