Allocate memory for column data
The sqlda structure stores a pointer to the data for each column in the sqldata field of an sqlvar_struct structure. Unlike the DESCRIBE...USING SQL DESCRIPTOR statement, the DESCRIBE...INTO statement does not allocate memory for this data. When the DESCRIBE...INTO statement allocates memory for the sqlda pointer, it initializes the sqldata fields of each sqlvar_struct structure to null.
- Allocate memory for the column data.
- Set the sqldata field for the sqlvar_struct structure associated with the column to the address of the memory allocated for the column data.
You can replace the code fragment in Allocating column data from a static character buffer with a series of system memory-allocation calls within the for loop. However, system memory-allocation calls can be expensive so it is often more efficient to have a single memory allocation and then align pointers into that memory area.
When you allocate the column data, make sure that the allocated memory is formatted for the column data type. This data type is one of the or SQL data types defined in the sqltypes.h header file. (See Determine the data type of a column.) Make the allocated memory large enough to accommodate the maximum size of the data in the column.
You must also ensure that the data for each column begins on a proper word boundary in memory. On many hardware platforms, integer and other numeric data types must begin on a word boundary. The C language memory-allocation routines allocate memory that is suitably aligned for any data type, including structures, but the routines do not perform alignment for the constituent components of the structure.
- The rtypalign() function returns the position
of the next proper word boundary for a specified data type.
This function accepts two arguments: the current position in the data buffer and the integer or SQL data type for which you want to allocate space.
- The rtypmsize() function returns the number
of bytes of memory that you must allocate for the specified or
SQL data type.
This function accepts two arguments: the integer or SQL data type (in sqltype) and the length (in sqllen) for each column value.
- Use the value that is in the associated sqllen field of sqlda.
- Compose a different qualifier with the values and macros that the datatime.h header file defines.
- Set the data type qualifier to
0
and have the database server set this qualifier during the fetch. For DATETIME values, the data type qualifier is the dt_qual field of the dtime_t structure. For INTERVAL values, the data type qualifier is the in_qual field of the intrvl_t structure.
For examples that allocate memory for the sqldata fields, see the demo3.ec and unload.ec demonstration programs that are supplied with .