Implementing data type hierarchy
About this task
If you are designing two or more similar data types, you should consider implementing your own data type hierarchy to avoid writing strategy and support functions for every possible combination of data type signatures.
To implement your own data type hierarchy:
Procedure
- Design a single supertype to which the strategy functions apply.
- Create implicit casts in SQL from all the subtypes to the supertype.
- Create implicit casts in SQL from the built-in data types
LVARCHAR, SENDRECV, IMPEXP, and IMPEXPBIN to the supertype and all
subtypes.
This is part of the normal opaque user-defined data type creation. For more information about how to create these implicit casts, refer to Informix® User-Defined Routines and Data Types Developer's Guide.
- Create the required strategy functions in SQL for just
the supertype.
You do not need to create strategy functions for the subtypes because casts from the subtype to the supertype exist.
- In SQL, create support functions for the supertype and
all the subtypes.
All of these SQL functions, however, can usually be mapped to the same C code; thus only one C function needs to be written.
Results
If the query optimizer is unable to find a function for a particular subtype when it is executing a query, the query optimizer implicitly casts the subtype to the supertype and uses the function defined for the supertype.
The support or strategy function that is defined for the supertype must internally determine what actual data type it is operating on, and then it must execute the code that applies for that particular data type. This means that the internal C code for a function defined for the supertype also contains the C code that applies to all subtypes.