skip to main content
OpenAccess SDK SQL Engine Core Functions API : Custom scalar functions : Using custom scalar functions with a C/C++ IP
 

Using custom scalar functions with a C/C++ IP

To use custom scalar functions with a C/C++ IP:
1. Register the custom scalar function with the OpenAccess SDK SQL engine. Using the INIT_SCALAR function, call dam_add_custom_scalar to register each custom scalar function. This example registers a custom scalar function named NEAR:
dam_add_custom_scalar(pMemTree, pList, "NEAR");
2. Obtain the custom conditions in the query. When IP EXECUTE is called, call dam_getSetOfCustomConditionLists to validate the custom scalar function and get the custom condition lists. For example:
pSetOfCustCondList = dam_getSetOfCustomConditionLists(hstmt, pbPartialLists);
If pbPartialLists is returned as True, a partial list is returned. A partial list is returned in the following cases:
*If a custom scalar function is an operand in a condition and another operand is not a COLUMN of the table.
*If a custom scalar function is used with non-literal parameters.
*If a custom scalar function is used in an expression.
*If a custom scalar condition is used in a JOIN that is bound by the OR operator.
When a partial list is returned, your IP cannot mark all the custom conditions in the list as having been evaluated. In this case, generate an exception and exit from the query execution; otherwise, the SQL engine reports an error on each target row validation.
3. Get details about each custom condition. Iterate through each set of custom condition lists (using dam_getFirstCondList and dam_getNextCondList, for example) and fetch the custom conditions (using dam_getFirstCond and dam_getNextCond, for example).
For each condition, call dam_describeCustomCond to get the following information:
*Column specified in the condition.
*Operator (for example =, >, or LIKE).
*Name of the scalar function.
*Parameters passed to the function. Parameters are returned as a ValExpList. You can navigate this list by calling dam_getFirstValExp and dam_getNextValExp. You can get details of each expression by calling dam_getValueofExpEx.
This example requests this information from the scalar function named NEAR:
dam_describeCustomCond(hstmt, hCond, phCol, piOpType, "NEAR", phValExpList);
4. Mark each custom condition list as having been processed. Call dam_setOption for each condition list, marking each one as having been processed. This example marks the DAM_CONDLIST_OPTION_EVALUATION option to a value of DAM_PROCESSING_OFF:
dam_setOption(DAM_CONDLIST_OPTION, DAM_HCONDLIST, DAM_CONDLIST_OPTION_EVALUATION, DAM_PROCESSING_OFF)
See dam_setOption for more information.
The SQL engine marks these condition lists as PROCESSED by the IP and skips their evaluation when validating target rows; otherwise, the SQL engine reports an error for each target row validation.
5. Build results that match the custom conditions.
*Custom scalar functions are only supported on INTERSECT type condition lists.
*The dam_getOptimalIndexAndConditions(), dam_getRestrictionList(), dam_getSetOfConditionLists() and dam_getSetOfConditionListsEx() functions only return non-custom scalar condition lists.
*If a standard scalar function and a custom scalar function are registered with same name, the first function that is registered has precedence.
For information about error messages that can occur while using custom scalar functions, refer to "Error Message Codes" in the OpenAccess SDK Troubleshooting Guide and Reference.