SQL Programming Manual for Pascal

Using Dynamic SQL
HP NonStop SQL Programming Manual for Pascal528614-001
7-22
Dynamically Allocating Memory
To allocate space for the output variable descriptions, you can call ALLOCATE_SQLDA
and pass the pointers to the output SQLDA and output names buffer.
Allocating Memory for the Values
After the descriptions of the input parameters and output variables are placed in
memory, the program must allocate space for the actual values. The user might enter
these values for input parameters or the system might return them for SELECT
Figure 7-5. Allocating the SQLDA and Names Buffer
VST0705.vsd
PROCEDURE ALLOCATE_SQLDA
(VAR SQLDA_HEADER_PTR :
SQLDA_HEADER_TYPE;
NUMVARS : INTEGER;
VAR NAMESBUF_PTR : CHAR_BUF_TYPE;
NAMES_BUFFER_SIZE : LONGINT);
VAR GENERIC_PTR : PTR_TO_ANY;
SQLDA_SIZE : LONGINT;
SIZE_TO_ALLOCATE : LONGINT;
. . .
. . .
{ Use the SIZEOF function to calculate the size of the SQLDA}
{ SQLDA_HEADER and SQLVAR_TYPE are declared in the main code}
{ Multiply the size of one SQLVAR entry by the number of }
{ input parameters: }
SQLDA_SIZE := SIZEOF(SQLDA_HEADER) + SIZEOF(SQLVAR_TYPE)
* NUMVARS;
SIZE_TO_ALLOCATE := SQLDA_SIZE + NAMES_BUFFER_SIZE;
{ Allocate the memory using MALLOC. MALLOC puts a pointer }
{ to the beginning of allocated memory in GENERIC_PTR: }
GENERIC_PTR.EXTADDR_FIELD := MALLOC(SIZE_TO_ALLOCATE);
{ Set sqlda_header_ptr to point to the portion of memory }
. . .
{ allocated for the SQLDA header: }
SQLDA_HEADER_PTR := GENERIC_PTR.SQLDA_HEADER_FIELD;
{ Set the pointer to the beginning of the names buffer: }
GENERIC_PTR.INT32_FIELD :=
GENERIC_PTR.INT32_FIELD + SQLDA_SIZE;
{ Copy the address into names buffer pointer }
NAMESBUF_PTR := GENERIC_PTR.CHAR_FIELD;
{Initialize the SQLDA header. The system declares }
{SQLDA_EYE_CATCHER as a constant when you issue INCLUDE }
{SQLDA. }
SQLDA_HEADER_PTR^.EYE_CATCHER := SQLDA_EYE_CATCHER;
SQLDA_HEADER_PTR^.NUM_ENTRIES := NUMVARS;