To transfer information between a program and the database server, every data item must have a data type. You can create a host variable with any one of the supported types.
Only a limited number of C data types are supported as host variables. Also, certain host variable types do not have a corresponding C type.
Macros defined in the sqlca.h header file can be used to declare a host variable of type VARCHAR, FIXCHAR, BINARY, DECIMAL, or SQLDATETIME. These macros are used as follows:
EXEC SQL BEGIN DECLARE SECTION; DECL_VARCHAR( 10 ) v_varchar; DECL_FIXCHAR( 10 ) v_fixchar; DECL_BINARY( 4000 ) v_binary; DECL_DECIMAL( 10, 2 ) v_packed_decimal; DECL_DATETIME v_datetime; EXEC SQL END DECLARE SECTION; |
The preprocessor recognizes these macros within a declaration section and treats the variable as the appropriate type.
The following data types are supported by the embedded SQL programming interface:
short int I; unsigned short int I; |
long int l; unsigned long int l; |
float f; |
double d; |
DECL_DECIMAL(p,s) typedef struct TYPE_DECIMAL { char array[1]; } TYPE_DECIMAL; |
Null terminated, blank-padded character string
char a[n]; /* n > 1 */ char *a; /* n = 2049 */ |
The SQL preprocessor assumes that a pointer to char points to a character array of size 2049 bytes and that this array can safely hold 2048 characters, plus the NULL terminator. In other words, a char* data type maps to a CHAR(2048) SQL type. If that is not the case, your application may corrupt memory.
If you are using a 16-bit compiler, requiring 2049 bytes can make the program stack overflow. Instead, use a declared array, even as a parameter to a function, to let the SQL preprocessor know the size of the array. WCHAR and TCHAR behave similarly to char.
NULL terminated UNICODE or wide character string Each character occupies two bytes of space and so may contain UNICODE characters.
WCHAR a[n]; /* n > 1 */ |
NULL terminated system-dependent character string A TCHAR is equivalent to a WCHAR for systems that use UNICODE (for example, Windows Mobile) for their character set; otherwise, a TCHAR is equivalent to a char. The TCHAR data type is designed to support character strings in either kind of system automatically.
TCHAR a[n]; /* n > 1 */ |
Fixed-length blank padded character string
char a; /* n = 1 */ DECL_FIXCHAR(n) a; /* n >= 1 */ |
Variable-length character string with a two-byte length field When supplying information to the database server, you must set the length field. When fetching information from the database server, the server sets the length field (not padded).
DECL_VARCHAR(n) a; /* n >= 1 */ typedef struct VARCHAR { a_sql_ulen len; TCHAR array[1]; } VARCHAR; |
Variable-length binary data with a two-byte length field When supplying information to the database server, you must set the length field. When fetching information from the database server, the server sets the length field.
DECL_BINARY(n) a; /* n >= 1 */ typedef struct BINARY { a_sql_ulen len; unsigned char array[1]; } BINARY; |
SQLDATETIME structure with fields for each part of a timestamp
DECL_DATETIME a; typedef struct SQLDATETIME { unsigned short year; /* for example: 1999 */ unsigned char month; /* 0-11 */ unsigned char day_of_week; /* 0-6, 0 = Sunday */ unsigned short day_of_year; /* 0-365 */ unsigned char day; /* 1-31 */ unsigned char hour; /* 0-23 */ unsigned char minute; /* 0-59 */ unsigned char second; /* 0-59 */ unsigned long microsecond; /* 0-999999 */ } SQLDATETIME; |
If you use a SQLDATETIME structure to enter a date, time, or timestamp into the database, the day_of_year and day_of_week members are ignored.
DT_LONGVARCHAR Long varying length character data. The macro defines a structure, as follows:
#define DECL_LONGVARCHAR( size ) \ struct { a_sql_uint32 array_len; \ a_sql_uint32 stored_len; \ a_sql_uint32 untrunc_len; \ char array[size+1];\ } |
The DECL_LONGVARCHAR struct may be used with more than 32KB of data. Data may be fetched all at once, or in pieces using the GET DATA statement. Data may be supplied to the server all at once, or in pieces by appending to a database variable using the SET statement. The data is not null terminated.
DT_LONGBINARY Long binary data. The macro defines a structure, as follows:
#define DECL_LONGBINARY( size ) \ struct { a_sql_uint32 array_len; \ a_sql_uint32 stored_len; \ a_sql_uint32 untrunc_len; \ char array[size]; \ } |
The DECL_LONGBINARY struct may be used with more than 32KB of data. Data may be fetched all at once, or in pieces using the GET DATA statement. Data may be supplied to the server all at once, or in pieces by appending to a database variable using the SET statement.
The structures are defined in the %SQLANY12%\SDK\Include\sqlca.h file. The VARCHAR, BINARY, and TYPE_DECIMAL types contain a one-character array and are not useful for declaring host variables. However, they are useful for allocating variables dynamically or typecasting other variables.
Discuss this page in DocCommentXchange.
|
Copyright © 2012, iAnywhere Solutions, Inc. - SQL Anywhere 12.0.1 |