Scalable Database Server, HiRDB Version 8 UAP Development Guide

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

8.2.7 Referencing structures

Organization of this subsection
(1) Overview
(2) Rules
(3) Usage examples

(1) Overview

The preprocessor features an option that allows you to use a structure written in C to specify multiple embedded variables at one time.

Structures can be used as embedded variables in the following locations:

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 structures, see 8.2.8 Use of pointers, structures, and structure qualifiers when the -E2 or -E3 option of the preprocessor is specified.

(2) Rules

  1. When you specify a structure as an embedded variable, the preprocessor assumes that each member of the structure was specified as an embedded variable and generates the same post source it would generate if the members were specified separately. The member expansion sequence in the post source is the same as the member declaration sequence in the structure. The sequence of the retrieval items and columns in SQL statements that specify the structure must match the member sequence.
  2. The members of a structure can also be specified individually as embedded variables.
  3. Structures that contain a union cannot be used.
  4. Structures that contain another structure cannot be used. However, structures that correspond to the variable-length character string type and the BINARY type can be used.

(3) Usage examples

A structure usage example is shown as follows.

 
  struct {
    char    xpcode[5];
    char    xpname[17];
    char    xcolor[3];
    long    xstock;
    long    xprice;
  } xrec;
     ...
  EXEC SQL
    DECLARE CR3 CURSOR FOR
      SELECT PCODE,PNAME,COLOR,SQUANTITY, PRICE FROM STOCK;
     ...
  EXEC SQL OPEN CR3 ;
 
  /*  heading */
  printf("    *****  STOCK TABLE LIST  *****\n\n");
  printf("    PRODUCT CODE  PRODUCT NAME            COLOR  PRICE      CURRENT STOCK\n");
  printf("    ----        ----------------  --  --------  --------\n");
 
  EXEC SQL WHENEVER SQLERROR GOTO END;
  EXEC SQL WHENEVER NOT FOUND GOTO END;
 
  /* FETCH */
  for(;;){
    EXEC SQL FETCH CR3 INTO :xrec;
    printf("    %4s      %-16s  %2s  %8d  %8d\n",
        xrec.xpcode, xrec.xpname, xrec.xcolor, xrec.xprice, xrec.xstock);
    }
END:
            ...
 

When you use a structure as an embedded variable and also want to use indicator variables, declare the indicator variables in a structure as well. Associate the individual members of the indicator variable structure in declaration sequence with the individual members of the embedded variable structure. An example is shown as follows.

 
  struct {
      char    xpcode[5];
      char    xpname[17];
      char    xcolor[3];
      long    xstock;
      long    xprice;
  } xrec;
  struct {
      short    xpcode_ind;
      short    xpname_ind;
      short    xcolor_ind;
      short    xstock_ind;
      short    xprice_ind;
  } xrec_ind;
       ...
  /* FETCH */
  for(;;){
    EXEC SQL FETCH CR3 INTO :xrec :xrec_ind;
      printf("    %4s      %-16s  %2s  %8d  %8d\n",
         xrec.xpcode, xrec.xpname, xrec.xcolor, xrec.xprice, xrec.xstock);
  }
      ...
 

You can also specify a pointer to a structure as an embedded variable. The area indicated by the pointer must be allocated beforehand.

 
struct  tag_xrec {
  char    xpcode[5];
  char    xpname[17];
  char    xcolor[3];
  long    xstock;
  long    xprice;
} *xrec_ptr;
struct tag_xrec_ind {
  short    xpcode_ind;
  short    xpname_ind;
  short    xcolor_ind;
  short    xstock_ind;
  short    xprice_ind;
} *xrec_ind_ptr;
      ...
/* FETCH */
xrec_ptr = (struct tag_xrec *)malloc(sizeof(struct tag_xrec));
xrec_ind_ptr = (struct tag_xrec_ind *)
                 malloc(sizeof(struct tag_xrec_ind));
  for(;;){
    EXEC SQL FETCH CR3 INTO :xrec_ptr :xrec_ind_ptr;
      printf("    %4s      %-16s  %2s  %8d  %8d\n",
             xrec_ptr->xpcode, xrec_ptr->xpname, xrec_ptr->xcolor,
             xrec_ptr->xprice, xrec_ptr->xstock);
  }
        ...