25.01.2015 Views

Using Caché Objects - InterSystems Documentation

Using Caché Objects - InterSystems Documentation

Using Caché Objects - InterSystems Documentation

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Caché</strong> ObjectScript. These queries are processed using the same %Library.ResultSet interface<br />

as queries written in SQL but are defined differently.<br />

To define a <strong>Caché</strong> ObjectScript query, specify %Query for the query type and provide necessary<br />

information for the related parameters (ROWSPEC and CONTAINID). For example, if a<br />

Book class has Author and Title properties, you could define an All query (to list all books)<br />

as follows:<br />

Query All() As %Query(CONTAINID = 1, ROWSPEC = "Title:%String,Author:%String")<br />

{<br />

}<br />

The actual query definition contains no other information, as the query processing code is<br />

defined in three class methods that are associated with it. These methods are:<br />

• querynameExecute — This method invokes the query. For queries that use the SELECT<br />

command, this method also prepares the query for subsequent traversal and data retrieval.<br />

• querynameFetch — Calling this method returns a row of the result set; each subsequent<br />

call returns the next row.<br />

• querynameClose — This method performs cleanup operations.<br />

where queryname is the name of the query.<br />

In addition, <strong>Caché</strong> automatically generates querynameGetInfo and querynameFetchRows<br />

methods from the row specification and querynameFetch method respectively. Your application<br />

does not call any of these methods directly — the %Library.ResultSet object uses them<br />

to process query requests.<br />

8.2.1 The querynameExecute Method<br />

The querynameExecute method provides all of the setup needed to retrieve data. For queries<br />

using SQL code, this typically includes declaring and opening a cursor. The<br />

querynameExecute method returns status information and uses a %Binary variable passed<br />

by reference as its first argument. This argument passes information between the query<br />

methods and contains all information needed by the various query methods.<br />

For example, if a query called ByName has no runtime parameters, the ByNameExecute<br />

method has the following signature:<br />

ClassMethod ByNameExecute(ByRef qHandle As %Binary) As %Status {<br />

// query setup code here<br />

Quit $$$OK<br />

}<br />

User-Written Class Queries<br />

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

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

Saved successfully!

Ooh no, something went wrong!