OTL bigint(int64)的处理

1.OTL_STL
这个宏的作用是声明后可以使用string类型。开始时不知道,存取VARCHAR2字段时都是用char[],总是得算计着字符串的长度,很麻烦(吐槽一下,官方的例子也总是用char[],为什么不用string呢,不解),用了这个宏之后,otl_stream对象输入,输出VARCHAR2字段时可以直接读写string变量,很方便。
2.OTL_BIGINT      这个宏的作用是声明后可以使用64位的整型和数据库交互。因为Oracle的NUMBER最多能有38位,所以有时候NUMBER字段中的数字放不到一个int里,这时候须要用到__int64,但是__int64并不是基类型,OTL提供的绑定变量类型声明中没有int64,但是有一个bigint,这时候就需要OTL_BIGINT这个宏来实现他们之间的转化。
#define OTL_BIGINT int64
这样声明后,就可以用bigint作为绑定变量的类型说明符了,比较特殊的是,bigint字段在select时也要注明,类似这样:"select c1 :#1<bigint>, c2 from t1",特别说明,c1必须放在第一列,否则会出错
我自己在工程中这样使用:
Makefile.setting:
USE_OTL_BIGINT=1 //是否启动otl_bigint的开关
Makefile推导模板  Makefile.base:
ifeq "$(USE_OTL_BIGINT)" "1"
        DEFS += USE_OTL_BIGINT
endif
包含otlv4.h头文件的otl.h:
#include "otlv4.h" 之前增加宏定义
#ifdef USE_OTL_BIGINT
#define OTL_BIGINT int64
/*This #define enables the mapping from <bigint> for 64-bit OCIs for LP64 platforms to signed 64-bit longs.
It's a more efficient alternative to the char[XXX] binding and bigint-string / string-bigint conversion
(see also the following #define's: OTL_BIGINT,  OTL_BIGINT_TO_STR,   OTL_STR_TO_BIGINT).*/
//将int64隐射到long上,这样的好处是不需要再定义OTL_BIGINT_TO_STR和OTL_STR_TO_BIGINT来进行转换。
#define OTL_ORA_MAP_BIGINT_TO_LONG
#endif
 
#define OTL_DESTRUCTORS_DO_NOT_THROW
#define OTL_STL
 
cpp中使用otl时可兼容是否开启otl_bigint:
char szSql[1024]="";
#ifdef USE_OTL_BIGINT 
char szBigint[32]="bigint";
char szHint[32]=":#1<bigint>";
#else
char szBigint[32]="double";
char szHint[32]="";
#endif
 aistring strSchemeZd = "ZD";
snprintf(szSql, sizeof(szSql), "select nvl(a.acct_id, -1) %s, nvl(a.region_code, -1)"
" from %s.SYS_RT_ACCOUNT a where a.acct_id = :val0<%s>"
" and a.valid_date <= :val1<int> and (a.expire_date > :val2<int> or a.expire_date is null)"
, szHint, strSchemeZd.c_str(), szBigint);
 
otl_stream 析入析出时:
int32 queryXXXXXX(
const int16 nSqlType,
const char* szSql, 
const int64 iQueryParam, 
const char* szQueryParam, 
const int32 nszParamType)
{
#ifdef USE_OTL_BIGINT 
int64 iTmpBigint = 0;
#else
double iTmpBigint = 0;
#endif
 
otl_stream stream(50, szSql, *pConn);
iTmpBigint = iQueryParam;
if (iQueryParam > -1) 
{
iTmpBigint = iQueryParam;
stream<<iTmpBigint;
}
if (NULL != szQueryParam) 
{
stream<<szQueryParam;
stream<<nszParamType;
}
 
if(stream.eof())//未查询到
{;
}
stream >> iTmpBigint;
XXX.set_acctId(static_cast<int64>(iTmpBigint));
 
 
 
 

猜你喜欢

转载自gotowqj.iteye.com/blog/2027474