This is my 30th participation in the August Challenge

Lecture 0 preamble

To introduce myself

Hello everyone, LET me introduce myself briefly. My name is Yao Xin.

Why this course

First of all, Cache, M technology is relatively unpopular, the data are all in English, there is no systematic information, I remember that the things I learned when I just joined the job are very basic, there are some complex structural changes, I do not know how to start. Later, some friends in the group always asked me to make a systematic Cache video, which I have been planning for a long time. The purpose is to help more students and make each friend more comfortable in the future work and study. When encountering difficulties or problems, people can consult in the group at any time. The atmosphere in the group is very harmonious, and there are many warm-hearted leaders who will not say that they are embarrassed to ask simple questions and no one will answer them. Send it out if you think it’s a problem.

Since I have been busy during this period and prepared in a hurry, some content may be omitted. If you have any ideas, you can leave a message in the group and answer questions after the meeting. You can put forward your ideas and discuss them together.

Course characteristics

  • When this lecture is going to be over.
  • Full coverage of all knowledge points without dead Angle, detailed explanation, easy to get started.
  • The content is practical and practical, and each knowledge point can be found corresponding reference examples.

The audience

  • 0 Basic students who are new to Cache.
  • Students who have some contact, but are not clear about some details and concepts.
  • Those of you who want to learn more about programming.

The effect

  • You can master all the knowledge points of Cache programming, to avoid some cold just don’t know how to start.
  • For a technology, only if you know it deeply enough, can you describe it in the simplest, colloquial terms. One of the main goals of this course is to make it simple.

How to study this course

  • Each time the courseware will be shared in the group.
  • Training videos will also be shared in the group.
  • Type each example in class manually.
  • There are certain assignments for.

Lecture 1 Introduction to Cache

  • Cache AN integrated object programming language database, the bottom layer is multidimensional array storage, and can be accessed using objects and SQL, meaning that there are three sets of interfaces to simultaneously target a set of data and metadata, maximizing development flexibility.

  • Many large third-class hospitals in China have been used for more than 10 years to witness its robustness, and it is more common abroad.

  • ObjectScript is an object programming language where ObjectScript source code is compiled into object code that is executed in the Cache virtual machine. The object code is highly optimized for operations typically found in business applications, including string manipulation and database access.

You can use Cache ObjectScript in the following context:

  • Interact from the command line of the Cache terminal.
  • As an implementation language for Cache object class methods.
  • Create Cache ObjectScript routines: Programs that are contained and executed in the Cache.
  • As an implementation language for stored procedures and triggers in Cache SQL.
  • As a server-side scripting language in Cache Server Pages applications.

Cache ObjectScript is a superset of the ISO 11756-1999 standard M programming language. Cache ObjectScript provides a number of significant improvements over the ISO standard M.

  • Integrated support for object-oriented programming.
  • Procedure and control blocks using {} syntax.
  • Relaxed space requirements.

The characteristics of

Some of the key features of CacheObjectScript include:

  • Powerful built-in functions are available for handling strings.
  • Object support, including methods, attributes, and polymorphisms.
  • Various commands used to directly control flow in an application.
  • A set of commands used to process I/O devices.
  • Support for multi-dimensional arrays: local variables and Global variables.
  • Support efficient embedded SQL.
  • Support for indirect and runtime calculations and command execution.

Introduction to M program in Lecture 2

Language overview

  • Variable names, class names, and method names are case sensitive.
  • Statements cannot start at the first character position on a line.
  • All commands must be indented. Comments must also be indented.
  • Most ObjectScript commands (and many functions and special variables) have long and short (abbreviated) forms (usually one or two characters)
// d ##class(YX.first).command () class method Command() {set YX = "YX" write YX,! S yx = "x" w yx,! }Copy the code
  • S is the abbreviation of set command, which is an assignment variable definition command. The yx value is Yao Xin.
  • W is the index of the write command and is the value of the display YX variable.

variable

  • In ObjectScript, a variable is the name of a location where a runtime value can be memorized. That is, variables are stored in memory.
  • You must use the SET command to define a variable, but you do not have to specify a type. You can also think of generics.
  • Variables are untyped; That is, they have no specified data type and can accept any data value.
  • Several variables are supported:
    • Local variable – a variable that can only be accessed by the Cache process that created it and that is automatically deleted when the process terminates. Local variables can be accessed from any namespace.
    s name = "yao xin"
    Copy the code
    • Process private global variables – variables that can only be accessed by the Cache process and deleted at the end of the process. The process private global can be accessed from any namespace. Process private global variables are especially useful for temporarily storing large data values.
    s ^||yx = "yao xin"
    Copy the code
    • Global variables – Permanent variables stored in the Cache database. The global can be accessed from any process and persists after the process that created it terminates. Global variables are specific to a single namespace.
    s ^yx = "yao xin"
    Copy the code
    • Array variables – Variables with one or more subscripts. All user-defined variables can be used as arrays, including local variables, process-private globals, global variables, and object properties.
     s color("red")  = "red"
     s color("yellow")  = "yellow"
     s color("green")  = "green"
    Copy the code
    • Special variables (also known as system variables) One of a special set of built-in variables that contain values for a particular aspect of the Cache operating environment. The Cache defines all special variables; The Cache sets all special variables to their initial values (sometimes empty string values). Some special variables can be set by the user; others can only be set by the cache. Special variables are not array variables.
    	w $zversion
     w $namespace
    Copy the code
    • Object properties – Values associated with and stored in a particular instance of an object.
    Property name As %String; /// d ##class(YX.First).Variable() ClassMethod Variable() { s mYx = .. %New() s myx.name = "yixin" w myx.name,! }Copy the code

expression

  • An expression is any set of tags that can be evaluated to produce a single value
/// d ##class(YX.First).Expression()
ClassMethod Expression()
{
	s name = "yao xin"
	w name,!
	w 1 + 2,!
	w $l(name),!
	w $zversion
}
Copy the code

function

  • A function is a routine that performs an operation (for example, converting a string to its equivalent ASCII code value) and returns a value. Call a function from the command line. This call provides parameter values to the function, which uses them to perform some operation. The function then returns a single value (result) to the calling command. You can use functions anywhere expressions are used.

(Ordinary routines are called functions; classes are called methods.)

  • The Cache provides a large number of system functions that cannot be modified. These functions begin with a dollar sign (” $”) and enclose arguments in parentheses; Parentheses are required even if no arguments are specified.

(Special variable names also begin with a single dollar sign, but without parentheses.)

  • Many systems provide function names with abbreviations.
// d ##class(YX.first).funciton () ClassMethod Funciton() {s ##class(YX.first).funciton () ClassMethod Funciton() {s ## $x = $l(" y ") w }Copy the code
  • A function always returns a value. Typically, this return value is supplied to the command.
  • In some functions, you do not need to provide a receiver for the return value. You can run the do or job command
d $classmethod("YX.First","Command")
Copy the code
  • A function can have no arguments, a single argument, or multiple arguments. Function arguments are positional arguments, separated by commas. Many parameters are optional. If a parameter is omitted, the Cache uses its default value. Because parameters are positional parameters, you generally cannot omit parameters in the specified parameter list.
S x = "x" x "x" x _ $e(yx, 1),! W "intercepts one or two characters:" _ $e(yx, 1, 2),!Copy the code
  • Typically, an argument can be specified as a string, a variable, or the return value of another function. In rare cases, parameters must be supplied as strings.
ClassMethod GetStr() {q "iS a developer"}Copy the code
The w "parameter is a string of" _ $e "(" Yao Xin is a development engineer ",1),! _ $e(yx, 2),! The w "argument returns the value of another function:" _ $e(.. GetStr(), 3),!Copy the code
  • In most cases, a variable must be defined before it can be specified as a function parameter, or an error will be generated. In a few cases, it is not necessary to define parameter variables.
_ $d(STR),! _ $d(STR = 0;Copy the code
  • Function parameters are input parameters that provide values to the function. The function does not modify the value of a variable supplied as an input parameter. In rare cases, functions return values and set output parameters.
S color = "red,green,yellow" w" S $p(color, ",", 1) = "write" w"Copy the code

Lesson 3 Grammar rules

Case sensitive

  • Case sensitive

    • Variable names (local, global, and process-private globals)
    • Variable subscript
    • The name of the class
    • The method name
    • The property name
    • I % of the instance variable of the property
    • Routine,
    • Macro name
    • The macro contains the file name (.inc file)
    • Tag name
    • The lock name
    • Embedded code instruction marker string,
    • Embedded SQL host variable name.
  • Case insensitive

    • The command name
    • The function name
    • Special variables
    • Namespace name
    • The SQL statement
    • Preprocessor instructions (such as #include)
    • Embedded code instructions (&HTML, &JS, &SQL)

The blank space

Spaces are considered syntactically meaningful. Whitespace refers to Spaces, tabs, and newlines unless otherwise specified.

I don’t need space

  • Label (also known as tag or entry point) : The label must appear in the first column without a space character. If a line has a label, there must be a space between the label and any code or comments on the line. If the tag has an argument list, there can be no space between the tag name and the opening parenthesis of the argument list. Arguments in the argument list can have Spaces before, between, or after them.

  • Multiline comment: The first line of a multiline comment must be preceded by one or more Spaces. The second and subsequent lines of multi-line comments do not require leading Spaces.

 /* 
asd */
Copy the code

Need a space

  • There must be only one space between the command and its first argument
s yx = "yx"
 s yx = "yx"
Copy the code
  • If a command uses a post-condition, there is no space between the command and the post-condition.
 q:yx '= ""
 q:(yx '= "")
Copy the code
  • There can be any number of Spaces between any pair of command arguments.
 if yx = ""       s yx = "yao xin"
Copy the code
  • If a line contains code, followed by a single line comment, there must be a space between them.
S yx = "yx"//Copy the code
  • Typically, each command is displayed on its own line, although multiple commands can be entered on the same line. In this case, there must be a space between them;
 yx = "yx"   w "1"   
Copy the code
  • If a command is parameterless, it must be followed by two Spaces.
 l  w "11"
Copy the code
  • CacheStudio provides built-in syntax checking, so it flags any Spaces that are used illegally.

annotation

  • /**/ Comments can be displayed on a single line or across lines.
  • // comment specifies that the rest of the line is comment; It can be the first element in the line, or it can follow other elements.
  • ; Comment specifies that the rest of the line is comment; It can be the first element in the line, or it can follow other elements.
  • ;; Annotations; A special type of annotation type. (Not recommended)
  • /// Comments are used as class reference content for the class or class member that follows. For the class itself, the /// comment before the beginning of the class definition provides a class Description of what the class references, which is also the value of the class’s Description keyword.

(Note: because the Cache is preserved in the object code (the code that is actually interpreted and executed); Comments, so including them affects performance, and they should not appear in loops.)

/ / / Comment describe / / / d # # class (YX. First). The Comment () ClassMethod Comment () {w $p (" apple, banana, organe "/ * * / fruit,", ", 2),! W "apple",/* fruit "*/" banana",! S x = "organe"/* fruit "*/ WRITE x,! W "organe"/* fruit "*/ //" other notes "}Copy the code
DHC-APP>d ##class(YX.First).Comment()
banana
apple banana
organe
organe
Copy the code

Literal values

A literal value is an expression that makes up a variable, a constant value consisting of a series of characters representing a particular string or value

	s x = "hello"
	s y = 10
Copy the code

string

  • The maximum string size is configurable. If long strings are enabled, the maximum string size is 3,641,144 characters. Otherwise, the maximum string size is 32,767 characters. Long strings are enabled by default.
  • A string literal is a group of zero or more characters enclosed by quotation marks
  • Strings can contain any character, including Spaces and control characters. The length of a string literal is measured by the number of characters in the string, not the number of bytes.
S STR =" This is a string "; s str ="this is a string" w $l(str),!Copy the code
  • All strings can be entered. You can use the $CHAR function to specify characters that cannot be typed.
	s str = "abc"
	w str,!
	zzdump str
	
	s str = $c(945) _ $c(946) _ $c(947)
	w str,!
	zzdump str
Copy the code
  • Not all string characters can be displayed. They can be non-print characters or control characters.
s str = "a" _ $c(0) _ "b" _ $c(9) _ "c" _ $c(10, 13) _ "d" w !! ,str,! w $l(str),! zzdump strCopy the code
  • To include the quote character (“) in a string, double the character
s str = """""" w !! ,str,! w $l(str),! zzdump strCopy the code
  • A string that does not contain any value is called an empty string. It is represented by two quotation marks (“”). An empty string is treated as a defined value. It has a length of 0, and an empty string is different from a string consisting of a null character ($CHAR(0))
S STR = "" w "empty string :" _ $l(STR),! STR = $c(0) w "$c(0) String: "_ $l(STR),!Copy the code

digital

Numeric literal values are values that ObjectScript evaluates to numbers.

// d ##class(yx.first).number () ClassMethod Number() {s x = +++ + 00008.000w ", $l(x),! W "digital values:", x,! W "=", x = 8,! W "+ 1:", x + 1; }Copy the code

Numeric strings are represented as string numbers enclosed in quotes.

$x = "+++00008.000" w "$x = "+++ +00008.000" W" W "digital values:", x,! W "=", x = 8,! W "+ 1:", x + 1;Copy the code
value The number of
0-9 Any number, but at least one.
Symbolic operators: minus (-) and plus (+). Any number, but must precede all other characters.
Decimal separator character (period or decimal character by default; Comma character in European locale). One at most.
The letter E (for scientific notation). One at most. It has to come between two numbers.

Scientific notation: A number expressed in exponential notation.

W 3.5 e10,!Copy the code

identifier

  • An identifier is the name of a variable, routine, or label. In general, legal identifiers consist of alphanumeric and numeric characters and are case sensitive.
  • The first character of an identifier can be a percent (%) character. Names beginning with the % character are reserved as system elements.
  • There are no reserved words in Cache. You can use any valid identifier as a variable name, function name, or label.
  • It is best to avoid using command names, function names, or other identifiers of such strings.
  • ObjectScript code includes support for embedded SQL to avoid using SQL reserved words to name any function, object, variable, or other entity.

The label

Labels are subroutines

Tags have the following naming conventions:

  • The first character must be an alphanumeric character or a percentage character (%).
  • They can be up to 31 characters long. Labels can be longer than 31 characters, but must be unique within the first 31 characters. The label reference matches only the first 31 characters of the label.
  • Case sensitive.
/// d ##class(YX.First).Number()
ClassMethod Label()
{
yao
XIN
31yao
%xin
}
Copy the code
  • You can use the $ZNAME function to validate the tag name. When validating tag names, do not use parameter parentheses.
	w $zname("%xin")
	w $zname("#%xin")
Copy the code

Note: The tag provides entry points, but it does not define encapsulated code units. This means that once the tag’s code executes, execution will continue to the next tag’s code unit, unless execution is stopped or redirected elsewhere.

// d ##class(yx.first).label () ClassMethod Label() {s x= $random(2) if x=0 {d label0 w "label0 end ",! Q} else {d label1 "label1 end ",! } 0 w "} 0 w W "label0 outer end ",! Label1 w "Start in label1 ",! W "label1 outer end ",! }Copy the code

There are three ways to stop executing code units:

  • Execution encountered QUIT or RETURN.
  • Execute the close curly brace (“} “) of the encountered attempt. When this happens, execution continues from the next line of code after the associated CATCH block.
  • Execution encounters the next procedure block (label with parameter parentheses). When a bracketed label row is encountered, execution is stopped even if there are no arguments in parentheses.