Terminal scripts can be used to run pre-designed commands on the terminal, like a batch file. You can write anything that can be executed on terminal, like for loop, if else and so on, inside Terminal scripts. In this article, I will show you how to call Terminal scripts, how to use parameters in Terminal scripts and how to avoid session disconnected when running Terminal scripts. If you have any information about how to use Terminal scripts or you have any feedback, please feel free to leave a comment.

4 6
2 3.1K

Some weeks ago, I was reading a book by Stephen Hawking and Leonard Mlodinow, The Grand Design. At a certain point, trying to define why do we exist? , why do we use the models we use in physics?, ...those kind of things you know... they pointed at the Game of Life example invented by the mathematician John Coward in 1970... Basically he wanted to show that a system with really basic fundamental laws (Physics) could evolve and "live" to become a more complex system (Chemistry) in which "something" (humans) could work out its own model and complex rules to explain its reality… the rules for this deterministic model that he exposed were so basic that I thought it could be funny to implement them in ObjectScript when I had some spare time... there are others implementations in JavaScript and other languages... but not in ObjectScript... and that had to be corrected!!… so here you are!

7 10
2 772

(Originally posted by @Ben Spead on June 25, 2014)

This code snippet generates a list of Ensemble Lookup Tables and Schema documents in the user's current namespace. Run the code by running the class method "test":


Class benspead.EnsTablesSchema
{
    classmethod test() {
        If ##class(%Dictionary.CompiledClass).%ExistsId("Ens.Util.LookupTableDocument") {
            // only supported in Ensemble 2012.1+
            Write !,!,"Exporting Ensemble Lookup Tables..."
            Set sc = $$$OK
            Set rs = ##class(%ResultSet).%New("Ens.Util.LookupTableDocument:List")
            Do rs.Execute()
            While rs.Next() {
                Set item=rs.Data("name")
                Write "document found: "_ item,!
            }
            Do rs.Close()
            Set rs=""
        }
        If ##class(%Dictionary.CompiledClass).%ExistsId("EnsLib.HL7.SchemaDocument") {
            Write !,!,"Exporting Ensemble HL7 Schemas..."
            Set sc = $$$OK
            Set rs = ##class(%ResultSet).%New("EnsLib.HL7.SchemaDocument:List")
            Do rs.Execute()
            While rs.Next() {
                Set item=rs.Data("name")
                Continue:$listfind($lb("2.1.HL7","2.2.HL7","2.3.HL7","2.4.HL7","2.5.HL7","2.6.HL7","2.7.HL7","2.3.1.HL7","2.5.1.HL7","2.7.1.HL7","ITK.HL7")
                                    ,item)
                Write "document found: "_ item,!
            }
            Do rs.Close()
            Set rs=""
        }
    }
}

Here's a link to the code on GitHub: https://github.com/intersystems-community/code-snippets/blob/master/src/...

1 3
0 773
Article
· Oct 26, 2018 1m read
Send an HTML Email

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

1 0
1 1K

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

1 0
0 251

*** archived ***

The question has come up several times and I saw mixed answers and no quick example

My personal preference is using CPIPE device as you get back exactly the output you will get at the command line interface of your OS .
The tricky thing is to stop reading in time.
The example just displays what you normally see in your console.
it becomes useful if you look for things that you can't get from any $system.whatever()

15 5
4 2.1K
Article
· Sep 27, 2018 2m read
Method to Create a Class

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 1
0 554
Article
· Sep 20, 2018 2m read
Get Day of the Week from Date

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)

2 1
1 882
Article
· Sep 13, 2018 1m read
Find a table given its name

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

2 6
0 700
Article
· Sep 6, 2018 1m read
Save a file using %Net.HttpRequest

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

3 0
1 911

(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("")
		}
	}
}

1 3
2 1.7K
Article
· Feb 16, 2018 2m read
Adopted Bitmap

The base class Bmap.Person defines persons within an organization distributed
by various countries. All records are indexed by (Country, PersonalId).
this structure doesn't allow use of bitmaps.

So a wrapper class Bmap.PersonQ around the data eliminates the top level of
the index (Country) and isolates the PersonalId (%Integer, MINVAL=1).
We are ready to use a Bitmap index.

A few performance figures on 300010 generated records.
You see that Relative Cost are sometimes quite misleading.

base

14 0
1 824

Some time ago I got a WRC case transferred where a customer asks for the availability of a raw DEFLATE compression/decompression function built-in Caché.

When we talk about DEFLATE we need to talk about Zlib as well, since Zlib is the de-facto standard free compression/decompression library developed in the mid-90s.

Zlib works on particular DEFLATE compression/decompression algorithm and the idea of encapsulation within a wrapper (gzip, zlib, etc.).
https://en.wikipedia.org/wiki/Zlib

9 6
1 2.5K

Sometimes, we need to copy part of the properties of an object into a different one.
The simplest thing would be to do the following:

Set obj1.FirstName = obj2.FirstName

Set obj1.SecondName = obj2.SecondName

What happens if the object contains a large number of properties? or we just want to extract an important group of data, and complement the information in another object?

Having the following classes:

4 6
0 1.4K

This article was written as an attempt to share the experience of installing the InterSystems Caché DBMS for production environment.
We all know that the development configuration of a DBMS is very different from real-life conditions.
As a rule, development is carried out in “hothouse conditions” with a bare minimum of security measures, but when we publish our project online, we must ensure its reliable and uninterrupted operation in a very aggressive environment.

7 2
5 1.6K
First-class functionwiki

In computer science, a programming language is said to have first-class functions if it treats functions as first-class citizens. This means the language supports passing functions as arguments to other functions, returning them as the values from other functions, and assigning them to variables or storing them in data structures. Some programming language theorists require support for anonymous functions (function literals) as well. In languages with first-class functions, the names of functions do not have any special status; they are treated like ordinary variables with a function type.

This post continues the article “Declarative development in Caché”.

[2, 3, 5, 7, 11, 13, 17].forEach(function(i) {
  console.log(i);
});

How to do something like this in Caché using COS?

Below are some exercises on this topic.

2 0
0 396