skip to main content
Designing and coding the IP : User defined scalar functions : Accessing statement or connection context
 

Accessing statement or connection context

If the IP implementer wants to share statement or connection context information between EXECUTE operations and scalar function implementation, the IP should be implemented as described in this section.
Constant Scalar functions take control of query execution prior to IP EXECUTE. If the function is a non-constant function, the function is called after IP EXECUTE is called. If the IP supports constant and non-constant functions, it can have functions initialize global context after checking if global context is not already initialized.
The IP EXECUTE operation also checks and initializes the global query context. Either the constant-scalar functions or the IP EXECUTE sets up the global context. Non-constant scalar functions have access to existing global context. IP EXECUTE should use START_QUERY to setup global context and END_QUERY to release the global context. This ensures that all non-constant scalar functions gets evaluated before the IP releases the global context.
All scalar functions (constant or non-constant) can get access to IP_HDBC by calling dam_getInfo(DAM_INFO_IP_HDBC).
/* Global Statement Descriptor Area */
typedef struct xxx_gstatement_struct {
char sFuncName[DAM_MAX_ID_LEN+1]; /* Name of the scalar function used in query */
int iAnyVal;
} XXX_GSTMT_DA;
 
XXX_GSTMT_DA xxx_init_global_context(DAM_HSTMT hstmt, XM_Tree *pMemTree)
{ XXX_GSTMT_DA *pgStmtDA;
int iValueLen;
int iRetCode;
iRetCode = dam_getInfo(NULL, hstmt, DAM_INFO_STMT_IP_CONTEXT,
(void *)&pgStmtDA, sizeof(pgStmtDA), &iValueLen);
if (pgStmtDA) {
/* context was alreadu initialized */
return pgStmtDA;
}
pgStmtDA = (XXX_GSTMT_DA *)xm_allocItem(pMemTree,
sizeof(XXX_GSTMT_DA), XM_NOFLAGS);
iRetCode = dam_setInfo(NULL, hstmt, DAM_INFO_STMT_IP_CONTEXT,
(void *)pgStmtDA, 0)
return pgStmtDA;
}
 
/* Scalar function xxx */
DAM_HVAL dam_func_ xxx(DAM_HSTMT hstmt, XM_Tree *pMemTree, DAM_HVALEXP_LIST hValExpList)
{
XXX_GSTMT_DA *pgStmtDA;
pgStmtDA = xxx_init_global_context(hstmt, pMemTree);
/* constant scalar functions can setup any information in this global structure.
Non constant functions can access any information initialized by IP execute */
strcpy(pgStmtDA->sFuncName, "abc");
}
 
ipExecute(IP_HDBC hdbc, DAM_HSTMT hstmt,...)
{
XXX_GSTMT_DA *pgStmtDA;
int iValueLen;
pgStmtDA = xxx_init_global_context(hstmt, pMemTree);
if (strlen(pgStmtDA->sFuncName) > 0) {
/* information setup by constant scalar function can be retrieved */
}
}
Note: Scalar functions are evaluated at the global context of the query. When executing join queries, if the IP uses dam_setIP_hstmt to save table information, scalar functions do not have access to individual table information. So if scalar functions were to call dam_getIP_hstmt, they would get NULL handle when processing join queries.