FreeTDS API

Macros | Functions | Variables
Conversion
LibTDS API

Conversions between datatypes. More...

Collaboration diagram for Conversion:

## Macros

#define | CASE_ALL_BINARY
#define | CASE_ALL_CHAR SYBCHAR: case SYBVARCHAR: case SYBTEXT: case XSYBCHAR: case XSYBVARCHAR
#define | is_monthname(s) (store_monthname(s, NULL) >= 0)
#define | string_to_result(s, cr) string_to_result(desttype, s, cr)

## Functions

static TDS_INT | binary_to_result (int desttype, const void *data, size_t len, CONV_RESULT *cr)
| Copy binary data to to result and return len or TDS_CONVERT_NOMEM.
static bool | is_alphabetic (const char *)
static bool | is_ampm (const char *)
static bool | is_dd_mon_yyyy (char *t)
static bool | is_numeric (const char *)
static bool | is_numeric_dateformat (const char *)
static bool | is_timeformat (const char *)
static TDS_INT | parse_int8 (const char *buf, const char *pend, TDS_UINT8 *res, bool *p_negative)
| convert a number in string to TDS_INT8 More...
static const char * | parse_numeric (const char *buf, const char *pend, bool *p_negative, size_t *p_digits, size_t *p_decimals)
| Parse a string for numbers. More...
static int | store_dd_mon_yyy_date (char *datestr, struct tds_time *t)
static int | store_hour (const char *, const char *, struct tds_time *)
static int | store_mday (const char *, struct tds_time *)
static int | store_monthname (const char *datestr, struct tds_time *t)
| Test if a string is a month name and store correct month number. More...
static int | store_numeric_date (const char *, struct tds_time *)
static int | store_time (const char *, struct tds_time *)
static int | store_year (int, struct tds_time *)
static int | store_yymmdd_date (const char *, struct tds_time *)
static int | string_to_datetime (const char *datestr, TDS_UINT len, int desttype, CONV_RESULT *cr)
static TDS_INT | string_to_float (const TDS_CHAR *src, TDS_UINT srclen, int desttype, CONV_RESULT *cr)
static TDS_INT | string_to_int (const char *buf, const char *pend, TDS_INT *res)
| convert a number in string to TDS_INT More...
static TDS_INT | string_to_int8 (const char *buf, const char *pend, TDS_INT8 *res)
| convert a number in string to TDS_INT8 More...
static int | string_to_numeric (const char *instr, const char *pend, CONV_RESULT *cr)
| convert a number in string to a TDSNUMERIC More...
static TDS_INT | string_to_result (int desttype, const char *s, CONV_RESULT *cr)
| Copy a terminated string to result and return len or TDS_CONVERT_NOMEM.
static TDS_INT | string_to_uint8 (const char *buf, const char *pend, TDS_UINT8 *res)
| convert a number in string to TDS_UINT8 More...
static int | stringz_to_numeric (const char *instr, CONV_RESULT *cr)
| convert a zero terminated string to NUMERIC More...
TDS_INT | tds_char2hex (TDS_CHAR *dest, TDS_UINT destlen, const TDS_CHAR *src, TDS_UINT srclen)
TDS_INT | tds_convert (const TDSCONTEXT *tds_ctx, int srctype, const TDS_CHAR *src, TDS_UINT srclen, int desttype, CONV_RESULT *cr)
| tds_convert convert a type to another. More...
static TDS_INT | tds_convert_bigdatetime (const TDSCONTEXT *tds_ctx, const TDS_BIGDATETIME *bigdatetime, int desttype, CONV_RESULT *cr)
static TDS_INT | tds_convert_bigtime (const TDSCONTEXT *tds_ctx, const TDS_BIGTIME *bigtime, int desttype, CONV_RESULT *cr)
static TDS_INT | tds_convert_binary (const TDS_UCHAR *src, TDS_INT srclen, int desttype, CONV_RESULT *cr)
static TDS_INT | tds_convert_bit (const TDS_CHAR *src, int desttype, CONV_RESULT *cr)
static TDS_INT | tds_convert_char (const TDS_CHAR *src, TDS_UINT srclen, int desttype, CONV_RESULT *cr)
static TDS_INT | tds_convert_date (const TDSCONTEXT *tds_ctx, const TDS_DATE *date, int desttype, CONV_RESULT *cr)
static TDS_INT | tds_convert_datetime (const TDSCONTEXT *tds_ctx, const TDS_DATETIME *dt, int desttype, unsigned precision, CONV_RESULT *cr)
static TDS_INT | tds_convert_datetime4 (const TDSCONTEXT *tds_ctx, const TDS_DATETIME4 *dt4, int desttype, CONV_RESULT *cr)
static TDS_INT | tds_convert_datetimeall (const TDSCONTEXT *tds_ctx, int srctype, const TDS_DATETIMEALL *dta, int desttype, CONV_RESULT *cr)
static TDS_INT | tds_convert_flt8 (const TDS_FLOAT *src, int desttype, CONV_RESULT *cr)
static TDS_INT | tds_convert_int (TDS_INT num, int desttype, CONV_RESULT *cr)
static TDS_INT | tds_convert_int1 (const TDS_TINYINT *src, int desttype, CONV_RESULT *cr)
static TDS_INT | tds_convert_int2 (const TDS_SMALLINT *src, int desttype, CONV_RESULT *cr)
static TDS_INT | tds_convert_int4 (const TDS_INT *src, int desttype, CONV_RESULT *cr)
static TDS_INT | tds_convert_int8 (const TDS_INT8 *src, int desttype, CONV_RESULT *cr)
static TDS_INT | tds_convert_int8_numeric (unsigned char scale, unsigned char sign, TDS_UINT8 num, CONV_RESULT *cr)
static TDS_INT | tds_convert_int_numeric (unsigned char scale, unsigned char sign, TDS_UINT num, CONV_RESULT *cr)
static TDS_INT | tds_convert_money (const TDS_MONEY *src, int desttype, CONV_RESULT *cr)
static TDS_INT | tds_convert_money4 (const TDS_MONEY4 *src, int desttype, CONV_RESULT *cr)
static TDS_INT | tds_convert_numeric (const TDS_NUMERIC *src, int desttype, CONV_RESULT *cr)
static TDS_INT | tds_convert_real (const TDS_REAL *src, int desttype, CONV_RESULT *cr)
static TDS_INT | tds_convert_time (const TDSCONTEXT *tds_ctx, const TDS_TIME *time, int desttype, CONV_RESULT *cr)
static TDS_INT | tds_convert_to_binary (int srctype, const TDS_CHAR *src, TDS_UINT srclen, int desttype, CONV_RESULT *cr)
static TDS_INT | tds_convert_uint2 (const TDS_USMALLINT *src, int desttype, CONV_RESULT *cr)
static TDS_INT | tds_convert_uint4 (const TDS_UINT *src, int desttype, CONV_RESULT *cr)
static TDS_INT | tds_convert_uint8 (const TDS_UINT8 *src, int desttype, CONV_RESULT *cr)
static TDS_INT | tds_convert_unique (const TDS_CHAR *src, int desttype, CONV_RESULT *cr)
TDSRET | tds_datecrack (TDS_INT datetype, const void *di, TDSDATEREC *dr)
| Convert from db date format to a structured date format. More...
TDS_SERVER_TYPE | tds_get_null_type (TDS_SERVER_TYPE srctype)
| Get same type but nullable. More...
size_t | tds_strftime (char *buf, size_t maxsize, const char *format, const TDSDATEREC *dr, int prec)
| format a date string according to an "extended" strftime(3) formatting definition. More...
unsigned char | tds_willconvert (int srctype, int desttype)
| Test if a conversion is possible. More...

## Variables

const char | tds_hex_digits [] = "0123456789abcdef"

## Detailed Description

Conversions between datatypes.

Supports, for example, dbconvert().

## Macro Definition Documentation

## § CASE_ALL_BINARY

#define CASE_ALL_BINARY

Value:
SYBBINARY: case SYBVARBINARY: case SYBIMAGE: case XSYBBINARY: case XSYBVARBINARY: \
case SYBLONGBINARY: case TDS_CONVERT_BINARY

## Function Documentation

## § parse_int8()

static TDS_INT parse_int8 | ( | const char * | buf,
|  | const char * | pend,
|  | TDS_UINT8 * | res,
|  | bool * | p_negative
| ) |  |

static

convert a number in string to TDS_INT8

Returns
  TDS_CONVERT_* or failure code on error

Here is the call graph for this function:
Here is the caller graph for this function:

## § parse_numeric()

static const char * parse_numeric | ( | const char * | buf,
|  | const char * | pend,
|  | bool * | p_negative,
|  | size_t * | p_digits,
|  | size_t * | p_decimals
| ) |  |

static

Parse a string for numbers.

Syntax can be something like " *[+-] *[0-9]*\.[0-9]* *".

The function ignore all spaces. It strips leading zeroes which could possibly lead to overflow. The function returns a pointer to the integer part followed by *p_digits digits followed by a dot followed by *p_decimals digits (dot and fractional digits are optional, in this case *p_decimals is 0).

Parameters

  buf | start of string
  pend | pointer to string end
  p_negative | store if number is negative
  p_digits | store number of integer digits
  p_decimals | store number of fractional digits

Returns
  pointer to first not zero digit. If NULL this indicate a syntax error.

Here is the caller graph for this function:

## § store_monthname()

static int store_monthname | ( | const char * | datestr,
|  | struct tds_time * | t
| ) |  |

static

Test if a string is a month name and store correct month number.

Returns
  month number (0-11) or -1 if not match

Parameters

  datestr | string to check
  t | where to store month (if NULL no store is done)

Here is the call graph for this function:

## § string_to_int()

static TDS_INT string_to_int | ( | const char * | buf,
|  | const char * | pend,
|  | TDS_INT * | res
| ) |  |

static

convert a number in string to TDS_INT

Returns
  TDS_CONVERT_* or failure code on error

Remarks
  Sybase's char->int conversion tolerates embedded blanks, such that "convert( int, ' - 13 ' )" works. If we find blanks, we copy the string to a temporary buffer, skipping the blanks. We return the results of atoi() with a clean string.

n.b. it is possible to embed all sorts of non-printable characters, but we only check for spaces. at this time, no one on the project has tested anything else.

Here is the call graph for this function:

## § string_to_int8()

static TDS_INT string_to_int8 | ( | const char * | buf,
|  | const char * | pend,
|  | TDS_INT8 * | res
| ) |  |

static

convert a number in string to TDS_INT8

Returns
  TDS_CONVERT_* or failure code on error

Here is the call graph for this function:

## § string_to_numeric()

static int string_to_numeric | ( | const char * | instr,
|  | const char * | pend,
|  | CONV_RESULT * | cr
| ) |  |

static

convert a number in string to a TDSNUMERIC

Returns
  sizeof(TDS_NUMERIC) on success, TDS_CONVERT_* failure code on failure

Here is the caller graph for this function:

## § string_to_uint8()

static TDS_INT string_to_uint8 | ( | const char * | buf,
|  | const char * | pend,
|  | TDS_UINT8 * | res
| ) |  |

static

convert a number in string to TDS_UINT8

Returns
  TDS_CONVERT_* or failure code on error

Here is the call graph for this function:

## § stringz_to_numeric()

static int stringz_to_numeric | ( | const char * | instr,
|  | CONV_RESULT * | cr
| ) |  |

static

convert a zero terminated string to NUMERIC

Returns
  sizeof(TDS_NUMERIC) on success, TDS_CONVERT_* failure code on failure

Here is the call graph for this function:

## § tds_convert()

TDS_INT tds_convert | ( | const TDSCONTEXT * | tds_ctx,
|  | int | srctype,
|  | const TDS_CHAR * | src,
|  | TDS_UINT | srclen,
|  | int | desttype,
|  | CONV_RESULT * | cr
| ) |  |

tds_convert convert a type to another.

If you convert to SYBDECIMAL/SYBNUMERIC you MUST initialize precision and scale of cr. Do not expect strings to be zero terminated. Databases support zero inside string. Using strlen may result on data loss or even a segmentation fault. Instead, use memcpy to copy destination using length returned. This function does not handle NULL, srclen should be >0. Client libraries handle NULLs each in their own way.

Parameters

  tds_ctx | context (used in conversion to data and to return messages)
  srctype | type of source
  src | pointer to source data to convert
  srclen | length in bytes of source (not counting terminator or strings)
  desttype | type of destination
  cr | structure to hold result

Returns
  length of result or TDS_CONVERT_* failure code on failure. All TDS_CONVERT_* constants are <0.

## § tds_datecrack()

TDSRET tds_datecrack | ( | TDS_INT | datetype,
|  | const void * | di,
|  | TDSDATEREC * | dr
| ) |  |

Convert from db date format to a structured date format.

Parameters

  datetype | source date type. SYBDATETIME or SYBDATETIME4
  di | source date
  dr | destination date

Returns
  TDS_FAIL or TDS_SUCCESS

## § tds_get_null_type()

TDS_SERVER_TYPE tds_get_null_type | ( | TDS_SERVER_TYPE | srctype | ) |

Get same type but nullable.

Parameters

  srctype | type requires

Returns
  nullable type

## § tds_strftime()

size_t tds_strftime | ( | char * | buf,
|  | size_t | maxsize,
|  | const char * | format,
|  | const TDSDATEREC * | dr,
|  | int | prec
| ) |  |

format a date string according to an "extended" strftime(3) formatting definition.

Parameters

  buf | output buffer
  maxsize | size of buffer in bytes (space include terminator)
  format | format string passed to strftime(3), except that z represents fraction of seconds.
  dr | date to convert
  prec | second fraction precision (0-7).

Returns
  length of string returned, 0 for error

## § tds_willconvert()

unsigned char tds_willconvert | ( | int | srctype,
|  | int | desttype
| ) |  |

Test if a conversion is possible.

Parameters

  srctype | source type
  desttype | destination type

Returns
  0 if not convertible

Here is the call graph for this function: