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 -> Re: Extracting the size of an Oracle LONG data type

Re: Extracting the size of an Oracle LONG data type

From: Thomas Kyte <tkyte_at_us.oracle.com>
Date: 1998/12/01
Message-ID: <3665f797.86847249@192.86.155.100>#1/1

A copy of this was sent to "Kelvin Baggs" <kbaggs_at_qtrnnmsd.telstra.com.au> (if that email address didn't require changing) On 1 Dec 1998 06:14:23 GMT, you wrote:

>Hi all
>
>I am wanting to query an Oracle database to find the size of an Oracle
>LONG, so that I can allocate the required amount of memory in a C program.
>
>Is it possible to find the size of a LONG in my table?
>

No, but you can PIECEWISE fetch it and dynamically allocate/reallocate the space for it. piecewise fetching works only from OCI but since you can mix OCI and PRO*C in the same application, you can use the same technique in a pro*c app.

The following is a subroutine I've used in pro*c apps to get a long (or long raw) out of the database.....

The way I do it is to fetch the ROWID of the LONG/LONG RAW in pro*c instead of the actual LONG or LONG RAW. Then, when I am ready to get the LONG/LONG RAW, I call a routine "get_long". get_long takes 3 parameter:

  1. a sql query in the form: "select <LONG/LONG RAW COLUMN> from T where rowid = :RID"
  2. a character string pointer to a rowid value that we just fetched
  3. a boolean "isRaw" that is set to 1 if fetching a long raw, set to 0 if fetching a long.

Get_long will execute the query and piecewise fetch the long/long raw 32k at a time, realloc-ing a buffer as needed to hold the whole thing. It then returns the buffer and you 'own' it (you have to call free() to deallocate it). It returns NULL on error.

Hope this helps...

>Thanks in advance.
>Kelvin Baggs

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#ifndef ORATYPES_ORACLE

# include <oratypes.h>
#endif

/* LDA and CDA struct declarations */

#include <ocidfn.h>
#ifdef __STDC__
#include <ociapr.h>
#else
#include <ocikpr.h>
#endif
#include "ocidem.h"


#include "getlong.h"

/* contents of getlong.h -------------------------------------

typedef struct TAGmy_long
{

	long			len;
	unsigned char	arr[1];
}
	my_long;



my_long * get_long( char * sql_statement, char * rowid, int isRaw );

char * get_long_emsg(void);

int get_long_errcd(void);

----------------------------------------- getlong.h    */


static int firstTime = 1;
static Lda_Def lda;
static Cda_Def cda;

my_long * get_long( char * sql_statement, char * rowid, int isRaw ) {

ub1       ucp[32765];
int       UCP_SIZE = sizeof(ucp);

sb2       indp = 0;
ub4       ret_len = 0;
ub2       retl = 0;
ub2       rcode = 0;
int       fetched = 0;
int       rc = 0;

my_long  * returnBuffer = NULL;
int		 returnBufferLen = 0;

   if ( firstTime )
   {

      sqllda( &lda );

      rc = oopen( &cda, &lda, NULL, -1, -1, NULL, -1 );
      if ( rc ) return NULL;

      firstTime = 0;

   }

    if (oparse(&cda, sql_statement, -1, 0, 2)) return NULL;

   if ( obndrv( &cda, "RID", -1, rowid, strlen(rowid)+2,

               STRING_TYPE, -1, 0, 0, -1, -1) ) return NULL;

    if (odefin(&cda, 1, ucp, UCP_SIZE, isRaw ? 24 : 8, -1,

               &indp, (text *) 0, 0, -1, &retl, &rcode)) return NULL;

    for( rc = oexfet(&cda, (ub4) 1, 0, 0); !rc; rc = ofetch(&cda) )    {

      for( fetched = 0, ret_len = 1; ret_len; fetched += ret_len )
      {
           if (oflng(&cda, 1, ucp, UCP_SIZE,
                        isRaw ? 24 : 8, &ret_len, fetched )) return NULL;

         if ( ret_len )
         {
            if ( returnBufferLen ) 
            {
               returnBuffer = 
                  (my_long*)realloc(returnBuffer, 
								returnBufferLen+UCP_SIZE+1+sizeof(my_long) );
            }
            else
            {
               returnBuffer = (my_long*)malloc( UCP_SIZE+1+sizeof(my_long) );
			   memset( returnBuffer, 0, UCP_SIZE+1+sizeof(my_long) );
            }
            if ( !returnBuffer ) return NULL;

            memmove( returnBuffer->arr+returnBufferLen, ucp, ret_len );

            returnBufferLen += UCP_SIZE;

			returnBuffer->len += ret_len;
            returnBuffer->arr[returnBufferLen] = 0;
         }
      }

   }
 return returnBuffer;
}

char * get_long_emsg(void)
{
static char msg[1024];  

   oerhms( &lda, cda.rc, msg, sizeof(msg) );    return msg;
}

int get_long_errcd(void)
{

   return cda.rc;
}  

Thomas Kyte
tkyte_at_us.oracle.com
Oracle Government
Herndon VA

--
http://govt.us.oracle.com/    -- downloadable utilities
 
----------------------------------------------------------------------------
Opinions are mine and do not necessarily reflect those of Oracle Corporation
 
Anti-Anti Spam Msg: if you want an answer emailed to you, 
you have to make it easy to get email to you.  Any bounced
email will be treated the same way i treat SPAM-- I delete it.
Received on Tue Dec 01 1998 - 00:00:00 CST

Original text of this message

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