![]() |
|
You can access the internal or binary format of an opaque data type with an ESQL/C host variable in two ways:
Both the fixed binary and var binary data types have a one-to-one mapping between their declaration and the internal data structure of the opaque data type. The database server invokes the following support functions of the opaque data type when the application transfers data in the fixed binary or var binary host variables:
The following sections describe the fixed binary and var binary data types.
The fixed binary data type allows you to access a fixed-length opaque-type column in its internal format. Follow these steps to transfer the internal format of a fixed-length opaque-type column between the database server and the ESQL/C application:
The following sections describe each of these steps in more detail.
Use the fixed binary data type to declare host variables that access the internal format of a fixed-length opaque data type. To declare a fixed binary host variable, use the following syntax.
Important: A fixed binary host variable is only valid for a column of a fixed-length opaque data type. If the opaque data type is of varying length, use a var binary host variable. If you do not know the internal data structure of a fixed-length opaque data type, you must also use a var binary host variable to access it.
For more information, see Accessing a Varying-Length Opaque Type.
To use a fixed binary host variable, you must reference a C data structure that maps the internal data structure of the opaque data type. You specify this C data structure as the structure name in the fixed binary declaration.
Informix suggests that you create a C header file (.h file) for the C data structure that defines a fixed-length opaque data type. You can then include this header file in each ESQL/C source file that uses fixed binary host variables to access the opaque data type.
For example, the following code fragment declares a fixed binary host variable called my_circle for the circle opaque data type:
In this example, the circle.h header file contains the declaration for the circle_t structure (see Figure 10-1 on page 10-4), which is the internal data structure for the circle opaque type. The declaration for the my_circle host variable specifies both the name of the opaque data type, circle, and the name of its internal data structure, circle_t.
The Opaque TypeWhen you declare a fixed binary host variable, you must specify the opaque type as a quoted string.
Important: Both the single quote (') and the double quote (") are valid quote characters. However, the beginning quote and ending quote characters must match.
The opaque type name is optional; it affects the declaration as follows:
You can declare several fixed binary variables in a single declaration. However, all variables must have the same opaque type, as the following declaration shows:
Your ESQL/C program must handle all manipulation of the internal data structure for the fixed binary host variable; it must explicitly allocate memory and assign field values.
Figure 10-7 shows how to use a fixed binary host variable to insert and select data in the circle_col column of the circle_tab table (see Figure 10-4 on page 10-6).
|
Figure 10-7 Accessing the Internal Format of the circle Opaque Data Type With a fixed binary Host Variable
|
To insert the data that a fixed binary host variable contains into an opaque-type column, the code fragment in Figure 10-7 on page 10-21 takes the following steps:
To insert a null value into an opaque-type column with a fixed binary host variable, set an indicator variable to -1. The following code fragment inserts a null value into the circle_col column with the fbin_circle host variable:
For more information on indicator variables, see Using Indicator Variables.
Selecting into a fixed binary Host VariableTo select the data that an opaque-type column contains into a fixed binary host variable, the code fragment in Figure 10-7 on page 10-21 takes the following steps:
When you select a null value from an opaque-type column into a fixed binary host variable, ESQL/C sets any accompanying indicator variable to -1.
The var binary data type allows you to access the internal format of either of the following opaque data types:
Follow these steps to transfer the internal format of either of these opaque data type columns between the database server and the ESQL/C application:
The following sections describe each of these steps in more detail.
To declare a var binary host variable, use the following syntax.
Figure 10-8 shows declarations for three var binary variables.
In the declaration of a var binary host variable, the name of the opaque type must be a quoted string.
Important: Both the single quote (') and the double quote (") are valid quote characters. However, the beginning quote and ending quote characters must match.
The opaque type name is optional; it affects the declaration as follows:
You can declare several var binary variables in a single declaration line. However, all variables must have the same opaque type, as Figure 10-8 on page 10-24 shows.
In an ESQL/C program, the varying-length C structure, ifx_varlena_t, stores a binary value for a var binary host variable. This data structure allows you to transfer binary data without knowing the exact structure of the internal format for the opaque data type. It provides a data buffer to hold the data for the associated var binary host variable.
Important: The ifx_varlena_t structure is an opaque structure to ESQL/C programs. That is, you should not access its internal structure directly. Informix does not guarantee that the internal structure of ifx_varlena_t will not change in future releases. Therefore, to create portable code, always use the ESQL/C accessor functions for this structure to obtain and store values in the ifx_varlena_t structure. For a list of these ESQL/C access functions, see The lvarchar pointer and var binary Library Functions.
This section uses a varying-length opaque data type called image to demonstrate how an ESQL/C var binary host variable accesses an opaque data 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. Figure 10-9 shows the internal data structure for the image data type in the database.
Figure 10-10 shows the CREATE TABLE statement that creates a table called image_tab that has a column of type image and an image identifier.
Figure 10-11 shows how to use a var binary host variable to insert and select data in the image_col column of the image_tab table (see Figure 10-10 on page 10-26).
|
Figure 10-11 Accessing the Internal Format of the image Opaque Data Type With a var binary Host Variable
|
For more information on the ifx_var_flag(), ifx_var_alloc(), ifx_var_setdata(), ifx_var_getdata(), and ifx_var_dealloc() functions, see The lvarchar pointer and var binary Library Functions.
Inserting from a var binary Host VariableTo insert the data that a var binary host variable contains into an opaque-type column, the code fragment in Figure 10-11 on page 10-27 takes the following steps:
To insert a null value into an opaque-type column with a var binary host variable, you can use either of the following methods:
To select the data that an opaque-type column contains into a var binary host variable, the code fragment in Figure 10-11 on page 10-27 takes the following steps:
To check for a null value from an opaque-type column with a var binary host variable, you can use either of the following methods: