25.01.2015 Views

Using Caché Objects - InterSystems Documentation

Using Caché Objects - InterSystems Documentation

Using Caché Objects - InterSystems Documentation

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Streams<br />

Set stream=##class(%Stream.Object).%Open(memo)<br />

Since the %Open method is fully polymorphic, the actual type of stream returned by<br />

%Open depends on what type is stored with the Person object<br />

• Read and process the stream data, closing the stream when done:<br />

Write stream.Read(100)<br />

Do stream.%Close()<br />

Writing a stream is similar. There are several ways to do this. In the first, we get an oref value<br />

and use it to insert the stream into the table:<br />

//Create a stream and write data to it<br />

Set stream = ##class(%GlobalCharacterStream).%New()<br />

Do stream.Write("Do re mi fa sol...")<br />

//Save the stream, record the stream oref in the %qstrhandle<br />

//structure, and turn the stream into a string representation<br />

//of an oref:<br />

Do stream.SaveStream()<br />

Set %qstrhandle(1,stream) = stream<br />

Set stream = stream_""<br />

//INSERT this string-oref value into the table, closing the<br />

//stream when done:<br />

&sql(INSERT INTO Person (Name,Memo) VALUES (:name, :stream))<br />

Kill %qstrhandle(1,stream)<br />

The second approach is similar, but uses %qacn instead of 1 in the %qstrhandle array. There<br />

is no need to save the stream before passing the oref. The filer will actually look for<br />

%qstrhandle($g(%qacn,1),stream)<br />

//Create a stream and write data to it<br />

Set stream = ##class(%GlobalCharacterStream).%New()<br />

Do stream.Write("Do re mi fa sol...")<br />

//Record the stream oref in the %qstrhandle structure, and<br />

//turn the stream into a string representation of an oref.<br />

Set %qacn=100<br />

Set %qstrhandle(%qacn,stream) = stream<br />

Set stream = stream_""<br />

//INSERT this string-oref value into the table, closing<br />

//the stream when we are done:<br />

&sql(INSERT INTO Person (Name,Memo) VALUES (:name, :stream))<br />

Kill %qstrhandle(%qacn,stream)<br />

You can also insert a literal value directly into the table:<br />

//Define the literal value and write it into the table<br />

Set stream = "Do re mi fa sol..."<br />

&sql(INSERT INTO Person (Name,Memo) VALUES (:name, :stream))<br />

Kill %qstrhandle(1,stream)<br />

138 <strong>Using</strong> <strong>Caché</strong> <strong>Objects</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!