![]() Last edit: 2009-05-04 Graham Wideman |
ModelRight |
<< Getting started with ModelRight scripting |
Outline of sections below
Having covered some basic skills in operating ModelRight's programming environment, we move on to discussing what objects ModelRight exposes for your code to manipulate and how to access them.
In broad outline, ModelRight's Automation is much like Word and Excel: Your code starts with a reference to some top-level object, which provides a reference to collections of subsidiary objects, and each object has properties your code can read or write. You might therefore expect to proceed something like this:
'--- NOT QUITE how it works! --- Set ATable = AModel.Tables("SomeTable") Set AColumn = ATable.Columns("SpecialColumn") AFieldLen = AColumn.Length ... and so on... |
Here,
ModelRight's data structure, while indeed capturing data of this type, is actually arranged more generally. Instead of specifically named collections, object types and properties, ModelRight's tree of model data is assembled mostly from objects of class MRObject each with a collection of children of class MRObject. To each MRObject is attached a collection of objects of class MRProperty which hold each specific property value. In aggregate, these hold data about models, tables, columns, views indexes and so on, as determined by the MRObject.Type field of each MRObject.
The rationale for the generic MRObjects stems from different vendor's databases having different features, requiring a modeling tool to maintain different collections and properties to capture the model's data for each database type. Rather than create a different internal data structure and automation model for each database vendor, ModelRight uses these generic MRObjects, and assembles the appropriate collections of children and properties as needed according to vendor-specific metamodel data, which we will meet below.
We will see how this plays out shortly, but the main impact on programming is that the above snippet of code looks more like the following:
'--- Using MRObjects --- Set ATable = AModel.ChildByName("Table", "SomeTable") Set AColumn = ATable.ChildByName("Column", "SpecialColumn") AFieldLen = AColumn.Property("Datatype Length").AsInteger ... and so on... |
There are more variations for accessing children and properties, as can be seen in the ModelRight Object Reference. Also, writing to properties involves some additional steps, covered in a later section.
Combining the above-described classes with a couple of additional classes gives us the following sketch of a typical database model, as contained in ModelRight objects:
Things to note:
Set MyColumn = MyTable.AllChildren(1) |
Retrieves a single child MRObject, which might contain information about a column, an index, or possibly something else. |
Set MyColumns = MyTable.Children("Column") For each AColumn in MyColumns ' do something with AColumn Next | First retrieves a collection of child MRObjects of type "Column", then iterates through them. |
Set MyColumn = MyTable.ChildByName("Column", "PersonId") | Retrieves a single child MRObject whose Type = "Column" and having name "PersonId". |
S = Column.Property("Datatype").AsString | Obtains string telling the value of this property. In this example, the Datatype property reports the datatype of the column to which it's attached. |
Writing a property is a slightly more complex process. See later Programming Topics section.
To consolidate this knowledge, here is a sample procedure which navigates a ModelRight model, picking out some basic info:
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 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 statements involving Context and Model are often-used preliminary steps to gain initial access to the ModelRight programming interface. For more details on this see the ScriptContext and MRModel sections in the ModelRight Objects Reference.
The above script produces results like this (using the sakilla database model supplied with ModelRight MySQL):
Model: Model 1 ModelMRObject: Model 1 ------ Tables and columns ------- Table: actor actor.actor_id : SMALLINT actor.first_name : VARCHAR(45) actor.last_name : VARCHAR(45) actor.last_update : TIMESTAMP Table: address address.address_id : SMALLINT address.address : VARCHAR(50) address.address2 : VARCHAR(50) [...] |
Armed with the background just presented, let's summarize what areas of knowledge you will need in order to access ModelRight's data:
Document Status | ||
Date (reverse) | Who | Description |
2009-04-01 to 05-04 | GW | Revisions |
2008-07-10 | GW | Original |