Compiling "M" Files
[This content is no longer valid. For the latest information on "M", "Quadrant", SQL Server Modeling Services, and the Repository, see the Model Citizen blog.]
Use the Microsoft code name “M” Compiler to compile “M” source files. The “M” Compiler installs with the SQL Server Modeling CTP. It can be accessed from the Microsoft SQL Server Modeling CTP Command Prompt. This topic discusses basic “M” compilation concepts. For a complete reference of compiler commands, see M.exe Command Line Reference.
There are two main uses of the compiler:
Validate the syntax of your “M” code without producing any output files. This option returns only the errors and warnings for the target “M” source files.
Create an “M” image file that targets a SQL Server 2008 database. This image file can be loaded into the SQL Server Modeling Services database by using Mx.exe. For more information about Mx.exe, see Loading Image Files into SQL Server Modeling Services.
This topic reviews some of the most common “M” compiler options and recommendations for their use.
Note
The compiler also accepts image files to resolve external references in “M” code. For more information about external references and image files, see Loading Image Files into SQL Server Modeling Services.
Targets and Packages
The “M” compiler can produce or not produce output when it translates “M” models. The target option determines whether the compiler produces output and what kind of output it produces. The following table lists the possible target parameters.
Target value | Description |
---|---|
None |
Produces no output. |
TSql10 |
Compiles “M” models into SQL Server database objects without applying SQL Server Modeling Services design patterns. |
Use the None option to validate the “M” syntax of the target source files without producing a SQL Server translation of the code. The TSql10 option instructs the “M” compiler to translate the target “M” models into an equivalent data model for SQL Server 2008.
When a target value of TSql10 is specified, the package option determines the file type of the Transact-SQL output. The following table lists the possible package parameters.
Package value | Description |
---|---|
Script |
Produces a Transact-SQL file that has a file type of .sql. You can run this script in a database tool like SQL Server Management Studio. |
Image |
Produces an “M” image file that has a file type of .mx. You can load this image into the database by using the Mx.exe tool. |
An “M” image file contains metadata about the compiled “M” models, including information about how to load these models into SQL Server. You can reference types in image files from other “M” source files. These external references are resolved by specifying the image file that contains them during compilation. One of the most common uses of image files is to load the “M” model into a SQL Server database by using Mx.exe.
Examples
The following example shows how to use the target option to compile an “M” source file, MyModel.m
, into a Transact-SQL script. Note that the out option sends the output to the console.
m.exe MyModel.m -target:TSql10 -package:Script -out:-
The following example produces the same output, but the out option directs that output to a Transact-SQL script file, MyModel.sql
, in the current directory.
m.exe MyModel.m -target:TSql10 -package:Script -out:MyModel.sql
The following example produces the same output, but the out option directs that output to an “M” image file, MyModel.mx
, in the current directory.
m.exe MyModel.m -target:TSql10 -package:Image -out:MyModel.mx
SQL Server Translations of “M” Constructs
When you specify a target of TSql10
, the “M” model is converted to a SQL Server 2008 data model. The following table describes language constructs in “M” and their representation in SQL Server. This list is not exhaustive, but it provides a basis for understanding the compilation to SQL Server.
“M” construct | TSql10 target |
---|---|
module |
SQL Server schema. |
extent |
Table owned by the schema. |
type fields |
Table columns with equivalent SQL data types. |
identity |
Primary key. |
computed values |
Views or user-defined functions. |
referenced types |
Foreign keys. |
For more information, see "M" to SQL Mapping.
Example
The following “M” file describes a model for shapes with a Square
type and Squares
extent.
module Shape
{
type Square
{
ID : Integer32;
SideLength : Integer32;
} where identity(ID);
Squares : { Square* };
}
If you compile this with a TSql10
target, you get the following Transact-SQL translation that creates the equivalent model in SQL Server.
set xact_abort on;
go
begin transaction;
go
set ansi_nulls on;
go
if not exists
(
select *
from [sys].[schemas]
where [name] = N'Shape'
)
execute [sp_executesql] N'create schema [Shape]';
go
create table [Shape].[Squares]
(
[ID] int not null,
[SideLength] int not null,
constraint [PK_Squares] primary key clustered ([ID])
);
go
commit transaction;
go
Note
The Transact-SQL output in this example has been shortened to include only the statements directly relating to the model. The complete compiler output contains other tables that support “M” language features but are not directly related to the defined model.
In the preceding “M” source file and TSql10
compilation output, you can observe several SQL Server translations of the “M” code. The Shape
module becomes a Shape SQL Server schema. The Squares
extent becomes a Squares table that is owned by the Shape
schema. The Integer32 fields, ID
and SideLength
, become int columns in the Squares table. The identity clause in the “M” code becomes a primary key constraint on the ID field of the Squares database table.