Re: Reading a Long raw into a PRO*C variable

From: Ian Cummings <Ian_Cummings_at_cegelecproj.co.uk>
Date: 1995/06/13
Message-ID: <3rjrdm$fjd_at_jupiter.sdd.cegelecproj.co.uk>#1/1


In article <3ri7gn$iss_at_sundog.tiac.net>, mortens_at_cibadiag.com says...
>
>Has anyone been able to move a long raw field from an Oracle table into
>a C variable in PRO*C. We are having trouble figuring out how to do this.

To get binary data into the database we use this method to store file data in LONG RAW columns. In summary this is what we did:

Define a C strucuture that will hold the RAW data. NOTE: The array length of 1 is just to keep the compiler happy, the size will actually be of variable length. If you are storing C strings, it is one way to ensure you allocate enough space for the string terminator character.

    typedef struct tagLONG_VARRAW
    {

      long len;
      unsigned char arr[1];

    } LONG_VARRAW; Within a SQL DECLARE SECTION (at global scope) we created a Pro*C type which was a reference to the C structure. The big number is the largest size which the SQL TYPE can go to, but because the type is a reference it wont actually allocate that amount of space.

    EXEC SQL TYPE LONG_VARRAW IS LONG VARRAW (2147483643) REFERENCE; Declare your Pro*C host variable where you need to use it

    LONG_VARRAW* h_data_ptr;

Within your Pro*C function which adds the data to the database, use something along these lines to create the variable space for the data

    /* malloc the space for the file and read file contents */     h_data_ptr = (LONG_VARRAW*)malloc (sizeof(LONG_VARRAW) + data_size);     h_data_ptr->len = data_size;

To insert the data into the database you just do a normal SQL INSERT using your host variables.


To get the data out of the database, you first need to find out the size of the data. We have a table which holds information about all files in the database (ie Name, Size, Type, etc.) so we get this data first and then allocate the space for the LONG RAW data.

  /* malloc some space for the contents */   h_data_ptr = (LONG_VARRAW*) malloc (sizeof (LONG_VARRAW) + h_data_size);

We can then read the file data out of the database using a simple SQL SELECT statement.

  /* read the contents from the table */   EXEC SQL select raw_data into :h_data_ptr

             from raw_data_table
             where data_row_id = :h_data_row_id;

Thats all there is to it really, apart from remembering to free the allocated space after you have finished with it.

Hope this helps,

Ian.

-- 
:-------------------------------------------------------------:
: Ian_Cummings_at_cegelecproj.co.uk  #include <std_disclaimer.h> :
: CEGELEC Projects Ltd, Rugby, England, CV21 1BU              :
: Tel- (+44) 01788 563563,        Fax- (+44) 01788 560767     :
:   Opinions expressed are my own, not those of my employer   :
:-------------------------------------------------------------:
Received on Tue Jun 13 1995 - 00:00:00 CEST

Original text of this message