⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 object.c

📁 oci的源码,可以在任何平台上编译,相当方便实用
💻 C
📖 第 1 页 / 共 4 页
字号:
/*
   +----------------------------------------------------------------------+
   |                                                                      |
   |                     OCILIB - C Driver for Oracle                     |
   |                                                                      |
   |                      (C Wrapper for Oracle OCI)                      |
   |                                                                      |
   +----------------------------------------------------------------------+
   |                      Website : http://ocilib.net                     |
   +----------------------------------------------------------------------+
   |               Copyright (c) 2007-2009 Vincent ROGIER                 |
   +----------------------------------------------------------------------+
   | This library is free software; you can redistribute it and/or        |
   | modify it under the terms of the GNU Library General Public          |
   | License as published by the Free Software Foundation; either         |
   | version 2 of the License, or (at your option) any later version.     |
   |                                                                      |
   | This library is distributed in the hope that it will be useful,      |
   | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
   | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    |
   | Library General Public License for more details.                     |
   |                                                                      |
   | You should have received a copy of the GNU Library General Public    |
   | License along with this library; if not, write to the Free           |
   | Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.   |
   +----------------------------------------------------------------------+
   |          Author: Vincent ROGIER <vince.rogier@gmail.com>             |
   +----------------------------------------------------------------------+
*/

/* ------------------------------------------------------------------------ *
 * $Id: object.c, v 3.2.0 2009/04/20 00:00 Vince $
 * ------------------------------------------------------------------------ */

#include "ocilib_internal.h"

/* ************************************************************************ *
 *                            PRIVATE FUNCTIONS
 * ************************************************************************ */

ub2 OCI_GetIndTabIndex(OCI_TypeInfo *typinf, int index)
{
    ub2 i, j;

    j = 1;

    for (i = 0; i < index; i++)
    {
        if (typinf->cols[i].type == OCI_CDT_OBJECT)
        {
            j += OCI_GetIndTabIndex(typinf->cols[i].typinf, i);
        }
        else
        {
            j++;
        }
    }

    return j;
}

/* ------------------------------------------------------------------------ *
 * OCI_ObjectInit
 * ------------------------------------------------------------------------ */

OCI_Object * OCI_ObjectInit(OCI_Connection *con, OCI_Object **pobj,
                            void *handle, OCI_TypeInfo *typinf,
                            sb2 *tab_ind, int index, boolean reset)
{
    OCI_Object * obj = NULL;
    boolean res      = TRUE;

    OCI_CHECK(pobj == NULL, NULL);

    if (*pobj == NULL)
        *pobj = (OCI_Object *) OCI_MemAlloc(OCI_IPC_OBJECT, sizeof(*obj), 1, TRUE);

    if (*pobj != NULL)
    {
        obj = *pobj;

        obj->con     = con;
        obj->handle  = handle;
        obj->typinf  = typinf;

        if (obj->objs == NULL)
        {
            obj->objs = (void **) OCI_MemAlloc(OCI_IPC_BUFF_ARRAY,
                                               sizeof(void *),
                                               typinf->nb_cols, TRUE);
        }
        else
        {
            OCI_ObjectReset(obj);
        }

        res = (obj->objs != NULL);

        if ((res == TRUE) && (obj->handle == NULL))
        {
            /* allocates handle for non fetched object */

            obj->hstate = OCI_OBJECT_ALLOCATED;

            OCI_CALL2
            (
                res, obj->con,

                OCI_ObjectNew(OCILib.env,  con->err, con->cxt,
                              (OCITypeCode) SQLT_NTY, obj->typinf->tdo,
                              (dvoid *) NULL,
                              (OCIDuration) OCI_DURATION_SESSION,
                              (boolean) FALSE,
                              (dvoid **) &obj->handle)
           )
        }
        else
            obj->hstate = OCI_OBJECT_FETCHED_CLEAN;


        if ((res == TRUE) && (obj->type == 0))
        {
            ub4 size = sizeof(obj->type);


            /* calling OCIObjectGetProperty() on objects that are attributes of
               parent objects leads to a segfault on MS Windows !
               We need to report that to Oracle! Because sub objects always are
               values, if the parent indicator array is not null, let's assign
               the object type properties ourselves */

            if (tab_ind == NULL)
            {
                OCIObjectGetProperty(OCILib.env, con->err, obj->handle,
                                     (OCIObjectPropId) OCI_OBJECTPROP_LIFETIME,
                                     (void *) &obj->type, &size);
            }
            else
            {
                obj->type = OCI_OBJECT_VALUE;
            }
        }

        if ((res == TRUE) && ((reset == TRUE) || (obj->tab_ind == NULL)))
        {
            if (tab_ind == NULL)
            {
                OCI_CALL2
                (
                    res, obj->con,

                    OCIObjectGetInd(OCILib.env, obj->con->err,
                                    (dvoid *) obj->handle,
                                    (dvoid **) &obj->tab_ind)
                )
            }
            else
            {
                obj->tab_ind = tab_ind;
                obj->idx_ind = OCI_GetIndTabIndex(obj->typinf, index);
            }
        }
    }
    else
        res = FALSE;

    /* check for failure */

    if (res == FALSE)
    {
        OCI_ObjectFree(obj);
        obj = NULL;
    }

    return obj;
}


/* ------------------------------------------------------------------------ *
 * OCI_ObjectReset
 * ------------------------------------------------------------------------ */

void OCI_ObjectReset(OCI_Object *obj)
{
    ub2 i;

    for (i = 0; i < obj->typinf->nb_cols; i++)
    {
        if (obj->objs[i] != NULL)
        {
            OCI_Datatype * data = (OCI_Datatype *) obj->objs[i];

            if (data->hstate == OCI_OBJECT_FETCHED_CLEAN)
                data->hstate =  OCI_OBJECT_FETCHED_DIRTY;

            switch (obj->typinf->cols[i].type)
            {
                case OCI_CDT_DATETIME:

                    OCI_DateFree((OCI_Date *) obj->objs[i]);
                    break;

                case OCI_CDT_LOB:

                    OCI_LobFree((OCI_Lob *) obj->objs[i]);
                    break;

                case OCI_CDT_FILE:

                    OCI_FileFree((OCI_File *) obj->objs[i]);
                    break;

                case OCI_CDT_OBJECT:

                    OCI_ObjectFree((OCI_Object *) obj->objs[i]);
                    break;

                case OCI_CDT_COLLECTION:

                    OCI_CollFree((OCI_Coll *) obj->objs[i]);;
                    break;

                case OCI_CDT_TIMESTAMP:

                    OCI_TimestampFree((OCI_Timestamp *) obj->objs[i]);
                    break;

                case OCI_CDT_INTERVAL:

                    OCI_IntervalFree((OCI_Interval *) obj->objs[i]);
                    break;
                case OCI_CDT_REF:

                    OCI_RefFree((OCI_Ref *) obj->objs[i]);
                    break;
            }

            obj->objs[i] = NULL;
        }
    }
}

/* ------------------------------------------------------------------------ *
 * OCI_ObjectGetIndex
 * ------------------------------------------------------------------------ */

int OCI_ObjectGetIndex(OCI_Object *obj, const mtext *attr, int type)
{
    int res = -1;
    ub2 i;

    OCI_CHECK_PTR(OCI_IPC_OBJECT, obj, -1);
    OCI_CHECK_PTR(OCI_IPC_STRING, attr, -1);

    for (i = 0; i < obj->typinf->nb_cols; i++)
    {
        OCI_Column *col = &obj->typinf->cols[i];

        if (((type == -1) || (col->type == type))  &&
            (mtscasecmp(col->name, attr) == 0))
        {
           res = (int) i;
           break;
        }
    }

    if (res == -1)
        OCI_ExceptionAttributeNotFound(obj->con, attr);

    return res;
}



/* ------------------------------------------------------------------------ *
 * OCI_ObjectGetAttr
 * ------------------------------------------------------------------------ */

boolean OCI_ObjectGetAttr(OCI_Object *obj, const mtext *attr, void ** p_value,
                          OCIInd* p_ind,  OCIType **p_tdo)
{
    boolean res = TRUE;
    void *ostr = NULL;
    int osize  = -1;

    ostr = OCI_GetInputMetaString(attr, &osize);

    OCI_CALL2
    (
        res, obj->con,

        OCIObjectGetAttr(OCILib.env, obj->con->err,  (dvoid *) obj->handle,
                        (dvoid *) (obj->tab_ind + obj->idx_ind),
                        (OCIType *) obj->typinf->tdo,
                        (CONST oratext **) (dvoid *) &ostr,
                        (ub4 *) &osize, (ub4) 1, (ub4 *) NULL, (ub4) 0,
                        (OCIInd *) p_ind, (dvoid **) NULL, (dvoid **) p_value,
                        (OCIType **) p_tdo)
    )

    OCI_ReleaseMetaString(ostr);

    return res;
}

/* ------------------------------------------------------------------------ *
 * OCI_ObjectSetAttr
 * ------------------------------------------------------------------------ */

boolean OCI_ObjectSetAttr(OCI_Object *obj, const mtext *attr, void * value,
                          OCIInd ind)
{
    boolean res = TRUE;

    void *ostr = NULL;
    int osize  = -1;

    ostr = OCI_GetInputMetaString(attr, &osize);

    OCI_CALL2
    (
        res, obj->con,

        OCIObjectSetAttr(OCILib.env, obj->con->err, (dvoid *) obj->handle,
                        (dvoid *) (obj->tab_ind + obj->idx_ind),
                        (OCIType *) obj->typinf->tdo,
                        (CONST oratext**) (dvoid *) &ostr,
                        (ub4 *) &osize, (ub4) 1, (ub4 *) NULL, (ub4) 0,
                        (OCIInd) ind, (dvoid *) NULL, (dvoid *) value)
    )

    OCI_ReleaseMetaString(ostr);

    return res;
}

/* ------------------------------------------------------------------------ *
 * OCI_ObjectSetNumber
 * ------------------------------------------------------------------------ */

boolean OCI_ObjectSetNumber(OCI_Object *obj, const mtext *attr,
                            void *value, uword size, uword flag)
{
    boolean res = FALSE;
    int index   = 0;

    OCI_CHECK_PTR(OCI_IPC_OBJECT, obj, FALSE);

    index = OCI_ObjectGetIndex(obj, attr, OCI_CDT_NUMERIC);

    if (index >= 0)
    {
        OCINumber num;

        res = OCI_NumberSet(obj->con, &num, value, size, flag) &&
              OCI_ObjectSetAttr(obj, attr, (void *) &num, OCI_IND_NOTNULL);
    }

    OCI_RESULT(res);

    return res;
}

/* ------------------------------------------------------------------------ *
 * OCI_ObjectGetNumber
 * ------------------------------------------------------------------------ */

boolean OCI_ObjectGetNumber(OCI_Object *obj, const mtext *attr, void *value,
                            uword size, uword flag)
{
    boolean res = FALSE;
    int index   = 0;

    OCI_CHECK_PTR(OCI_IPC_OBJECT, obj, FALSE);

    index = OCI_ObjectGetIndex(obj, attr, OCI_CDT_NUMERIC);

    if (index >= 0)
    {
        OCIInd ind      = 0;
        OCIType *tdo    = NULL;
        OCINumber *num  = NULL;

        if (OCI_ObjectGetAttr(obj, attr, (dvoid **) (dvoid *) &num, &ind, &tdo) &&
            num && (ind == OCI_IND_NOTNULL))
        {
            res = OCI_NumberGet(obj->con, num, value, size, flag);
        }
    }
    else if (OCI_ObjectGetIndex(obj, attr, OCI_CDT_TEXT) >= 0)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -