Example #1
0
//-----------------------------------------------------------------------------
// NumberVar_SetValueFromLong()
//   Set the value of the variable from a Python long.
//-----------------------------------------------------------------------------
static int NumberVar_SetValueFromLong(
    udt_NumberVar *var,                 // variable to set value for
    unsigned pos,                       // array position to set
    PyObject *value)                    // value to set
{
    udt_Buffer textBuffer;
    PyObject *textValue;
    sword status;

    textValue = PyObject_Str(value);
    if (!textValue)
        return -1;
    if (cxBuffer_FromObject(&textBuffer, textValue,
            var->environment->encoding) < 0)
        return -1;
    status = OCINumberFromText(var->environment->errorHandle,
            (text*) textBuffer.ptr, textBuffer.size,
            (text*) var->environment->numberFromStringFormatBuffer.ptr,
            var->environment->numberFromStringFormatBuffer.size, NULL, 0,
            &var->data[pos]);
    cxBuffer_Clear(&textBuffer);
    Py_DECREF(textValue);
    return Environment_CheckForError(var->environment, status,
            "NumberVar_SetValueFromLong()");
}
Example #2
0
/* fill C structure (OCINumber) from a string. */
static void set_oci_number_from_str(OCINumber *result, VALUE str, VALUE fmt, VALUE nls_params, OCIError *errhp)
{
    oratext *fmt_ptr;
    oratext *nls_params_ptr;
    ub4 fmt_len;
    ub4 nls_params_len;

    StringValue(str);
    /* set from string. */
    if (NIL_P(fmt)) {
        int rv = oranumber_from_str(result, RSTRING_PTR(str), RSTRING_LEN(str));
        if (rv == ORANUMBER_SUCCESS) {
            return; /* success */
        } else {
            const char *default_msg = NULL;
            switch (rv) {
            case ORANUMBER_INVALID_NUMBER:
                default_msg = "invalid number";
                break;
            case ORANUMBER_NUMERIC_OVERFLOW:
                default_msg = "numeric overflow";
                break;
            }
            oci8_raise_by_msgno(rv, default_msg);
        }
    }
    StringValue(fmt);
    fmt_ptr = RSTRING_ORATEXT(fmt);
    fmt_len = RSTRING_LEN(fmt);
    if (NIL_P(nls_params)) {
        nls_params_ptr = NULL;
        nls_params_len = 0;
    } else {
        StringValue(nls_params);
        nls_params_ptr = RSTRING_ORATEXT(nls_params);
        nls_params_len = RSTRING_LEN(nls_params);
    }
    chkerr(OCINumberFromText(errhp,
                             RSTRING_ORATEXT(str), RSTRING_LEN(str),
                             fmt_ptr, fmt_len, nls_params_ptr, nls_params_len,
                             result));
}
Example #3
0
//-----------------------------------------------------------------------------
// NumberVar_SetValueFromDecimal()
//   Set the value of the variable from a Python decimal.Decimal object.
//-----------------------------------------------------------------------------
static int NumberVar_SetValueFromDecimal(
    udt_NumberVar *var,                 // variable to set value for
    unsigned pos,                       // array position to set
    PyObject *value)                    // value to set
{
    PyObject *textValue, *format, *tupleValue;
    udt_Buffer textBuffer, formatBuffer;
    sword status;

    tupleValue = PyObject_CallMethod(value, "as_tuple", NULL);
    if (!tupleValue)
        return -1;
    if (NumberVar_GetFormatAndTextFromDecimal(tupleValue, &textValue,
            &format) < 0) {
        Py_DECREF(tupleValue);
        return -1;
    }
    Py_DECREF(tupleValue);
    if (cxBuffer_FromObject(&textBuffer, textValue,
            var->environment->encoding) < 0)
        return -1;
    if (cxBuffer_FromObject(&formatBuffer, format,
            var->environment->encoding) < 0) {
        cxBuffer_Clear(&textBuffer);
        return -1;
    }
    status = OCINumberFromText(var->environment->errorHandle,
            (text*) textBuffer.ptr, textBuffer.size, (text*) formatBuffer.ptr,
            formatBuffer.size,
            var->environment->nlsNumericCharactersBuffer.ptr,
            var->environment->nlsNumericCharactersBuffer.size,
            &var->data[pos]);
    cxBuffer_Clear(&textBuffer);
    cxBuffer_Clear(&formatBuffer);
    Py_DECREF(textValue);
    Py_DECREF(format);
    return Environment_CheckForError(var->environment, status,
            "NumberVar_SetValueFromDecimal()");
}