![]() Last edit: 2009-05-04 Graham Wideman |
ModelRight |
<< Some ModelRight Programming Topics in more Detail |
The script samples on this page are written in VB Script language and are intended to run in ModelRight's VB Script environment. A second page of sample code shows how to manipulate ModelRight files using COM Automation from other languages.
Each of the samples makes use of the "sakilla" sample data model supplied with ModelRight (for MySQL). If you would like to follow allong with these samples, you may wish to start by making a copy of that model (just copy the sakilla.wer file) to a convenient location, and modify that with the sample scripts below.
The sample code is not particularly dedicated to that model, so you can start with whatever model you like.
The syntax, functions and features of the Visual Basic Script language (not specific to ModelRight) are as documented at Microsoft Developer Network: VBScript. In general it is very similar to Visual Basic (up to version 6, not VB.NET) and Visual Basic for Applications (as in Microsoft Word and Excel), though all variables are of "variant" type -- there is no use of the Dim statement.
Here is a script that demonstrates loops which visit several levels of the database model, printing some information about each object to the Document window.
Sub Evaluate_OnLoad Set Context = CreateObject("SCF.ScriptContext") Set Document = Context.ScriptDocument Set ThisScript = Context.Object Set Model = ThisScript.Model Document.WriteLine "Model: " & Model.Name & " " & Model.Id Set ModelMRObject = Model.AsObject Document.WriteLine "ModelMRObject: " & ModelMRObject.Name Set Tables = ModelMRObject.Children("Table") Document.WriteLine "------ Tables and columns -------" For Each Table In Tables Set TableNameProp = Table.Property("Name") TableName = TableNameProp.AsString Document.WriteLine Table.TypeName & ": " & Table.Name Set Columns = Table.Children("Column") For Each Column in Columns Document.WriteLine " " & TableName _ & "." & Column.Property("Name").AsString _ & " : " & Column.Property("Datatype").AsString Next Next end sub |
The above script produces results like this (using the sakilla database model supplied with ModelRight MySQL):
Model: Model 1 {F848A0DB-3725-460A-8C03-0DD0D5F9F2C0} ModelMRObject: Model 1 ------ Tables and columns ------- Table: actor actor.actor_id : SMALLINT actor.first_name : VARCHAR(45) actor.last_name : VARCHAR(45) actor.edit_by : VARCHAR(33) actor.edit_date : TIMESTAMP Table: address address.address_id : SMALLINT address.address : VARCHAR(50) [...] |
1. Gaining access to ModelRight's loaded data:
The first step is to gain access to ModelRight's data, which this code does by using the VBScript COM method CreateObject, and requesting a reference to the object called "SCF.ScriptContext". As you will see by referring to the Object Reference guide, ScriptContext provides references to Document and Model, which are both needed in this example.
2. The code then proceeds in a straightforward manner to obtain from Model the set of child MRObjects representing Tables, and then each of their children representing Columns, reading some properties and printing out some information along the way.
This sample code demonstrates modifying an existing database model. The scenario: we're revising some "housekeeping fields" in the model. The existing "last_update" field will be renamed to "edit_date", and we will add a new field "edit_by". To keep the example clearer, we'll just apply it to the one actor table. To apply it to all tables, use the loop strategy from the previous example.
Sub Evaluate_OnLoad Set Framework = CreateObject("SCF.ScriptFramework") Set Model = Framework.CurrentModel Set ModelMRObject = Model.AsObject Set Context = CreateObject("SCF.ScriptContext") Set Document = Context.ScriptDocument Document.WriteLine "--- start ---" Set Table = ModelMRObject.ChildByName("Table", "actor") '----------------------------------- ' Rename existing column '----------------------------------- OldColName = "last_update" Set Col = Table.ChildByName("Column", OldColName) if Col is Nothing then Document.Writeline "could not find " & OldColName exit sub end if NewColName = "edit_date" Model.BeginTransaction "Rename column to " & NewColName Set PropVal = Framework.CreatePropertyValue("Column", "Name") PropVal.FromString(NewColName) Col.SetProperty "Name", PropVal Model.EndTransaction '----------------------------------- ' Add new column '----------------------------------- ChildOrderEditDate = Table.ChildOrder(Col) NewColName = "edit_by" Model.BeginTransaction "Add column " & NewColName Set Col = Framework.CreateObject("Column", Table) Table.SetChildOrder Col, ChildOrderEditDate Set PropValue = Framework.CreatePropertyValue("Column", "Name") PropValue.FromString(NewColName) Col.SetProperty "Name", PropValue Col.SetDataType("VARCHAR") Set PropValue = Framework.CreatePropertyValue("Column", "Datatype Length") PropValue.FromInteger(33) Col.SetProperty "Datatype Length", PropValue Model.EndTransaction Document.WriteLine "--- done ---" end sub |
This sample code demonstrates the manipulation of the "graphics" part of the data, that is to say the visibility and style features of a particular diagram.
Scenario: To enhance conceptual understanding of the system, we want to alter the background color of tables to convey which subsystem they pertain to. Also, we want to reduce clutter by hiding housekeeping fields. To keep things simple, the sample code will just change the color of one table, and hide its "last_update" field.
Before and after:
Here's the code which demonstrates how to access the graphics part of the model.
Sub Evaluate_OnLoad Set Framework = CreateObject("SCF.ScriptFramework") Set Model = Framework.CurrentModel Set ModelMRObject = Model.AsObject Set Context = CreateObject("SCF.ScriptContext") Set Document = Context.ScriptDocument Document.WriteLine "--- start ---" Set ModelSubset = ModelMRObject.ChildByName("Model Subset", "Model Subset 1") Document.WriteLine "Model Subset: " & ModelSubset.Name Set Diagram = ModelSubset.ChildByName("Diagram", "Diagram 1") Document.WriteLine "Diagram: " & Diagram.Name Model.BeginTransaction "Fiddle with graphics" '------------------------------------------- ' Change table background gradient to red '------------------------------------------- Set TableGraphics = Diagram.ChildByName("Table Graphics", "film") Document.WriteLine "TableGraphics: " & TableGraphics.Name PropName = "Fill Blend To Color" Set PropValue = Framework.CreatePropertyValue("Table Graphics", PropName) PropValue.FromInteger &H8080FF TableGraphics.SetProperty PropName, PropValue '------------------------------------------- ' Hide housekeeping field '------------------------------------------- Set ColumnGraphics = TableGraphics.ChildByName("Column Graphics", "last_update") Document.WriteLine "ColumnGraphics: " & ColumnGraphics.Name PropName = "Not Displayed" Set PropValue = Framework.CreatePropertyValue("Column Graphics", PropName) PropValue.FromBoolean True ColumnGraphics.SetProperty PropName, PropValue Model.EndTransaction Document.WriteLine "--- done ---" end sub |
If you are interested in creating versions of your database model diagrams that have maximum impact for different audiences, it will be worthwhile examining the Model Subset branches in the Metamodel Browser to see how much control you can actually gain. Under control of scripts, you can lay out diagrams and style them completely following your own rules.
Document Status | ||
Date (reverse) | Who | Description |
2009-04-18 to 05-04 | GW | Revisions |