#ObjectScript

14 Followers · 1.6K Posts

InterSystems ObjectScript is a scripting language to operate with data using any data model of InterSystems Data Platform (Objects, Relational, Key-Value, Document, Globals) and to develop business logic for serverside applications on InterSystems Data Platform.

Documentation.

Question brett morgan · Nov 13, 2018

Hi

Totally new to IRIS and Cache.

Trying to evaluate it and work out how we could use it.

As a standard application database. Object or relational etc. does not matter. 

Issue is ObjectScript.

So:

1) Can we develop, maintain and use an IRIS database and never use ObjectScript i.e. use only Java, Python, C++ interfaces etc. (exactly which one does not matter)? Would that make designing and using the IRIS database more prone to inefficiency and error?

4
0 1046
Question Ben Spead · Nov 14, 2018

I need to write a script to answer a couple of fairly simple questions:

1) What is the current routine DB (name and location) for this namespace?

2) What is the current data DB (name and location) for this namespace?

3) Is global ^ABC mapped to a different location than the default data DB?

Can anyone point me to some system APIs that would allow me to answer these questions?

Thanks!

Ben

2
0 605
Question Kevin Dunn · Nov 1, 2018

I'm trying to  interpret a ObjectScript pattern and I am stumped on the first part. I understand everything else but not sure what 1.A means. I know A stands for Alpha characters just not sure what the 1. means

3
0 628
Article Gevorg Arutiunian · Aug 22, 2018 1m read

(Originally posted on Intersystems CODE by @Eduard Lebedyuk, 10/12/15) The following code snippet outputs all filenames in the file path "dir" in the Cache/IRIS terminal. The class method "test" runs the code:


Class eduardlebedyuk.filenamesInDir Extends %RegisteredObject
{
	classmethod test() {
		// replace dir with file path you want
		set dir = "D:\directory" 
		set dir = ##class(%File).NormalizeDirectory(dir)
		set file=$ZSEARCH(dir_"*")
		while file'="" {
			write !,file
			set file=$ZSEARCH("")
		}
	}
}
3
2 2075
Article Gevorg Arutiunian · Oct 26, 2018 1m read

This code snippet contains the class method "test" which sends an HTML email. Change the literal strings in the method to customize the email's from address, to address, subject, and body:


Class objectscript.sendEmail Extends %RegisteredObject
{
	classmethod test() {
		set m=##class(%Net.MailMessage).%New()
		set m.From="user@company.com"
		 
		set m.IsHTML=1
		 
		do m.To.Insert("user@company.com")
		set m.Subject="Sent by IRIS mail"
		set m.Charset="iso-8859-1"
		do m.TextData.Write("<HTML><HEAD><TITLE></TITLE>"_$char(13,10))
		do m.TextData.Write("<META http-equiv=Content-Type content=""text/html; charset=iso-8859-2""></HEAD>"_$char(13,10))
		do m.TextData.Write("<BODY><FONT face=Arial size=2>Test <B>Test</B></FONT></BODY></HTML>")
		set s=##class(%Net.SMTP).%New()
		set s.smtpserver="mail.company.com"
		set status=s.Send(m)
	}
}

Here's a link to the code on GitHub

0
1 1280
Question Tiago Ribeiro · Oct 18, 2018

Hello all...

Is there two matching patterns in Caché, is there a difference in use one or the other?
Example:

USER>set a = "(1234)"

Using $match.

USER>w $match(a,"\([0-9]{4}\)")

USER>1

Using literal match (sorry if not correctly term expression)

USER>W a?1"("4n1")"

USER>1

What is different from using one or the other?

The 'a?1"("4n1")"' does this pattern exist only Caché.
Exists performance improvement with use pattern Caché?

2
0 661
Article Gevorg Arutiunian · Oct 18, 2018 1m read

This code snippet uses %ZEN.Auxiliary.jsonSQLProvider. The namespace and string of SQL can be edited for different situations. The class method "test" runs the code:


Class eduardlebedyuk.passQuestionParams
{
	classmethod test(pValue = 50) {
		s ns = $Namespace
	    zn "samples"
	    s tSQL = "SELECT ID, Name FROM Sample.Person WHERE Id > ?"
	    s tPR = ##class(%ZEN.Auxiliary.jsonSQLProvider).%New()
	    s tPR.sql = tSQL
	    s tPR.%Format = "tw"
	    s tPR.maxRows = 100
    s tParam = ##class(%ZEN.Auxiliary.parameter).%New()
    s tParam.value = pValue
    d tPR.parameters.SetAt(tParam,1)
  
    d tPR.%DrawJSON() 
    //d ##class(%ZEN.Auxiliary.jsonSQLProvider).%WriteJSONFromSQL(,,,,,tPR)  //same thing
    zn ns
}

}

(Originally posted to Intersystems CODE by @Eduard Lebedyuk, 5/13/15)

Here's a link to the code on GitHub

0
0 347
Question Sabarinathan M · Mar 13, 2018

Hi All,

I need some help with sending PDF files to printer using cache instead of using third party tools(Adobe, foxit reader, etc..).
Sometime i getting access issue with the exe files.

Can able to send text data to printer using below code.

Set Dev="|PRN|PrinterName"
OPEN Dev:(/DATATYPE="TEXT"):80
USE Dev Write "Test printing",!
CLOSE Dev


Any suggestions on sending PDF to printer?

3
0 1004
Question Tuan Minh Do · Oct 4, 2018

Hello,

I am a beginner with Object Script and I hope anyone can help me solving my questions.

1. Is there a way to time a loop in Object Script? 

2. My code leads to the following error, even though it executes the method.

Is there a way to ignore this specific error, so that I can repeat my code in a time loop over and over again without stopping? 

3
1 1067
Question Alexey Maslov · Sep 28, 2018

There are some classes in our code base that contain Methods only (no properties). I told my colleagues that converting them into the ClassMethods should improve performance as it would eliminate unnecessary OREF support at run-time. Some of them replied that it would be microseconds, so what is the reason to bother.

Is it possible to estimate the impact of OREF support of method calls at run-time? E.g., as a % of all CPU load. 

12
0 590
Article Gevorg Arutiunian · Sep 27, 2018 2m read

The following class method "test" contains code that can create a new class, create new properties for classes, or create new methods for classes. "test" runs all three of these processes once, and the code that performs each action is labelled by comments in the method:


ClassMethod test() As %Status
{
	set sc = $$$OK
// Create a class
set class = ##class(%ClassDefinition).%New("MyClass")
set class.Description = "This is my test class"_$c(13,10)_"testing %ClassDefinition"
set class.Super = "%Persistent"

// Create a property and add it
set property = ##class(%PropertyDefinition).%New("MyClass.MyProperty")
set property.Type = "%String"
set property.Description="This is a property"
set sc1 = class.Properties.Insert(property)
do:$$$ISERR(sc1) $system.Status.DisplayError(sc1)
set sc = $$$ADDSC(sc, sc1)

// Create a method and add it
set method = ##class(%MethodDefinition).%New("MyClass.MyMethod")
set method.ReturnType = "%Integer"
set method.FormalSpec = "x:%Integer,y:%Integer=10"
set method.Description = "Return product of x and y"
set method.CodeMode = "code"
set method.Code = " new result"_$c(13,10)_" set result=x*y"_$c(13,10)_" quit result"
set sc2 = class.Methods.Insert(method)
do:$$$ISERR(sc2) $system.Status.DisplayError(sc2)
set sc = $$$ADDSC(sc, sc2)

// Save the class definition
set sc3 = class.%Save()
do:$$$ISERR(sc3) $system.Status.DisplayError(sc3)
set sc = $$$ADDSC(sc, sc3)

return sc

}

Here's a link to the code on GitHub

1
2 773
Article Gevorg Arutiunian · Sep 13, 2018 1m read

The following code snippet includes a class method "test" that runs code to find a class based on the class's name. "test" takes one argument, which is the name of the table:


Class objectscript.findTable Extends %RegisteredObject
{
	classmethod test(name as %String="mytable")  
    {
			#Dim result as %ResultSet
			#Dim tName as %String
			#Dim contain as %Integer
		Set contain=0
		Set result = ##class(%ResultSet).%New("%Dictionary.ClassDefinition:Summary")
		Do result.Execute()

		While(result.Next()) 
		{
			Set tName=$get(result.Data("Name"))
			&sql(select position (:name in :tName) into :contain)
			Write:contain'=0 tName, " ... ", name, " (", contain,")", !
		}
	    Return $$$OK
 }

}

Here's a link to the code on GitHub

6
0 841
Article Gevorg Arutiunian · Sep 20, 2018 2m read

This code snippet determines the day of the week associated with a date. The class method "test" takes a date as a string in "mm/dd/yyyy" format, and returns an integer corresponding to a day of the week:


Class cartertiernan.getDayfromDate Extends %RegisteredObject
{
	classmethod test(date) as %Integer {
		//Set date = $ZDATE(date) //  Looks like: mm/dd/yyyy
    Set monthList = $LISTBUILD(0,3,3,6,1,4,6,2,5,0,3,5) // (Jan,Feb,Mar,Apr,...)
    Set centuryList = $LISTBUILD(6,4,2,0) // first two digits divisiable by 4, then subsequent centuries. EX (2000, 2100, 2200, 2300)
    Set dayList = $LISTBUILD("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday") // Index goes from 0-6
     
    Set day = $PIECE(date,"/",2) // get the day 
    Set monthVal = $LIST(monthList,($PIECE( date,"/",1 ))) // get the month value
    Set first2DigsYear = $PIECE( date,"/",3 ) \ 100 // get the last 2 digits of the year
    Set last2DigsYear = $PIECE( date,"/",3 ) # 100 // get the first 2 digits of the year
     
    // Used for DEBUG perpouses
    /*write !,"day: ",day
    write !,"Month: ",monthVal
    write !,"last2: ",last2DigsYear
    write !,"first2: ",first2DigsYear
    write !,"cen Val: ",$LIST(centuryList,(first2DigsYear # 4) + 1),!!*/
     
    // Look here for formula explination (its the "Basic method for mental calculation")
    // http://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week
    Set dayOfWeekVal = ( day + monthVal + last2DigsYear + (last2DigsYear\4) + $LIST(centuryList,(first2DigsYear # 4) + 1 ) ) # 7
 
    Quit dayOfWeekVal
}

}

Here's a link to the code on GitHub

(originally posted to CODE by Carter Tiernan, 6/18/14)

1
1 1165
Question Santhosh Gladson · Sep 20, 2018

Hello Experts,

I have a silly question of using InsertParam method and usage. I understand it is name value pair. I am using GET method to send a url and to get the details from server. URL has multiparameter

URL -  api/Identifier/Image/?Verify.EDD={EDD}&Verify.Ethnicity={ETHNICITY}

I have used the URL in a method and called that method directly in the business operation. I am using Insertparam method -

do httpRequest.InsertParam("EDD",..Data.Verify.EDD)
do httpRequest.InsertParam("ETHNICITY",..Data.Verify.Ethnicity)

where httpRequest is an object of %Net.HttpRequest class

1
0 610
Question Scott Roth · Sep 13, 2018

We have the need to write a function that can loop through say a field in an OBX segment within HL7 and compare it to a string passed. Is it possible to have the user enter the Operator ( >,<,=,<>) as a variable inside Cache object script? Does anyone have any examples they can share?

Thanks

Scott Roth

The Ohio State University Wexner Medical Center

8
0 647
Question Galena Teneva · Jul 5, 2018

Hi,
it is possible to convert a csv file to json file?

I want to stream json files and output the data as it comes in from the files. So my files are "csv" type and I want to convert these files to "json" type.


I can read csv file as follows:

ClassMethod ReadFile()
{
    set stream = ##class(%Stream.FileCharacter).%New()
    set sc = stream.LinkToFile("*.csv")
    do stream.Rewind()
    while'stream.AtEnd {
        set line=stream.ReadLine()
        write line,!
}
What should I add to convert it to "json" and read it again?

Thank you

7
1 1853
Question Stephen De Gabrielle · Sep 6, 2018

Hi,

I have a routing rule that calls some utility classmethods, but for some reason the compiled version insists on linking to a utility function in a different package.

The call to 'SendToEaling(HL7)' in  isn't compiling to a call to the LNWTIEPackage as expected:

##class(LNWTIEPackage.RoutingRules.Utility).SendToEaling((pContext.HL7))

but is instead becoming a call to the LNWDeploy package

##class(LNWDeploy.RoutingRules.Utility).SendToEaling((pContext.HL7))

Specifically 

<assign property="@SendToEaling" value="SendToEaling(HL7)"></assign>

 gets compiled to

5
1 477
Question Sylvie Greverend · Sep 6, 2018

I am trying to initialize a list of %string property to an empty list, and after add eleements.
d ##class(Test.Test).Test("hello") ; works perfectly
d ##class(Test.Test).Test("") ; does not work, the list stays empty

How can I do that. Thank you so much

1
0 646
Article Gevorg Arutiunian · Sep 6, 2018 1m read

This code snippet allows for a file on the web to be saved into the file system. Specify the server and GET request, as well as the directory the file should be saved to. The class method "test" runs the code:


Class objectscript.saveFileHTTP Extends %RegisteredObject
{
	classmethod test() {
		Set httprequest = ##class(%Net.HttpRequest).%New()
		Set httprequest.Server = "docs.intersystems.com"
		Do httprequest.Get("documentation/cache/20172/pdfs/GJSON.pdf")
    Do $System.OBJ.Dump(httprequest.HttpResponse)
	 
	Set stream=##class(%FileBinaryStream).%New()
	Set stream.Filename="c:\test.pdf"

	Write stream.CopyFrom(httprequest.HttpResponse.Data)
	Write stream.%Save()
}

}

Here's a link to the code on GitHub

0
1 1071