Re: Pro*C/C++ : How can i manage text files in RAW or LONG type

From: Thomas Kyte <tkyte_at_us.oracle.com>
Date: 1997/09/10
Message-ID: <341a14b0.10126090_at_newshost>#1/1


On Wed, 10 Sep 1997 18:40:43 +0200, Pierre Didierjean <pdj_at_web-data.fr> wrote:

>Hello
>
>I'm looking for an example in PRO*C to manage text file in RAW or LONG
>mode ...
>
>How can i get the exact size of the text file in the database ?
>I don't want to add a column with the size of each text.
>
>Thanks

If you don't store the length of the long/raw in the database, then PRO*C cannot tell how long it is. OCI on the other hand can. You can easily mix oci and pro*c together, letting OCI just fetch the long/raw out and doing everything else in pro*c. I'm including a small 'api' for pro*c that lets pro*c get longs (datatype code would change for a long raw is all). You might use it in a pro*c program as follows. Foo is a table with a LONG field in it called 'THETEXT' with an attribute column of 'THE_WHOLE_VIEW'. Instead of fetching the long column in pro*c, we fetch the rowid column that identifies the rowid of the long.

static void process()
{

varchar	view_name[100];
varchar	rowid[25];
char	* cp;

EXEC SQL DECLARE C1 CURSOR FOR
	SELECT THE_WHOLE_VIEW, ROWID 
	  FROM FOO 
	 WHERE ROWNUM < 10;

	EXEC SQL OPEN C1;

	for(;;)
	{
		EXEC SQL WHENEVER NOTFOUND DO break;
		EXEC SQL FETCH C1 INTO :view_name, :rowid;

		view_name.arr[view_name.len] = 0;
		rowid.arr[rowid.len] = 0;

		printf( "%s, %s\n", view_name.arr, rowid.arr );
		printf( "View Text = '%s'", 
			(cp=get_long( "select thetext from foo where rowid = :rid",
rowid.arr ))?cp:"(null)" );
	}

	EXEC SQL CLOSE C1;

}  

the OCI routines would look like:

#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"



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

char * get_long( char * sql_statement, char * rowid ) {

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;

char   * 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, 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,
                        8, &ret_len, fetched )) return NULL;

         if ( ret_len )
         {
            if ( returnBufferLen ) 
            {
               returnBuffer = 
                  (char*)realloc(returnBuffer,returnBufferLen+UCP_SIZE+1);
            }
            else
            {
               returnBuffer = (char*)malloc( UCP_SIZE+1 );
            }
            if ( !returnBuffer ) return NULL;
            memmove( returnBuffer+returnBufferLen, ucp, ret_len );

            returnBufferLen += UCP_SIZE;
            returnBuffer[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
Bethesda MD

http://govt.us.oracle.com/ -- downloadable utilities



Opinions are mine and do not necessarily reflect those of Oracle Corporation Received on Wed Sep 10 1997 - 00:00:00 CEST

Original text of this message