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

What Is an Opaque Data Type?

An opaque data type is an atomic data type that you define for the database. An opaque type gets its name from the fact that the database server knows nothing about the internal representation of the type. Unlike built-in types, for which Universal Server knows the internal format, the opaque types are encapsulated; that is, Universal Server has no knowledge of the format of the data within an opaque type.

When you define an opaque data type, you extend the data type system of the database server. The new opaque data type can be used in the same way as any built-in data type that the database server provides. To define the opaque data type to the database server, you must provide the following information in the C language:

The following sections introduce each of these parts of an opaque data type. For information on how to create these parts, see "How Do You Create an Opaque Data Type?".

The Internal Structure

To create an opaque type, you must first provide a data structure that stores the data in its internal representation. This data structure is called the internal structure of the opaque data type because it is how the data is stored on disk. The support functions that you write (see "Support Functions") operate on this internal structure; the database server never sees it. You create the internal structure as a C-language data structure.

You can define an internal structure that supports either of the following kinds of opaque types:

A Fixed-Length Opaque Type

A fixed-length opaque type has an internal structure whose size is the same for all possible values of the opaque type. Fixed-length opaque types are useful for data that you can represent in fixed-length fields, such as numeric values. For an example of this data type, see "A Fixed-Length Opaque Type: circle".

You provide the size when you register the opaque type in the database. For more information, see "Final Structure Size".

A Varying-Length Opaque Type

A varying-length opaque type has an internal structure whose size might be different for different values of the opaque type. Varying-length opaque types are useful for storage of multirepresentational data, such as images. For example, image sizes vary from one picture to another. You might store data up to a certain size within the opaque type and use a smart large object in the opaque type if the image size exceeds that size. For an example of this data type, see "A Varying-Length Opaque Type: image".

When you register the opaque type in the database, you indicate that the size is varying, and you can indicate a maximum size for the internal structure. For more information, see "Final Structure Size".

Support Functions

The support functions of an opaque data type tell the database server how to interact with the internal structure (see "The Internal Structure") of the opaque type. Because an opaque type is encapsulated, the database server has no knowledge of the internal structure. It is the support functions, which you write, that interact with this internal structure. The database server calls these support functions to perform the interactions.

The following table summarizes the support functions of the opaque data type.

(1 of 2)

Function Purpose

input

Converts the opaque-type data from its external representation to its internal representation.

output

Converts the opaque-type data from its external representation to its internal representation.

receive

Converts the opaque-type data from its internal representation on the client computer to its internal representation on the server computer.

send

Converts the opaque-type data from its internal representation on the server computer to its internal representation on the client computer.

import

Performs any tasks needed to process an opaque type when a bulk copy imports the opaque type in its external representation.

export

Performs any tasks needed to process an opaque type when a bulk copy exports the opaque type in its external representation.

importbinary

Performs any tasks needed to process an opaque type when a bulk copy imports the opaque type in its internal representation.

exportbinary

Performs any tasks needed to process an opaque type when a bulk copy exports the opaque type in its internal representation.

assign()

Performs any tasks needed to process an opaque type before storing it to disk.

destroy()

Performs any tasks needed to process an opaque type necessary before the database server removes a row that contains the type.

lohandles()

Returns a list of the smart large objects embedded in the opaque type.

compare()

Compares two values of opaque-type data during a sort.

For more information on the opaque-type support functions, see Chapter 6, "Writing Support Functions."

Additional SQL-Invoked Routines

The support functions provide the basic functionality that the database server needs to interact with your opaque data type. However, you might want to write additional user-defined routines to provide the following kinds of functions for your opaque data type:

Built-In Functions

A built-in function is a predefined function that Universal Server provides for use in an SQL expression. Universal Server supports the built-in functions on the built-in data types. To have a built-in function operate on the opaque data type, you must write a version of the function that accepts the opaque data type in its parameter list.

For general information about these built-in functions, see "Built-In Functions". For information on how to implement a built-in function on an opaque type, see "Built-In Functions for Opaque Data Types".

Operator Functions

An operator function is a user-defined function that has a corresponding operator symbol. For an operator function to operate on the opaque data type, you must write a version of the function that accepts the opaque data type in its parameter list.

For general information about the operator functions that Universal Server provides, see "Operators and Operator Functions". For information on how to implement an operator function on an opaque type, see "Operator Functions for Opaque Data Types".

Aggregrate Functions

An aggregate function returns one value for a set of queried rows. Universal Server supports only the following aggregate functions on an opaque data type:

For any other aggregate function to work with your opaque type, you must create your own version of the aggregate function. For more information on aggregate functions, see the Expression segment in the Informix Guide to SQL: Syntax.

End-User Routines

Universal Server allows you to define SQL-invoked functions or procedures that the end user can use in expressions of SQL statements. These end-user routines provide additional functionality that an end user might need to work with the opaque data type. Examples of end-user routines include the following:

    Because the opaque type is encapsulated, an end-user function is the only way users can access fields of the internal structure.

    Several of the support functions serve as casting functions between basic data types that the database server uses. You might also write additional casting functions between the opaque type and other data types (built-in, opaque, or complex) of the database.

    If an operation or task is performed often on the opaque type, you might want to write an end-user routine to perform this task.

For more information about how to write end-user routines, see "Creating End-User Routines".




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