Skip Headers
Oracle® Objects for OLE C++ Class Library Developer's Guide
10g Release 2 (10.2)

Part Number B14308-01
Go to Documentation Home
Home
Go to Book List
Book List
Go to Table of Contents
Contents
Go to Index
Index
Go to Master Index
Master Index
Go to Feedback page
Contact Us

Example: Writing a Clob

Note: For more information about sample code, see Sample Code and Applications.

#include <oracl.h>

#include <iostream.h>

#include <fstream.h>

// Example for OClob::Write

int main()

{

//Initialize oo4o, connect, execute sql

OStartup();

ODatabase odb("ExampleDB", "scott", "tiger");

ODynaset odyn(odb, "SELECT * FROM PART");

if (!odyn.IsOpen())

{

cout <<"Connect Error: "<<odb.GetErrorText()<<endl;

cout <<"SQL Error: "<<odyn.GetErrorText()<<endl;

return 1;

}

//prior to modifying a lob for the first time,

//we must first insert an Empty lob and then commit

OValue val;

val.SetEmpty();

odyn.AddNewRecord();

oresult ores = odyn.SetFieldValue((const char *)"PART_DESC", val);

ores = odyn.Update();

OClob oclob;

odyn.StartEdit();

odyn.GetFieldValue("PART_DESC", &oclob);

unsigned char *buffer = 0;

try

{

// calculate an optimum buffersize of approximately 32k bytes

unsigned long optchunk = oclob.GetOptimumChunkSize();

unsigned int bufsize = ((int)(32768/optchunk)) *optchunk;

buffer = (unsigned char *)malloc(bufsize);

//open file and get file size

fstream fs;

fs.open("C:\\Oracle\\Ora81\\Oo4o\\Cpp\\Workbook\\Lob\\partdesc.dat", ios::in);

fs.setmode(filebuf::binary);

fs.seekg(0, ios::end);

unsigned long filesize = fs.tellg();

fs.seekg(0, ios::beg);

unsigned long totalwritten = 0;

unsigned long amtread = 0;

int piecetype = OLOB_FIRST_PIECE;

//By taking advantage of streaming we get the best performance

//and we don't need to allocate a huge buffer.

if (filesize <= bufsize)

piecetype = OLOB_ONE_PIECE;

else

oclob.EnableStreaming(filesize);

while(totalwritten != filesize)

{

fs.read(buffer, bufsize);

amtread = fs.gcount();

oclob.Write(buffer, amtread, piecetype);

totalwritten = totalwritten + amtread;

if ((filesize - totalwritten)<=bufsize)

piecetype = OLOB_LAST_PIECE;

else

piecetype = OLOB_NEXT_PIECE;

}

oclob.DisableStreaming();

ores = odyn.Update();

fs.close();

}

catch(OException E)

{

cout<<E.GetFailedMethodName()<< " Error: "<<E.GetErrorText()<<endl;

}

if (buffer)

free(buffer);

OShutdown();

return 0;

}