dbconvert_ps

Description

Convert data from one datatype to another, with precision and scale support for numeric and decimal datatypes.

Syntax

DBINT dbconvert_ps(dbproc, srctype, src, srclen,
                 desttype, dest, destlen, typeinfo)
 
DBPROCESS        *dbproc;
int                          srctype;
BYTE                     *src;
DBINT                   srclen;
int                          desttype;
BYTE                     *dest;
DBINT                   destlen;
DBTYPEINFO        *typeinfo;

Parameters

dbproc

A pointer to the DBPROCESS structure that provides the connection for a particular front-end/server process. It contains all the information that DB-Library uses to manage communications and data between the front end and server. In dbconvert_ps, the DBPROCESS is used only to supply any custom null values that the program may have specified using dbsetnull. If dbproc is NULL, dbconvert_ps uses the default values for null value data conversions.

srctype

The datatype of the data which is to be converted. This parameter can be any of the server datatypes, as listed in Table 2-8.

src

A pointer to the data that is to be converted. If this pointer is NULL, dbconvert_ps will place an appropriate null value in the destination variable. You can use dbdata to get the server data.

srclen

The length, in bytes, of the data to be converted. If the srclen is 0, the source data is assumed to be NULL and dbconvert_ps will place an appropriate null value in the destination variable. Otherwise, this length is ignored for all datatypes except char, text, binary, and image. For SYBCHAR data, a length of -1 indicates that the string is null-terminated. You can use dbdatlen to get the length of the server data.

desttype

The datatype that the source data is to be converted into. This parameter can be any of the server datatypes, as listed in Table 2-8.

dest

A pointer to the destination variable that will receive the converted data. If this pointer is NULL, dbconvert_ps will call the user-supplied error handler (if any) and return -1.

destlen

The length, in bytes, of the destination variable. destlen is ignored for fixed-length datatypes. For a SYBCHAR, SYBBOUNDARY, or SYBSENSITIVITY destination, the value of destlen must be the total length of the destination buffer space.

Table 2-6 describes special values for destlen:

Table 2-6: Special values for destlen (dbconvert_ps)

Value of destlen

Applicable to

Meaning

-1

SYBCHAR, SYBBOUNDARY, SBYSENSITIVITY

There is sufficient space available. The string will be trimmed of trailing blanks and given a terminating null.

-2

SYBCHAR

There is sufficient space available. The string will not be trimmed of trailing blanks, but will be given a terminating null.

typeinfo

A pointer to a DBTYPEINFO structure containing information about the precision and scale of decimal or numeric values. An application sets a DBTYPEINFO structure with values for precision and scale before calling dbconvert_ps to convert data into DBDECIMAL or DBNUMERIC variables.

If typeinfo is NULL:

  • If the source value is of type SYBNUMERIC or SYBDECIMAL, dbconvert_ps picks up precision and scale values from the source. In effect, the source data is copied to the destination space.

  • If the source value is not SYBNUMERIC or SYBDECIMAL, dbconvert_ps uses a default precision of 18 and a default scale of 0.

If srctype is not SYBDECIMAL or SYBNUMERIC, typeinfo is ignored.

A DBTYPEINFO structure is defined as follows:

 typedef struct typeinfo {
      DBINT    precision;
      DBINT    scale;
 } DBTYPEINFO;

Legal values for precision are from 1 to 77. Legal values for scale are from 0 to 77. scale must be less than or equal to precision.

Returns

The length of the converted data, in bytes, if the datatype conversion succeeds.

If the conversion fails, dbconvert_ps returns either -1 or FAIL, depending on the cause of the failure. dbconvert_ps returns -1 to indicate a NULL destination pointer or an illegal datatype. dbconvert_ps returns FAIL to indicate other types of failures.

If dbconvert_ps fails, it will first call a user-supplied error handler (if any) and set the global DB-Library error value.

This routine may fail for several reasons: the requested conversion was not available; the conversion resulted in truncation, overflow, or loss of precision in the destination variable; or a syntax error occurred in converting a character string to some numeric type.

Usage

See also

dbaltbind, dbaltbind_ps, dbbind, dbbind_ps, dbconvert, dberrhandle, dbsetnull, dbsetversion, dbwillconvert, Errors, Types