INFORMIX
Informix-ESQL/C Programmer's Manual
Chapter 8: Working with Simple Large Objects
Home Contents Index Master Index New Book

Locating Simple Large Objects in Files

You can locate simple-large-object data in the following types of files:

When you use a file as a simple-large-object location, a locator structure uses the lc_file structure for the lc_union structure. Figure 8-10 summarizes the lc_union.lc_file fields.

Figure 8-10
Fields in lc_union.lc_file Structure Used for Simple Large Objects Located in Files

Field Data Type Description

lc_fname

char *

The address of the pathname string that contains the file for the simple-large-object data. The program sets this field when it uses named files for simple-large-object locations.

lc_mode

int

The permission bits to use to create a new file. This value is the third argument passed to the system open() function. For valid values of lc_mode, refer to your system documentation.

lc_fd

int

The file descriptor of the file that contains the simple-large-object data. The program sets this field when it uses open files.

lc_position

long

The current seek position within the opened file. This is an internal field and must not be modified by the ESQL/C program.

The locator.h file provides the following macro shortcuts to use when you access simple large objects stored in files:

Tip: Informix recommends that you use these shortcut names when you access the locator structure. The shortcut names improve code readability and reduce coding errors. This manual uses these shortcut names when it refers to the lc_fname, lc_fd, and lc_position fields of the lc_union.lc_file structure.
When you use files for simple-large-object data, also set the loc_oflags field of the locator structure. The loc_oflags field is of type integer and it contains the host-system file-open mode flags. These flags determine how the file is to be accessed once it is opened:

One of these flags is passed to the loc_open( ) function when ESQL/C opens the file. ESQL/C reads the data and writes it to the current location (which the loc_position field indicates) in the file. If ESQL/C is unable to read or write to a file, it sets the loc_status field of the locator structure to -463 or -464, respectively. If ESQL/C is unable to close a file, it sets loc_status to -462. ESQL/C updates the SQLCODE variable with this same value.

Locating Simple Large Objects in Open Files

To have ESQL/C locate the TEXT or BYTE data in an open file, set the loc_loctype field of the locator structure to LOCFILE.

To use an open file as a simple-large-object location, your ESQL/C program must open the desired file before it accesses the simple-large-object data. It must then store its file descriptor in the loc_fd field of the locator structure to specify this file as the simple-large-object location. The loc_oflags field should also contain a file-open mode flag to tell ESQL/C how to access the file when it opens it. For a list of file-open mode flags, see page 8-20.

The demo directory contains the following two sample ESQL/C programs that demonstrate how to handle simple-large-object data located in an open file:

These programs assume the stores7 database as the default database for the simple-large-object data. The user can specify another database (on the default database server) as a command-line argument:

Each of these programs is briefly explained in the following sections.

Selecting a Simple Large Object into an Open File

The getcd_of sample program from the demo directory shows how to select a simple large object from the database into an open file. Figure 8-11 shows a code excerpt that selects the cat_descr column into a file that the user specifies.

Figure 8-11
Code Excerpt from the getcd_of Sample Program

To prepare the locator structure for the SELECT statement, the getcd_of program sets the cat_descr locator structure fields as follows:

To access the file descriptor (loc_fd) field of the locator structure, the getcd_of program uses the name cat_descr.loc_fd. However, the actual name of this field within the locator structure is as follows:

The shortcut name of loc_fd is defined as a macro within the locator.h file.

After ESQL/C writes data to an open file, it sets the following fields of the locator structure:

Inserting a Simple Large Object from an Open File

The updcd_of sample program from the demo directory shows how to insert a simple large object from an open file into the database. The program updates the cat_descr TEXT column of the catalog table from an open file that contains a series of records; each consists of a catalog number and the text to update the corresponding cat_descr column. The program assumes that this input file has the following format:

Figure 8-12 shows a code excerpt that illustrates how to use the locator structure to update the cat_descr column of the catalog table from an open file.

Figure 8-12
Code Excerpt from the updcd_of Sample Program

The updcd_of program opens the input file (descfl) that the user specified in response to a prompt, calls the getcat_num() function to read a catalog number from the file, and then calls the getdesc_len() function to determine the length of the text for the update to the cat_descr column. The program performs a SELECT statement to verify that the catalog number exists in the catalog table.

If this number exists, the updcd_of program prepares the locator structure as follows to update cat_descr from the text in the open file:

If you insert a null simple-large-object value, your program also needs to set the loc_indicator field to -1.

The updcd_of program is then able to perform the database update. After ESQL/C reads data from the open file and sends it to the database server, ESQL/C updates the loc_size field with the number of bytes read from the open file and sent to the database server. ESQL/C also sets the loc_status field to indicate the status of the operation: 0 for success and a negative value if an error has occurred. For possible causes of the error, see page 8-20.

Locating Simple Large Objects in Named Files

To have ESQL/C locate the TEXT or BYTE data in a named file, set the loc_loctype field of the locator structure to LOCFNAME, as shown in the following example:

To use a named file as a simple-large-object location, your ESQL/C program must specify a pointer to the filename in the loc_fname field of the locator structure. You must also set the loc_oflags field with a file-open mode flag to tell ESQL/C how to access the file when it opens it. For a list of file-open mode flags, see page 8-20.

To open a named file, ESQL/C opens the file named in the loc_fname field with the mode flags that the loc_oflags field specifies. If this file does not exist, ESQL/C creates it. ESQL/C then puts the file descriptor of the open file in the loc_fd field and proceeds as if your program opened the file. If ESQL/C cannot open this file, it sets the loc_status field (and SQLCODE) to -461. When the transfer is complete, ESQL/C closes the file, which releases the file descriptor in the loc_fd field.

The demo directory contains the following two sample ESQL/C programs that demonstrate how to handle simple-large-object data located in a named file:

These programs assume the stores7 database as the default database for the simple-large-object data. The user can specify another database (on the default database server) as a command-line argument as follows:

Each of these programs is briefly explained in the following sections.

Selecting a Simple Large Object into a Named File

The getcd_nf sample program from the demo directory shows how to select a simple large object from the database into a named file. The following code excerpt prompts the user to enter a catalog number for the catalog table and the name of the file to which the program writes the contents of the cat_descr column for that row. The program stores the name of the file in the descfl array. It then executes a SELECT statement to read the cat_descr TEXT column from the catalog table and write it to a file that the user specifies in response to a prompt.

Figure 8-13 shows a code excerpt from the getcd_nf sample program.

Figure 8-13
Code Excerpt from the getcd_nf Sample Program

The program sets the cat_descr locator structure fields as follows:

The getcd_nf program then executes the SELECT statement to retrieve the row. After ESQL/C writes data to the named file, it sets the following fields of the locator structure:

Inserting a Simple Large Object from a Named File

The updcd_nf sample program from the demo directory shows how to insert a simple large object from a named file into the database. The program updates the cat_descr TEXT column from a named input file. The program assumes this input file has the following format:

Figure 8-14 shows a code excerpt that updates the cat_descr column in the catalog table from text in a named file.

Figure 8-14
Code Excerpt from the updcd_nf Sample Program

The updcd_nf program in Figure 8-14 first performs a SELECT statement on the catalog table for a catalog number that the user enters in response to a prompt. The SELECT statement returns the catalog_num and cat_descr columns. The prdesc() function (page 8-66) displays the current content of cat_descr.

The program then asks whether the user wants to update this description. If the user answers yes (ans[0] == 'y'), the updcd_nf program prepares the locator structure as follows to update the cat_descr column from text in a file that the user has specified:

If you insert a null simple-large-object value, your program also needs to set the loc_indicator field to -1.

After ESQL/C reads data from the named file and sends it to the database server, ESQL/C updates the loc_size field with the number of bytes read from the named file and sent to the database server. ESQL/C also sets the loc_status field to indicate the status of the operation: 0 for success and a negative value if an error has occurred. See page 8-20 for possible causes of the error.




Informix-ESQL/C Programmer's Manual, version 9.1
Copyright © 1998, Informix Software, Inc. All rights reserved.