cd /home/project/shared git clone -b try-iris http://github.com/intersystems/FirstLook-ObjectScript
/home/project/shared/FirstLook-ObjectScript/FirstLookObjectScript.xml
.cuk
Now, on the Classes page, you should see FirstLook.ObjectScript.cls
and FirstLook.Person.cls
in the list of classes. In InterSystems IRIS, the name of the package — FirstLook — is appended with the name of the class (ObjectScript or Person). The extension .cls
is used to denote class files.
If you want to play with a few ObjectScript commands, a good way to do this is to use the InterSystems IRIS Web Terminal. Back in the Cloud IDE, use the menubar to navigate to InterSystems > Web Terminal. The prompt indicates which namespace you are in. If you are not in the USER namespace, execute the following command: In ObjectScript, you assign a value to a variable by using the To display the contents of a variable, use the The variable you just created exists only in this Terminal session’s working memory. If you want to store a value in the database, you can use a global, which looks like a variable preceded by a caret ( As you have seen, ObjectScript gives you several options for working with your data. Using globals gives you the most control over how your data is stored, using objects makes it easy to work with single instances of a class, and SQL gives you the power to operate across the rows of a table. How you choose to think about your data is entirely up to you.F*.cls
in the Class Name box in the left column of the page.Try ObjectScript and globals
set $namespace ="USER"
set
command:set x = "Welcome to ObjectScript!"
write
command:write x
// Welcome to ObjectScript!
^
):set ^Settings("Color") = "Red"
^Settings
will live on after you close your Terminal session.set ^Settings("Auto1", "Properties", "Color") = "Red"
set ^Settings("Auto1", "Properties", "Model") = "SUV"
set ^Settings("Auto2", "Owner") = "Mo"
set ^Settings("Auto2", "Properties", "Color") = "Green"
zwrite
command:zwrite ^Settings
^Settings("Auto1","Properties","Color")="Red"
^Settings("Auto1","Properties","Model")="SUV"
^Settings("Auto2","Owner")="Mo"
^Settings("Auto2","Properties","Color")="Green"
^Settings("Color")="Red"
$get()
function. This function returns the empty string if you try to retrieve a value from a global node that does not exist, avoiding potential undefined errors.set ^testglobal(1) = 8888
set ^testglobal(2) = 9999
set globalValue = $get(^testglobal(1))
write "The value of ^testglobal(1) is ", globalValue
The value of ^testglobal(1) is 8888
$order()
function, which returns the next subscript in a global. Passing in a global node with a subscript equal to the empty string causes $order()
to return the first subscript. A return value equal to the empty string indicates that $order()
has reached the last subscript.write $order(^testglobal(""))
// 1
write $order(^testglobal(1))
// 2
write $order(^testglobal(2))
$order()
loop within a class method in an ObjectScript class file. The following method has been provided for you in FirstLook.ObjectScript.cls.
/// Iterate over global ^testglobal
ClassMethod Iterate() {
// Start by setting subscript to ""
set subscript = ""
// "Argumentless" for loop
for {
// Get the next subscript
set subscript = $order(^testglobal(subscript))
// When we get to the end, quit the for loop
quit:(subscript = "")
// Otherwise, write the subscript and the value
// stored at ^testglobal(subscript)
write !, "subscript=", subscript, ", value=", ^testglobal(subscript)
}
}
write
statement tells ObjectScript to move to the next line before writing the subscript and value of each node in the global.do ##class(FirstLook.ObjectScript).Iterate()
subscript=1, value=8888
subscript=2, value=9999
Try ObjectScript and objects
FirstLook.Person.cls
defines a class Person
and then lets you create instances of that class, such as the person John Smith or the person Jane Doe.Class FirstLook.Person Extends %Persistent
{
Property FirstName As %String [ Required ];
Property LastName As %String [ Required ];
}
Person
class extends the built-in InterSystems IRIS class %Persistent
, which gives you access to some helpful methods, such as %New()
and %Save()
. Then the properties of the class are listed. In this case, you are simply storing a person’s first and last name.set $namespace = "USER"
Person
object, use the %New()
method, which returns a “handle” to the new person, more formally known as an object reference, or OREF. Then set the new person’s properties and call the %Save()
method to store the new person in the database.set person = ##class(FirstLook.Person).%New()
set person.FirstName = "Sarah"
set person.LastName = "Aarons"
set status = person.%Save()
write status
1
%Save()
method returns a status, which has the value 1
if it succeeds.D
appended to the end, in this case ^FirstLook.PersonD
.Person
node contains a list of property values, where the list is denoted by $lb
, for “list build.”zwrite ^FirstLook.PersonD
^FirstLook.PersonD=1
^FirstLook.PersonD(1)=$lb("","Sarah","Aarons")
FirstLook.Person.cls
contains a WriteName()
method, which writes a person’s name./// Given an instance of a person, write person's name
Method WriteName() {
write "The name of this person is:"
write !, ..FirstName
write !, ..LastName
}
person
currently refers to Sarah Aarons, you can write her name like so:do person.WriteName()
The name of this person is:
Sarah
Aarons
Person
class.Try ObjectScript and SQL
FirstLook.Person.cls
, does a SELECT command on all objects in the class:/// Query for all stored names
Query Select() As %SQLQuery [SqlProc]
{
SELECT %ID, FirstName, LastName
FROM Person
ORDER By LastName, FirstName
}
do ##class(%ResultSet).RunQuery("FirstLook.Person", "Select")
ID:FirstName:LastName:
4:Kate:Aarons:
1:Sarah:Aarons:
2:Andrew:Shaw:
3:Peter:Shaw:
FirstLook.Person.cls
, to place the results of the query in a result set and then iterate through each row in the set:/// Run select query and write all names in result set
ClassMethod WriteAllNames() {
// Create a new ResultSet object
set resultSet = ##class(%ResultSet).%New()
// Specify the class query to execute by setting
// the ClassName and QueryName properties.
set resultSet.ClassName = "FirstLook.Person"
set resultSet.QueryName = "Select"
// Execute the query
set status = resultSet.%Execute()
// If status = $$$OK, query is successful
if (status = $$$OK) {
// Iterate over results
while (resultSet.%Next()) {
// Write person's first and last name
write !, resultSet.FirstName, " ", resultSet.LastName
}
}
}
resultSet.%Execute()
returns a status of 1
if it is successful. This is often written in code using the $$$OK
macro for easier readability. The code loops through the result set using resultSet.%Next()
, which returns true
if another row exists.do ##class(FirstLook.Person).WriteAllNames()
// Kate Aarons
// Sarah Aarons
// Andrew Shaw
// Peter Shaw
To give you the best possible experience, this site uses cookies and by continuing to use the site you agree that we can save them on your device.