对于指定的 GBase 8s ESQL/C 或 SQL 数据类型,rtypmsize() 函数返回您必须在内
存中分配的字节数。
语法
mint rtypmsize(sqltype, sqllen)
mint sqltype;
GBase 8s ESQL/C 编程指南
南大通用数据技术股份有限公司
- 914 -
mint sqllen;
sqltype
GBase 8s ESQL/C 或 SQL 数据类型的整数代码。
sqllen
对于指定的数据类型,数据文件中的字节数。
用法
当您使用 sqlda 结构来动态地将数据访存至缓冲区时,rtypalign() 和 rtypmsize() 函
数是有用的。这些函数为列数据提供独立于机器的存储。
随同 DESCRIBE 语句初始化的 sqlda 结构,提供 rtypmsize() 函数来使用。在
DESCRIBE 语句确定列信息之后,sqltype 和 sqllen 组件的值在每一 sqlda.sqlvar 结构中
同名的组件中。
当 rtypmsize() 确定字符数据的大小时,请记住下列大小信息:
对于 CCHARTYPE(char)和 CSTRINGTYPE(string), GBase 8s ESQL/C 为空
终止符将字符的数目加一个字节。
对于 CFIXCHARTYPE(fixchar),GBase 8s ESQL/C 不添加空终止符。
在 unload.ec 演示程序中,您可看到 rtypmsize() 函数的应用。
返回代码
0
sqltype 不是有效的 SQL 类型。
>0
返回值是 sqltype 数据类型要求的字节数。
示例
此样例程序在 demo 目录中的 rtypmsize.ec 文件中。
/*
* rtypmsize.ec *
This program prepares a select statement on all columns of the catalog table. Then it
GBase 8s ESQL/C 编程指南
南大通用数据技术股份有限公司
- 915 -
displays the data type of each column and the number of bytes needed to store it in memory.
*/
#include
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");
GBase 8s ESQL/C 编程指南
南大通用数据技术股份有限公司
- 916 -
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);
/* column hdgs. */
printf("\n\tColumn Type Size\n\n");
/*
* 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%-8t%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
GBase 8s ESQL/C 编程指南
南大通用数据技术股份有限公司
- 917 -
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
输出
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.