SQL/MP Programming Manual for C

Dynamic SQL Operations
HP NonStop SQL/MP Programming Manual for C429847-008
10-32
Allocate Memory for the SQLDA Structures and
Names Buffers
Example 10-5 allocates memory for input parameter values. You can use the same
code later to allocate memory for output variables.
Example 10-5. Allocating Memory for Parameters and Columns
int setupvarbuffers (sqlda_ptr)
sqldaptr sqlda_ptr; /* pointer to input or output SQLDA */
{
int num_entries; /* # of input parameters/output columns*/
int mem_reqd; /* buffer size to be allocated */
int i; /* loop counter */
/* Pass the SQLDA pointer to a function to handle any */
/* unsupported data types and scale (code omitted here).*/
/* If you do not need to handle the scale, you can set */
/* scale to 0. */
num_entries = sqlda_ptr->num_entries;
for (i = 0; i < num_entries; i++)
{
switch ( sqlda_ptr->sqlvar[i].data_type )
{
case _SQLDT_ASCII_F : /* char type */
mem_reqd = sqlda_ptr->sqlvar[i].data_len;
break;
case _SQLDT_ASCII_V: /* varchar type */
mem_reqd = sqlda_ptr->sqlvar[i].data_len + 2;
break;
...
/* For the numeric data types, either save the */
/* scale information found in bits 0:7, or set */
/* these bits to 0. Then extract the length */
/* from bits 8:15: */
case _SQLDT_16BIT_S:
case _SQLDT_16BIT_U:
...
/* Set scale information to zero: */
sqlda_ptr->sqlvar[i].data_len =
sqlda_ptr->sqlvar[i].data_len & 0377;
/* extract length from bits 8:15: */
mem_reqd = sqlda_ptr->sqlvar[i].data_len & 0377;
break;
... /* If data type is unsupported, return -1
} /* end of switch statement; check data type */
/* Allocate memory for the parameter or column; assign */
/* address to the var_ptr field in the input SQLDA. */
sqlda_ptr->sqlvar[i].var_ptr=(long) malloc (mem_reqd);
} /* end of loop for memory for each parameter value */
/* or column value */
return (0);
}