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
@@ -12,6 +12,7 @@ Some most encountered COS expressions
12
12
| Save an object | ` Set status = object.%Save() ` |
13
13
| Retrieve the ID of a saved object | ` Set id = object.%Id() ` |
14
14
| Retrieve the OID of a saved object | ` Set oid = object.%Oid() ` |
15
+ | Determine if an object was modified | ` Set variable = object.%IsModified() ` |
15
16
| Validate an object without saving | ` Set status = object.%ValidateObject() ` |
16
17
| Validate a property without saving | ` Set status = ##class(package.class).PropertyIsValid(object.Property) ` |
17
18
| Print status after error | ` Do $system.Status.DisplayError(status) ` <br >` Zwrite status ` (v2012.2+) |
@@ -23,6 +24,7 @@ Some most encountered COS expressions
23
24
| Clone an object | ` Set clonedObject = object.%ConstructClone() ` |
24
25
| Write a property | ` Write object.property ` |
25
26
| Set a property | ` Set object.property = value ` |
27
+ | Write a class parameter | ` Write ##class(package.class).#PARAMETER ` |
26
28
| Set a serial (embedded) property | ` Set object.property.embeddedProperty = value ` |
27
29
| Link two objects | ` Set object1.referenceProperty = object2 ` |
28
30
| Populate a class | ` Do ##class(package.class).Populate(count, verbose) ` |
@@ -33,22 +35,25 @@ Some most encountered COS expressions
33
35
| Find classname of an object | ` Write $classname(oref) ` |
34
36
| Start the SQL shell | ` Do $system.SQL.Shell() ` |
35
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 ` |
36
39
37
40
## ObjectScript Commands
38
41
39
42
| Command | Description |
40
43
| -------------------------------| -------------------------------------------------------------------------------------|
41
44
| ` Write ` | Display text strings, value of variable or expression. |
45
+ | ` Zwrite ` | Display array, list string, bit string.
42
46
| ` Set ` | Set value of variable. |
43
47
| ` Do ` | Execute method, procedure, or routine. |
44
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. |
45
50
| ` Halt ` | Stop Caché process and close Terminal. |
46
51
| ` Kill ` | Destroy variable(s). |
47
52
| ` If {} ElseIf {} Else {} ` | Evaluate conditions and branch. |
48
53
| ` For {} ` , ` While {} ` , ` Do {} While ` | Execute block of code repeatedly. |
49
54
| ` Try {} Catch {} ` , ` Throw ` | Handle errors. |
50
55
51
- ## ObjectScript Functions
56
+ ## ObjectScript Date/Time Functions and Special Variables
52
57
53
58
| Action | Code |
54
59
| --------------------------------------------------| -------------------------------------------------------------|
@@ -58,13 +63,64 @@ Some most encountered COS expressions
58
63
| Time conversion (internal → external) | ` Set variable = $zt(internalTime, format) ` |
59
64
| Display current internal date/time string | ` Write $horolog ` |
60
65
| Display UTC date/time string | ` Write $ztimestamp ` |
61
- | Display length of a string | ` Write $length(string) ` |
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) ` |
62
87
| Build a list | ` Set listString = $listbuild(list items, separated by comma) ` |
63
88
| Retrieve an item from a list | ` Set variable = $list(listString, position) ` |
89
+ | Put item into list string | ` Set $list(listString, position) = substring ` |
64
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) ` |
65
106
| Display random integer from start to start+count | ` Write $random(count) + start ` |
66
- | Check if variable exists | ` Write $data(variable) ` |
67
- | Return value of variable, or "" If undefined | ` Write $get(variable) ` |
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 ` |
68
124
69
125
## Collections
70
126
0 commit comments