The rtypmsize() function

The rtypmsize() function returns the number of bytes you must allocate in memory for the specified or SQL data type.

Syntax

mint rtypmsize(sqltype, sqllen)
   mint sqltype;
   mint sqllen;
sqltype
The integer code of the or SQL data type. For more information, see Data type constants.
sqllen
The number of bytes in the data file for the specified data type.

Usage

The rtypalign() and rtypmsize() functions are useful when you use an sqlda structure to dynamically fetch data into a buffer. These functions provide machine independence for the column-data storage.

The rtypmsize() function is provided to use with the sqlda structure that a DESCRIBE statement initializes. After a DESCRIBE statement determines column information, the value of sqltype and sqllen components are in the components of the same name in each sqlda.sqlvar structure.

When rtypmsize() determines sizes for character data, keep in mind the following size information:
  • For CCHARTYPE (char) and CSTRINGTYPE (string), adds one byte to the number of characters for the null terminator.
  • For CFIXCHARTYPE (fixchar), does not add a null terminator.

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

Return codes

0
The sqltype is not a valid SQL type.
>0
The return value is the number of bytes that the sqltype data type requires.

Example

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

   This program prepares a select statement on all columns of the
   catalog table. Then it displays the data type of each column and 
   the number of bytes needed to store it in memory.
*/

#include <stdio.h>

EXEC SQL include sqltypes;

#define WARNNOTIFY      1
#define NOWARNNOTIFY    0

EXEC SQL BEGIN DECLARE SECTION;
    char db_name[20];
EXEC SQL END DECLARE SECTION;

main(argc, argv)
int argc;
char *argv[];
{
    mint i;
    char db_stmnt[50];
   int4 exp_chk();
    struct sqlda *sql_desc;
    struct sqlvar_struct *col;

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

    if (argc > 2)            /* correct no. of args? */
      {
      printf("\nUsage: %s [database]\nIncorrect no. of argument(s)\n",
         argv[0]);
      exit(1);
      }
   strcpy(db_name, "stores7");
    if (argc == 2)
      strcpy(db_name, argv[1]);

   EXEC SQL connect to :db_name;
   sprintf(db_stmnt, "CONNECT TO %s", argv[1]);
   exp_chk(db_stmnt, NOWARNNOTIFY);

   printf("Connected to '%s' database.", db_name);

    EXEC SQL prepare query_1 from 'select * from catalog'; /* prepare select */
    if(exp_chk("Prepare", WARNNOTIFY) == 1)
      exit(1);
    EXEC SQL describe query_1 into sql_desc;           /* setup sqlda */
    if(exp_chk("Describe", WARNNOTIFY) == 1)
      exit(1);
    printf("\n\tColumn              Type    Size\n\n");       /* column hdgs. */
    /*
     * For each column in the catalog table display the column name and
     * the number of bytes needed to store the column in memory.
     */
    for(i = 0, col = sql_desc->sqlvar; i < sql_desc->sqld; i++, col++)
   printf("\t%-20s%-8s%3d\n", col->sqlname, rtypname(col->sqltype),
       rtypmsize(col->sqltype, col->sqllen));

    printf("\nRTYPMSIZE 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

RTYPMSIZE Sample ESQL Program running.

Connected to stores7 database.

Column            Type         Size
catalog_num       serial       4
stock_num         smallint     s
manu_code         char         4
cat_descr         text         64
cat_picture       byte         64
cat_advert        varchar      256

RTYPMSIZE Sample Program over.