Example VB Code - Teradata Meta Data Services

Teradata Meta Data Services Programmer Guide

Product
Teradata Meta Data Services
Release Number
15.00
Language
English (United States)
Last Update
2018-09-28
Product Category
Teradata Tools and Utilities

Example VB Code

The following code assumes you have used the References dialog from the Program menu item to select the "MetaCOM 2.1 Type Library" for your VB project. You could then use something similar to the following to set up a definition to your initial repository MetaActive class object:

Public Repos As METACOMEXPORT21Lib.MetaActive

Alternatively, you could use the following if you were assured of no name-space collisions:

Public Repos As MetaActive

In either case, you would follow this up with the creation of the object by

Set Repos = new MetaActive

There are still other alternatives to this, such as using the "CreateObject" call with the ProgID. See the Visual Basic documentation (or scripting reference if you are using VBScript of JScript) for complete information.

Also, see the sample\vb directory installed with the MDS SDK for more example code.

Initialization

This section shows example code to connect an application to the MDS repository.

'/////////////////////////////////////////////////////////////////////
'
'     Initialize Repository
'
'     Sets up the connection to the repository using the global object
'     Repository
'
'/////////////////////////////////////////////////////////////////////
Function InitializeRepository () As Boolean
    Set Repos = New MetaActive
 
    Repos.Initialize userName, password
 
    InitializeRepository = True
    Exit Function
End Function

Writing MDS Objects

This section shows example code to write an object.

'///////////////////////////////////////////////////////////////////
'
'   WriteObject – creates an object
'
'///////////////////////////////////////////////////////////////////
Function WriteObject(classID As Long, objectName As String) As Long
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' Add the object of objectName to the class classID -- we have
    ' hard-coded the following 9 properties to match those created
    ' above for Class1 as an example.
    ' (All values are completely arbitrary.)
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 
On Error GoTo errWrite
 
    Dim value As New MetaPropertyItem
    Dim info As New MetaInfo
 
    value.PropertyID = 501 ' vtString/sqlVarchar
    value.value = "This is a string value sent to the database"
    info.PropertyItems.Add value
 
    value.PropertyID = 502 ' vtLong/sqlInteger (4 bytes)
    value.value = 12345678
    info.PropertyItems.Add value
 
    value.PropertyID = 503 ' vtInteger/sqlSmallInt (2 bytes)
    value.value = 32000
    info.PropertyItems.Add value
 
    Dim helper As MetaHelper
    Set helper = New MetaHelper
    value.PropertyID = 504 ' vtBinary/sqlBinary (4 hex bytes)
    value.value = helper.StringHexToBinary("CD1F9A2B")
    info.PropertyItems.Add value
    Set helper = Nothing
 
    value.PropertyID = 5 ' vtString/sqlChar
    value.StringLiteral = "date" ' sending the Teradata "date" keyword
    info.PropertyItems.Add value
 
    value.PropertyID = 506 ' vtDouble/sqlDecimal (4 size; 1 digit)
    'value.value = -36.59 ' displays as -36.6
    'value.value = -36.8 ' displays as -36.8
    value.value = 36.8 ' displays 36.8
    'value.value = 3666.8 ' gets META_E_DB_ACCESS_ERROR
    'value.value = 366.58 ' displays 366.6
    info.PropertyItems.Add value
 
    value.PropertyID = 507 ' vtByte/sqlTinyInt (1 byte)
    value.value = 127
    info.PropertyItems.Add value
 
    value.PropertyID = 508 ' vtString/sqlDate (Teradata Date)
    value.value = "2001-07-20"
    info.PropertyItems.Add value
 
    value.PropertyID = 509 ' vtDate/sqlDouble (Variant Date)
    value.value = Date ' The Date is a double value.
    info.PropertyItems.Add value
 
    info.classID = classID
    info.Description = objectName
    info.objectName = objectName & " Description"
 
    '
    ' Finally, write this object out to the Reposiroty
    '
    Repos.WriteObject info
 
    exitWrite:
        WriteObject = info.ObjectID
        Set value = Nothing
        Set info = Nothing
 
        Exit Function
 
    errWrite:
        MsgBox "COM call FAILED: " & Err.Description & _
                                            " [0x" & Hex(Err.Number) & "]"
    GoTo exitWrite
End Function
 

Adding Many Objects to a Collection

This section shows example code of adding multiple objects to a collection.

'///////////////////////////////////////////////////////////////////
'
'   AddManyToCollection
'
'///////////////////////////////////////////////////////////////////
Sub AddManyToCollection()
 
    Dim keyList As New MetaInfoKeyList
    Dim testList As New MetaInfoKeyList
    Dim key As New MetaInfoKey
    Dim testKey As MetaInfoKey
 
    DisplayHeadingLine "MetaActive::AddManyToCollection"
 
    'add to relationship using relationship GUID
    key.ObjectID = Class1Obj1
    keyList.Add key
    key.ObjectID = Class1Obj2
    keyList.Add key
    Repos.AddManyToCollection DESTINATION, Class0Obj1, _
               RelGUID_Class0Class1, 0, keyList
 
    'add to relationship using relationshipID
    keyList.Clear
    Set testList = Nothing
    key.ObjectID = Class1Obj3
    keyList.Add key
    key.ObjectID = Class1Obj4
    keyList.Add key
    Repos.AddManyToCollection DESTINATION, Class0Obj2, "", _
               RelID_Class0Class1, keyList
 
    ' add to origin relationship
    keyList.Clear
    Set testList = Nothing
    key.ObjectID = Class0Obj3
    keyList.Add key
 
    Repos.AddManyToCollection ORIGIN, Class1Obj5, RelGUID_Class0Class1, _
               0, keyList
End Sub
 

Getting Objects in a Collection

This section shows example code of reading objects in a collection.

'//////////////////////////////////////////////////////////////////
'
'   GetCollections
'
'//////////////////////////////////////////////////////////////////
Sub GetCollections()
 
On Error GoTo errCollections
 
    Dim infoList As MetaInfoList
 
    'get destination objects using the relationshipID
    Set infoList = Repos.GetCollections(DESTINATION, Class0Obj1, _
            "", RelID_Class0Class1)
 
    Set infoList = Nothing
 
    ' get destination objects via relationship GUID
    Set infoList = Repos.GetCollections(DESTINATION, Class0Obj1, _
            "", RelGUID_Class0Class1, 0)
 
    Set infoList = Nothing
 
    'get destination object(s) via name
    Set testList = Repos.GetCollections(DESTINATION, Class0Obj1, _
            "", RelID_Class0Class1, "Entry Six")
 
    Set infoList = Nothing
 
    Set testList = Repos.GetCollections(DESTINATION, Class0Obj2, _
        "", RelID_Class0Class1, "Entry%")
 
exitCollection:
    Set infoList = Nothing
 
    Exit Sub
 
errCollections:
    MsgBox "COM call FAILED: " & Err.Description & _
                                        " [0x" & Hex(Err.Number) & "]"
    GoTo exitCollection
End Sub
 
'///////////////////////////////////////////////////////////////////
'
'   GetCollectionsByProperty
'
'///////////////////////////////////////////////////////////////////
Function GetCollectionsByProperty (name As String, _
                                   value As Variant, _
                                   ObjectID As Long, _
                                   relID As Long, _
                                   relGUID As String) As Boolean
 
    Dim keyList As New MetaInfoKeyList
    Dim key As New MetaInfoKey
    Dim testList As MetaInfoList
    Dim filterList As New MetaFilterList
    Dim sortList As New MetaInfoKeyList
    Dim filter As New MetaFilter
    Dim val As Boolean
 
    filter.filter.name = name
    filter.filter.value = value
    filter.Logical = META_AND
    filter.operator = EQUAL
    filterList.Add filter
 
    Set testList = Repos.GetCollectionsByProperty(DESTINATION, ObjectID, _
                   relGUID, relID, "", filterList, sortList)
 
    Set filterList = Nothing
    filter.operator = LESS_THAN
    filterList.Add filter
 
    Set testList = Repos.GetCollectionsByProperty(DESTINATION, ObjectID, _
                   relGUID, relID, "", filterList, sortList)
 
    Set filterList = Nothing
    filter.operator = GREATER_THAN_OR_EQUAL
    filterList.Add filter
 
    Set testList = Repos.GetCollectionsByProperty(DESTINATION, ObjectID, _
                   relGUID, relID, "", filterList, sortList)
 
    ' now do two values separated by OR
    Set filterList = Nothing
    Set testList = Nothing
    filter.operator = LESS_THAN
    filter.Logical = META_OR
    filterList.Add filter
    filter.operator = GREATER_THAN
    filterList.Add filter
 
    Set testList = Repos.GetCollectionsByProperty(DESTINATION, ObjectID, _
                   relGUID, relID, "", filterList, sortList)
 
    GetCollectionsByProperty = True
 
   Exit Function
End Function
 

Getting Objects of a Class

This section shows example code of reading objects of a class.

'///////////////////////////////////////////////////////////////////
'
'   GetClassObjects
'
'///////////////////////////////////////////////////////////////////
Sub GetClassObjects()
    Dim testList As MetaInfoList
    Dim info As MetaInfo
    Dim class1info As New MetaClassInfo
    Dim class2info As New MetaClassInfo
 
    'get the definitions of the classes
    class1info.name = CANNED_CLASS_NAME1
    class1info.ObjectID = ScriptingClass1
    class1info.Read
 
    class2info.name = CANNED_CLASS_NAME2
    class2info.ObjectID = ScriptingClass2
    class2info.Read
 
    'get the class objects via GUID
    Set testList = Repos.GetClassObjects(class1info.ObjectGUID, 0)
    Set testList = Nothing
 
    'get class objects via classID
    Set testList = Repos.GetClassObjects("", ScriptingClass1)
    Set testList = Nothing
 
    'get class objects via object name
    Dim names(6) As String
    Dim i As Integer
 
    names(0) = "Entry One"
    names(1) = "Entry Two"
    names(2) = "Entry Three"
    names(3) = "Entry Four"
    names(4) = "Entry Five"
    For i = 0 To 4
        Set testList = Repos.GetClassObjects("", class1info.ObjectID, names(i))
    Next
    Set testList = Nothing
 
    Exit Sub
End Sub
 
'///////////////////////////////////////////////////////////////////
'
'   GetClassObjectsByProperty
'
'///////////////////////////////////////////////////////////////////
Sub GetClassObjectsByProperty()
 
    Dim i As Integer
    Dim goodCount As Integer
    Dim lastdate As Date
    Dim info As MetaInfo
    Dim class2info As New MetaClassInfo
    Dim errorExpected As Boolean
    Dim testList As MetaInfoList
    Dim filterList As New MetaFilterList
    Dim sortList As New MetaInfoKeyList
    Dim filter As New MetaFilter
    Dim sortKey As New MetaInfoKey
 
    'get the definitions of the class
    class2info.name = CANNED_CLASS_NAME1
    class2info.ObjectID = ScriptingClass1
    class2info.Read
 
    ' check multiple properties
    ' the condition used is: long >= 100 AND integer >= 100 AND date >= Jan 1, 1986
 
    filter.filter.name = LONG_PROPERTY_NAME
    filter.filter.value = 100
    filter.operator = GREATER_THAN_OR_EQUAL
    filter.Logical = META_AND
    filterList.Add filter
    filter.filter.name = INTEGER_PROPERTY_NAME
    filterList.Add filter
    filter.filter.name = DATE_PROPERTY_NAME
    filter.filter.value = CDate(#1/1/1986#)
    filterList.Add filter
 
    Set testList = Repos.GetClassObjectsByProperty("", ScriptingClass1, _
                                 "", filterList, sortList)
 
    filter.filter.name = LONG_PROPERTY_NAME
    filter.filter.value = 100
    filter.operator = GREATER_THAN_OR_EQUAL
    filter.Logical = META_AND
    filterList.Add filter
 
    Set testList = Repos.GetClassObjectsByProperty("", ScriptingClass1, _
                                 "E%e%", filterList, sortList)
 
    ' request sorted results
 
    filterList.Clear
    filter.filter.name = LONG_PROPERTY_NAME
    filter.filter.value = 100
    filter.operator = GREATER_THAN_OR_EQUAL
    filter.Logical = META_AND
    filterList.Add filter
    sortKey.name = STRING_PROPERTY_NAME
    sortList.Add sortKey
 
    Set testList = Repos.GetClassObjectsByProperty("", ScriptingClass1, _
                                 "", filterList, sortList)
 
    ' test fetching DIM objects
    filterList.Clear
    filter.filter.name = "SynchronizationLevel"
    filter.filter.value = 1
    filter.operator = GREATER_THAN_OR_EQUAL
    filter.Logical = META_AND
    filterList.Add filter
    sortList.Clear
 
    Set testList = Repos.GetClassObjectsByProperty("", TableClassID, _
                                 "", filterList, sortList)
 
    Exit Sub
End Sub
 

Replacing/Removing Objects from a Collection

This section shows example code of replacing and removing objects from a collection.

'///////////////////////////////////////////////////////////////////
'
'   TestReplaceCollection
'
'///////////////////////////////////////////////////////////////////
Sub TestReplaceCollection()
 
    Dim errorExpected As Boolean
    Dim keyList As New MetaInfoKeyList
    Dim testList As MetaInfoKeyList
    Dim key As New MetaInfoKey
    Dim info As MetaInfoKey
 
     key.ObjectID = Class1Obj1
    keyList.Add key
    key.ObjectID = Class1Obj2
    keyList.Add key
    key.ObjectID = Class1Obj3
    keyList.Add key
    key.ObjectID = Class1Obj4
    keyList.Add key
 
    Repos.ReplaceCollection DESTINATION, Class0Obj5, "", RelID_Class0Class1, _
                 keyList
    Exit Sub
End Sub
 
'///////////////////////////////////////////////////////////////////
'
' RemoveManyFromCollection
'
'///////////////////////////////////////////////////////////////////
Sub RemoveManyFromCollection()
 
    Dim keyList As New MetaInfoKeyList
    Dim testList As New MetaInfoKeyList
    Dim info As MetaInfoKey
    Dim key As New MetaInfoKey
 
    key.ObjectID = Class2Obj7
    keyList.Add key
    key.ObjectID = Class2Obj8
    keyList.Add key
 
    Repos.RemoveManyFromCollection DESTINATION, Class1Obj5, _
                           "", RelID_Class1Class2, keyList, 0
 
    Exit Sub
End Sub
 

Reading an Object

This section shows example code of reading an object.

'////////////////////////////////////////////////////////////////////////
'
'   ReadObject
'
'////////////////////////////////////////////////////////////////////////
Public Sub ReadObject (ObjectID As Long)
 
    Dim info As New MetaInfo
 
    info.ObjectID = ObjectID
    Repos.ReadObject info
 
End Sub