我成功编译了最初在AIX上编译的64位Linux上的一些UDF
Linux正在运行IBM DB2 v9.5
这是有时为某些值返回的函数
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include "cbudf.hpp"
using namespace std;
extern "C"
void SQL_API_FN cbzdt(SQLUDF_VARCHAR_FBD *pArg,
SQLUDF_VARCHAR *pResult,
SQLUDF_NULLIND *pArgInd,
SQLUDF_NULLIND *pResultInd,
SQLUDF_CHAR *pSqlState,
SQLUDF_VARCHAR *pFuncName,
SQLUDF_VARCHAR *pSpecName,
SQLUDF_VARCHAR *pMsgTxt)
{
short i;
unsigned long x = 0;
cout << "entered cbzdt";
if (pArg->length < CM_UDF_ZDT_SIZE){
*pResultInd = CM_UDF_IS_NULL_IND;
cout << "pArg length is:" << pArg->length << " and that is smaller than CM_UDF_ZDT_SIZE: " << CM_UDF_ZDT_SIZE;
}
else
{
cout << "entered else";
for (i = 0; i < 4; i++)
x = (x << 8) | pArg->data[i];
if (x & 0xff000000){
*pResultInd = CM_UDF_IS_NULL_IND;
cout << "x mask is true.. making null";
}
else
{
cout << "entered nested else";
*pResultInd = CM_UDF_NOT_NULL_IND;
CtDate zDate(x);
// strcpy(pResult, (const char *)(zDate.asString("%Y-%m-%d")));
strncpy(pResult, (const char *)(zDate.asString("%Y-%m-%d")),11);
if (pResult[0] == '3'){ // if year is 3000
pResult[9] = '1'; // fix day
cout << "pResult =1";
}
}
}
}
这是数据:
COID_ TIMESTAMP_ CHANGE_DATE
-------------------------- -------------------------- ---------------------------
1814 2010-02-17-15.44.22.995784 x'0025780F000000000804011B'
1814 2010-02-17-15.44.55.583734 x'0025780F000000000804011B'
1814 2010-02-17-15.46.09.929014 x'0025780F000000000804011B'
1324 2009-12-02-21.31.13.058337 x'0025769F000000000804011B'
1324 2009-12-02-21.32.47.387150 x'0025769F000000000804011B'
1324 2009-12-02-21.34.03.020405 x'0025769F000000000804011B'
1324 2009-12-08-19.54.13.492488 x'002576A1000000000804011B'
1324 2009-12-08-19.55.22.041794 x'002576A1000000000804011B'
1358 2009-12-09-15.14.05.593887 x'002576A1000000000804011B'
After function applied:
COID_ TIMESTAMP_ 3
-------------------------- -------------------------- ----------
1814 2010-02-17-15.44.22.995784 2011-01-05
1814 2010-02-17-15.44.55.583734 2011-01-05
1814 2010-02-17-15.46.09.929014 2011-01-05
1324 2009-12-02-21.31.13.058337 -
1324 2009-12-02-21.32.47.387150 -
1324 2009-12-02-21.34.03.020405 -
1324 2009-12-08-19.54.13.492488 -
1324 2009-12-08-19.55.22.041794 -
1358 2009-12-09-15.14.05.593887 -
有人有什么想法吗?
我的猜测是这个if语句:
if (x & 0xff000000){
*pResultInd = CM_UDF_IS_NULL_IND;
在某些值上返回true。它可能是与64位相关的东西吗?
此外,couts不会在DB2输出中打印
由于
答案 0 :(得分:0)
将二进制zDate的一部分转换为可用日期的规则是什么?您可能在SQL中提出了等效的转换函数,并完全避免使用外部编译的UDF。 HEX函数将二进制值转换为十六进制字符串,然后您可以使用SUBSTR迭代以构建整数值。
至于cout消息,我不认为cout在外部UDF中会有多大用处。您可以更好地将调试信息保存到本地文件中。