|
| 1 | +# Cheat sheet |
| 2 | +Some most encountered COS 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 | +| Validate an object without saving | Set status = object.%ValidateObject(), | |
| 16 | +| Validate a property without saving | Set status = ##class(package.class).PropertyIsValid(object.Property) | |
| 17 | +| Print status after error | Do $system.Status.DisplayError(status)<br>ZWrite status (v2012.2+) | |
| 18 | +| Obtain status details after error | Do $system.Status.DecomposeStatus(status, .err) | |
| 19 | +| Remove an object from process memory | Set object = "" | |
| 20 | +| Delete an existing object of a class | Set status = ##class(package.class).%DeleteId(id) | |
| 21 | +| Delete all saved objects of a class | Do ##class(package.class).%DeleteExtent()<br>Do ##class(package.class).%KillExtent() | |
| 22 | +| Reload properties of a saved object | Do object.%Reload() | |
| 23 | +| Clone an object | Set cloneDobject = object.%ConstructClone() | |
| 24 | +| Write a property | Write object.property | |
| 25 | +| Set a property | Set object.property = value | |
| 26 | +| Set a serial (embedded) property | Set object.property.embeddedProperty = value | |
| 27 | +| Link two objects | Set object1.referenceProperty = object2 | |
| 28 | +| Populate a class | Do ##class(package.class).Populate(count, verbose) | |
| 29 | +| Remove all objects in process memory | kill | |
| 30 | +| List all objects in process memory | Do $system.OBJ.ShowObjects() | |
| 31 | +| Display all properties of an object | Do $system.OBJ.Dump(object) zWrite object (v2012.2+) | |
| 32 | +| Determine If variable is an object reference | If $isobject(variable) ; 1=yes, 0=no | |
| 33 | +| Find classname of an object | Write $classname(oref) | |
| 34 | +| Start the SQL shell | Do $system.SQL.Shell() | |
| 35 | +| Test a class query | Do ##class(%ResultSet).RunQuery(class, query) | |
| 36 | + |
| 37 | +## ObjectScript Commands |
| 38 | + |
| 39 | +| Command | Description | |
| 40 | +|-------------------------------|-------------------------------------------------------------------------------------| |
| 41 | +| Write | Display text strings, value of variable or expression. | |
| 42 | +| Set | Set value of variable. | |
| 43 | +| Do | Execute method, procedure, or routine. | |
| 44 | +| Quit or Return (v2013.1) | Terminate method, procedure, or routine. Optionally return value to calling method. | |
| 45 | +| Halt | Stop Caché process and close Terminal. | |
| 46 | +| Kill | Destroy variable(s). | |
| 47 | +| If {} ElseIf {} Else {} | Evaluate conditions and branch. | |
| 48 | +| For {}, While {}, Do {} While | Execute block of code repeatedly. | |
| 49 | +| Try {} Catch {}, Throw | Handle errors. | |
| 50 | + |
| 51 | +## ObjectScript Functions |
| 52 | + |
| 53 | +| Action | Code | |
| 54 | +|--------------------------------------------------|-------------------------------------------------------------| |
| 55 | +| Date conversion (external → internal) | Set variable = $zdh(“mm/dd/yyyy”) | |
| 56 | +| Date conversion (internal → external) | Set variable = $zd(internalDate, format) | |
| 57 | +| Time conversion (external → internal) | Set variable = $zth(“hh:mm:ss”) | |
| 58 | +| Time conversion (internal → external) | Set variable = $zt(internalTime, format) | |
| 59 | +| Display current internal date/time string | Write $horolog | |
| 60 | +| Display UTC date/time string | Write $ztimestamp | |
| 61 | +| Display length of a string | Write $length(string) | |
| 62 | +| Build a list | Set listString = $listbuild(list items, separated by comma) | |
| 63 | +| Retrieve an item from a list | Set variable = $list(listString, position) | |
| 64 | +| Display the length of a list | Write $listlength(listString) | |
| 65 | +| Display ranDom integer from start to count | Write $ranDom(count) + start | |
| 66 | +| Check If variable exists | Write $data(variable) | |
| 67 | +| Return value of variable, or "" If undefined | Write $get(variable) | |
| 68 | + |
| 69 | +## Collections |
| 70 | + |
| 71 | +| Action | Code | |
| 72 | +|--------------------------------------------------------------|--------------------------------------------------------------------------------------------------------| |
| 73 | +| 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 | |
| 74 | +| Insert an element at the end of a list | Do listObject.Insert(value)<br>Do object.listProperty.Insert(value) | |
| 75 | +| Insert an element into a list | Do listObject.SetAt(value, position)<br>Do object.listProperty.SetAt(value, position) | |
| 76 | +| Remove an element from a list | Do listObject.RemoveAt(position)<br>Do object.listProperty.RemoveAt(position) | |
| 77 | +| Display an element of a list | Write listObject.GetAt(position)<br>Write object.listProperty.GetAt(position) | |
| 78 | +| Display the size of a list | Write listObject.Count()<br>Write object.listProperty.Count() | |
| 79 | +| Clear all the elements of a list | Do listObject.Clear()<br>Do object.listProperty.Clear() | |
| 80 | +| 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 | |
| 81 | +| Insert an element into an array | Do arrayObject.SetAt(value, key)<br>Do object.arrayProperty.SetAt(value, key) | |
| 82 | +| Remove an element from an array | Do arrayObject.RemoveAt(key)<br>Do object.arrayProperty.RemoveAt(key) | |
| 83 | +| Display an element of an array | Write arrayObject.GetAt(key)<br>Do object.arrayProperty.GetAt(key) | |
| 84 | +| Display the size of an array | Write arrayObject.Count()<br>Do object.arrayProperty.Count() | |
| 85 | +| Clear all elements of an array | Do arrayObject.Clear()<br>Do object.arrayProperty.Clear() | |
| 86 | + |
| 87 | +## Relatioships |
| 88 | + |
| 89 | +| Action | Code | |
| 90 | +|-----------------------------------------|----------------------------------------------------------------------------------------------------------| |
| 91 | +| Parent-to-children object linking | Do parentObject.childRefProperty.Insert(chilDobject)<br>Set chilDobject.parentRefProperty = parentObject | |
| 92 | +| One-to-many object linking | Do oneObject.manyRefProperty.Insert(manyObject)<br>Set manyObject.OneRefProperty = OneObject | |
| 93 | +| Write a property of a child object | Write parentObject.childRefProperty.GetAt(position).property<br> | |
| 94 | +| Write a property of a many object | Write oneObject.manyRefProperty.GetAt(position).property | |
| 95 | +| Display the count of child/many objects | Write parentObject.childRefProperty.Count()<br>Write oneObject.manyRefProperty.Count() | |
| 96 | +| Open a many/child object directly | Set object = ##class(package.class).IDKEYOpen(parentID, childsub) | |
| 97 | +| Retrieve the id of a child object | Set status = ##class(package.class).IDKEYExists(parentID, childsub, .childID) | |
| 98 | +| Clear the child/many objects | Do parentObject.childRefProperty.Clear()<br>Do oneObject.manyRefProperty.Clear() | |
| 99 | + |
| 100 | +## Streams |
| 101 | + |
| 102 | +| Action | Code | |
| 103 | +|-------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------| |
| 104 | +| 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 | |
| 105 | +| Add text to a stream | Do streamObject.Write(text)<br>Do object.streamProperty.Write(text) | |
| 106 | +| Add a line of text to a stream | Do streamObject.WriteLine(text)<br>Do object.streamProperty.WriteLine(text) | |
| 107 | +| Read len characters of text from a stream | Write streamObject.Read(len)<br>Write object.streamProperty.Read(len) | |
| 108 | +| Read a line of text from a stream | Write streamObject.ReadLine(len)<br>Write object.streamProperty.ReadLine(len) | |
| 109 | +| Go to the beginning of a stream | Do streamObject.Rewind()<br>Do object.streamProperty.Rewind() | |
| 110 | +| Go to the end of a stream, for appending | Do streamObject.MoveToEnd()<br>Do object.streamProperty.MoveToEnd() | |
| 111 | +| Clear a stream | Do streamObject.Clear()<br>Do object.streamProperty.Clear() | |
| 112 | +| Display the length of a stream | Write streamObject.Size<br>Write object.streamProperty.Size | |
| 113 | + |
| 114 | +## Unit Testing Macros |
| 115 | + |
| 116 | +| Action | Code | |
| 117 | +|-----------------------------|------------------------------------------------| |
| 118 | +| Assert equality | Do $$$AssertEquals(value1, value2, message) | |
| 119 | +| Assert inequality | Do $$$AssertNotEquals(value1, value2, message) | |
| 120 | +| Assert status is OK | Do $$$AssertStatusOK(status, message) | |
| 121 | +| Assert status isn't OK | Do $$$AssertStatusNotOK(status, message) | |
| 122 | +| Assert condition is true | Do $$$AssertTrue(condition, message) | |
| 123 | +| Assert condition isn't true | Do $$$AssertNotTrue(condition, message) | |
| 124 | +| Log message | Do $$$LogMessage(message) | |
| 125 | + |
| 126 | +## Other Macros |
| 127 | + |
| 128 | +| Action | Code | |
| 129 | +|----------------------------------|-----------------------------------------| |
| 130 | +| Return a good status | Quit $$$OK | |
| 131 | +| Return an error status | Quit $$$ERROR($$$GeneralError, message) | |
| 132 | +| Check If status is good | If $$$ISOK(status) | |
| 133 | +| Check If status is an error | If $$$ISERR(status) | |
| 134 | +| Return a null object reference | Quit $$$NULLOREF | |
| 135 | +| Place a new line within a string | Write string1_$$$NL_string2 | |
0 commit comments