The rtypalign() function

The rtypalign() function returns the position of the next proper boundary for a variable of the specified data type.

Syntax

32 bit
mint rtypalign(pos, type)
   mint pos;
   mint type;
64 bit
mlong rtypalign(pos, type)
   mlong pos;
   mint type;
pos
The current position in a buffer.
type
An integer that corresponds to the data type of a C or variable. This type can be any data type except the following:
  • var binary
  • CFIXBINTYPE
  • CVARBINTYPE
  • SQLUDTVAR
  • SQLUDTFIXED

For more information, see Data type constants.

Usage

The rtypalign() and rtypmsize() functions are useful when you use an sqlda structure to dynamically fetch data into a buffer. On many hardware platforms, integer and other numeric data types must begin on a work boundary. The C language memory allocation routines allocate memory that is suitably aligned for any data type, including structures. However, these routines do not perform alignment for the constituent components of the structure. The programmer is responsible for performing that alignment with functions such as rtypalign() and rtypmsize(). These functions provide machine independence for storing column data.

After a DESCRIBE statement determines column information, stores the value of type in sqlda.sqlvar->sqltype.

You can see an application of the rtypalign() function in the unload.ec demonstration program.

Return codes

>0
The return value is the offset of the next proper boundary for a variable of type data type.

Example

This sample program is in the rtypalign.ec file in the demo directory.
/*
   * rtypalign.ec *

   The following program prepares a select on all columns of the orders
   table and then calculates the proper alignment for each column in a buffer.
*/

#include <decimal.h>

EXEC SQL include sqltypes;

#define WARNNOTIFY      1
#define NOWARNNOTIFY    0

main()
{
    mint i, pos;
    int4 ret, exp_chk();
    struct sqlda *sql_desc;
    struct sqlvar_struct *col;

    printf("RTYPALIGN Sample ESQL Program running.\n\n");

    EXEC SQL connect to 'stores7';              /* open stores7 database */
    exp_chk("Connect to", NOWARNNOTIFY);

    EXEC SQL prepare query_1 from "select * from orders";   /* prepare select */
    if(exp_chk("Prepare", WARNNOTIFY) == 1)
      exit(1);

    EXEC SQL describe query_1 into sql_desc;   /* initialize sqlda */
    if(exp_chk("Describe", WARNNOTIFY) == 1)
      exit(1);

    col = sql_desc->sqlvar;
    printf("\n\ttype\t\tlen\tnext\taligned\n");         /* display column hdgs. */
    printf("\t\t\t\tposn\tposn\n\n");
    /*
    * For each column in the orders table
     */
    i = 0;
    pos = 0;
    while(i++ < sql_desc->sqld)
      {
      /* Modify sqllen if SQL type is DECIMAL or MONEY */
      if(col->sqltype == SQLDECIMAL || col->sqltype == SQLMONEY)
         {
         col->sqllen = sizeof(dec_t);
         }
      /*
       * display name of SQL type, length and un-aligned buffer position
       */
      printf("\t%s\t\t%d\t%d", rtypname(col->sqltype), col->sqllen, pos);

      pos = rtypalign(pos, col->sqltype);          /* align pos. for type */
      printf("\t%d\n", pos);

      pos += col->sqllen;                          /* set next position */
      ++col;                                       /* bump to next column */
   }
   printf("\nRTYPALIGN Sample Program over.\n\n");
}

/*
 *  The exp_chk() file contains the exception handling functions to
 *  check the SQLSTATE status variable to see if an error has occurred
 *  following an SQL statement. If a warning or an error has
 *  occurred, exp_chk() executes the GET DIAGNOSTICS statement and
 *  prints the detail for each exception that is returned.
 */

EXEC SQL include exp_chk.ec

For a complete listing of the exp_chk() function, see Guide to the exp_chk.ec file or see the exp_chk.ec file for a listing of this exception-handling function.

Output

RTYPALIGN Sample ESQL Program running.
type len next posn aligned posn
serial 4 0 0
date 4 4 4
integer 4 8 8
char 40 12 12
char 1 52 52
char 10 53 53
date 4 63 64
decimal 22 68 68
money 22 90 90
date 4 112 112
RTYPALIGN Sample Program over.