Since you’ve probably seen this on every other blog in the world already, I’m not going to post that the language that was known as X# and Xen has now shipped as COmega. It’s not a commercial product, but available at Microsoft Research. (Well, I guess I did post about it then, didn’t I?).

I’ve heard so much about this language that I had to download it, install it and play with it. It installs the compiler, two tools, SQL2COmega and XSD2COmega, some samples, and Visual Studio template projects. The tools’ purpose is to create COmega classes to be used with the XML data and SQL Server data of your choice. Very cool. This language is at least partially (if not mostly) about mapping between data models (Classes<->XML and Classes<->relational databases), a theme I keeping coming back to.

In keeping with the concept of rite of passage programs in the last post, I had to write my “select * from authors” program. BTW, it is also required that you write these in the middle of the night, preferably after 2am. Yes, I know COmega comes with a Northwind Sample project, but it *has* to be authors, OK? I already noticed that COmega installs a set of Visual Studio templates and a “New Project” got me a new COmega console app. Let’s try that out…

You can run SQL2COmega from inside Visual Studio by clicking “add reference”. This creates a library and adds a reference to it in your program. First thing to notice is that you’re better off naming columns (not even sure if you can do “select * from…”) because the “anonymous structure” classes you use are strongly typed. The other oddity the “select” statement is part of the language, so the table must be DB.[TableName], the compiler is comiling this as well. Reminiscent of embedded SQL, if I say so myself.

Well, it works. Cool. I’ll leave it up to Mark to write “99 bottles of beer”. It should look amazingly similar to his XQuery version.

One wish for COmega is to have a .NET 2.0/Visual Studio 2005 B1 compatible version. It may run under .NET 2.0, but I haven’t tried this because the installer complains about not having Visual Studio 2003 installed. It isn’t installed on my “new technologies” partition. And just maybe… that it might be a “non-research project” some day.

Here’s the program. Next step (with any new database API, especially mapping technologies) is to run this process with SQL Profiler on. And, of course, to do this without Visual Studio, and figure out if its doing any “magic”. Didn’t seem to be any. And to reflect upon SQL2COmega and XSD2COmega to figure out how they’re doing mapping.

using System;
using System.Data.SqlTypes;  // for SqlString
using pubs;                  // for referenced library from SQL2COmega

public class Test {

    static void Main() {

      // au is an “anonymous structure”
      // select is a built-in language keyword
      // DB is a property of Database class
      // generated by SQL2COmega
      // NOT a database schema name
      struct{SqlString au_id; SqlString au_lname;}* au
          = select au_id, au_lname from DB.authors;

      foreach( row in au )
         Console.WriteLine(“{0} {1}”, row.au_id, row.au_lname);