Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.misc -> OCILobWrite always return -2 (OCI_INVALID_HANDLE)

OCILobWrite always return -2 (OCI_INVALID_HANDLE)

From: Jeff Chan <jchan_at_issl.com.hk>
Date: Thu, 22 Nov 2001 20:47:05 +0800
Message-ID: <9tis6j$1d002@hkunae.hku.hk>


Dear all,

    I am trying to write an OCI function that return BLOB to PL/SQL.

my PL/SQL function definition likes this...

FUNCTION PLS_PROCESS_BLOB
(

  inbb IN BLOB,
   ) RETURN BLOB AS
  EXTERNAL LIBRARY externProcedures
  WITH CONTEXT
  NAME "C_Process_Blob"
  LANGUAGE C
  PARAMETERS
  (
  CONTEXT,
  inblob OCILOBLOCATOR,
  inblob INDICATOR short,
  RETURN INDICATOR short,
  RETURN OCILOBLOCATOR); In the C function C_Process_Blob, I initialize the handle as normal. But unfortunately, when I try to run OCILobwrite, it always return Error. Here is a part of my code. What's wrong, why the last OCILobwrite function call always return -2 (OCI_INVALID_HANDLE)? If the handle svchp and errhp is invalid, why OCILobRead is OK but not OCILobWrite?.. I have totally no idea..

Could anyone can help me?

Thanks you very much.

Jeff.

////////////////////////////////////////////////////////////////////////////
///////////////////////////////////

_EXPORT OCILobLocator * C_Process_Blob(OCIExtProcContext *with_context, //Context

          OCILobLocator *inbb,
          short inbb_indicator,
          short *ret_indicator)    //return length
{
 ub1 bufp[MAXBUFLEN];
 ub4 offset = 1;
 ub4 amtp = 0;
 ub4 nbytes = 0;
 ub4 filelen = 0;
 ub4 total = 0;
 OCIEnv *envhp;
 OCIError *errhp;
 OCISvcCtx *svchp;
 int status;

 ub4 loblen;
 ub4 remainder;
 OCILobLocator *outbb;

 unsigned char *inbbcontent;
 unsigned long inbbcontentLen;

 // Obtain OCI handle for SQL statement using the context passed.

 status = OCIExtProcGetEnv(with_context,                       // With
context
       &envhp,
       &svchp,
       &errhp);

 checkerr(errhp, status);

(void) OCILobGetLength(svchp, errhp, inbb, &loblen);
 amtp = loblen;
 memset ( bufp, '\0', MAXBUFLEN);

 inbbcontent= OCIExtProcAllocCallMemory(with_context,loblen);

 // set amount to be read per iteration
 amtp = nbytes = MAXBUFLEN;

 while (amtp)
 {
  if (OCILobRead(svchp, errhp, inbb, &amtp, (ub4) offset, (dvoid *) bufp,

       (ub4) nbytes, (dvoid *)0,
       (sb4 (*)(dvoid *, CONST dvoid *, ub4, ub1)) 0,
       (ub2) 0, (ub1) SQLCS_IMPLICIT))

  {
   OCIExtProcRaiseExcpWithMsg(with_context,(int)20111,"Error",0);   }

  if (amtp > 0)
  {
   total += amtp;
   memcpy(inbbcontent+offset, bufp, amtp);    offset += nbytes;
  }
  else

     break;
 }

inbbcontentLen = offset;

 if (OCIDescriptorAlloc((dvoid *) envhp, (dvoid **) &outbb,

       (ub4)OCI_DTYPE_LOB, (size_t) 0, (dvoid **) 0))  {
  OCIExtProcRaiseExcpWithMsg(with_context,(int)20111,"Error",0);  }

 // check NULL
 if(inbb_indicator == OCI_IND_NULL)
   {
  offset = 1;
  if (OCILobWrite(svchp, errhp, outbb, &amtp, offset, (dvoid *) "\0",

        (ub4) 1, OCI_ONE_PIECE, (dvoid *)0,
        (sb4 (*)(dvoid *, dvoid *, ub4 *, ub1 *)) 0,
        (ub2) 0, (ub1) SQLCS_IMPLICIT) != OCI_SUCCESS)
  {
   OCIExtProcRaiseExcpWithMsg(with_context,(int)20111,"Error",0);   }
 }

 offset = 1;
 remainder= loblen;

 while (remainder > 0)
 {
  amtp = nbytes = (remainder > MAXBUFLEN) ? MAXBUFLEN : remainder;

  memcpy(bufp, inbbcontent+offset-1, nbytes);

  if ((status = OCILobWrite(svchp, errhp, outbb, &amtp, offset, (dvoid *) bufp,

        (ub4) nbytes, (remainder > MAXBUFLEN) ? OCI_ONE_PIECE : OCI_FIRST_PIECE,

        (dvoid *)0,
        (sb4 (*)(dvoid *, dvoid *, ub4 *, ub1 *)) 0,
        (ub2) 0, (ub1) SQLCS_IMPLICIT)) != OCI_SUCCESS)
///<-----Always return OCI_INVALID_HANDLE   {
   OCIExtProcRaiseExcpWithMsg(with_context,(int)20111,"Error.",0);   }

  remainder -= nbytes;
  offset += nbytes;
 }

 *ret_indicator=(short)OCI_IND_NOTNULL;

 return outbb;

} Received on Thu Nov 22 2001 - 06:47:05 CST

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US