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:
A simple spaghetti recipe:
1. Cook some spaghetti noodles.
2. Cover the noodles in spaghetti sauce.
The following code fragment creates a 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"
"A simple spaghetti recipe: ",
"1. Cook some spaghetti noodles. ",
"2. Cover the noodles in spaghetti sauce.");
/* 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:
- The create_spec LO handle was allocated and identifies the new smart large object.
- The lofd LO file descriptor identifies the open smart large object.
- The new smart large object has user-specified storage characteristics
for logging behavior and estimated size.
The smart large object inherits the other storage characteristics.
The following table shows the complete storage characteristics
for this new smart large object.
Storage characteristic | Value | Specified by |
---|---|---|
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 | 2 KB (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 |
Logging attribute | ON | mi_lo_specset_flags() with MI_LO_ATTR_LOG |
Last-access time attribute | ON | column-level |
Storage characteristics for the cat_descr column shows the column-level storage characteristics for the cat_descr column and System-specified storage characteristics for the sb1 sbspace 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.