The primary functions of a DSP are to enable processing of DDL scripts into both Script DOM and Schema Model representations, and to enable the reverse function of reproducing scripts from the two model representations.
A Script DOM provides implementation that parses a DDL script into an object model that represents the script. The Script DOM also provides implementation to reproduce the original script from the model.
The following diagram shows how data flows through the Script DOM.
Data Flow through Script DOM
.png)
The Script DOM parser translates the script, stored in an unstructured text file, into an object that inherits the IScriptFragment interface. The script generator in Script DOM takes an object that inherits the IScriptFragment interface and produces the original script. In the database schema providers for SQL Server, which are included with Database Edition, IScriptFragment abstracts an Abstract Syntax Tree (AST) and a token stream.
Tokens provide an unstructured representation of a script. Each token in the collection has:
The token type (such as keyword or string literal)
The token string
The source file in which the token occurred
The offset within that file at which the token occurred
Abstract Syntax Trees (ASTs) provide a structured representation of the script. Each node in the AST represents a batch of statements, a statement, or a component of a statement (such as an expression). ASTs are used to analyze scripts after they have been parsed. ASTs are also used to programmatically build scripts.
The Schema Model representation is an interface-based object model that models a live database instance. Interfaces are arranged in a derivation hierarchy that includes a single abstract layer that is shared by all DSPs, together with any number of abstract layers targeting more-specific model details. For example, the ISql90Table interface inherits ISqlTable, which inherits the abstract layer interface ITable.
The Schema Model takes the IScriptFragment objects and translates them into Schema Model elements (and also performs the reverse conversion, from Schema Model Elements into IScriptFragment objects). The Schema Model is composed of some number of Model Composer implementations. Each implementation creates a model from some other resource in the system. For example, the ScriptModelComposer composes a model from the script files maintained in the database project.
The Schema Model infrastructure (ModelStore and its supporting classes) implements integrated Script DOM references directly from model elements. This enables modeling of objects, such as stored procedures, which contain arbitrary scripts.
A Schema Model consists of a collection of elements and annotations. Elements describe the artifact being modeled (such as tables, views, stored procedures, triggers, columns, and so on). Annotations are used to associate arbitrary data with the model. Annotations are used by the database project core components, and they can also be used by project features and feature extensions. Both elements and annotations can be named or anonymous.
Model Elements
Elements are composed of properties and relationships. Properties represent basic data such as integers, Boolean values, and strings, and they are used to capture the details of the model. Relationships represent named and typed connections between elements. For example, an ITable element maintains a relationship of type IColumn named “Columns” that associates the table with its columns.
There are three basic types of relationships:
Peer — represents a dependency by one element on another element in an arbitrary manner. In SQL Server, the relationship between a view and a table would best be modeled as a peer relationship.
Composing — represents one element that consists of other elements. In SQL Server, the relationship between a table and its columns is best modeled as a composing relationship. A key principle of a composing relationship is that the two elements are created at the same time, as a single action. The elements do not have a dependency order, for creation or removal. However, the dependency direction of parent to child is maintained by the model in order to enable associative dependencies. For example, if a column has a dependency on a particular type, the parent table’s dependency on its composing column means that the table also depends on the type.
Hierarchical — represents a hierarchy. Hierarchical relationships differ from composing relationships because the dependency direction is from child to parent (instead of the other way around). An example in SQL Server is the relationship between a schema and an owned object such as a table or a view. A table participates in a hierarchical relationship with its schema.
The following diagram shows the three different types of relationships that can be represented in the Schema Model.
Object Relationships in Schema Model
.png)
Each relationship in a model declares whether it is a multi-relationship or a single relationship. In all cases, an element can maintain an empty relationship. Models can be incomplete models of the real artifact.
Elements are interface-based in order to support the following features in a store implementation:
All model element classes implement the public IModelElement interface. Properties, relationships, and annotations can be accessed by using the ModelStore metadata API from this class. The interfaces, such as ITable, serve to provide access (bound at compile time) to properties and relationships in a simpler and more-maintainable manner.
Model Annotations
Like elements, annotations are composed of properties. Unlike elements, annotations do not participate in relationships. Instead, annotations are attached to elements or to the model store itself. Annotations are strongly typed, and a single instance of an annotation can be attached to multiple elements in addition to the model store. Model-attached annotations represent an annotation instance that is “global” to the model.