Global Script Functions (IGlobalScript Class)

The global functions for use in Global scripts are described below. If using these functions from a global script event, call them using the IGlobalScript class (e.g., IGlobalScript.AddLog).

AddLog
SendMail

GetRecordInfo
GetJSONRecordInfo
GetReceiversInUse

 

AddLog (String, enmLogMessageType)

Note: this function supersedes AddLog(str As String, eType As Integer)

Writes a log message in the DBMoto.log file.

VB.NET syntax

Public Shared Sub AddLog (str As String, eType As enmLogMessageType)

C# syntax

public static void AddLog(string str, enmLogMessageType eType)

Parameters

- str = message to log

- eType = enumerator enmLogMessageType with the possible values: Information = 0, Warning = 1, Error = 2

Example [VB.NET syntax]

AddLog ("The current record has been inserted", 0) 

Example [C# syntax]

AddLog("The current record has been inserted", 0)

AddLog (String, enmLogMessageType, Record, enmRecordImage)

Note: this function supersedes AddLog(String, Int, Record, enmRecordImage)

Writes a log message that includes the specified record information in the DBMoto.log file.

VB.NET Syntax

Public Shared Sub AddLog (str As String, eType As enmLogMessageType, 
			   record As DBMotoPublic.IRecord, eRecordImage As enmRecordImage)

C# Syntax

public static void AddLog(string str, enmLogMessageType  eType, DBMotoPublic.IRecord record, 
		           enmRecordImage eRecordImage)

Parameters

- str = message to log

- eType = enumerator enmLogMessageType with the possible values: Information = 0, Warning = 1, Error = 2

- record = IRecord object to be logged along with the message

- eRecordImage = enumerator enmRecordImage for type of record print required. The record image uses the Values After of the record if it comes from an INSERT operation, otherwise the Values Before. The parameter can have the value 1, 2 or 4:

1 = KeyValues - Print only the primary key columns values
2 = Image - Printthe whole image of the record
4 = LogInfo - Print the log information (Transaction ID, Transaction Timestamp)

Example [VB.NET syntax]

Public Overrides Sub Record_onAfterMapping(recSource As IRecord, recTarget As IRecord, 
					    ByRef AbortRecord As Boolean)
Dim S as String
AddLog("The current record has been inserted", 0, recSource, 4)
End Sub

SendMail (subject, body)

Sends an email using the SMTP settings defined in the Mail tab of the Replication Agent Options dialog.

VB.NET syntax

Public Shared Sub SendMail (sSubject As String, sMessageBody As String)

C# syntax

public static void SendMail(string sSubject, string sMessageBody)

Parameters

- sSubject = subject of the email

- sMessageBody = body of the email

Example [VB.NET syntax]

SendMail ("Message from Syniti DR", 
	   "An error occurred that requires intervention by the system administrator.")

Example [C# syntax]

SendMail("Message from Syniti DR", 
         "An error occurred that requires intervention by the system administrator.")

SendMail (subject, body, to)

Sends an email using the SMTP settings defined in the Mail tab of the Replication Agent Options dialog.

VB.NET syntax

Public Shared Sub SendMail (sSubject As String, sMessageBody As String, sRcptTo As String)

C# syntax

public static void SendMail(string sSubject, string sMessageBody, string sRcptTo)

Parameters

- sSubject = subject of the email

- sMessageBody = body of the email

- sRcptTo = the email address of the intended recipient

Example [VB.NET syntax]

SendMail ("Message from Syniti DR", 
          "An error occurred that requires intervention by the system administrator.", 
	   "somebody@company.com")

Example [C# syntax]

SendMail("Message from Syniti DR", 
	  "An error occurred that requires intervention by the system administrator.", 
	  "somebody@company.com")

SendMail (sSubject, sMessageBody, sSMTPServer, sSMTPPort, sRcptFrom, sRcptTo, bAuthentication, sUser, sPassword, bUseSSL)

Sends an email using the parameters defined when the function is called. When parameters are not passed, the Mail settings in the Replication Agent Options dialog will be used instead.

VB.NET syntax

Public Shared Sub SendMail (sSubject As String, sMessageBody As String, 
                            sSMTPServer As String, sRcptFrom As String, sRcptTo As String)

C# syntax

public static void SendMail(string sSubject, string sMessageBody, string sSMTPServer, 
		             string sRcptFrom, string sRcptTo)

Parameters

- sSubject = subject of the email

- sMessageBody = body of the email

- sSMTPServer = the SMTP server for outgoing mail

- sSMTPPort = the SMTP server port for outgoing mail

- sRcptFrom = the email address from which you want to send the message

- sRcptTo = the email address of the intended recipient.

- bAuthentication = True if authentication should be used, otherwise False

- sUser = If bAuthentication is True, provide a user ID.

- sPassword = f bAuthentication is True, provide a password

- bUseSSL = True if SSL (Secure Sockets Layer) for email encryption should be used, otherwise False.

Example [VB.NET syntax]

SendMail ("Message from Syniti DR", 
	   "An error occurred that requires intervention by the system administrator.", 
          "www.smtp.com", "DBMoto Notification Agent", "somebody@company.com")

Example [C# syntax]

 SendMail("Message from Syniti DR", 
	   "An error occurred that requires intervention by the system administrator.", 
          "www.smtp.com", "Syniti DR Notification Agent", "somebody@company.com")

GetRecordInfo(Record, enmRecordImage)

Returns a string containing formatted information about the record.

VB.NET syntax

Public Shared Function GetRecordInfo (record As DBMotoPublic.IRecord, eRecordImage As enmRecordImage) As String

C# syntax

public static string GetRecordInfo(DBMotoPublic.IRecord record, enmRecordImage eRecordImage)

Parameters

- record = IRecord object to be logged along with the message

- eRecordImage = enumerator enmRecordImage for type of record print required. The record image uses the Values After of the record if it comes from an INSERT operation, otherwise the Values Before. The parameter can have the value 1, 2 or 4:

1 = KeyValues - Print only the primary key columns values
2 = Image - Print the whole image of the record
4 = LogInfo - Print the log information (Transaction ID, Transaction Timestamp)

Example [VB.NET syntax]

Imports Microsoft.VisualBasic
Imports DBMotoPublic
Imports DBMotoScript
Imports DBRS.GlobalScript 
Namespace DBRS
    Public Class ReplicationScript : Inherits IReplicationScript 
	Public Overrides Sub Record_onAfterMapping(recSource As IRecord, recTarget As IRecord, 
                                                  ByRef AbortRecord As Boolean)
	   Dim S as String = GetRecordInfo(recSource, enmRecordImage.KeyValues)
          AddLog("The current record has been inserted: " + S, 0)
       End Sub
    End Class
End Namespace

Example [C# syntax]

using System;
using System.Data;
using DBMotoPublic;
using DBMotoScript;
namespace DBRS
{
    public class ReplicationScript : IReplicationScript
    {
       public override void Record_onAfterMapping(DBMotoPublic.IRecord recSource, 
				                   DBMotoPublic.IRecord recTarget, ref bool AbortRecord) 
       { 
           string = GlobalScript.GetRecordInfo(recSource, enmRecordImage.KeyValues); 
           GlobalScript.AddLog("The current record has been inserted: " + s, 0);
        }
    }
}

GetJSONRecordInfo

All four variants of this function below are helper functions that can be useful when mapping a generic structured or unstructured data into a JSON column type.

GetJSONRecordInfo(record,eRecordImage)

Returns a string in JSON format. For example:

{
"FIELD1" = "Value1",
"FIELD2" = "Value2",
"FIELD3" = "Value3"
}

Object names and values are picked up from the IRecord object (and use the fields based on the enmRecordImage parameter).

VB.NET syntax

Public Shared Function GetJSONRecordInfo(ByVal record As DBMotoPublic.IRecord, ByVal eRecordImage As enmRecordImage) As String 

C# syntax

public static string GetJSONRecordInfo(DBMotoPublic.IRecord record, enmRecordImage eRecordImage) 

GetJSONRecordInfo(args)

Returns a string in JSON format. For example:

{
 "FIELD1" = "Value1",
"FIELD2" = "Value2",
"FIELD3" = "Value3"
}

Object names and values are picked from a list of generic <propname1, propvalue1, propname2, propvalue2, ...>.

VB.NET syntax

Public Shared Function GetJSONRecordInfo(ParamArray ByVal args() As Object) As String 

C# syntax

public static string GetJSONRecordInfo(params object[] args)  

GetJSONRecordInfo(T value)

Returns the JSON format for a specific object of type T. This function can be used when the value passed is not a string value but something else (such as a DateTime, a GUID, etc.)

VB.NET syntax

Public Shared Function GetJSONRecordInfo(Of T)(ByVal value As T) As String

C# syntax

public static string GetJSONRecordInfo<T>(T value) 

GetJSONRecordInfo T (jsonString)

The inverse of the function above. Given a certain value in JSON string format, it parses back the string and returns the object (of type T) from which it was generated.

VB.NET syntax

Public Shared Function GetJSONRecordInfo(Of T)(ByVal jsonString As String) As T  

C# syntax

public static T GetJSONRecordInfo<T>(string jsonString)   

GetReceiversInUse

Returns a list of receivers in use by the replications in a Syniti DR connection as a ReceiversInfo object. The definition of the ReceiverInfo class is given below. For more information on using this function, see Writing a Script to Determine Receiver Use.

VB.NET syntax

Public Shared Function GetReceiversInUse (ConnectionName As String, IsSource As Boolean, 
				           OnlyActiveReplications As Boolean) As ReceiverInfo()

C# syntax

public static ReceiverInfo[] GetReceiversInUse(string ConnectionName, bool IsSource, bool OnlyActiveReplications)

Parameters

- ConnectionName = Name of the IBM Db2 for i connection for which to retrieve the receivers in use

- IsSource = True if ConnectionName refers to a source connection, false otherwise

- OnlyActiveReplications = If true, retrieve only receivers used by active (Enabled) replications, and skip the disabled ones

Example [VB.NET syntax]

Imports Microsoft.VisualBasic
Imports DBMotoPublic
Imports DBMotoScript
Imports DBRS.GlobalScript
Imports System.Data
Namespace DBRS
   Public Class ReplicationScript : Inherits IReplicationScript
	Public Overrides Sub LogReader_onBeforeMirroring(bSource as Boolean)
          Dim arrReceivers As ReceiverInfo() = GetReceiversInUse ("AS400", True, True)
	   Dim recInfo As ReceiverInfo
	   For Each recInfo in arrReceivers
		AddLog("Receiver in use: " + recInfo.JournalLibrary + "." + recInfo.JournalName  + 
                      "/" + recInfo.ReceiverLibrary + "." + recInfo.ReceiverName, 0)
	   Next
	End Sub
   End Class
End Namespace

Example [C# syntax]

using System;
using System.Data;
using DBMotoPublic;
using DBMotoScript;
namespace DBRS
{
   public class ReplicationScript : IReplicationScript
   {
	public override void LogReader_onBeforeMirroring(bool bSource)
	{
          ReceiverInfo[] arrReceivers = GlobalScript.GetReceiversInUse("AS400", true, true);
          foreach (ReceiverInfo recInfo in arrReceivers)
          {
		GlobalScript.AddLog("Receiver in use: " + recInfo.JournalLibrary + "." + recInfo.JournalName 
                                   + "/" + recInfo.ReceiverLibrary + "." + recInfo.ReceiverName, 0);
          }
        }
    }
}

Other

This function returns a value of type ReceiverInfo. The definition for the class is below.

Public Class ReceiverInfo
	Public JournalLibrary As String
	Public JournalName As String
	Public ReceiverLibrary As String
	Public ReceiverName As String
End Class

Related Topics
Replication Script Events

Handling Events for INSERT, UPDATE and DELETE Operations

Replication Script Properties

IRecord Interface

Writing a Global Script

Writing a Replication Script

Writing Scripts with Visual Basic .NET