skip to main content
Designing and coding the IP : Bulk insert : Bulk insert processing in C/C++ : Example
 

Example

1. Obtain the first row to be inserted using dam_getFirstInsertRow.
2. Obtain the first value set using dam_getFirstValueSet and pass the row element to dam_getBulkValueToSet.
Use dam_getColToSet to obtain details about the column.
3. Call dam_getBulkValueToSet to obtain column value buffer, buffer length, buffer size, and the number of values in the buffer.
You must save the bulk information returned by dam_getBulkValueToSet for all the columns.
4. Obtain the next value set using dam_getNextValue Set.
If the value returned is not null, repeat Steps 3 and 4 until all the values are retrieved.
...
 
while (hRowElem) {
 
hcol = dam_getColToSet(hRowElem);
 
iRetCode = dam_getBulkValueToSet(pStmtDA->dam_hstmt, hRowElem, (void **)&pValBuf, &pLenBuf, &iValSize, &iRowCount);
if (iRetCode != DAM_SUCCESS) {
return iRetCode;
}
 
/* Add code to store or save the buffers here*/
}
 
/*get next row element */
hRowElem = dam_getNextValueSet(pStmtDA->dam_hstmt);
}
5. For each row, use the saved bulk information to retrieve the column values from the data buffer and to build the rows.
a. Increment the pointer to the data buffer by size of the value (PiValSize *piValSize) to move to the next value in the buffer.
b. Increment the pointer to the length buffer by 1 (p++) to obtain the length of the next value in the buffer.
6. Insert rows to the data source and mark the row insert status using pParamStatusArray[iRow] = DAM_ROW_SUCCESS or pParamStatusArray[iRow] = DAM_ROW_ERROR.
If a row is inserted successfully, its status is marked as DAM_ROW_SUCCESS. Otherwise, the status is marked as DAM_ROW_ERROR.
The following code snippet illustrates how to read values from the bulk buffer.
for(iRow = 0;iRow < *piRowCount;iRow++)
{
int iXoType = ColXoType; /* XoType of the column */
int iValSize = *piValSize; /* Size of each value */
DAM_HBULKVAL pValBuf = pValBuffer; /* Bulk Values buffer */
DAM_HBULKVALLEN * pLenBuf = pLenBuffer; /* Buffer containing the lengths */
long iValLen; /* data length */
if (*(pLenBuf+iRow) != XO_NULL_DATA)
{
switch (iXoType)
{
case XO_TYPE_CHAR:
case XO_TYPE_VARCHAR:
case XO_TYPE_NUMERIC:
case XO_TYPE_DECIMAL:
case XO_TYPE_BIGINT:
{
char sColVal[256];
 
strncpy((char*)pColVal, (char*)pValBuf+(iRow*iValSize), *(pLenBuf+iRow)+1);
*iValLen = *(pLenBuf+iRow);
}
break;
 
case XO_TYPE_WCHAR:
case XO_TYPE_WVARCHAR:
{
wchar_t wsColVal[256];
 
wcsncpy((wchar_t*)wsColVal, (const wchar_t *)((char*)pValBuf+(iRow*iValSize)),*(pLenBuf+iRow)+1;
iValLen = *(pLenBuf+iRow);
}
break;
 
case XO_TYPE_INTEGER:
{
long iVal;
 
iVal = *((long *)((char*)pValBuf+(iRow*iValSize)));
iValLen = *(pLenBuf+iRow);
}
break;
case XO_TYPE_BIT:
case XO_TYPE_TINYINT:
{
unsigned char iVal;
 
iVal = *((char*)pValBuf+(iRow*iValSize));
iValLen = *(pLenBuf+iRow);
}
break;
case XO_TYPE_SMALLINT:
{
short int iVal;
 
iVal = *((short int *)((char*)pValBuf+(iRow*iValSize)));
iValLen = *(pLenBuf+iRow);
}
break;
case XO_TYPE_REAL:
{
float iVal;
 
iVal = *(float *)((char*)pValBuf+(iRow*iValSize));
iValLen = *(pLenBuf+iRow);
}
break;
 
case XO_TYPE_DOUBLE:
case XO_TYPE_FLOAT:
{
double iVal;
 
iVal = *(double *)((char*)pValBuf+(iRow*iValSize));
iValLen = *(pLenBuf+iRow);
}
break;
case XO_TYPE_DATE:
case XO_TYPE_TIME:
case XO_TYPE_TIMESTAMP:
{
strncpy((char*)pColVal, (char*)pValBuf+(iRow*iValSize), *(pLenBuf+iRow)+1);
iValLen = *(pLenBuf+iRow);
}
 
break;
default:
} /* type */
}
else /* NULL */
{
*piValLen = XO_NULL_DATA; /* to avoid tm_trace to crash */
}
}