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.

Object Extents<br />

(It is similar to the TRUNCATE TABLE command found in older relational database products).<br />

CAUTION:<br />

%KillExtent is intended for use only in a development environment and<br />

should not be used in a live application. %KillExtent bypasses constraints<br />

and user-implemented callbacks, potentially causing data integrity problems.<br />

12.2.4 Testing if <strong>Objects</strong> Exist<br />

There are two basic ways with which you can test if a specific object instance is stored within<br />

the database.<br />

The first way is to use the %ExistsId method provided by the %Persistent class. This is a<br />

class method that takes an ID value and returns a true value (1) if the specified object is<br />

present in the database and false (0) otherwise. For example:<br />

Write ##class(Sample.Person).%ExistsId(1),! // should be 1<br />

Write ##class(Sample.Person).%ExistsId(-1),! // should be 0<br />

The other way is to use an SQL statement and test the value of SQLCODE:<br />

&sql(SELECT %ID FROM Sample.Person WHERE %ID = '1')<br />

Write SQLCODE,! // should be 0: success<br />

&sql(SELECT %ID FROM Sample.Person WHERE %ID = '-1')<br />

Write SQLCODE,! // should be 100: not found<br />

12.3 Object Extents<br />

A set of object instances of a similar type (class) stored within the database is referred to as<br />

an extent.<br />

For a single persistent class (with no subclasses), an extent is the same as a table in relational<br />

database terms. For example, consider the persistent class MyApp.MyPerson:<br />

Class MyApp.MyPerson Extends %Persistent [classtype = persistent]<br />

{<br />

Property Name As %String;<br />

}<br />

The set of all instances of MyApp.MyPerson stored within the database is the MyApp.MyPerson<br />

extent. This is equivalent to a MyApp.MyPerson table with a Name column.<br />

Extents become more interesting when we consider subclasses. An object extent includes all<br />

instances of itself and any of its subclasses.<br />

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

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

Saved successfully!

Ooh no, something went wrong!