Esempio n. 1
0
OCI_EXPORT boolean OCI_API  OCI_DequeueSubscribe
(
    OCI_Dequeue    *dequeue, 
    unsigned int    port, 
    unsigned int    timeout,
    POCI_NOTIFY_AQ  callback
)
{
    boolean res             = TRUE;
    ub4     oci_namespace   = OCI_SUBSCR_NAMESPACE_AQ;

#if OCI_VERSION_COMPILE >= OCI_10_2

    ub4     oci_port        = (ub4) port;
    ub4     oci_timeout     = (ub4) timeout;
    ub4     oci_protocol    = OCI_SUBSCR_PROTO_OCI;
    ub4     oci_msgpres     = OCI_SUBSCR_PRES_DEFAULT;

#endif

    OCI_Connection *con = NULL;
     
    OCI_CHECK_INITIALIZED(FALSE);
    OCI_CHECK_DATABASE_NOTIFY_ENABLED(FALSE);

    OCI_CHECK_PTR(OCI_IPC_DEQUEUE, dequeue, FALSE);

    con = dequeue->typinf->con;

    /* clear any previous subscription */

    OCI_DequeueUnsubscribe(dequeue);

    /* allocate subcription handle */

    res = (OCI_SUCCESS == OCI_HandleAlloc(con->env,
                                          (dvoid **) (void *) &dequeue->subhp,
                                          OCI_HTYPE_SUBSCRIPTION, (size_t) 0,
                                          (dvoid **) NULL));

#if OCI_VERSION_COMPILE >= OCI_10_2

    /* set port number */

    if (oci_port > 0)
    {
        OCI_CALL3
        (
            res, con->err,

            OCIAttrSet((dvoid *) dequeue->subhp, (ub4) OCI_HTYPE_SUBSCRIPTION,
                        (dvoid *) &oci_port, (ub4) sizeof (oci_port),
                        (ub4) OCI_ATTR_SUBSCR_PORTNO, con->err)
        )
    }

    /* set timeout */

    if (oci_timeout > 0)
    {
        OCI_CALL3
        (
            res, con->err,

            OCIAttrSet((dvoid *) dequeue->subhp, (ub4) OCI_HTYPE_SUBSCRIPTION,
                        (dvoid *) &oci_timeout, (ub4) sizeof (oci_timeout),
                        (ub4) OCI_ATTR_SUBSCR_TIMEOUT, con->err)
        )
    }

    /* set protocol  */

    OCI_CALL3
    (
        res, con->err,

        OCIAttrSet((dvoid *) dequeue->subhp, (ub4) OCI_HTYPE_SUBSCRIPTION,
                    (dvoid *) &oci_protocol, (ub4) sizeof(oci_protocol),
                    (ub4) OCI_ATTR_SUBSCR_RECPTPROTO, con->err)
    )

    /* set presentation  */

    OCI_CALL3
    (
        res, con->err,

        OCIAttrSet((dvoid *) dequeue->subhp, (ub4) OCI_HTYPE_SUBSCRIPTION,
                    (dvoid *) &oci_msgpres, (ub4) sizeof(oci_msgpres),
                    (ub4) OCI_ATTR_SUBSCR_RECPTPRES, con->err)
    )

#else

    OCI_NOT_USED(port);
    OCI_NOT_USED(timeout);

#endif
    
    /* set name  */

    if (dequeue->name != NULL)
    {
        /* for AQ subscription, the name should be "[shema.]queue[:consumer]" */

        mtext buffer[(OCI_SIZE_OBJ_NAME*2) + 2] = MT("");

        mtext *str  = NULL;
        size_t size = sizeof(buffer)/sizeof(mtext);

        void *ostr  = NULL;
        int osize   = -1;
            
        mtsncat(buffer, dequeue->name, size);

        if (dequeue->consumer != NULL)
        {
            size -= mtslen(dequeue->name);
            mtsncat(buffer, MT(":"), size);
            size -= (size_t) 1;

            mtsncat(buffer, dequeue->consumer, size);
        }           

        /* queue name must be uppercase */

        for (str =  buffer; *str != 0; str++)
        {
            *str = (mtext) mttoupper(*str);
        }

        ostr = OCI_GetInputMetaString(buffer, &osize);

        OCI_CALL3
        (
            res, con->err,

            OCIAttrSet((dvoid *) dequeue->subhp, (ub4) OCI_HTYPE_SUBSCRIPTION,
                        (dvoid *) ostr, (ub4) osize,
                        (ub4) OCI_ATTR_SUBSCR_NAME, con->err)
        )

        OCI_ReleaseMetaString(ostr);
    }

    /* set namespace  */

    OCI_CALL3
    (
        res, con->err,

        OCIAttrSet((dvoid *) dequeue->subhp, (ub4) OCI_HTYPE_SUBSCRIPTION,
                    (dvoid *) &oci_namespace, (ub4) sizeof(oci_namespace),
                    (ub4) OCI_ATTR_SUBSCR_NAMESPACE, con->err)
    )

    /* set context pointer to dequeue structure */

    OCI_CALL3
    (
        res, con->err,

        OCIAttrSet((dvoid *) dequeue->subhp, (ub4) OCI_HTYPE_SUBSCRIPTION,
                    (dvoid *) dequeue, (ub4) 0,
                    (ub4) OCI_ATTR_SUBSCR_CTX, con->err)
    )
    
    /* internal callback handler */

    OCI_CALL3
    (
        res, con->err,

        OCIAttrSet((dvoid *) dequeue->subhp, (ub4) OCI_HTYPE_SUBSCRIPTION,
                    (dvoid *) OCI_ProcNotifyMessages, (ub4) 0,
                    (ub4) OCI_ATTR_SUBSCR_CALLBACK, con->err)
    )

    /* all attributes set, let's register the subscription ! */

    OCI_CALL3
    (
        res, con->err,

        OCISubscriptionRegister(con->cxt, &dequeue->subhp, (ub2) 1, con->err,(ub4) OCI_DEFAULT)
    )

   /* set callback on success */

    if (res)
    {
        dequeue->callback = callback;
    }
    else
    {
        /* clear subscription on failure */

        OCI_DequeueUnsubscribe(dequeue);
    }

    OCI_RESULT(res);

    return res;
}
Esempio n. 2
0
OCI_TypeInfo * OCI_API OCI_TypeInfoGet2(OCI_Library *pOCILib, OCI_Connection *con, const mtext *name, 
					unsigned int type, ExceptionSink* xsink)
{
    OCI_TypeInfo *typinf = NULL;
    OCI_Item     *item   = NULL;
    OCIDescribe *dschp   = NULL;
    OCIParam *parmh1     = NULL;
    OCIParam *parmh2     = NULL;
    mtext *str           = NULL;
    //int etype            = OCI_DESC_COLUMN;
    int ptype            = 0;
    ub1 item_type        = 0;
    ub4 attr_type        = 0;
    ub4 num_type         = 0;
    boolean res          = TRUE;
    boolean found        = FALSE;
    ub2 i;
    
    mtext obj_schema[OCI_SIZE_OBJ_NAME+1];
    mtext obj_name[OCI_SIZE_OBJ_NAME+1];

    OCI_CHECK_INITIALIZED2(pOCILib, NULL);

    OCI_CHECK_PTRQ(pOCILib, OCI_IPC_CONNECTION, con, NULL, xsink);
    OCI_CHECK_PTRQ(pOCILib, OCI_IPC_STRING, name, NULL, xsink);

    if (type == OCI_TIF_TABLE)
        item_type = OCI_PTYPE_TABLE;
    else if (type == OCI_TIF_VIEW)
        item_type = OCI_PTYPE_VIEW;
    else if (type == OCI_TIF_TYPE)
        item_type = OCI_PTYPE_TYPE;
    else
        return NULL;

    obj_schema[0] = 0;
    obj_name[0] = 0;

    /* is the schema provided in the object name ? */

    for (str = (mtext *) name; *str != 0; str++)
    {
        if (*str == MT('.'))
        {
            mtsncat(obj_schema, name, str-name);
            mtsncat(obj_name, ++str, (size_t) OCI_SIZE_OBJ_NAME);
            break;
        }
    }

    /* if the schema is not provided, we just copy the object name */

    if (obj_name[0] == 0)
    {
        mtsncat(obj_name, name, (size_t) OCI_SIZE_OBJ_NAME);
    }

    /* type name must be uppercase */

    for (str = obj_name; *str != 0; str++)
        *str = (mtext) mttoupper(*str);

    /* schema name must be uppercase */

    for (str = obj_schema; *str != 0; str++)
        *str = (mtext) mttoupper(*str);

    /* first try to find it in list */

    item = con->tinfs->head;

    /* walk along the list to find the type */

    while (item != NULL)
    {
        typinf = (OCI_TypeInfo *) item->data;
     
        if ((typinf != NULL) && (typinf->type == type))
        {
            if ((mtscasecmp(typinf->name,   obj_name  ) == 0) &&
                (mtscasecmp(typinf->schema, obj_schema) == 0))
            {
                found = TRUE;
                break;
            }
        }

        item = item->next;
    } 

    /* Not found, so create type object */

    if (found == FALSE)
    {
        item = OCI_ListAppend(pOCILib, con->tinfs, sizeof(OCI_TypeInfo));

        res = (item != NULL);

        if (res == TRUE)
        {
            typinf = (OCI_TypeInfo *) item->data;

            typinf->type        = type;
            typinf->con         = con;
            typinf->name        = mtsdup(pOCILib, obj_name);
            typinf->schema      = mtsdup(pOCILib, obj_schema);
            typinf->struct_size = 0;

            res = (OCI_SUCCESS == OCI_HandleAlloc2(pOCILib, pOCILib->env,
                                                  (dvoid **) (void *) &dschp, 
                                                  OCI_HTYPE_DESCRIBE, (size_t) 0, 
                                                  (dvoid **) NULL));
        }

        if (res == TRUE)
        {
            if (type == OCI_TIF_TYPE)
            {            
                void *ostr1 = NULL;
                void *ostr2 = NULL;
                int osize1  = -1;
                int osize2  = -1;
               
                attr_type = OCI_ATTR_LIST_TYPE_ATTRS;
                num_type  = OCI_ATTR_NUM_TYPE_ATTRS;
                ptype     = OCI_DESC_TYPE;

                ostr1 = OCI_GetInputMetaString(pOCILib, typinf->schema, &osize1);
                ostr2 = OCI_GetInputMetaString(pOCILib, typinf->name,   &osize2);

                OCI_CALL2Q
                (
		   pOCILib, res, con, 
                   
		   OCITypeByName(pOCILib->env, con->err, con->cxt,
                                 (text *) ostr1, (ub4) osize1, 
                                 (text *) ostr2, (ub4) osize2, 
                                 (text *) NULL, (ub4) 0, 
                                 OCI_DURATION_SESSION, OCI_TYPEGET_ALL,
                                 &typinf->tdo),

		   xsink
                )

                OCI_CALL2Q
                (
                    pOCILib, res, con, 
                    
                    OCIDescribeAny(con->cxt, con->err, (void *) typinf->tdo,
                                   0, OCI_OTYPE_PTR, OCI_DEFAULT, 
                                   OCI_PTYPE_TYPE, dschp),

		    xsink
                )
                
                OCI_ReleaseMetaString(ostr1);
                OCI_ReleaseMetaString(ostr2);
            }
            else
            {
                mtext buffer[(OCI_SIZE_OBJ_NAME*2) + 2];
                
                size_t size = sizeof(buffer)/sizeof(mtext);
                void *ostr1 = NULL;
                int osize1  = -1;

                attr_type = OCI_ATTR_LIST_COLUMNS;
                num_type  = OCI_ATTR_NUM_COLS;
                ptype     = OCI_DESC_TABLE;
                str       = buffer;

                str[0] = 0;                

                if ((typinf->schema != NULL) && (typinf->schema[0] != 0))
                {
                    str = mtsncat(buffer, typinf->schema, size);
                    size -= mtslen(typinf->schema);
                    str = mtsncat(str, MT("."), size);
                    size -= (size_t) 1;
                }

                mtsncat(str, typinf->name, size);

                ostr1 = OCI_GetInputMetaString(pOCILib, str, &osize1);

                OCI_CALL2Q
                (
                    pOCILib, res, con, 
                    
                    OCIDescribeAny(con->cxt, con->err, (dvoid *) ostr1, 
                                   (ub4) osize1, OCI_OTYPE_NAME, 
                                   OCI_DEFAULT, item_type, dschp),

		    xsink
                )

                OCI_ReleaseMetaString(ostr1);
            }
                  
            OCI_CALL2Q
            (
                pOCILib, res, con, 
                
                OCIAttrGet(dschp, OCI_HTYPE_DESCRIBE, &parmh1, 
                           NULL, OCI_ATTR_PARAM, con->err),

		xsink
            )

            /* do we need get more attributes for collections ? */

            if (type == OCI_TIF_TYPE)
            {
                OCI_CALL2Q
                (
                    pOCILib, res, con, 
                    
                    OCIAttrGet(parmh1, OCI_DTYPE_PARAM, &typinf->tcode, 
                               NULL, OCI_ATTR_TYPECODE, con->err),

		    xsink
                )

            }

            if (typinf->tcode == SQLT_NCO)
            {      
                typinf->nb_cols = 1;
                
                ptype  = OCI_DESC_COLLECTION;
                //etype  = OCI_DESC_TYPE;
                parmh2 = parmh1;

                OCI_CALL2Q
                (
                    pOCILib, res, con, 
                    
                    OCIAttrGet(parmh1, OCI_DTYPE_PARAM, &typinf->ccode, 
                               NULL, OCI_ATTR_COLLECTION_TYPECODE, con->err),

		    xsink
                )
            }
            else
            {