コード例 #1
0
ファイル: xoidkey.c プロジェクト: RicardoVivas/oracle-scripts
/*---------------------------------------------------------------------
 * print_number - Print NUMBER value
 *---------------------------------------------------------------------*/
static void print_number(oci_t  *ocip, OCINumber *rawnum, boolean seqlcr)
{
  oratext     numbuf[NUM_BUF_SIZE+1];
  ub4         numsize = NUM_BUF_SIZE;
  sword       ret;

  ret = OCINumberToText(ocip->errp, (const OCINumber *)rawnum,
                        (const oratext *)"9999999.999", 10, 
                        (const oratext *)0, 0,
                        &numsize, numbuf);

  if (ret == OCI_ERROR)
  {
    if (seqlcr)
    {
      printf ("Sequence LCR: number too large for given format!");
    }
    else
    {
      ocierror(ocip, (char *)"OCINumberToText error ", FALSE);
    }
  }

  printf ("%.*s", numsize, numbuf);
}
コード例 #2
0
ファイル: NumberVar.c プロジェクト: amansubhan/pysms
//-----------------------------------------------------------------------------
// NumberVar_GetValue()
//   Returns the value stored at the given array position.
//-----------------------------------------------------------------------------
static PyObject *NumberVar_GetValue(
    udt_NumberVar *var,                 // variable to determine value for
    unsigned pos)                       // array position
{
    PyObject *result, *stringObj;
    char stringValue[200];
    long integerValue;
    ub4 stringLength;
    sword status;

    if (var->type == &vt_Boolean || var->type == &vt_Integer) {
        status = OCINumberToInt(var->environment->errorHandle, &var->data[pos],
                sizeof(long), OCI_NUMBER_SIGNED, (dvoid*) &integerValue);
        if (Environment_CheckForError(var->environment, status,
                "NumberVar_GetValue(): as integer") < 0)
            return NULL;

        if (var->type == &vt_Boolean)
            return PyBool_FromLong(integerValue);
#if PY_MAJOR_VERSION >= 3
        return PyLong_FromLong(integerValue);
#else
        return PyInt_FromLong(integerValue);
#endif
    }

    if (var->type == &vt_NumberAsString || var->type == &vt_LongInteger) {
        stringLength = sizeof(stringValue);
        status = OCINumberToText(var->environment->errorHandle,
                &var->data[pos],
                (text*) var->environment->numberToStringFormatBuffer.ptr,
                var->environment->numberToStringFormatBuffer.size, NULL, 0,
                &stringLength, (unsigned char*) stringValue);
        if (Environment_CheckForError(var->environment, status,
                "NumberVar_GetValue(): as string") < 0)
            return NULL;
        stringObj = cxString_FromEncodedString(stringValue, stringLength,
                var->environment->encoding);
        if (!stringObj)
            return NULL;
        if (var->type == &vt_NumberAsString)
            return stringObj;
#if PY_MAJOR_VERSION >= 3
        result = PyNumber_Long(stringObj);
#else
        result = PyNumber_Int(stringObj);
#endif
        Py_DECREF(stringObj);
        if (result || !PyErr_ExceptionMatches(PyExc_ValueError))
            return result;
        PyErr_Clear();
    }

    return OracleNumberToPythonFloat(var->environment, &var->data[pos]);
}
コード例 #3
0
ファイル: ocinumber.c プロジェクト: ashleysharpe/ruby-oci8
/* Converts to BigDecimal via number in scientific notation */
static VALUE onum_to_d_real(OCINumber *num, OCIError *errhp)
{
    char buf[64];
    ub4 buf_size = sizeof(buf);
    const char *fmt = "FM9.99999999999999999999999999999999999999EEEE";

    if (!cBigDecimal) {
        rb_require("bigdecimal");
        cBigDecimal = rb_const_get(rb_cObject, id_BigDecimal);
    }
    chkerr(OCINumberToText(errhp, num, (const oratext *)fmt, strlen(fmt),
                           NULL, 0, &buf_size, TO_ORATEXT(buf)));
    return rb_funcall(rb_cObject, id_BigDecimal, 1, rb_usascii_str_new(buf, buf_size));
}
コード例 #4
0
ファイル: ocinumber.c プロジェクト: ashleysharpe/ruby-oci8
/*
 *  call-seq:
 *     onum.to_char(fmt = nil, nls_params = nil)  -> string
 *
 *  Returns a string containing a representation of self.
 *  <i>fmt</i> and <i>nls_params</i> are same meanings with
 *  <code>TO_CHAR</code> of Oracle function.
 */
static VALUE onum_to_char(int argc, VALUE *argv, VALUE self)
{
    OCIError *errhp = oci8_errhp;
    VALUE fmt;
    VALUE nls_params;
    char buf[512];
    ub4 buf_size = sizeof(buf);
    oratext *fmt_ptr;
    oratext *nls_params_ptr;
    ub4 fmt_len;
    ub4 nls_params_len;
    sword rv;

    rb_scan_args(argc, argv, "02", &fmt /* nil */, &nls_params /* nil */);
    if (NIL_P(fmt)) {
        rv = oranumber_to_str(_NUMBER(self), buf, sizeof(buf));
        if (rv > 0) {
            return rb_usascii_str_new(buf, rv);
        }
        oranumber_dump(_NUMBER(self), buf);
        rb_raise(eOCIException, "Invalid internal number format: %s", buf);
    }
    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);
    }
    rv = OCINumberToText(errhp, _NUMBER(self),
                         fmt_ptr, fmt_len, nls_params_ptr, nls_params_len,
                         &buf_size, TO_ORATEXT(buf));
    if (rv == OCI_ERROR) {
        sb4 errcode;
        OCIErrorGet(errhp, 1, NULL, &errcode, NULL, 0, OCI_HTYPE_ERROR);
        if (errcode == 22065) {
            /* OCI-22065: number to text translation for the given format causes overflow */
            if (NIL_P(fmt)) /* implicit conversion */
                return rb_usascii_str_new_cstr("overflow");
        }
        chkerr(rv);
    }
    return rb_usascii_str_new(buf, buf_size);
}
コード例 #5
0
ファイル: NumberVar.c プロジェクト: 15580056814/hue
//-----------------------------------------------------------------------------
// NumberVar_GetValue()
//   Returns the value stored at the given array position.
//-----------------------------------------------------------------------------
static PyObject *NumberVar_GetValue(
    udt_NumberVar *var,                 // variable to determine value for
    unsigned pos)                       // array position
{
    PyObject *result, *stringObj;
    char stringValue[200];
    long integerValue;
    ub4 stringLength;
    sword status;

#if PY_MAJOR_VERSION < 3
    if (var->type == &vt_Integer || var->type == &vt_Boolean) {
#else
    if (var->type == &vt_Boolean) {
#endif
        status = OCINumberToInt(var->environment->errorHandle, &var->data[pos],
                sizeof(long), OCI_NUMBER_SIGNED, (dvoid*) &integerValue);
        if (Environment_CheckForError(var->environment, status,
                "NumberVar_GetValue(): as integer") < 0)
            return NULL;
#if PY_MAJOR_VERSION < 3
        if (var->type == &vt_Integer)
            return PyInt_FromLong(integerValue);
#endif
        return PyBool_FromLong(integerValue);
    }

    if (var->type == &vt_NumberAsString || var->type == &vt_LongInteger) {
        stringLength = sizeof(stringValue);
        status = OCINumberToText(var->environment->errorHandle,
                &var->data[pos],
                (text*) var->environment->numberToStringFormatBuffer.ptr,
                var->environment->numberToStringFormatBuffer.size, NULL, 0,
                &stringLength, (unsigned char*) stringValue);
        if (Environment_CheckForError(var->environment, status,
                "NumberVar_GetValue(): as string") < 0)
            return NULL;
        stringObj = cxString_FromEncodedString(stringValue, stringLength,
                var->environment->encoding);
        if (!stringObj)
            return NULL;
        if (var->type == &vt_NumberAsString)
            return stringObj;
#if PY_MAJOR_VERSION >= 3
        result = PyNumber_Long(stringObj);
#else
        result = PyNumber_Int(stringObj);
#endif
        Py_DECREF(stringObj);
        if (result || !PyErr_ExceptionMatches(PyExc_ValueError))
            return result;
        PyErr_Clear();
    }

    return OracleNumberToPythonFloat(var->environment, &var->data[pos]);
}


#ifdef SQLT_BFLOAT
//-----------------------------------------------------------------------------
// NativeFloatVar_GetValue()
//   Returns the value stored at the given array position as a float.
//-----------------------------------------------------------------------------
static PyObject *NativeFloatVar_GetValue(
    udt_NativeFloatVar *var,            // variable to determine value for
    unsigned pos)                       // array position
{
    return PyFloat_FromDouble(var->data[pos]);
}


//-----------------------------------------------------------------------------
// NativeFloatVar_SetValue()
//   Set the value of the variable which should be a native double.
//-----------------------------------------------------------------------------
static int NativeFloatVar_SetValue(
    udt_NativeFloatVar *var,            // variable to set value for
    unsigned pos,                       // array position to set
    PyObject *value)                    // value to set
{
    if (!PyFloat_Check(value)) {
        PyErr_SetString(PyExc_TypeError, "expecting float");
        return -1;
    }
    var->data[pos] = PyFloat_AS_DOUBLE(value);
    return 0;
}