A Varying-Length Opaque Type: image
The image data type is an example of a varying-length opaque type. The image data type encapsulates an image such as a JPEG, GIF, or PPM file. If the image is less than 2 kilobytes, the data structure for the data type stores the image directly. However, if the image is greater than 2 kilobytes, the data structure stores a reference (an LO-pointer structure) to a smart large object that contains the image data. An image stored in the smart large object can be referenced by multiple rows, but the database server only needs to store a single copy of it in an sbspace.
This section briefly outlines how to create the image data type.
Creating the Varying-Length Internal Structure
Figure 5-7 shows the internal structure for the image data type in the database.
Writing the image Support Functions
The following table shows possible function signatures for the basic support functions of the image data type.
The image data type has an embedded smart large object. Therefore, it has the following additional support functions.
The assign() function decides whether to store the image directly in the row or in a separate smart large object. This simple varying-length opaque type does not require a destroy() function.
Registering the image Opaque Type
Figure 5-8 shows the SQL statements that register the image data type and its basic support functions in the database.
The CREATE OPAQUE TYPE statement in Figure 5-8 registers the image data type that has the following characteristics:
The CREATE FUNCTION statements in Figure 5-8 register the following basic support functions:
Figure 5-8 also shows the CREATE CAST statements to create the appropriate cast definitions for the input, output, receive, and send support functions. For more information on the type of casts to create, see "Creating the Casts".
Figure 5-9 shows the SQL statements that register the other support functions for the image data type in the database.
The CREATE FUNCTION statements in Figure 5-9 register the following support functions:
Figure 5-8 also shows the CREATE CAST statements to create the cast definitions for the import, export, importbinary, and exportbinary support functions. For more information on these casts, see page 5-17.
Granting Privileges on the image Data Type
The CREATE OPAQUE TYPE statement (see Figure 5-8) creates the image 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 image data type in SQL statements:
The CREATE FUNCTION statements (see Figure 5-8 and Figure 5-9) register the support functions for the image 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 image Data Type
The image data type has the following end-user functions:
The actual code for the end-user functions would be written in the C language and put in the image.so shared library. The following SQL statements register the end-user functions for the image opaque type with the database.
Providing Indexes for the image Data Type
Suppose you decide that the default secondary access method, a generic B-tree index, does not adequately handle data in columns of type image. Because the image data type holds spatial data, it is a good candidate for an R-tree index. To be able to use an R-tree index, you must first install a spatial DataBlade module that implement the R-tree index. For more information, see "Extending the rtree_ops Operator Class".
When the strategy and support functions for the R-tree secondary access method are registered, you are ready to define R-tree indexes on the image data type. The code fragment in Figure 5-10 creates an R-tree index called image_ix on the image_col column. This CREATE INDEX statement assumes that the DataBlade module enhances the existing default operator class for the R-tree access secondary method.
Using the image Opaque Type
Figure 5-10 shows the SQL statements that create a table called image_tab that has a column of type image and an image identifier.
For information about the CREATE INDEX statement in Figure 5-10, see "Providing Indexes for the image Data Type".
In the INFORMIX-ESQL/C Programmer's Manual, the chapter on opaque types shows examples of how to access the image opaque type with a var binary host variable.
|