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?
There's an autogenerated method GetStored for each property which wraps direct global access:
You can read more about autogenerated methods in this article.
Thanks, Ed! This is nice!
But this will approach will not execute getter method if any, right?
No, this approach would go to the global.
Marked this approach as a preferred for Object access if the property is not Calculated and not Transient.
Be aware that GetStored doesn't work for Calculated or Transient properties, but does work fro SQLComputed.
Maybe getters could be considered as bad practice in general cause its not called with get stored and not called via SQL
Are you talking about implicit or explicit getters?
Please elaborate.
Do we have two types of getters?
I mean the one which you code a method like Class.PropertyGet()
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.
And GetStored effectively bypasses any of these, right?
Yes
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.
SQLCOMPUTECODE is for calculated properties only I believe.
No.
Here's the docs.
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.
Thanks, Danny!
good point on slow opening an instance with 100 properties and maybe lot more got swizzled.
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 .
Interesting
Social networks
InterSystems resources
Log in or sign up
Log in or create a new account to continue
Log in or sign up
Log in or create a new account to continue
Log in or sign up
Log in or create a new account to continue