The Art of Mapping Globals to Classes 1 of 3
Tags: mapping, sql, tutorial, globals
Published: 2021-07-24 16:44:12

<p style="text-align: center;">
  <span style="font-size:26px;">The Art of Mapping Globals to Classes 1 of 3</span>
</p>

<span style="font-size:20px;">Looking to breathe new life into an old MUMPS application?  Follow these steps to map your existing globals to classes and expose all that beautiful data to Objects and SQL.</span>

By following the simple steps in this article and the next two you will be able to map all but the craziest globals to Caché classes.  For the crazy ones I will put up a zip file of different mappings I have collected over the years.  This is NOT for new data; if you don’t already have existing global please just use the default storage.

If you still can’t make heads or tails of your globals, send some example data to [Support@InterSystems.com](mailto:Support@InterSysetms.com) and we will be happy to help you figure it out.

Steps for mapping a global to a class:

  1. Identify a repeating pattern in the global data.
  2. Identify what makes up a unique key.
  3. Identify the properties and their types.
  4. Define the properties in the class (don’t forget the properties from the variable subscripts).
  5. Defined the IdKey index.
  6. Define the Storage Definition: <ol style="list-style-type:lower-alpha;">
      <li>
        Define the Subscripts up to and including the IdKey.
      </li>
      <li>
        Define the Data section.
      </li>
      <li>
        Ignore the Row ID section.  99% of the time the default is what you want so let the system fill that in.
      </li>
    </ol>

  7. Compile and test your class / table.

On to the example!

Say you have 2 globals that look something like this:

^mapping("Simple",1)="Brendan Bannon^55192^SQL Manager"

^mapping("Simple",2)="Nicole Aaron^63375^Support Specialist"

^mapping("Simple",3)="Kyle Baxter^61155^Senior Support Specialist"

^mapping("Simple",4)="Prasad Kari^58471^Support Specialist"

^mapping("Simple",5)="Clive Morgan^57982^Senior Support Specialist"

^index("Simple","HireDate",55192,1)=""

^index("Simple","HireDate",57982,5)=""

^index("Simple","HireDate",58471,4)=""

^index("Simple","HireDate",61155,3)=""

^index("Simple","HireDate",63375,2)=""

Let's go through the 7 steps to create a class that will let use look at this data via SQL and Objects.

Step 1

<p style="margin-left:.25in;">
  The ^mapping global is about as simple as it gets.  Each node looks like it contains the same type of data.  We will deal with the ^index global after we finish with the ^mapping global.
</p>

Step 2

<p style="margin-left:.25in;">
  The first subscript is just a constant. The second subscript is an incremented counter that looks to be unique for each row of data.
</p>

Step 3

<p style="margin-left:.25in;">
  Looks like the properties could be:  Name, HireDate, Title.  Without looking at the ^index global or having someone that knows how the data is used it would be hard to know that 55192 is a date, let alone a hire date.  Don’t forget to define a property for the value in the second subscript.
</p>

Step 4

<p style="margin-left:.25in;">
  Property Name As %String;
</p>

<p style="margin-left:.25in;">
  Property HireDate As %Date;
</p>

<p style="margin-left:.25in;">
  Property Title As %String;
</p>

<p style="margin-left:.25in;">
  Property Sub2 As %Integer;
</p>

Step 5

<p style="margin-left:.25in;">
  You must define an IdKey index for every class that uses Caché SQL Storage
</p>

<p style="margin-left:.25in;">
       Index Master On Sub2 [ IdKey ];
</p>

<p style="margin-left:.25in;">
  It is not required to define an index for the index maps, but it is a good idea.
</p>

<p style="margin-left:.25in;">
       Index  hireDateindex On HireDate;
</p>

  
  
Step 6

<p style="margin-left:.25in;">
  Now we need to create the mapping between the class and the global.  I like the wizard so I will show the screen shots for that, but if you are good at typing xml you can define the Caché SQL Storage definition manually.
</p>

<p style="margin-left:.25in;">
  Click on the Storage icon.  You can call the Storage Definition anything you want, I went with the default.  Click on Caché SQL Storage and click finish.
</p>

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xylztr4b1bfgh2v0rpuj.png)

<p style="margin-left:.25in;">
  In the window below I changed the Map Name, but Map1 works as well.  The only thing you need to fill in is the Global Name.  Don’t forget the “^”.  Every persistent class needs to have one data map.  It can have multiple index maps.
</p>

<p style="margin-left:.25in;">
  Clicking OK will exit you from the wizard so stay away from that until you are all done.
</p>

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0f7w5jxf15ccw7mh60tk.png) 

Step 6a

<p style="margin-left:.25in;">
  I like to do Subscripts next.  In here we are going to define all the global subscripts up to and including the IdKey (the IdKey can be more than one subscript).  In the expression box you refer to a field by putting it in {}.  You can put in any valid COS expression here.  We are keeping it simple so we just have a constant a Subscript 1 and a field at Subscript 2.
</p>

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jxswxcc588mt046pj4j7.png)

Step 6b

<p style="margin-left:.25in;">
  For data we are describing any subscripts that come after the IdKey plus all the data to the right of the equal sign.  The Node column would be used for any additional subscripts (my next post will have an example of this).  Piece and Delimiter are describing the location of the properties in the global.  The default is to use the $PIECE command to parse the global data.
</p>

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s472az08m6xjeiiveot4.png)

Step 6c

<p style="margin-left:.25in;">
  Nothing to see here, I told you to leave it blank.
</p>

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r2fg2stkyw720k5czkzp.png)

Now repeat step 6 for the ^index global.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b0pdfydt14ss8ryonqx2.png) 

Step 6a

<p style="margin-left:.25in;">
  Four subscripts this time:  two constants, HireDate and the IdKey (Sub2).  Every Index map must be able to construct the IdKey.  Most of the time it will be in the subscripts, but it could be part of the data.
</p>

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cl22d72xj4i7b8v9htng.png)

Step 6b

<p style="margin-left:.25in;">
  There are no additional subscripts and no data so this one is blank
</p>

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ic7tl2lpxe0oyvxk4ix4.png) 

Step 6c

<p style="margin-left:.25in;">
  Still nothing to do here.
</p>

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/emj2znixfjknxua16350.png) 

Step 7

All that is left now is to compile the class and then try to query the table to make sure it correctly displays the data.

Compilation started on 08/15/2016 15:20:58 with qualifiers 'fck /checkuptodate=expandedonly'  
Compiling class Mapping.Example1  
Compiling table Mapping.Example1  
Compiling routine Mapping.Example1.1  
Compilation finished successfully in 0.270s.

SELECT Sub2, Name, HireDate, Title FROM Mapping.Example1

<p style="margin-left:.25in;">
  Sub2              Name                                    HireDate              Title
</p>

<p style="margin-left:.25in;">
  1                      Brendan Bannon              1992-02-10          SQL Manager
</p>

<p style="margin-left:.25in;">
  2                      Nicole Aaron                      2014-07-07          Support Specialist
</p>

<p style="margin-left:.25in;">
  3                      Kyle Baxter                         2008-06-08          Senior Support Specialist
</p>

<p style="margin-left:.25in;">
  4                      Prasad Kari                          2001-02-01          Support Specialist
</p>

<p style="margin-left:.25in;">
  5                      Clive Morgan                     1999-10-01          Senior Support Specialist
</p>

<p style="margin-left:.25in;">
   
</p>

<p style="margin-left:.25in;">
  And for those of you who like to type here is what the storage definition looks like in xml
</p>

<p style="margin-left:.25in;">
   
</p>

<p style="margin-left:.25in;">
  /// Every class can only have 1 active Storage Definition.<br />/// Each Storage Definition can have more than one map.<br />Storage NewStorage1<br />{<br /><br />&lt;SQLMap name="HireDateIndex"><br />&lt;ConditionalWithHostVars>&lt;/ConditionalWithHostVars><br />&lt;Global>^index&lt;/Global><br />&lt;Subscript name="1"><br />&lt;Expression>"Simple"&lt;/Expression><br />&lt;/Subscript><br />&lt;Subscript name="2"><br />&lt;Expression>"HireDate"&lt;/Expression><br />&lt;/Subscript><br />&lt;Subscript name="3"><br />&lt;Expression>{HireDate}&lt;/Expression><br />&lt;/Subscript><br />&lt;Subscript name="4"><br />&lt;Expression>{Sub2}&lt;/Expression><br />&lt;/Subscript><br />&lt;Type>index&lt;/Type><br />&lt;/SQLMap><br />&lt;SQLMap name="SimpleDataMap"><br />&lt;Data name="HireDate"><br />&lt;Delimiter>"^"&lt;/Delimiter><br />&lt;Piece>2&lt;/Piece><br />&lt;/Data><br />&lt;Data name="Name"><br />&lt;Delimiter>"^"&lt;/Delimiter><br />&lt;Piece>1&lt;/Piece><br />&lt;/Data><br />&lt;Data name="Title"><br />&lt;Delimiter>"^"&lt;/Delimiter><br />&lt;Piece>3&lt;/Piece><br />&lt;/Data><br />&lt;Global>^mapping&lt;/Global><br />&lt;Subscript name="1"><br />&lt;Expression>"Simple"&lt;/Expression><br />&lt;/Subscript><br />&lt;Subscript name="2"><br />&lt;Expression>{Sub2}&lt;/Expression><br />&lt;/Subscript><br />&lt;Type>data&lt;/Type><br />&lt;/SQLMap><br />&lt;StreamLocation>^Mapping.Example1S&lt;/StreamLocation><br />&lt;Type>%CacheSQLStorage&lt;/Type><br />}
</p>

<p style="margin-left:.25in;">
   
</p>

<p style="margin-left:.25in;">
  For those that don't want to type here is a file with the globals and class:  <a href="/sites/default/files/inline/files/mappingexample1.zip">mappingexample1.zip</a>
</p>

<p style="margin-left:.25in;">
   
</p>

<p style="margin-left:.25in;">
  If you want to learn more have a look at <a href="https://community.intersystems.com/post/art-mapping-globals-classes%C2%A0-2-3">Part 2</a>
</p>