|
| 1 | +# Caché ObjectScript Quick Reference |
| 2 | +A list of some common ObjectScript expressions |
| 3 | + |
| 4 | +## Object/SQL Basics |
| 5 | + |
| 6 | +| Action | Code | |
| 7 | +|----------------------------------------------|-----------------------------------------------------------------------------------| |
| 8 | +| Call a class method | `Do ##class(package.class).method(arguments)`<br>`Set variable = ##class(package.class).method(arguments)`<br>Note: place a . before each pass-by-reference argument | |
| 9 | +| Call an instance method | `Do object.method(arguments)`<br>`Set variable = object.method(arguments)`<br>Note: place a . before each pass-by-reference argument | |
| 10 | +| Create a new object | `Set object = ##class(package.class).%New()` | |
| 11 | +| Open an existing object | `Set object = ##class(package.class).%OpenId(id, concurrency, .status)` | |
| 12 | +| Save an object | `Set status = object.%Save()` | |
| 13 | +| Retrieve the ID of a saved object | `Set id = object.%Id()` | |
| 14 | +| Retrieve the OID of a saved object | `Set oid = object.%Oid()` | |
| 15 | +| Determine if an object was modified | `Set variable = object.%IsModified()` | |
| 16 | +| Validate an object without saving | `Set status = object.%ValidateObject()` | |
| 17 | +| Validate a property without saving | `Set status = ##class(package.class).PropertyIsValid(object.Property)` | |
| 18 | +| Print status after error | `Do $system.Status.DisplayError(status)`<br>`Zwrite status` (v2012.2+) | |
| 19 | +| Obtain status details after error | `Do $system.Status.DecomposeStatus(status, .err)` | |
| 20 | +| Remove an object from process memory | `Set object = ""` | |
| 21 | +| Delete an existing object of a class | `Set status = ##class(package.class).%DeleteId(id)` | |
| 22 | +| Delete all saved objects of a class | `Do ##class(package.class).%DeleteExtent()`<br>`Do ##class(package.class).%KillExtent()` | |
| 23 | +| Reload properties of a saved object | `Do object.%Reload()` | |
| 24 | +| Clone an object | `Set clonedObject = object.%ConstructClone()` | |
| 25 | +| Write a property | `Write object.property` | |
| 26 | +| Set a property | `Set object.property = value` | |
| 27 | +| Write a class parameter | `Write ##class(package.class).#PARAMETER` | |
| 28 | +| Set a serial (embedded) property | `Set object.property.embeddedProperty = value` | |
| 29 | +| Link two objects | `Set object1.referenceProperty = object2` | |
| 30 | +| Populate a class | `Do ##class(package.class).Populate(count, verbose)` | |
| 31 | +| Remove all objects in process memory | `Kill` | |
| 32 | +| List all objects in process memory | `Do $system.OBJ.ShowObjects()` | |
| 33 | +| Display all properties of an object | `Do $system.OBJ.Dump(object)`<br>`Zwrite object` (v2012.2+) | |
| 34 | +| Determine If variable is an object reference | `If $isobject(variable) ; 1=yes, 0=no` | |
| 35 | +| Find classname of an object | `Write $classname(oref)` | |
| 36 | +| Start the SQL shell | `Do $system.SQL.Shell()` | |
| 37 | +| Test a class query | `Do ##class(%ResultSet).RunQuery(class, query)` | |
| 38 | +| Declare a variable's type for Studio Assist | `#dim object as package.class` | |
| 39 | + |
| 40 | +## ObjectScript Commands |
| 41 | + |
| 42 | +| Command | Description | |
| 43 | +|-------------------------------|-------------------------------------------------------------------------------------| |
| 44 | +| `Write` | Display text strings, value of variable or expression. | |
| 45 | +| `Zwrite` | Display array, list string, bit string. |
| 46 | +| `Set` | Set value of variable. | |
| 47 | +| `Do` | Execute method, procedure, or routine. | |
| 48 | +| `Quit` or `Return` (v2013.1) | Terminate method, procedure, or routine. Optionally return value to calling method. | |
| 49 | +| `Continue` | Stop current loop iteration, and continue looping. | |
| 50 | +| `Halt` | Stop Caché process and close Terminal. | |
| 51 | +| `Kill` | Destroy variable(s). | |
| 52 | +| `If {} ElseIf {} Else {}` | Evaluate conditions and branch. | |
| 53 | +| `For {}`, `While {}`, `Do {} While` | Execute block of code repeatedly. | |
| 54 | +| `Try {} Catch {}`, `Throw` | Handle errors. | |
| 55 | + |
| 56 | +## ObjectScript Date/Time Functions and Special Variables |
| 57 | + |
| 58 | +| Action | Code | |
| 59 | +|--------------------------------------------------|-------------------------------------------------------------| |
| 60 | +| Date conversion (external → internal) | `Set variable = $zdh("mm/dd/yyyy")` | |
| 61 | +| Date conversion (internal → external) | `Set variable = $zd(internalDate, format)` | |
| 62 | +| Time conversion (external → internal) | `Set variable = $zth("hh:mm:ss")` | |
| 63 | +| Time conversion (internal → external) | `Set variable = $zt(internalTime, format)` | |
| 64 | +| Display current internal date/time string | `Write $horolog` | |
| 65 | +| Display UTC date/time string | `Write $ztimestamp` | |
| 66 | + |
| 67 | +## ObjectScript Branching Functions |
| 68 | + |
| 69 | +| Action | Code | |
| 70 | +|--------------------------------------------------|-------------------------------------------------------------| |
| 71 | +| Display result for value of expression | `Write $case(expression, value1:result1, value2:result2, …, :resultN)` | |
| 72 | +| Display result for first true condition | `Write $select(condition1:result1, condition2:result2, …, 1:resultN)` | |
| 73 | + |
| 74 | +## ObjectScript String Functions |
| 75 | + |
| 76 | +| Action | Code | |
| 77 | +|--------------------------------------------------|-------------------------------------------------------------| |
| 78 | +| Display substring extracted from string | `Write $extract(string, start, end)` | |
| 79 | +| Display right-justified string within width characters | `Write $justify(string, width)` | |
| 80 | +| Display length of string | `Write $length(string)` | |
| 81 | +| Display number of delimited pieces in string | `Write $length(string, delimiter)` | |
| 82 | +| Display piece from delimited string | `Write $piece(string, delimiter, pieceNumber)` | |
| 83 | +| Set piece into delimited string | `Set $piece(string, delimiter, pieceNumber) = piece` | |
| 84 | +| Display string after replacing substring | `Write $replace(string, subString, replaceString)`| |
| 85 | +| Display reversed string | `Write $reverse(string)` | |
| 86 | +| Display string after replacing characters | `Write $translate(string, searchChars, replaceChars)` | |
| 87 | +| Build a list | `Set listString = $listbuild(list items, separated by comma)` | |
| 88 | +| Retrieve an item from a list | `Set variable = $list(listString, position)` | |
| 89 | +| Put item into list string | `Set $list(listString, position) = substring` | |
| 90 | +| Display the length of a list | `Write $listlength(listString)` | |
| 91 | + |
| 92 | +## ObjectScript Existence Functions |
| 93 | + |
| 94 | +| Action | Code | |
| 95 | +|--------------------------------------------------|-------------------------------------------------------------| |
| 96 | +| Check if variable exists | `Write $data(variable)` | |
| 97 | +| Return value of variable, or default If undefined | `Write $get(variable, default)` | |
| 98 | +| Return next valid subscript in array | `Write $order(array(subscript))` | |
| 99 | + |
| 100 | +## Additional ObjectScript Functions |
| 101 | + |
| 102 | +| Action | Code | |
| 103 | +|--------------------------------------------------|-------------------------------------------------------------| |
| 104 | +| Increment ^global by increment | `$increment(^global, increment)` <br> `$sequence(^global, increment)` | |
| 105 | +| Match a regular expression | `Set matches = $match(string, regularexpression)` | |
| 106 | +| Display random integer from start to start+count | `Write $random(count) + start` | |
| 107 | + |
| 108 | +## ObjectScript Special Variables |
| 109 | + |
| 110 | +| Action | Code | |
| 111 | +|--------------------------------------------------|-------------------------------------------------------------| |
| 112 | +| Display process ID | `Write $job` | |
| 113 | +| Display current namespace | `Write $namespace` | |
| 114 | +| Change current namespace | `Set $namespace = newnamespace` | |
| 115 | +| Display username | `Write $username` | |
| 116 | +| Display roles | `Write $roles` | |
| 117 | + |
| 118 | +## Utilities |
| 119 | + |
| 120 | +| Action | Code | |
| 121 | +|--------------------------------------------------|-------------------------------------------------------------| |
| 122 | +| Change current namespace | `Do ^%CD` <br> `zn "newnamespace"` | |
| 123 | +| Display a ^global | `Do ^%G` <br> `zwrite ^global` | |
| 124 | + |
| 125 | +## Collections |
| 126 | + |
| 127 | +| Action | Code | |
| 128 | +|--------------------------------------------------------------|--------------------------------------------------------------------------------------------------------| |
| 129 | +| Create a new standalone list<br>Work with a list property | `Set listObject=##class(%ListOfDataTypes).%New()`<br>Use methods below on a list collection property | |
| 130 | +| Insert an element at the end of a list | `Do listObject.Insert(value)`<br>`Do object.listProperty.Insert(value)` | |
| 131 | +| Insert an element into a list | `Do listObject.SetAt(value, position)`<br>`Do object.listProperty.SetAt(value, position)` | |
| 132 | +| Remove an element from a list | `Do listObject.RemoveAt(position)`<br>`Do object.listProperty.RemoveAt(position)` | |
| 133 | +| Display an element of a list | `Write listObject.GetAt(position)`<br>`Write object.listProperty.GetAt(position)` | |
| 134 | +| Display the size of a list | `Write listObject.Count()`<br>`Write object.listProperty.Count()` | |
| 135 | +| Clear all the elements of a list | `Do listObject.Clear()`<br>`Do object.listProperty.Clear()` | |
| 136 | +| Create a new standalone array<br>Work with an array property | `Set arrayObject=##class(%ArrayOfDataTypes).%New()`<br>Use methods below on an array collection property | |
| 137 | +| Insert an element into an array | `Do arrayObject.SetAt(value, key)`<br>`Do object.arrayProperty.SetAt(value, key)` | |
| 138 | +| Remove an element from an array | `Do arrayObject.RemoveAt(key)`<br>`Do object.arrayProperty.RemoveAt(key)` | |
| 139 | +| Display an element of an array | `Write arrayObject.GetAt(key)`<br>`Do object.arrayProperty.GetAt(key)` | |
| 140 | +| Display the size of an array | `Write arrayObject.Count()`<br>`Do object.arrayProperty.Count()` | |
| 141 | +| Clear all elements of an array | `Do arrayObject.Clear()`<br>`Do object.arrayProperty.Clear()` | |
| 142 | + |
| 143 | +## Relationships |
| 144 | + |
| 145 | +| Action | Code | |
| 146 | +|-----------------------------------------|------------------------------------------------------------------------------------------| |
| 147 | +| Parent-to-children object linking | `Do parentObject.childRefProperty.Insert(chilDobject)`<br>`Set chilDobject.parentRefProperty = parentObject` | |
| 148 | +| One-to-many object linking | `Do oneObject.manyRefProperty.Insert(manyObject)`<br>`Set manyObject.OneRefProperty = OneObject` | |
| 149 | +| Write a property of a child object | `Write parentObject.childRefProperty.GetAt(position).property` | |
| 150 | +| Write a property of a many object | `Write oneObject.manyRefProperty.GetAt(position).property` | |
| 151 | +| Display the count of child/many objects | `Write parentObject.childRefProperty.Count()`<br>`Write oneObject.manyRefProperty.Count()` | |
| 152 | +| Open a many/child object directly | `Set object = ##class(package.class).IDKEYOpen(parentID, childsub)` | |
| 153 | +| Retrieve the id of a child object | `Set status = ##class(package.class).IDKEYExists(parentID, childsub, .childID)` | |
| 154 | +| Clear the child/many objects | `Do parentObject.childRefProperty.Clear()<br>Do oneObject.manyRefProperty.Clear()` | |
| 155 | + |
| 156 | +## Streams |
| 157 | + |
| 158 | +| Action | Code | |
| 159 | +|-------------------------------------------|---------------------------------------------------------------------------------------| |
| 160 | +| Create a new stream | `Set streamObject=##class(%Stream.GlobalCharacter).%New()`<br>`Set streamObject=##class(%Stream.GlobalBinary).%New()`<br>Use methods below on a stream property | |
| 161 | +| Add text to a stream | `Do streamObject.Write(text)`<br>`Do object.streamProperty.Write(text)` | |
| 162 | +| Add a line of text to a stream | `Do streamObject.WriteLine(text)`<br>`Do object.streamProperty.WriteLine(text)` | |
| 163 | +| Read len characters of text from a stream | `Write streamObject.Read(len)`<br>`Write object.streamProperty.Read(len)` | |
| 164 | +| Read a line of text from a stream | `Write streamObject.ReadLine(len)`<br>`Write object.streamProperty.ReadLine(len)` | |
| 165 | +| Go to the beginning of a stream | `Do streamObject.Rewind()`<br>`Do object.streamProperty.Rewind()` | |
| 166 | +| Go to the end of a stream, for appending | `Do streamObject.MoveToEnd()`<br>`Do object.streamProperty.MoveToEnd()` | |
| 167 | +| Clear a stream | `Do streamObject.Clear()`<br>`Do object.streamProperty.Clear()` | |
| 168 | +| Display the length of a stream | `Write streamObject.Size`<br>`Write object.streamProperty.Size` | |
| 169 | + |
| 170 | +## Unit Testing Macros |
| 171 | + |
| 172 | +| Action | Code | |
| 173 | +|-----------------------------|--------------------------------------------------| |
| 174 | +| Assert equality | `Do $$$AssertEquals(value1, value2, message)` | |
| 175 | +| Assert inequality | `Do $$$AssertNotEquals(value1, value2, message)` | |
| 176 | +| Assert status is OK | `Do $$$AssertStatusOK(status, message)` | |
| 177 | +| Assert status isn't OK | `Do $$$AssertStatusNotOK(status, message)` | |
| 178 | +| Assert condition is true | `Do $$$AssertTrue(condition, message)` | |
| 179 | +| Assert condition isn't true | `Do $$$AssertNotTrue(condition, message)` | |
| 180 | +| Log message | `Do $$$LogMessage(message)` | |
| 181 | + |
| 182 | +## Other Macros |
| 183 | + |
| 184 | +| Action | Code | |
| 185 | +|----------------------------------|-------------------------------------------| |
| 186 | +| Return a good status | `Quit $$$OK` | |
| 187 | +| Return an error status | `Quit $$$ERROR($$$GeneralError, message)` | |
| 188 | +| Throw exception on error | `$$$ThrowOnError(status, code)` or `$$$TOE(status, code)` | |
| 189 | +| Check if status is good | `If $$$ISOK(status)` | |
| 190 | +| Check if status is an error | `If $$$ISERR(status)` | |
| 191 | +| Return a null object reference | `Quit $$$NULLOREF` | |
| 192 | +| Place a new line within a string | `Write string1_$$$NL_string2` | |
0 commit comments