Written by

Senior Startups and Community Programs Manager at InterSystems Corporation
Question Evgeny Shvarov · Jun 28, 2019

Getting The Value of The Property of Instance or Getting The Value for the Column of The Record

Hi guys!

As you know there are two (at least) ways to get the stored value of the property of InterSystems IRIS class if you know the ID of an instance (or a record).

1. Get it by as a property of an instance with "Object access":

ClassMethod GetPropertyForID(stId As %Integer) As %String

{

set obj=..%OpenId(stId)

return obj.StringData

}

2. Get it as a value of a column of the record with "SQL access":

ClassMethod GetColumnForID(stId As %Integer) As %String

{

&sql(select StringData into :sd from Ideal.StoredData where id = :stId)

return sd

}

What do you prefer?

And why?

To reproduce the case and play with it on InterSystems IRIS 2019.2 you can use the ObjectScript repo.  
It is the persistent class Ideal.StoredData with one property StringData and simple populating util.

To set up the case on your laptop do the following (e.g. on mac, expecting you have git and docker installed):


# clone git@github.com:evshvarov/objectscript.git

# docker-compose build

# docker-compose up -d

# docker-compose exec iris iris session iris

USER>zn "OSCRIPT"

OSCRIPT>d ##class(Ideal.StoredData).AddRecords()       

OSCRIPT>w ##class(Ideal.StoredData).GetPropertyForID(1)
Value1
OSCRIPT>w ##class(Ideal.StoredData).GetColumnForID(1)  
Value1
OSCRIPT>
What Data Model Do you use with InterSystems IRIS?
VoteShow results

Comments

Evgeny Shvarov  Jun 29, 2019 to Eduard Lebedyuk

Thanks, Ed! This is nice!

But this will approach will not execute getter method if any, right?

0
Eduard Lebedyuk  Jun 29, 2019 to Evgeny Shvarov

No, this approach would go to the global.

0
Evgeny Shvarov  Jun 29, 2019 to Eduard Lebedyuk

Marked this approach as a preferred for Object access if the property is not Calculated and not Transient.

0
Evgeny Shvarov  Jun 29, 2019 to Eduard Lebedyuk

Maybe getters could be considered as bad practice in general cause its not called with get stored and not called via  SQL

0
Eduard Lebedyuk  Jun 29, 2019 to Evgeny Shvarov

Are you talking about implicit or explicit getters?

Please elaborate.

0
Evgeny Shvarov  Jun 29, 2019 to Eduard Lebedyuk

Do we have two types of getters?

I mean the one which you code a method like Class.PropertyGet()

0
Eduard Lebedyuk  Jun 29, 2019 to Evgeny Shvarov

That's explicit if you code it.

By default PropertyGet()  method exists, but hidden - it's an implicit getter.

Getters defined via SQLComputeCode work for both SQL and objects iirc.

0
Evgeny Shvarov  Jun 29, 2019 to Eduard Lebedyuk

And GetStored effectively bypasses any of these, right?

0
Evgeny Shvarov  Jun 29, 2019 to Eduard Lebedyuk

Interesting.

SQLComputed property stores the calculated data on disk by default so GetStored works in this case.

And doesn't store if Calculated or Transient flags are defined for the property and GetStored throws an error.

0
Evgeny Shvarov  Jun 29, 2019 to Eduard Lebedyuk

SQLCOMPUTECODE is for calculated properties only I believe.

0
Danny Wijnschenk · Jun 29, 2019

If i need one value, i use GetStored, if i need a few values, i use SQL, if i need a lot of them or need the references to other objects, I open the object instance.

To open an instance and using only 1 property of a class with lots of properties is potentially slower than GetStored or SQL.

0
Evgeny Shvarov  Jun 29, 2019 to Danny Wijnschenk

Thanks, Danny!

good point on slow opening an instance with 100 properties and maybe lot more got swizzled.

0
Marco den Hartog  Jul 6, 2019 to Danny Wijnschenk

Agreed with Danny. It really depends on the use case you are working on and what you really need from a table or object and how frequently it is retrieved.

So many you can ask your question a little bit different Evgeny.

The first approach I often use when building API and need to return a lot of values of the object. SQL when I need a few just as Danny mentioned and the GetStored when I need some value fast .

0