InterSystem Object Script语法简介

1, 基本语法

a, terminal 的使用:常用命令

############ 1,切换NameSpace (3种方式)
USER>do ^%CD
Namespace: t1
You're in namespace T1
Default directory is e:\helthc\mgr\t1\

T1>set $namespace="demo"

DEMO>zn "%sys"
%SYS>
############ 2,变量赋值
%SYS>set a="hello"
%SYS>write a
hello

%SYS>s num=1+1
%SYS>w num
2

############ 3,w的特殊用法:w # -->清屏,  w!-->换行
%SYS>w !

%SYS>w "a"_"b"
ab
############ 4, 数组
%SYS>set arr(0)=1
%SYS>set arr(1)=2
%SYS>set arr(1,1)="inner1"
%SYS>set arr(1,2)="inner2"

%SYS>zw arr
arr(0)=1
arr(1)=2
arr(1,1)="inner1"
arr(1,2)="inner2"
 
%SYS>w arr(0)
1
%SYS>w arr(1)
2
%SYS>w arr(1,1)
inner1

b,逻辑控制:选择/循环

  • if语句, case语句
USER>if 0{w "true"} else{w "false"}
false
USER>if 1{w "true"} else{w "false"}
true
USER>if 2{w "true"} else{w "false"}
true

USER>w "a"="a"
1
USER>w "a"="a1"
0
USER>if ("a"="a"){w "true"} else{w "false"}
true
USER>if ("a"="a1"){w "true"} else{w "false"}
false

--------------------case-------
USER>w $case(1,1:"yes",2:"no",:"other")
yes
USER>w $case(2,1:"yes",2:"no",:"other")
no
USER>w $case(3,1:"yes",2:"no",:"other")
other
  • for循环
USER>for x=1:1:5{w "output==>"_x,!}
output==>1
output==>2
output==>3
output==>4
output==>5

c, 常用函数

  • 随机数
USER>set num=$R(10)
USER>w num
6
USER>w $R(2)
1
USER>w $R(2)
0
  • 时间函数,字符串截取

%SYS>w $H  //date, time 秒计
65352,35669

%SYS>w $p("a b c"," ",1)
a
%SYS>w $p("a b c"," ",2)
b
%SYS>w $p($H,",",1)
65352

%SYS>w $zd($p($H,",",1))
12/05/2019
%SYS>w $zdt($p($h,",",1),1)
12/05/2019
%SYS>w $zdt($p($h,",",1),3)
2019-12-05

2, 类与对象

  • 在studio中创建一个类:
    Demo命名空间–>new cache class (默认继承%Persistent类): 写入属性 -->编译
Class com.eyeof.Test1 Extends %Persistent
{
	Property age As %Integer;
	Property name As %String;
	
	//普通方法
	Method f1()
	{
		w "hello world, Method f1 "
	}
	ClassMethod f2()
	{
		w "hello world, ClassMethod f1 "
	}

	//方法传参
	Method info(salary1 As %Integer, profile1 As %Integer, name, state As %String = "CA")
	{
		w name_"/"_state_"/"_"total salary="_(salary1+profile1)
	}
	
	//方法返回值
	ClassMethod returnObj(name As %String, age As %Integer) As Test1
	{
	 	 Set person = ##class(Test1).%New()
	     set person.name= name
	     set person.age=age 
		 Quit person
	}
	//以下是编译后,生成的代码段
	Storage Default
	{
	<Data name="Test1DefaultData">
	<Value name="1">
	<Value>%%CLASSNAME</Value>
	</Value>
	<Value name="2">
	<Value>count1</Value>
	</Value>
	<Value name="3">
	<Value>count2</Value>
	</Value>
	<Value name="4">
	<Value>age</Value>
	</Value>
	<Value name="5">
	<Value>name</Value>
	</Value>
	</Data>
	<DataLocation>^com.eyeof.Test1D</DataLocation>
	<DefaultData>Test1DefaultData</DefaultData>
	<IdLocation>^com.eyeof.Test1D</IdLocation>
	<IndexLocation>^com.eyeof.Test1I</IndexLocation>
	<StreamLocation>^com.eyeof.Test1S</StreamLocation>
	<Type>%Library.CacheStorage</Type>
	}
}
  • 使用终端:创建对象,调用方法
#切换到:该代码所在的namespace中
Username: eyeof
Password: ******
USER>zn "t1"

T1>do ##class(com.eyeof.Test1).f2()
hello world, ClassMethod f1
 
T1>set t1=##class(com.eyeof.Test1).%New()
T1>d t1.f1()
hello world, Method f1

===============方法传参==========
T1>d t2.info(2000,500,"lisi")
lisi/CA/total salary=2500

T1>set salary=3000
T1>set profile=500
T1>set name="lisi"
T1>do t2.info(.salary,.profile,.name)
lisi/CA/total salary=3500

===============方法返回值==========
T1 2e1>set obj= ##class(com.eyeof.Test1).returnObj("a",23)
 
T1 2e1>zw obj
obj=<OBJECT REFERENCE>[[email protected]]
+----------------- general information ---------------
|      oref value: 5
|      class name: com.eyeof.Test1
| reference count: 2
+----------------- attribute values ------------------
|       %Concurrency = 1  <Set>
|                age = 23
|               name = "a"
+-----------------------------------------------------

3, 类和数据库表

  • 在studio 中创建类: 定义属性,方法
Class T1.Patient Extends (%Persistent, %XML.Adaptor, %Populate)
{
Property IDCard As %String;
Property PatNo As %String;
Property Name As %String;
Property Gender As %String;
Property DOB As %Date;
Property Citizenship As %String;
Property PhoneNumber As %String;

ClassMethod FindPatient(id As %String) As T1.Patient
{  Set patient= ##class(T1.Patien).%OpenId(id) 
   Quit patient
}

ClassMethod GetPatientsByName(name As %String) As %ListOfObjects
{
	set queryStr = "SELECT * FROM T1.Patient WHERE Name like ?"
	set nameParameter = "%"_name_"%"
	SET tStatement = ##class(%SQL.Statement).%New()
	SET qStatus = tStatement.%Prepare(queryStr)
	IF qStatus'=1 {WRITE "%Prepare failed:" DO $System.Status.DisplayError(qStatus) QUIT ""}
	
	#Dim rset As %SQL.StatementResult = tStatement.%Execute(nameParameter)
	if 0'=rset.SQLCODE{quit ""}
	Write "Counts: ", rset.%ROWCOUNT,! 
	
	#Dim rtnList as %Library.ListOfObjects = ##class(%Library.ListOfObjects).%New()
	WHILE rset.%Next() {
		#dim patInst As T1.Patient = ##class(T1.Patient).%New()
		set patInst.Name = rset.%Get("Name")
		set patInst.Gender = rset.%Get("Gender")
		set patInst.DOB = rset.%Get("DOB")
		do rtnList.Insert(patInst)
	}
	quit rtnList
}
}
  • 在terminal中创建对象,保存到数据库的表中
T1>set p = ##class(T1.Patient).%New()
T1>set p.PatNo = "234567"
T1>set p.Name = "Li Xunhuan"
T1>set p.Gender = "Male"
T1>set p.DOB = $zdh("1980-10-01",3)
T1>w p.%Save()
1
  • 在Potal中: 使用sql查看表数据

在这里插入图片描述

  • 在terminal 中:随机生成10条数据, 查询记录, 测试xml的用法(打印,保存)
T1>d ##class(T1.Patient).Populate(10)
 
T1>Do ##class(T1.Patient).XMLDTD()
<!ELEMENT Patient (IDCard?,PatNo?,Name?,Gender?,DOB?,Citizenship?,PhoneNumber?)>
<!ELEMENT IDCard (#PCDATA) >
<!ELEMENT PatNo (#PCDATA) >
<!ELEMENT Name (#PCDATA) >
<!ELEMENT Gender (#PCDATA) >
<!ELEMENT DOB (#PCDATA) >
<!ELEMENT Citizenship (#PCDATA) >
<!ELEMENT PhoneNumber (#PCDATA) >
 
T1>Do ##class(T1.Patient).XMLSchema()
<s:complexType name="Patient">
    <s:sequence>
        <s:element minOccurs="0" name="IDCard" type="s:string"/>
        <s:element minOccurs="0" name="PatNo" type="s:string"/>
        <s:element minOccurs="0" name="Name" type="s:string"/>
        <s:element minOccurs="0" name="Gender" type="s:string"/>
        <s:element minOccurs="0" name="DOB" type="s:date"/>
        <s:element minOccurs="0" name="Citizenship" type="s:string"/>
        <s:element minOccurs="0" name="PhoneNumber" type="s:string"/>
    </s:sequence>
</s:complexType>

T1>Set Patient=##class(T1.Patient).%OpenId(2)
T1>zw Patient
Patient=<OBJECT REFERENCE>[[email protected]]
+----------------- general information ---------------
| instance different from latest class version
|      oref value: 4
|      class name: T1.Patient
|           %%OID: $lb("2","T1.Patient")
| reference count: 3
+----------------- attribute values ------------------
|       %Concurrency = 1  <Set>
|        Citizenship = ""
|                DOB = 51043
|             Gender = "Male"
|             IDCard = ""
|               Name = "Li Xunhuan"
|              PatNo = 234567
|        PhoneNumber = ""
+-----------------------------------------------------
 
T1>Set XMLWriter=##class(%XML.Writer).%New()
T1>Do XMLWriter.OutputToFile("E:\Patient.XML")
T1>Do XMLWriter.RootObject(Patient)
T1>

T1>Set Patient=##class(T1.Patient).GetPatientsByName("Quixote,Bill O.")
Counts: 0
T1>zw Patient
Patient=<OBJECT REFERENCE>[10@%Library.ListOfObjects]
+----------------- general information ---------------
|      oref value: 10
|      class name: %Library.ListOfObjects
| reference count: 2
+----------------- attribute values ------------------
|            Data(1) = ""
|        ElementType = "%RegisteredObject"
|            Oref(1) = "[email protected]"
|               Size = 1  <Set>
+-----------------------------------------------------
发布了276 篇原创文章 · 获赞 37 · 访问量 11万+

猜你喜欢

转载自blog.csdn.net/eyeofeagle/article/details/103369066