The export support function

When a bulk-copy utility performs an unload of opaque-type data to its external unload representation, the database server calls the export support function. For example, when performs a bulk unload of an opaque-type column with the UNLOAD statement, the database server calls the export support function for the opaque type.

Function signature

The export support function takes the appropriate server internal representation of the opaque data type and returns the external unload representation of that type, encapsulated in an mi_impexp structure, as the following signature shows:
mi_impexp *export(srvr_internal_rep)
export
The name of the C-language function that implements the export support function for the opaque type. It is recommended that you include the name of the opaque type in its export function.
srvr_internal_rep
The appropriate format for the server internal representation of the opaque data type. The passing mechanism of this argument value depends on the kind of opaque type, as Send Support Function for circle Opaque Type through Send support function for image opaque type show. Most opaque types are passed by reference.

An mi_impexp is always passed by reference. Therefore, the return value of the export support function must always be a pointer to the mi_impexp data type. For information about how to obtain information from this varying-length structure, see Information about varying-length data.

Sample code fragments

The following code fragment declares a sample export support function for a fixed-length opaque type named circle (which Internal representation of the circle opaque data type declares).
Figure 1: Export support function for circle opaque type
/* Export support function: circle */
mi_impexp *circle_exp(srvr_intrnl_rep)
   circle_t *srvr_intrnl_rep;
{
   return ((mi_impexp *)circle_output(srvr_intrnl_rep));
}

The circle_exp() function is a cast function from the circle_t internal representation (on the server computer) to the mi_impexp data type (which contains the external unload representation for circle). The database server executes circle_exp() when it needs a cast function to convert from the server internal representation of the circle opaque type to the SQL data type IMPEXP. For more information, see Support functions as casts.

The circle_exp() function accepts as an argument a pointer to the circle_t data type. Because circle cannot fit into an MI_DATUM structure, it must be passed by reference. If your fixed-length opaque type can fit into an MI_DATUM structure, the export support function can pass the server internal representation by value.

The following code fragment declares a sample export function for a fixed-length opaque type named two_bytes (which Internal representation for the two_bytes opaque data type declares).
Figure 2: Export support function for two_bytes opaque type
/* Export support function: two_bytes */
mi_impexp *two_bytes_exp(srvr_intrnl_rep)
   two_bytes_t srvr_intrnl_rep;
{
   return ((mi_impexp *)two_bytes_output(srvr_intrnl_rep));
}

The two_bytes opaque type must be registered as PASSEDBYVALUE to tell the database server that it can be passed by value.

The following code fragment declares a sample export support function for a varying-length opaque type named image (which Internal representation for the image opaque data type declares).
Figure 3: Export support function for image opaque type
/* Export support function: image */
mi_impexp *image_exp(srvr_intrnl_rep)
   mi_lvarchar *srvr_intrnl_rep;

Usage

The image opaque type stores its data inside an mi_lvarchar structure, which must be passed by reference. The image_exp() function is a cast function from the mi_lvarchar data type (which contains the server internal representation of image) to the mi_impexp data type (which contains the external unload representation of image).

In most cases, the export support function can be the same as the output support function, because the external representation and the external unload representation are usually the same. For such opaque types, you can handle the export functions in either of the following ways:
  • Call the output function inside the export function.

    The export functions for the circle opaque type (Export support function for circle opaque type) and the two_bytes opaque type (Export support function for two_bytes opaque type) use this method.

  • Omit the export function from the definition of the opaque type.

    You must still create the explicit cast from the opaque data type to the IMPBIN data type with the CREATE CAST statement. However, instead of listing an export support function as the cast function, list the output support function. The database server would then automatically call the appropriate output support function to unload the opaque type to its external unload representation.

Typically, only opaque data types that contain smart large objects have import and export functions defined. The external unload representation can include a client file name (which contains the smart-large-object data), a length, and an offset. The export support function can obtain the LO handle of the smart large object from the server internal representation of the opaque type. With this LO handle, export can use the mi_lo_to_file() function (with the MI_O_CLIENT_FILE file-mode constant) to:
  • Create the specified file on the client computer.
  • Write the smart-large-object data into this file at the specified offset and for the number of bytes that the length specifies.
Finally, the export function can put the client file name, length of data, and starting offset into the external unload representation that is to be written to the copy file.
Tip: For opaque types with smart large objects, you can choose whether to provide support for an external representation (a client file name, length, and offset) in the input and output support functions or the import and export support functions. When you define the input and output support functions to handle this external representation, applications can use this representation as a literal value for opaque-type data.

Function tasks

For an opaque type that does require an export support function, the export function performs the following tasks:
  • Accepts as an argument a pointer to the appropriate server internal representation of the opaque type

    If the opaque data type is passed by reference, the export function accepts a pointer to the server internal representation. If the opaque data type is passed by value, the export function accepts the actual value of the internal representation instead of a pointer to this representation. For more information, see Determine the passing mechanism for an opaque type.

  • Allocates enough space to hold the external unload representation of the opaque type

    The export function can use the mi_alloc() DataBlade® API function to allocate the space for the character string. For more information about memory management, see Manage user memory.

  • Creates the external unload representation from the individual members of the server internal representation

    The DataBlade API provides functions to convert various DataBlade API data types from their internal to external representations. For example, to convert the mi_date value in the image_t structure to its appropriate external representation, the image_exp() function can call the mi_date_to_string() function. For a list of these DataBlade API functions, see Conversion of opaque-type data between text and binary representations.

  • Copies the external unload representation into an mi_impexp structure

    You can use the mi_new_var() function to create a new mi_impexp structure and the mi_get_vardata() or mi_get_vardata_align() function to obtain a pointer to the data portion of this structure.

  • Returns a pointer to the external unload representation for the opaque type

    This character string must be in the data portion of an mi_impexp structure. Therefore, the export support function returns a pointer to this mi_impexp structure.

Because the image opaque type contains a smart large object, it would require an export function, which can save in the external unload representation that is written to the copy file the name of the client file that contains the smart-large-object data.