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

📄 dirpath.c

📁 oci的源码,可以在任何平台上编译,相当方便实用
💻 C
📖 第 1 页 / 共 3 页
字号:
/*
   +----------------------------------------------------------------------+
   |                                                                      |
   |                     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: dirpath.c, v 3.2.0 2009/04/20 00:00 Vince $
 * ------------------------------------------------------------------------ */

#include "ocilib_internal.h"

/* ************************************************************************ *
 *                            PUBLIC FUNCTIONS
 * ************************************************************************ */

/* ------------------------------------------------------------------------ *
 * OCI_DirPathCreate
 * ------------------------------------------------------------------------ */

OCI_DirPath * OCI_API OCI_DirPathCreate(OCI_TypeInfo *typinf,  mtext *partition,
                                        unsigned int nb_cols, unsigned int nb_rows)
{
    OCI_DirPath *dp = NULL;

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

    boolean res = TRUE;

    OCI_CHECK_PTR(OCI_IPC_TYPE_INFO, typinf, NULL);

    OCI_CHECK_COMPAT(typinf->con, typinf->type != OCI_TIF_TYPE, NULL);
    OCI_CHECK_BOUND(typinf->con, nb_cols, 1, typinf->nb_cols, NULL);

    /* allocate direct path structure */

    dp = (OCI_DirPath *) OCI_MemAlloc(OCI_IPC_DIRPATH, sizeof(*dp), 1, TRUE);

    if (dp != NULL)
    {
        dp->con       = typinf->con;
        dp->status    = OCI_DPS_NOT_PREPARED;
        dp->typinf    = typinf;
        dp->nb_rows   = (ub4) nb_rows;
        dp->nb_cols   = (ub4) nb_cols;
        dp->nb_cur    = (ub4) dp->nb_rows;
        dp->err_col   = -1;
        dp->err_row   = -1;
        dp->nb_prcsd  =  0;

        /* allocates direct context handle */

        if (res == TRUE)
        {
            res = (OCI_SUCCESS == OCI_HandleAlloc((dvoid *) OCILib.env,
                                                  (dvoid **) (void *) &dp->ctx,
                                                  (ub4) OCI_HTYPE_DIRPATH_CTX,
                                                  (size_t) 0, (dvoid **) NULL));
        }

        /* set table name attribute */

        if (res == TRUE)
        {
            osize = -1;
            ostr  = OCI_GetInputMetaString(dp->typinf->name, &osize);

            OCI_CALL2
            (
                res, dp->con,

                OCIAttrSet((dvoid *) dp->ctx, (ub4) OCI_HTYPE_DIRPATH_CTX,
                           (dvoid *) ostr, (ub4) osize,
                           (ub4) OCI_ATTR_NAME, dp->con->err)
            )

            OCI_ReleaseMetaString(ostr);
        }

        /* set schema name attribute */

        if ((res == TRUE) && (dp->typinf->schema != NULL) && (dp->typinf->schema[0] != 0))
        {
            osize = -1;
            ostr  = OCI_GetInputMetaString(dp->typinf->schema, &osize);

            OCI_CALL2
            (
                res, dp->con,

                OCIAttrSet((dvoid *) dp->ctx, (ub4) OCI_HTYPE_DIRPATH_CTX,
                           (dvoid *) ostr, (ub4) osize,
                           (ub4) OCI_ATTR_SCHEMA_NAME, dp->con->err)
            )

            OCI_ReleaseMetaString(ostr);
        }

        /* set partition name attribute */

        if ((res == TRUE) && (partition != NULL) && (partition[0] != 0))
        {
            osize = -1;
            ostr  = OCI_GetInputMetaString(partition, &osize);

            OCI_CALL2
            (
                res, dp->con,

                OCIAttrSet((dvoid *) dp->ctx, (ub4) OCI_HTYPE_DIRPATH_CTX,
                           (dvoid *) ostr, (ub4) osize,
                           (ub4) OCI_ATTR_SUB_NAME, dp->con->err)
            )

            OCI_ReleaseMetaString(ostr);
        }

        if (OCILib.ver_runtime >= OCI_9)
        {
            /* set array size attribute */

            OCI_CALL2
            (
                res, dp->con,

                OCIAttrSet((dvoid *) dp->ctx, (ub4) OCI_HTYPE_DIRPATH_CTX,
                           (dvoid *) &dp->nb_rows, (ub4) sizeof(ub4),
                           (ub4) OCI_ATTR_NUM_ROWS, dp->con->err)
            )
        }

        /* set columns count attribute */

        OCI_CALL2
        (
            res, dp->con,

            OCIAttrSet((dvoid *) dp->ctx, (ub4) OCI_HTYPE_DIRPATH_CTX,
                       (dvoid *) &dp->nb_cols, (ub4) sizeof(ub4),
                       (ub4) OCI_ATTR_NUM_COLS, dp->con->err)
        )

        /* allocating the column array */

        if (res == TRUE)
        {
            dp->cols = (void *) OCI_MemAlloc(OCI_IPC_DP_COL_ARRAY,
                                             sizeof(OCI_DirPathColumn),
                                             dp->nb_cols, TRUE);

            res = (dp->cols != NULL);
        }
    }
    else
        res = FALSE;

   /* handle errors */

    if (res == FALSE)
    {
        OCI_DirPathFree(dp);
        dp = NULL;
    }

    OCI_RESULT(res);

    return dp;
}

/* ------------------------------------------------------------------------ *
 * OCI_DirPathFree
 * ------------------------------------------------------------------------ */

boolean OCI_API OCI_DirPathFree(OCI_DirPath *dp)
{
    ub4 i;

    OCI_CHECK_PTR(OCI_IPC_DIRPATH, dp, FALSE);

    for (i = 0; i < dp->nb_cols; i++)
    {
        OCI_FREE(dp->cols[i].data);
        OCI_FREE(dp->cols[i].lens);
        OCI_FREE(dp->cols[i].flags);
        OCI_FREE(dp->cols[i].format);
   }

    OCI_FREE(dp->cols);

    OCI_HandleFree(dp->strm, OCI_HTYPE_DIRPATH_STREAM);
    OCI_HandleFree(dp->arr,  OCI_HTYPE_DIRPATH_COLUMN_ARRAY);
    OCI_HandleFree(dp->ctx,  OCI_HTYPE_DIRPATH_CTX);

    OCI_FREE(dp);

    OCI_RESULT(TRUE);

    return TRUE;
}

/* ------------------------------------------------------------------------ *
 * OCI_DirPathSetColumn
 * ------------------------------------------------------------------------ */

boolean OCI_API OCI_DirPathSetColumn(OCI_DirPath *dp, unsigned int index,
                                     mtext *name, unsigned int maxsize,
                                     mtext *format)
{
    OCI_DirPathColumn *dpcol = NULL;
    OCI_Column *col = NULL;
    OCIParam *hattr = NULL;
    OCIParam *hlist = NULL;
    void *ostr   = NULL;
    int osize    = -1;
    boolean res  = TRUE;
    ub2 i;

    OCI_CHECK_PTR(OCI_IPC_DIRPATH, dp, FALSE);
    OCI_CHECK_DIRPATH_STATUS(dp, OCI_DPS_NOT_PREPARED, FALSE);
    OCI_CHECK_PTR(OCI_IPC_STRING, name, FALSE);
    OCI_CHECK_BOUND(dp->con, index, 1, dp->nb_cols, FALSE);

    /* check if column exists */

    for (i = 0; i < dp->typinf->nb_cols; i++)
    {
        if (mtscasecmp(name, dp->typinf->cols[i].name) == 0)
        {
            break;
        }
    }

    /* check if column was found */

    if (i >= dp->typinf->nb_cols)
    {
        OCI_ExceptionDirPathColNotFound(dp, name, dp->typinf->name);

        res = FALSE;
    }

    /* set column information */

    if (res == TRUE)
    {
        col   = &dp->typinf->cols[i];
        dpcol = &dp->cols[index-1];

       /* default column attributes */

        dpcol->maxsize      = (ub2) maxsize;
        dpcol->bufsize      = (ub2) maxsize + 1;
        dpcol->sqlcode      = SQLT_CHR;
        dpcol->type         = OCI_DDT_TEXT;
        dpcol->index        = i;
        dpcol->format_size  = 0;

        switch (col->type)
        {
            case OCI_CDT_TEXT :

                dpcol->maxsize *= sizeof(dtext);
                dpcol->bufsize *= sizeof(dtext);

                break;

            case OCI_CDT_NUMERIC :

                if ((format != NULL) && (format[0] != 0))
                {
                    dpcol->format      = mtsdup(format);
                    dpcol->format_size = (ub4) mtslen(format);
                    dpcol->type        = OCI_DDT_NUMBER;
                    dpcol->sqlcode     = SQLT_NUM;
                    dpcol->bufsize     = sizeof(OCINumber);
                    dpcol->maxsize     = sizeof(OCINumber);
               }
                else
                {
                    dpcol->type    = OCI_DDT_OTHERS;
                }

                break;

            case OCI_CDT_DATETIME  :
            case OCI_CDT_TIMESTAMP :
            case OCI_CDT_INTERVAL  :

                dpcol->type    = OCI_DDT_OTHERS;

                if ((format != NULL) && (format[0] != 0))
                {
                    dpcol->format       = mtsdup(format);
                    dpcol->format_size  = (ub4) mtslen(format);
                    dpcol->maxsize      = (ub2) dpcol->format_size;
                    dpcol->bufsize     *= sizeof(dtext);
                }

                break;

            case OCI_CDT_LOB :

                if (col->subtype == OCI_BLOB)
                {
                    dpcol->type    = OCI_DDT_BINARY;
                    dpcol->sqlcode = SQLT_BIN;
                }

                break;

            case OCI_CDT_LONG :

                if (col->subtype == OCI_BLONG)
                {
                    dpcol->type    = OCI_DDT_BINARY;
                    dpcol->sqlcode = SQLT_BIN;
                }

                break;

            case OCI_CDT_RAW :

                dpcol->type    = OCI_DDT_BINARY;
                dpcol->sqlcode = SQLT_BIN;

                break;

            default :

                res = FALSE;
                OCI_ExceptionDatatypeNotSupported(dp->con, NULL, col->ocode);

                break;
        }
    }

    /* if supported datatype, set direct path column attributes */

    if (res == TRUE)
    {
        /* get column parameter list handle */

        OCI_CALL2
        (
            res, dp->con,

            OCIAttrGet(dp->ctx, OCI_HTYPE_DIRPATH_CTX, &hlist,
                       NULL, OCI_ATTR_LIST_COLUMNS, dp->con->err)
        )

       /* get colum attribute handle */

        OCI_CALL2
        (
            res, dp->con,

            OCIParamGet((dvoid *) hlist, OCI_DTYPE_PARAM, dp->con->err,
                        (dvoid** ) (dvoid *) &hattr, (ub4) index)
        )

        /* set column name */

        if (res == TRUE)
        {
            osize = -1;
            ostr  = OCI_GetInputMetaString(name, &osize);

            OCI_CALL2
            (
                res, dp->con,

                OCIAttrSet((dvoid *) hattr, (ub4) OCI_DTYPE_PARAM,
                           (dvoid *) ostr, (ub4) osize,
                           (ub4) OCI_ATTR_NAME, dp->con->err)
            )

            OCI_ReleaseMetaString(ostr);
        }

        /* set column type */

        OCI_CALL2
        (
            res, dp->con,

            OCIAttrSet((dvoid *) hattr, (ub4) OCI_DTYPE_PARAM,
                       (dvoid *) &dpcol->sqlcode, sizeof(dpcol->sqlcode),
                       (ub4) OCI_ATTR_DATA_TYPE, dp->con->err)
        )

        /* set column size */

        OCI_CALL2
        (
            res, dp->con,

            OCIAttrSet((dvoid *) hattr, (ub4) OCI_DTYPE_PARAM,
                       (dvoid *) &dpcol->maxsize, sizeof(dpcol->maxsize),
                       (ub4) OCI_ATTR_DATA_SIZE, dp->con->err)
        )

        /* set column precision */

        if (col->prec != 0)
        {
            OCI_CALL2
            (
                res, dp->con,

                OCIAttrSet((dvoid *) hattr, (ub4) OCI_DTYPE_PARAM,
                           (dvoid *) &col->prec, sizeof(col->prec),
                           (ub4) OCI_ATTR_PRECISION, dp->con->err)
            )
        }

        /* set column scale */

⌨️ 快捷键说明

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