# OTL 4.0, OTL LOB stream class

# OTL LOB stream class

This class is used for reading / writing Large Objects (Oracle 8(i)'s [N]CLOBs/BLOBs, DB2's CLOBs/BLOBs, MS SQL Server's TEXT/IMAGE, MySQL's LONGTEXTs/LONGBLOBs, Sybase's (via ODBC) TEXTs/IMAGEs) in the stream mode. The stream mode allows an unlimited number of bytes to be read/written, regardless of the buffer size.

otl_lob_stream does not have any specific constructor by which an actual instance of otl_lob_stream can be created. Instead, the stream gets initialized by writing / reading an otl_lob_stream variable to / from the otl_stream.

Potentially, the otl_lob_stream class may raise an OTL defined exception.

    class otl_lob_stream {
    public:

- Set the total number of bytes (or Unicode characters) to be written to the otl_lob_stream. In other words, set the total size of the LOB. The total length of the LOB normally needs to set before the first LOB chunk should be written into the otl_lob_stream.
  However, OTL 4.0.138 and higher does not require that. OTL/ODBC/DB2-CLI completely relaxes that requirement. OTL/OCI8i/9i/10g does not require that, if the last LOB chunk / piece in the sequence is indicated through a call to otl_long_string::set_last_piece(true).

         void set_len(const int alen);

- Write the whole CLOB / TEXT / etc. into the stream.

otl_lob_stream& operator<<(const std::string& s);
// when OTL_STL is defined and OTL_UNICODE is not defined
otl_lob_stream& operator<<(const ACE_TString& s);
// when OTL_ACE is defined and OTL_UNICODE is not defined

- Read the whole CLOB / TEXT / etc. from the stream into a string

otl_lob_stream& operator>>(std::string& s);
// when OTL_STL is defined and OTL_UNICODE is not defined
otl_lob_stream& operator>>(ACE_TString& s);
// when OTL_ACE is defined and OTL_UNICODE is not defined
void setStringBuffer(const int chunk_size);
// sets an internal buffer of "chunk_size" to help reduce
// the number of calls to string::operator +=() in
// the operators >>() above. The bigger the buffer,
// the fewer the calls to the +=() to concatenate chunks
// of the CLOB value to the output string.

- Write a chunk of the LOB into the stream.

         otl_lob_stream& operator<<(const otl_long_string& s);

         otl_lob_stream& operator<<(const otl_long_unicode_string& s);
           // In case if #define OTL_UNICODE is enabled

- Read a chunk of LOB from the stream.

        otl_lob_stream& operator>>(otl_long_string& s);

        otl_lob_stream& operator>>(otl_long_unicode_string& s);
           // In case if #define OTL_UNICODE is enabled

- Get the length of the LOB to be read in bytes, or in Unicode characters.. In OTL/OCI8/8i/9i, the actual length of the LOB is available immediately after the intialization of the stream. In OTL/ODBC and OTL/DB2-CLI, the function returns 0 before the first chunk of the LOB is read, and it returns the actual length of the LOB after the first chunk of the LOB is read.

This difference is due to the differences in behavior of the underlying database APIs. So, for writing portable [across multiple databases] code, it is recommended to use this function carefully.

         int len(void);

- Check the stream for "end-of-file" condition

         int eof(void);

- Close the stream.

         void close(void);

    }; // end of otl_lob_stream

The ODBC as well as DB2 CLI standards recommend to put LOBs at the end of INSERT/SELECT statements. For example:

INSERT INTO my_tab (f1,f2,f3_lob,f4_lob)...
SELECT f1,f2,f3_lob,f4_lob FROM my_tab...

The recommendation applies especially in the case of piece-wise data operations, that is, in the OTL LOB stream mode. At the same time, the standards say that it is up to the ODBC drivers/database APIs to either enforce or not to enforce that rule.the LOB's part of OCI8 in Oracle does not require that kind of thing, so it is really up to the developer to decide what to do. However, the general recommendation for writing portable code, that uses the LOB stream mode, would be to put LOBs at the end of the column lists in INSERT or SELECT statements.

In order to use the otl_lob_stream class for OTL/ODBC and OTL/DB2-CLI, a call to otl_stream::set_lob_stream_mode() is required.

Also, see example 56, 57, 122, 123, 124, 125, 126, 127, 128, 129.

Prev Next Contents Go Home

Copyright © 1996, 2007, Sergei Kuchin, email: skuchin@ispwest.com, skuchin@gmail.com
.

Permission to use, copy, modify and redistribute this document for any purpose is hereby granted without fee, provided that the above copyright notice appear in all copies.