SQLRETURN SQL_API SQLSetDescRec()

in src/odbc/rsodbc/rsdesc.cpp [1056:1176]


SQLRETURN  SQL_API SQLSetDescRec(SQLHDESC phdesc,
                                    SQLSMALLINT hRecNumber, 
                                    SQLSMALLINT hType,
                                    SQLSMALLINT hSubType, 
                                    SQLLEN        iOctetLength,
                                    SQLSMALLINT hPrecision, 
                                    SQLSMALLINT hScale,
                                    SQLPOINTER    pData, 
                                    SQLLEN *    plStrLen,
                                    SQLLEN *    plIndicator)
{
    SQLRETURN rc = SQL_SUCCESS;
    RS_DESC_INFO *pDesc = (RS_DESC_INFO *)phdesc;
    RS_DESC_REC     *pDescRec;
    int iIsWritableField;

    if(IS_TRACE_LEVEL_API_CALL())
        TraceSQLSetDescRec(FUNC_CALL, 0, phdesc, hRecNumber, hType, hSubType, iOctetLength, hPrecision, hScale, pData, plStrLen, plIndicator);

    if(!VALID_HDESC(phdesc))
    {
        rc = SQL_INVALID_HANDLE;
        goto error;
    }

    // Clear error list
    pDesc->pErrorList = clearErrorList(pDesc->pErrorList); 

    if(hRecNumber <= 0)
    {
        rc = SQL_ERROR;
        addError(&pDesc->pErrorList,"07009", "Invalid descriptor index", 0, NULL);
        goto error; 
    }

    iIsWritableField = isWritableField(pDesc, SQL_DESC_TYPE);
    if(iIsWritableField)
    {
        iIsWritableField = isWritableField(pDesc, SQL_DESC_DATETIME_INTERVAL_CODE);
        if(iIsWritableField)
        {
            iIsWritableField = isWritableField(pDesc, SQL_DESC_OCTET_LENGTH);
            if(iIsWritableField)
            {
                iIsWritableField = isWritableField(pDesc, SQL_DESC_PRECISION);
                if(iIsWritableField)
                {
                    iIsWritableField = isWritableField(pDesc, SQL_DESC_SCALE);
                    if(iIsWritableField)
                    {
                        iIsWritableField = isWritableField(pDesc, SQL_DESC_DATA_PTR);
                        if(iIsWritableField)
                        {
                            iIsWritableField = isWritableField(pDesc, SQL_DESC_OCTET_LENGTH_PTR);
                            if(iIsWritableField)
                            {
                                iIsWritableField = isWritableField(pDesc, SQL_DESC_INDICATOR_PTR);
                            }
                        }
                    }
                }
            }
        }
    }

    if(!iIsWritableField)
    {
        rc = SQL_ERROR;
        addError(&pDesc->pErrorList,"HY000", "Field is not writable", 0, NULL);
        goto error;
    }

    if(pData == NULL && plIndicator == NULL && pDesc->iType == RS_ARD)
    {
        // Unbind it
        releaseDescriptorRecByNum(pDesc, hRecNumber);
    }
    else
    {
        // Find if rec already exist, then it's re-bind.
        pDescRec = checkAndAddDescRec(pDesc, hRecNumber, TRUE, NULL);
        if(pDescRec == NULL)
        {
            rc = SQL_ERROR;
            if(pDesc->iRecListType ==  RS_DESC_RECS_ARRAY_LIST)
                addError(&pDesc->pErrorList,"HY000", "Cannot add new rec in an array", 0, NULL);
            else
                addError(&pDesc->pErrorList,"HY001", "Memory allocation error", 0, NULL);
            goto error;
        }

        // Set values
        if(pDescRec)
        {
            pDescRec->hType    = hType;
            pDescRec->hDateTimeIntervalCode = hSubType;
            pDescRec->iOctetLen = (int) iOctetLength;
            pDescRec->iPrecision = hPrecision;
            pDescRec->hScale = hScale;
            pDescRec->pValue = pData;
            pDescRec->plOctetLen = (SQLINTEGER *)plStrLen;
            pDescRec->pcbLenInd = plIndicator;

            if(pDesc->iType == RS_APD
                 || pDesc->iType == RS_ARD)
            {
                if(pDescRec->cbLen == 0)
                {
                    pDescRec->cbLen  = iOctetLength;
                }
            }
        }
    }

error:

    if(IS_TRACE_LEVEL_API_CALL())
        TraceSQLSetDescRec(FUNC_RETURN, rc, phdesc, hRecNumber, hType, hSubType, iOctetLength, hPrecision, hScale, pData, plStrLen, plIndicator);

    return rc;
}