INFORMIX
Informix-ESQL/C Programmer's Manual
Chapter 15: Determining SQL Statements
Home Contents Index Master Index New Book

Determining Statement Information at Runtime

Consider a dynamic-management structure when you execute an SQL statement under the following conditions:

The following sections briefly outline how to handle each of the categories of unknown number and data types of values.

Handling an Unknown Select List

For a SELECT statement, the columns in the select list identify the column values that are received from the database server. In the SELECT statement described and illustrated in the demo1.ec example program (see page 1-117 of this manual), the values returned from the query are placed into the host variables that are listed in an INTO host_var clause of the SELECT statement.

However, when your program creates a SELECT statement at runtime, you cannot use an INTO clause because you do not know at compile time what host variables are needed. If the type and number of the values that your ESQL/C program receives are not known at compile time, your program must perform the following tasks:

1. Declare a dynamic-management structure to serve as storage for the select-list column definitions. This structure can be either a system-descriptor area or an sqlda structure.

X/O

    2. Use the DESCRIBE statement to examine the select list of the prepared SELECT statement and describes the columns.

    3. Specify the dynamic-management structure as the location of the data fetched from the database. From the dynamic-management structure, the program can move the column values into host variables.

Important: Use a dynamic-management structure only if you do not know the number and data types of the select-list columns at compile time. For information about how to execute a SELECT if you do know the number and data types of select-list columns, see "Executing SELECT Statements".
For information on how to identify columns in the select list of a SELECT statement with a system-descriptor area, see page 16-13. To use an sqlda structure, see page 17-17.

Handling an Unknown Column List

For an INSERT statement, the values in the VALUES clause identify the column values to be inserted into the new row. If the data types and number of the values that the ESQL/C program inserts are not known at compile time, you cannot simply use host variables to hold the data being inserted. Instead, your program must perform the following tasks:

1. Define a dynamic-management structure to serve as storage for the unknown column definitions. This structure can be either a system-descriptor area or an sqlda structure.

X/O

    2. Use the DESCRIBE statement to examine the column list of the prepared INSERT statement and describe the columns.

    3. Specify the dynamic-management structure as the location of the data to be inserted when the INSERT statement executes.

Important: Use a dynamic-management structure only if you do not know the number and data types of the column-list columns at compile time. For information about how to execute an INSERT if you do know the number and data types of column-list columns, see "Executing Non-SELECT Statements".
For information on how to identify columns in the VALUES column list of an INSERT statement with a system-descriptor area, see page 16-29. To use an sqlda structure, see page 17-30.

Determining Unknown Input Parameters

If you know the data types and number of input parameters of an SQL statement, use the USING host_var clause (see page 14-51). However, if you do not know the data types and number of these input parameters at compile time, you cannot use host variables to provide the parameter values; you do not have enough information about the parameters to declare the host variables.

Neither can you use the DESCRIBE statement to define the unknown parameters because DESCRIBE does not examine:

Your ESQL/C program must follow these steps to define the input parameters in any of the preceding statements:

1. Determine the number and data types of the input parameters. Unless you write a general-purpose, interactive interpreter, you usually have this information. If you do not have it, you must write C code that analyzes the statement string and obtains the following information:

    2. Store the definitions and values of the input parameters in a dynamic-management structure. This structure can be either a system-descriptor area or an sqlda structure.

X/O
    Use of the system-descriptor area conforms to X/Open standards.

    3. Specify the dynamic-management structure as the location of the input parameter values when the statement executes.

Important: Use a dynamic-management structure only if you do not know the number and data types of the input parameters at compile time. For information about how to execute a parameterized SQL statement if you do know the number and data types of column-list columns, see "Executing Statements with Input Parameters".
For information on how to handle input parameters in the WHERE clause of a dynamic SELECT statement with a system-descriptor area, see page 16-36; to use an sqlda structure, see page 17-32. For information on how to handle input parameters as arguments of a user-defined function with a system-descriptor area, see page 16-43; to use an sqlda structure, see page 17-41. For information on how to handle input parameters in the WHERE clause of a dynamic UPDATE or DELETE statement with a system-descriptor area, see page 16-46; to use an sqlda structure, see page 17-43.

Determining Return Values Dynamically

For an EXECUTE FUNCTION statement, the values in the INTO clause identify where to store the return values of a user-defined function. If the data types and number of the function return values are not known at compile time, you cannot use host variables in the INTO clause of EXECUTE FUNCTION to hold the values. Instead, your program must perform the following tasks:

1. Define a dynamic-management structure to serve as storage for the definitions of the value(s) that the user-defined function returns.

X/O

    2. Use the DESCRIBE statement to examine the prepared EXECUTE FUNCTION statement and describe the return value(s).

    3. Specify the dynamic-management structure as the location of the data returned by the user-defined function.

    From the dynamic-management structure, the program can move the return values into host variables.

Important: Use a dynamic-management structure only if you do not know at compile time the number and data types of the return values that the user-defined function returns. If you know this information at compile time, refer to "Executing User-Defined Routines in Universal Server" for more information.
For information on how to use a system-descriptor area to hold function return values, see page 16-22. To use an sqlda structure to hold return values, see page 17-26.

Handling Statements That Contain User-Defined Data Types

This section provides information about how to perform dynamic SQL on statements that contain columns with the following user-defined data types:

SQL Statements with Opaque-Type Columns

For dynamic execution of opaque-type columns, keep the following items in mind:

Inserting Opaque-Type Data
When the DESCRIBE statement describes a prepared INSERT statement, it sets the type and length fields of a dynamic-management structure to the data type of the column. Figure 15-13 shows the type and length fields for the dynamic-management structures.

Figure 15-13
Type and Length fields of Dynamic-Management Structures

Dynamic-Management Structure Type Field Length Field

system-descriptor area

TYPE field of an item descriptor

LENGTH field of an item descriptor

sqlda structure

sqltype field of an sqlvar_struct structure

sqllen field of an sqlvar_struct structure

If the INSERT statement contains a column whose data type is an opaque data type, the DESCRIBE statement identifies this column with one of the following type-field values:

These data-type constants represent an opaque type in its internal format.

When you put opaque-type data into a dynamic-management structure, you must ensure that the type field and length field are compatible with the data type of the data that you provide for the INSERT, as follows:

The input and output support functions for the opaque type do not reside on the client computer. Therefore, the client application cannot call them to convert the opaque-type data in the dynamic-management structure from its external to its internal format. To provide the opaque-type data in its external representation, set the type-field value to a character data type. When the database server receives the character data (the external representation of the opaque type), it calls the input support function to convert the external representation of the opaque type to its internal representation. If the data is some other type and valid support or casting functions exist, the database server can call these functions instead to convert the value.

For example, suppose you use a system-descriptor area to hold the insert values and you want to send the opaque-type data to the database server in its external representation. In the following code fragment, the SET DESCRIPTOR statement resets the TYPE field to SQLCHAR, so that the TYPE field matches the data type of the host variable (char) that it assigns to the DATA field:

Truncation of Opaque-Type Data
If you specify a host variable that is not large enough to hold the full return value from the server, ESQL/C normally truncates the data to fit the host variable and puts the actual length in an indicator variable. This indicator variable can be one that you explicitly provide or, for dynamic SQL, one of the following fields of a dynamic-management structure.
Dynamic-Management Structure Indicator Field

system-descriptor area

INDICATOR field of an item descriptor

sqlda structure

sqlind field of an sqlvar_struct structure

However, these indicator fields are defined as a short integer and therefore can only store sizes up to 32 kilobytes.

This size limitation of the indicator field affects how ESQL/C handles truncation of opaque-type data that is larger than 32 kilobytes. When ESQL/C receives opaque-type data that is larger than 32 kilobytes and the host variable is not large enough to hold the opaque-type data, ESQL/C truncates the data to 32 kilobytes. ESQL/C performs this truncation at 32 kilobytes even if you program a host variable that is larger than 32 kilobytes (but still not large enough for the data).

SQL Statements with Distinct-Type Columns

For dynamic execution of distinct-type columns, the dynamic-management structures have been modified to hold the following information about a distinct type:

These values are in the following fields of a dynamic-management structure.
Dynamic-Management Structure Source-Type Field Extended-Identifier Field

system-descriptor area

SOURCETYPE field of an item descriptor

SOURCEID field of an item descriptor

sqlda structure

sqlsourcetype field of an sqlvar_struct structure

sqlsourceid field of an sqlvar_struct structure

When the DESCRIBE statement describes a prepared statement, it stores information about columns of the statement in a dynamic-management structure. (For more information, see "Using the DESCRIBE Statement".) There is no special constant in the sqltypes.h file to indicate a distinct data type. Therefore, the type field of the dynamic-management structure cannot directly indicate a distinct type. (Figure 15-13 shows the type fields of the dynamic-management structures.)

Instead, the type field in the dynamic-management structure has a special value to indicate that a distinct bit is set for a distinct-type column. The type field indicates the source type of the distinct data combined with the distinct bit. The sqltypes.h header file provides the following data-type constants and macros to identify the distinct bit for a distinct column.
Source Type Distinct-Bit Constant Distinct-Bit Macro

LVARCHAR

SQLDLVARCHAR

ISDISTINCTLVARCHAR(type_id)

BOOLEAN

SQLDBOOLEAN

ISDISTINCTBOOLEAN(type_id)

Any other data type

SQLDISTINCT

ISDISTINCTTYPE(type_id)

Use the following algorithm to determine if a column is a distinct type:

The following table summarizes the pseudo-code of the preceding algorithm.
Source Type Type Field Source-Type Field Extended-Identifier Field

Built-in data type

SQLUDTVAR + SQLDISTINCT

Data-type constant of built-in data type

0

LVARCHAR

SQLUDTVAR + SQLDLVARCHAR

0

Extended identifier of LVARCHAR

BOOLEAN

SQLUDTFIXED + SQLDBOOLEAN

0

Extended identifier of BOOLEAN

All other data types

source type + SQLDISTINCT

0

0




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