MSDN Magazine - February 2009 - (Page 67) C:\>m.exe /t:none wixbits.m Microsoft (R) "Codename M" Compiler version 1.0.0925.0 Copyright (C) Microsoft Corporation. All rights reserved. C:\> Figure 2 SQL Generated by “M” create table [WixBits].[Packages]( [Id] int not null identity, [Comments] nvarchar(max) null, [Compressed] bit not null default 1, [Description] nvarchar(max) null, [InstallerVersion] int not null default 200, [Language] int not null default 1033, [Manufacturer] nvarchar(max) not null, [Product] int not null, constraint [PK_Packages] primary key clustered ([Id]), constraint [FK_Packages_Product_WixBits_Products] foreign key ([Product]) references [WixBits].[Products] ([Id]) ); create table [WixBits].[Packages_Keywords]( [_Id] bigint not null identity, [Packages_Id] int not null, [Item] nvarchar(max) not null, constraint [PK_Packages_Keywords] primary key clustered ([_Id]), constraint [FK_Packages_Keywords_Packages_Id_WixBits_Packages] foreign key ([Packages_Id]) references [WixBits].[Packages] ([Id]) on delete cascade ); If there are no errors reported by the “M” compiler, your code is clean. Notice here that in addition to passing a file with a .m extension to m.exe, I am also specifying a target (via the /t switch) of none. This means to just do the syntax checking. Without the target switch, the “M” compiler will attempt to use your program to generate a SQL script to create tables and insert values. SQL Generation If you try to generate SQL from the sample bit of WiX model data (after adding the closing double quote), you’ll get an error report like this one: C:\>m wixbits.m Microsoft (R) "Codename M" Compiler version 1.0.0925.0 Copyright (C) Microsoft Corporation. All rights reserved. C:\wixbits.m(16,9): error M2010: The referenced type 'Product' does not define an identity constraint. You can also get this error message if you choose M Mode | Reach SQL Preview from inside Intellipad. What’s happening is that the “M” compiler is trying to generate a SQL script that matches the types and values defined in the program. It does so by attempting to map top-level named values (called extents) to create table statements and to map the values themselves into insert statements. However, because “M” has all kinds of general-purpose data description uses, not everything in it maps to SQL constructs—for example, entity types without identity fields. It’s still valid “M,” but not valid for generating SQL. Generating SQL is the default action of the “M” compiler because a modern relational database is a fabulous environment for storing and manipulating data. It has all kinds of wonderful features such as security, replication, versioning, auditing, and on and on. If you make use of SQL Server for hosting your data, you get all of those features, plus a host of robustness and performance optimizations. To make your “M” types compatible with SQL Server, the mapping requires an identity column, which you can add using the following code: type Package { Id : Integer32 = AutoNumber(); } where identity Id; Figure 3 Mapping “M” Types to SQL insert into [WixBits].[Packages] ([Product], [Description], [Manufacturer]) values (@WixBits_Products_Id0, N'A really super pad for notes', @ WixBits_Products_Manufacturer0); declare @WixBits_Packages_Id0 bigint = @@identity; insert into [WixBits].[Packages_Keywords] ([Item], [Packages_Id]) values (N'Installer', @WixBits_Packages_Id0); insert into [WixBits].[Packages_Keywords] ([Item], [Packages_Id]) values (N'Super', @WixBits_Packages_Id0); insert into [WixBits].[Packages_Keywords] ([Item], [Packages_Id]) values (N'Notepad', @WixBits_Packages_Id0); insert into [WixBits].[Packages_Keywords] ([Item], [Packages_Id]) values (N'Sample', @WixBits_Packages_Id0); Collection fields—the Keywords field of type Text*—maps to another table to hold the zero or more items in that collection, with the corresponding foreign key constraint. Figure 3 illustrates how the values map. Packaging and Deployment As you can see, I am creating a field named Id and using the AutoNumber function to set a default value. Then, I add a constraint on the type using the where keyword that marks the Id field as the identity column. With this in place, I have enough data to get valid SQL for the type, as shown in Figure 2. Hopefully the SQL generated by the “M” compiler matches what you’d write yourself. Module names map to SQL schema names. Type names map to table names. AutoNumber maps to “identity” on the field, and the identity constraint maps to the primary key constraint on the field. Intrinsic “M” types such as Integer32 and Text map go corresponding SQL types such as int and nvarchar. msdnmagazine.com The default output of the “M” compiler is SQL, which you’ll likely want to push into a SQL Server 2008 instance. By default, that SQL is packaged as a single SQL script file: C:\>m.exe wixbits.m Microsoft (R) "Codename M" Compiler version 1.0.0925.0 Copyright (C) Microsoft Corporation. All rights reserved. C:\>dir wixbits.sql 11/22/2008 04:43 PM 2,545 wixbits.sql You are free to use this script file to populate your SQL database instance however you like, such as with sqlcmd.exe or SQL Server Management Studio. However, the SQL script has a lot less of the metadata than the original “M” source code. If you’d like to maintain that metadata—and there is value in doing so, as I will explain February 2009 67 http://www.msdnmagazine.com
Table of Contents Feed for the Digital Edition of MSDN Magazine - February 2009 MSDN Magazine - February 2009 Contents Toolbox CLR Inside Out Data Points Cutting Edge Patterns In Practice Best Practices .Net Interop "Oslo" Basics Patterns Silverlight Under The Table Foundations Windows With C++ .NET Matters Going Places { End Bracket } MSDN Magazine - February 2009 MSDN Magazine - February 2009 - (Page Splash1) MSDN Magazine - February 2009 - Contents (Page Cover1) MSDN Magazine - February 2009 - Contents (Page Cover2) MSDN Magazine - February 2009 - Contents (Page 1) MSDN Magazine - February 2009 - Contents (Page 2) MSDN Magazine - February 2009 - Contents (Page 3) MSDN Magazine - February 2009 - Contents (Page 4) MSDN Magazine - February 2009 - Contents (Page 5) MSDN Magazine - February 2009 - Contents (Page 6) MSDN Magazine - February 2009 - Contents (Page 7) MSDN Magazine - February 2009 - Contents (Page 8) MSDN Magazine - February 2009 - Contents (Page 9) MSDN Magazine - February 2009 - Contents (Page 10) MSDN Magazine - February 2009 - Toolbox (Page 11) MSDN Magazine - February 2009 - Toolbox (Page 12) MSDN Magazine - February 2009 - Toolbox (Page 13) MSDN Magazine - February 2009 - Toolbox (Page 14) MSDN Magazine - February 2009 - CLR Inside Out (Page 15) MSDN Magazine - February 2009 - CLR Inside Out (Page 16) MSDN Magazine - February 2009 - CLR Inside Out (Page 17) MSDN Magazine - February 2009 - CLR Inside Out (Page 18) MSDN Magazine - February 2009 - CLR Inside Out (Page 19) MSDN Magazine - February 2009 - CLR Inside Out (Page 20) MSDN Magazine - February 2009 - CLR Inside Out (Page 21) MSDN Magazine - February 2009 - CLR Inside Out (Page 22) MSDN Magazine - February 2009 - Data Points (Page 23) MSDN Magazine - February 2009 - Data Points (Page 24) MSDN Magazine - February 2009 - Data Points (Page 25) MSDN Magazine - February 2009 - Data Points (Page 26) MSDN Magazine - February 2009 - Data Points (Page 27) MSDN Magazine - February 2009 - Data Points (Page 28) MSDN Magazine - February 2009 - Data Points (Page 29) MSDN Magazine - February 2009 - Data Points (Page 30) MSDN Magazine - February 2009 - Cutting Edge (Page 31) MSDN Magazine - February 2009 - Cutting Edge (Page 32) MSDN Magazine - February 2009 - Cutting Edge (Page 33) MSDN Magazine - February 2009 - Cutting Edge (Page 34) MSDN Magazine - February 2009 - Cutting Edge (Page 35) MSDN Magazine - February 2009 - Cutting Edge (Page 36) MSDN Magazine - February 2009 - Cutting Edge (Page 37) MSDN Magazine - February 2009 - Cutting Edge (Page 38) MSDN Magazine - February 2009 - Patterns In Practice (Page 39) MSDN Magazine - February 2009 - Patterns In Practice (Page 40) MSDN Magazine - February 2009 - Patterns In Practice (Page 41) MSDN Magazine - February 2009 - Patterns In Practice (Page 42) MSDN Magazine - February 2009 - Patterns In Practice (Page 43) MSDN Magazine - February 2009 - Patterns In Practice (Page 44) MSDN Magazine - February 2009 - Patterns In Practice (Page 45) MSDN Magazine - February 2009 - Best Practices (Page 46) MSDN Magazine - February 2009 - Best Practices (Page 47) MSDN Magazine - February 2009 - Best Practices (Page 48) MSDN Magazine - February 2009 - Best Practices (Page 49) MSDN Magazine - February 2009 - Best Practices (Page 50) MSDN Magazine - February 2009 - Best Practices (Page 51) MSDN Magazine - February 2009 - Best Practices (Page 52) MSDN Magazine - February 2009 - Best Practices (Page 53) MSDN Magazine - February 2009 - Best Practices (Page 54) MSDN Magazine - February 2009 - Best Practices (Page 55) MSDN Magazine - February 2009 - Best Practices (Page 56) MSDN Magazine - February 2009 - .Net Interop (Page 57) MSDN Magazine - February 2009 - .Net Interop (Page 58) MSDN Magazine - February 2009 - .Net Interop (Page 59) MSDN Magazine - February 2009 - .Net Interop (Page 60) MSDN Magazine - February 2009 - .Net Interop (Page 61) MSDN Magazine - February 2009 - .Net Interop (Page 62) MSDN Magazine - February 2009 - "Oslo" Basics (Page 63) MSDN Magazine - February 2009 - "Oslo" Basics (Page 64) MSDN Magazine - February 2009 - "Oslo" Basics (Page 65) MSDN Magazine - February 2009 - "Oslo" Basics (Page 66) MSDN Magazine - February 2009 - "Oslo" Basics (Page 67) MSDN Magazine - February 2009 - "Oslo" Basics (Page 68) MSDN Magazine - February 2009 - "Oslo" Basics (Page 69) MSDN Magazine - February 2009 - "Oslo" Basics (Page 70) MSDN Magazine - February 2009 - "Oslo" Basics (Page 71) MSDN Magazine - February 2009 - Patterns (Page 72) MSDN Magazine - February 2009 - Patterns (Page 73) MSDN Magazine - February 2009 - Patterns (Page 74) MSDN Magazine - February 2009 - Patterns (Page 75) MSDN Magazine - February 2009 - Patterns (Page 76) MSDN Magazine - February 2009 - Patterns (Page 77) MSDN Magazine - February 2009 - Patterns (Page 78) MSDN Magazine - February 2009 - Patterns (Page 79) MSDN Magazine - February 2009 - Patterns (Page 80) MSDN Magazine - February 2009 - Patterns (Page 81) MSDN Magazine - February 2009 - Patterns (Page 82) MSDN Magazine - February 2009 - Patterns (Page 83) MSDN Magazine - February 2009 - Silverlight (Page 84) MSDN Magazine - February 2009 - Silverlight (Page 85) MSDN Magazine - February 2009 - Silverlight (Page 86) MSDN Magazine - February 2009 - Silverlight (Page 87) MSDN Magazine - February 2009 - Silverlight (Page 88) MSDN Magazine - February 2009 - Silverlight (Page 89) MSDN Magazine - February 2009 - Silverlight (Page 90) MSDN Magazine - February 2009 - Silverlight (Page 91) MSDN Magazine - February 2009 - Silverlight (Page 92) MSDN Magazine - February 2009 - Silverlight (Page 93) MSDN Magazine - February 2009 - Silverlight (Page 94) MSDN Magazine - February 2009 - Under The Table (Page 95) MSDN Magazine - February 2009 - Under The Table (Page 96) MSDN Magazine - February 2009 - Under The Table (Page 97) MSDN Magazine - February 2009 - Under The Table (Page 98) MSDN Magazine - February 2009 - Under The Table (Page 99) MSDN Magazine - February 2009 - Under The Table (Page 100) MSDN Magazine - February 2009 - Foundations (Page 101) MSDN Magazine - February 2009 - Foundations (Page 102) MSDN Magazine - February 2009 - Foundations (Page 103) MSDN Magazine - February 2009 - Foundations (Page 104) MSDN Magazine - February 2009 - Foundations (Page 105) MSDN Magazine - February 2009 - Foundations (Page 106) MSDN Magazine - February 2009 - Windows With C++ (Page 107) MSDN Magazine - February 2009 - Windows With C++ (Page 108) MSDN Magazine - February 2009 - Windows With C++ (Page 109) MSDN Magazine - February 2009 - Windows With C++ (Page 110) MSDN Magazine - February 2009 - .NET Matters (Page 111) MSDN Magazine - February 2009 - .NET Matters (Page 112) MSDN Magazine - February 2009 - .NET Matters (Page 113) MSDN Magazine - February 2009 - .NET Matters (Page 114) MSDN Magazine - February 2009 - Going Places (Page 115) MSDN Magazine - February 2009 - Going Places (Page 116) MSDN Magazine - February 2009 - Going Places (Page 117) MSDN Magazine - February 2009 - Going Places (Page 118) MSDN Magazine - February 2009 - Going Places (Page 119) MSDN Magazine - February 2009 - { End Bracket } (Page 120) MSDN Magazine - February 2009 - { End Bracket } (Page Cover3) MSDN Magazine - February 2009 - { End Bracket } (Page Cover4)
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.