1
- # Cheat sheet
2
- Some most encountered COS expressions
1
+ # Caché ObjectScript Quick Reference
2
+ A list of some common ObjectScript expressions
3
3
4
4
## Object/SQL Basics
5
5
@@ -14,6 +14,7 @@ Some most encountered COS expressions
14
14
| Save an object | ` Set status = object.%Save() ` |
15
15
| Retrieve the ID of a saved object | ` Set id = object.%Id() ` |
16
16
| Retrieve the OID of a saved object | ` Set oid = object.%Oid() ` |
17
+ | Determine if an object was modified | ` Set variable = object.%IsModified() ` |
17
18
| Validate an object without saving | ` Set status = object.%ValidateObject() ` |
18
19
| Validate a property without saving | ` Set status = ##class(package.class).PropertyIsValid(object.Property) ` |
19
20
| Print status after error | ` Do $system.Status.DisplayError(status) ` <br >` Zwrite status ` (v2012.2+) |
@@ -25,6 +26,7 @@ Some most encountered COS expressions
25
26
| Clone an object | ` Set clonedObject = object.%ConstructClone() ` |
26
27
| Write a property | ` Write object.property ` |
27
28
| Set a property | ` Set object.property = value ` |
29
+ | Write a class parameter | ` Write ##class(package.class).#PARAMETER ` |
28
30
| Set a serial (embedded) property | ` Set object.property.embeddedProperty = value ` |
29
31
| Link two objects | ` Set object1.referenceProperty = object2 ` |
30
32
| Populate a class | ` Do ##class(package.class).Populate(count, verbose) ` |
@@ -35,22 +37,25 @@ Some most encountered COS expressions
35
37
| Find classname of an object | ` Write $classname(oref) ` |
36
38
| Start the SQL shell | ` Do $system.SQL.Shell() ` |
37
39
| Test a class query | ` Do ##class(%ResultSet).RunQuery(class, query) ` |
40
+ | Declare a variable's type for Studio Assist | ` #dim object as package.class ` |
38
41
39
42
## ObjectScript Commands
40
43
41
44
| Command | Description |
42
45
| -------------------------------| -------------------------------------------------------------------------------------|
43
46
| ` Write ` | Display text strings, value of variable or expression. |
47
+ | ` Zwrite ` | Display array, list string, bit string.
44
48
| ` Set ` | Set value of variable. |
45
49
| ` Do ` | Execute method, procedure, or routine. |
46
50
| ` Quit ` or ` Return ` (v2013.1) | Terminate method, procedure, or routine. Optionally return value to calling method. |
51
+ | ` Continue ` | Stop current loop iteration, and continue looping. |
47
52
| ` Halt ` | Stop Caché process and close Terminal. |
48
53
| ` Kill ` | Destroy variable(s). |
49
54
| ` If {} ElseIf {} Else {} ` | Evaluate conditions and branch. |
50
55
| ` For {} ` , ` While {} ` , ` Do {} While ` | Execute block of code repeatedly. |
51
56
| ` Try {} Catch {} ` , ` Throw ` | Handle errors. |
52
57
53
- ## ObjectScript Functions
58
+ ## ObjectScript Date/Time Functions and Special Variables
54
59
55
60
| Action | Code |
56
61
| --------------------------------------------------| -------------------------------------------------------------|
@@ -60,13 +65,64 @@ Some most encountered COS expressions
60
65
| Time conversion (internal → external) | ` Set variable = $zt(internalTime, format) ` |
61
66
| Display current internal date/time string | ` Write $horolog ` |
62
67
| Display UTC date/time string | ` Write $ztimestamp ` |
63
- | Display length of a string | ` Write $length(string) ` |
68
+
69
+ ## ObjectScript Branching Functions
70
+
71
+ | Action | Code |
72
+ | --------------------------------------------------| -------------------------------------------------------------|
73
+ | Display result for value of expression | ` Write $case(expression, value1:result1, value2:result2, …, :resultN) ` |
74
+ | Display result for first true condition | ` Write $select(condition1:result1, condition2:result2, …, 1:resultN) ` |
75
+
76
+ ## ObjectScript String Functions
77
+
78
+ | Action | Code |
79
+ | --------------------------------------------------| -------------------------------------------------------------|
80
+ | Display substring extracted from string | ` Write $extract(string, start, end) ` |
81
+ | Display right-justified string within width characters | ` Write $justify(string, width) ` |
82
+ | Display length of string | ` Write $length(string) ` |
83
+ | Display number of delimited pieces in string | ` Write $length(string, delimiter) ` |
84
+ | Display piece from delimited string | ` Write $piece(string, delimiter, pieceNumber) ` |
85
+ | Set piece into delimited string | ` Set $piece(string, delimiter, pieceNumber) = piece ` |
86
+ | Display string after replacing substring | ` Write $replace(string, subString, replaceString) ` |
87
+ | Display reversed string | ` Write $reverse(string) ` |
88
+ | Display string after replacing characters | ` Write $translate(string, searchChars, replaceChars) ` |
64
89
| Build a list | ` Set listString = $listbuild(list items, separated by comma) ` |
65
90
| Retrieve an item from a list | ` Set variable = $list(listString, position) ` |
91
+ | Put item into list string | ` Set $list(listString, position) = substring ` |
66
92
| Display the length of a list | ` Write $listlength(listString) ` |
93
+
94
+ ## ObjectScript Existence Functions
95
+
96
+ | Action | Code |
97
+ | --------------------------------------------------| -------------------------------------------------------------|
98
+ | Check if variable exists | ` Write $data(variable) ` |
99
+ | Return value of variable, or default If undefined | ` Write $get(variable, default) ` |
100
+ | Return next valid subscript in array | ` Write $order(array(subscript)) ` |
101
+
102
+ ## Additional ObjectScript Functions
103
+
104
+ | Action | Code |
105
+ | --------------------------------------------------| -------------------------------------------------------------|
106
+ | Increment ^global by increment | ` $increment(^global, increment) ` <br > ` $sequence(^global, increment) ` |
107
+ | Match a regular expression | ` Set matches = $match(string, regularexpression) ` |
67
108
| Display random integer from start to start+count | ` Write $random(count) + start ` |
68
- | Check if variable exists | ` Write $data(variable) ` |
69
- | Return value of variable, or "" If undefined | ` Write $get(variable) ` |
109
+
110
+ ## ObjectScript Special Variables
111
+
112
+ | Action | Code |
113
+ | --------------------------------------------------| -------------------------------------------------------------|
114
+ | Display process ID | ` Write $job ` |
115
+ | Display current namespace | ` Write $namespace ` |
116
+ | Change current namespace | ` Set $namespace = newnamespace ` |
117
+ | Display username | ` Write $username ` |
118
+ | Display roles | ` Write $roles ` |
119
+
120
+ ## Utilities
121
+
122
+ | Action | Code |
123
+ | --------------------------------------------------| -------------------------------------------------------------|
124
+ | Change current namespace | ` Do ^%CD ` <br > ` zn "newnamespace" ` |
125
+ | Display a ^global | ` Do ^%G ` <br > ` zwrite ^global ` |
70
126
71
127
## Collections
72
128
0 commit comments