skip to main content
Designing and coding the IP : User defined scalar functions : Registering user defined scalar functions : In an IP developed in C/C++
 

In an IP developed in C/C++

To register user-defined scalar functions in an IP developed in C/C++:
1. Implement and register INIT_SCALAR as part of your IP. Refer to Chapter 2, in OpenAccess SDK SQL Engine Programmer’s Reference for C/C++ for information on implementing and using the INIT_SCALAR function.
2. In the INIT_SCALAR function, use dam_add_scalar to register each function.
/*Register it */
int mem_ip_init_scalar(IP_HDBC hdbc,
XM_Tree *pMemTree,
DAM_HSCALAR_LIST pList )
{
dam_add_scalar(pMemTree, pList,"INTVAL",1, ip_func_intval,XO_TYPE_INTEGER,1);
return DAM_SUCCESS;
}
 
/* Implementation of INTVAL scalar function
* out = INTVAL(a) – out is returned same as the input value
*/
DAM_HVAL ip_func_intval(DAM_HSTMT hstmt, XM_Tree *pMemTree, DAM_HVALEXP_LIST hValExpList)
{
char sFunctionName[]="Intval";
DAM_HVALEXP hValExp;
DAM_HVAL hVal;
long *piInputVal;
long lVal;
int iInputLen;
int iRetCode;
char sBuf[128];
/* get the input */
hValExp = dam_getFirstValExp(hValExpList);
iRetCode = dam_getValueOfExp(pMemTree, hValExpList, hValExp, XO_TYPE_INTEGER,
(void **)&piInputVal, &iInputLen);
if (iRetCode != DAM_SUCCESS) {
sprintf(sBuf, "Invalid input to scalar function %s().", sFunctionName);
dam_addError(NULL, hstmt, DAM_ERR_01000, 0, sBuf);
tm_trace(sqldrv_tmHandle, UL_TM_ERRORS, "%s\n", (sBuf));
return NULL;
}
 
if (iInputLen == XO_NULL_DATA) { /* return NULL */
hVal = dam_createVal(pMemTree, XO_TYPE_INTEGER, NULL, XO_NULL_DATA);
return hVal;
}
lVal = *piInputVal;
hVal = dam_createVal(pMemTree, XO_TYPE_INTEGER, &lVal, sizeof(lVal));
return hVal;
}
3. Optionally, in the INIT_SCALAR function, use dam_add_scalarEx2 to register each function with a qualifier.
/*Register it */
int mem_ip_init_scalar(IP_HDBC hdbc,
XM_Tree *pMemTree,
DAM_HSCALAR_LIST pList )
{
dam_add_scalarEx2(pMemTree, pList,"INTEGER", "ADD",1, ip_func_integer_add,
XO_TYPE_INTEGER,1);
dam_add_scalarEx2(pMemTree, pList,"STRING", "ADD",1, ip_func_String_add,
XO_TYPE_VARCHAR,1);
return DAM_SUCCESS;
}