Partager via


Oslo 101 - Building your first 'Hello World' in "Oslo"

Folks, now that we have the "Oslo" May CTP (you can download them here), let’s get started on this exciting new platform end-to-end. You'd be surprised that all you need today is SQL 2008 Express or above (except SQL Server CE). Follow the instructions here (and keep the default names as is e.g. keep the Name of the Repository as "Repository", Target DB Server as ".", etc.). Once you're happy with the installation, fire up the "Intellipad" (which should be visible from your Start button otherwise find it under \program files\Microsoft Oslo\1.0\bin) - welcome to the new world! Let’s start building our first "Oslo" Model with the brand new "M" language (this is the code name that would most likely change when "Oslo" actually lands).

1) In our “Intellipad”, let’s create a module called "OsloWorld.Org" where we are modeling any organization (by the way, a module structure in “Oslo” is mandatory and is the basic container that contains a set of related types and scopes out in the same way as Namespace does in C#):

module OsloWorld.Org

{

}

Save this file as “OsloWorld” in your new folder C/:OsloWorld and choose the type as “M source file “*.m”). Note: as soon as you save the file, you’ll see a new Menu item pop up on your “Intellipad” called “M Mode”. Click on this and select T-SQL Preview. On your right you’ll eventually see a dynamically generated SQL that is mapped to this “M” sourcecode (WOW!). However at this time, you’ll not see much except “Unable to generate SQL..” as there is nothing to compute anyway.

2) Let’s now declare an extent called Employees that represent the OsloWorld. In the extent declaration , we are saying that Employees are made up of a collection of 0 or more Employee which shortly we’ll define in our type declaration

module OsloWorld.Org

{

                  Employees: Employee*;

}

You will see a compilation error on your T-SQL on the right pane as we have not yet declared what Employee type is.

3) Let’s now define the Employee type as follows:

module OsloWorld.Org

{

Employees: Employee*;

type Employee {

       EmpID: Integer32 => AutoNumber();

  } where identity (EmpID);

}

What we’ve done is added the type declaration that syas EmpID is of the type integer and will automatically be generated for us and this field also acts as a primary key. Take note that on the right pane, the dynamic T-SQL generates those constraints automatically!

4) Now we add a few more fields with more constraints (that are self-explanatory) that you can also see them appear in the right pane on the T-SQL

module OsloWorld.Org

{

Employees: Employee*;

type Employee {

EmpID: Integer32 => AutoNumber();

EmpName: Text where value.Count < 30;

EmpAge: Integer32 where value < 150;

Sex: Text where value.Count < 2;

} where identity (EmpID);

}

5) Let’s add one more field in the end which kind of connects the organizational structure i.e. have another field called ReportsTo. This field is of the type Employee but could be null. This extent is represented as ReportsTo: Employee? And lets add one more constraint to this that this Employee should belong to the Employees. So the entire module looks like:

module OsloWorld.Org

{

    Employees: Employee*;

    type Employee {

        EmpID: Integer32 => AutoNumber();

        EmpName: Text where value.Count < 30;

        EmpAge: Integer32 where value < 150;

        Sex: Text where value.Count < 2;

        ReportsTo: Employee? where value in Employees;

    } where identity (EmpID);

}

 

Let’s now compile our Model. Go to the command window and set the path to point to the bin dir of Oslo:

set PATH=%PATH%;"%PROGRAMFILES%\Microsoft Oslo\1.0\bin"

When we compile the “.m” model file with “m.exe”, we get the image file with extension “.mx”. This image file contains the generated SQL and related manifest & other metadata. So here is the command for the compilation:

m.exe OsloWorld.m /p:image

Once we’ve compiled the “.m” file into the image file – check the directory for OsloWorld.mx file that was output. All you need to do now is install this image onto the Repository with the following loader utility (mx.exe):

mx.exe install OsloWorld.mx -d Repository

Now that you’ve installed the image onto the repository, lets fireup the “Quadrant” to interact with the Models. Go to the Menu: View -> Explorer -> Repository. On the left pane, click to expand Catalog and then expand the OsloWorld.Org. You will see the Employees appear under the OsloWorld.Org. Drag and drop the Employees to the Design Pane. Click on the Employees workpad and then on the "Quadrant" Menu, click on Edit -> Insert Item. Enter the first row (Note that EmpID of 1 has already been populated for you by the autonumber). Enter an employee name, age, sex and leave the ReportsTo field as null). Click on the commit row. Your first Oslo Model has one instance stored in the Repository! You can enter a second row now and choose different names, etc. when you click on the ReportsTo field, you will see a small down arrow appear and choose the first employee as the ReportsTo person. Keep typing more employees till you are happy that a substantial hierarchy has been reached. Now on the Employees workpad, click on the Table -> Tree and check out the Tree view of the organization!