Home | Previous Page | Next Page   Data Manipulation > Using Smart Large Objects > Creating a Smart Large Object >

Sample Code to Create a New Smart Large Object

Suppose you want to create a new smart large object for the cat_descr column in the catalog2 table that contains the following data:

The rain in Spain stays mainly in the plain. In Hartford, Hereford, and 
Hampshire, hurricanes hardly happen.

The following code fragment creates a new smart large object, which assumes the storage characteristics of its column, cat_descr, and then modifies the logging behavior:

#include "int8.h"
#include "mi.h"

#define BUFSZ 10000

{
   MI_CONNECTION *conn;
   MI_LO_SPEC *create_spec = NULL;
   MI_LO_HANDLE *descrip = NULL;
   MI_LO_FD lofd;
   char buf[BUFSZ];
   mi_integer buflen = BUFSZ;
   mi_int8 offset, est_size;
   mi_integer numbytes;
...
/* Allocate and initialize the LO-specification structure */
   if ( mi_lo_spec_init(conn, &create_spec) == MI_ERROR )
      handle_lo_error("mi_lo_spec_init( )");

/* Obtain the following column-level storage characteristics
 * for the cat_desc column:
 *      sbspace name = sb1 (this sbspace must already exist)
 *      keep last access time is ON
 */
   if ( mi_lo_colinfo_by_name(conn, "catalog2.cat_descr",
         create_spec) == MI_ERROR )
      handle_lo_error("mi_lo_colinfo_by_name( )");

/* Provide user-specified storage characteristics:
 *     logging behavior is ON
 *     size estimate of two kilobytes
 */
   mi_lo_specset_flags(create_spec, MI_LO_ATTR_LOG);
   ifx_int8cvint(2000, &est_size);
   mi_lo_specset_estbytes(create_spec, &est_size)

/* Create an LO handle and LO file descriptor for the new
 * smart large object
 */
   if ( lofd = mi_lo_create(conn, create_spec, MI_LO_RDWR, 
         &descrip) == MI_ERROR )
      handle_lo_error("mi_lo_create( )");

/* Copy data into the character buffer 'buf' */
   sprintf(buf, "%s %s %s"
      "The rain in Spain stays mainly in the plain. ",
      "In Hartford, Hereford, and Hampshire, hurricanes",
      " hardly happen.");

/* Write contents of character buffer to the open smart
 * large object to which lofd points.
 */
   ifx_int8cvint(0, &offset);
   if ( numbytes = mi_lo_writewithseek(conn, lofd, buf,
         buflen, &offset, MI_LO_SEEK_SET) == MI_ERROR )
      handle_lo_error("mi_lo_writewithseek( )");

/* Close the LO file descriptor */
   mi_lo_close(conn, lofd);

/* Free LO-specification structure */
   mi_lo_spec_free(conn, create_spec);

After the mi_lo_create( ) function executes, the following items are true:

Table 41. Storage Characteristics for the New Smart Large Object
Storage Characteristic Value Specified By
Disk-storage information:
Size of extent Calculated by smart-large-object optimizer system-specified
Size of next extent Calculated by smart-large-object optimizer system-specified
Minimum extent size Calculated by smart-large-object optimizer system-specified
Size of smart large object Two kilobytes (smart-large-object optimizer uses as size estimate) mi_lo_specset_estbytes( )
Maximum size of I/O block Calculated by smart-large-object optimizer system-specified
Name of sbspace sb1 column-level
Attribute information:
Logging ON mi_lo_specset_flags( ) with MI_LO_ATTR_LOG
Last-access time ON column-level

Table 35 shows the column-level storage characteristics for the cat_descr column and Table 34 shows the system-specified storage characteristics for the sb1 sbspace.

The mi_lo_writewithseek( ) function writes the buf data to the smart large object that lofd identifies. When the write operation is successful, the descrip LO handle is ready to be stored in the CLOB column with the INSERT statement.

For more information on how to insert a value into a column, see Executing SQL Statements.

Home | [ Top of Page | Previous Page | Next Page | Contents | Index ]