Scalable Database Server, HiRDB Version 8 UAP Development Guide

[Contents][Index][Back][Next]

8.2.6 Specifying pointers as environment variables

Organization of this subsection
(1) Overview
(2) Rules
(3) Notes on using repetition-type pointers in machines that use a RISC-type CPU

(1) Overview

In C, the -E option allows you to declare pointers as embedded variables. When you use this function, you can directly specify dynamically allocated areas in SQL statements.

For details about SQL preprocessor options, see the option descriptions in 8.2.2 Preprocessing in UNIX, or 8.2.3 Preprocessing in Windows. For details about SQL statements that can use pointers, see 8.2.8 Use of pointers, structures, and structure qualifiers when the -E2 or -E3 option of the preprocessor is specified.

Declare pointer variables according to the C syntax. An example is shown as follows.

 
long  *xprice;
long  *xstock;
char  *xpname;
...
xprice  =  (long *)malloc(sizeof(long));
xstock  =  (long *)malloc(sizeof(long));
xpname  =  (char *)malloc(MAX_CHAR_LEN+1);
memset(xspname, ' ', MAX_CHAR_LEN);
xspname [MAX_CHAR_LEN] = '\0';
EXEC SQL FETC CUR1 INTO :xprice,:xstock,:xpname;
 

(2) Rules

  1. When specifying a pointer variable, add a colon before the variable name in the SQL statement. An asterisk cannot be used.
  2. The size of the value to be referenced becomes the size of the data type specified in the declaration. However, this does not apply to the fixed-length character string type (CHAR).
  3. The data length of a fixed-length character string-type pointer is determined during execution, and not during preprocessing. The value size becomes the length (strlen (pointer variable)) up to the end (\0) of the character string that is stored in the area indicated by the pointer. When storing the search results of a single-row SELECT statement or a FETCH statement, you must first clear the entire area with a character other than \0 before executing the SQL statement and then specify \0 at the end of the area.
  4. You must allocate the area that the pointer points to. If the pointer is a fixed-length character string-type pointer, allocate an extra byte to the area so that \0 can be stored. If the pointer is an invalid value or if the area allocated for storing the data is too small, the operation is not guaranteed.
  5. Pointers to pointers cannot be used.
  6. Pointers to structures can be specified.
  7. Pointers to classes cannot be used.
  8. Pointers to arrays cannot be used. To use a pointer to an array, use a structure and declare the structure as follows:
     
    struct {
    long  xprice[50];
    long  xstock[50];
    char  xpname[50][17];
    } *xrec_ptr;
     

(3) Notes on using repetition-type pointers in machines that use a RISC-type CPU

  1. Because repetition column-type variables have the following structure, address (1), which coincides with a word boundary, must be specified in the pointer.

    [Figure]

    Normally, there is no problem because areas allocated with malloc() are already adjusted to word boundaries. However, if you calculate and allocate the memory address on your own, you must adjust the address to a word boundary.
    If the address specified in the pointer does not coincide with a word boundary, a memory access exception occurs when the UAP uses macros for repetition column manipulation to reference or set data. For details about the structure of embedded variables in repetition columns, see B.2(5) Expansion format of repetition columns.
  2. For FLOAT-type repetition columns, the data length of the repetition elements becomes larger than the area that stores the number of repetitions. The boundary address must be adjusted to the word length of the repetition items. In the pointer, specify address (2), which includes the leading free space.

    [Figure]

    The preprocessor creates a post source that automatically uses address (3), which is 4 bytes from the beginning, as the beginning of the repetition column. Macros that manipulate a FLOAT-type repetition column also use address (3) as the beginning of the column. If you use the SQL descriptor area to specify the address of the repetition column directly, specify address (3).
  3. Because the maximum number of repetition elements is determined by the declared value, a memory access exception may occur if an area smaller than the declared value is allocated.
    Normally, problems can be avoided by allocating the memory as shown in the following coding.
     
    PD_MV_SINT(32) *ptr;     /* maximum element count 32 */
    ptr = malloc(sizeof(*ptr));
    EXEC SQL FETCH CUR1 INTO :ptr;