Writing a Global Script

A global script is a shared module that defines a global class of functions or defines event handlers for global events. It can be used to define general user functions that need to be called from inside a replication script or an expression script.

Global scripts can use or define the following sets of functions:

  • Microsoft C# Libraries
    Based on the scripting language selection in the Global Script Editor, you can use either C# or VB.NET to define scripts and functions for use in scripts.

  • Microsoft.VisualBasic Namespace Functions
    A .NET Framework Class Library that makes available Visual Basic 6 functions such as [Strings] Left, Mid, Right, Instr; [Math] Cos, Arc, Power and so on.

  • Global Functions
    Functions implemented as part of Syniti Data Replication.

  • Global Script Events
    Events that apply to all replications.

  • User Functions
    Functions that users define in the Global Script dialog typically for use from within Replication scripts.

  • Create Table Rules or Functions
    Functions used to customize how target tables are created.

  • Field Mapping Rules or Functions
    Functions used to determine how to map source and target columns when defining a replication.

Use the Global Script Editor to write functions which are then typically used in replication scripts and expressions. For example, you may have a conversion function that you use in several different replication scripts. Instead of repeating the conversion function for each replication script, it can be defined in a global script and then called from each replication script.  

  1. In the Management Center Metadata Explorer, select the metadata for which you want to write a global script.

  2. From the right mouse button menu, choose Global Script.
    The Global Script Editor opens to display a stub for the script. The default script language is C#.

    using System;
    using System.Data;
    using DBMotoPublic;
    using DBMotoScript;
    namespace DBRS
    {
        public class GlobalScript : IGlobalScript
        {
        }
        public class MappingRule : IMappingRule
        {
        }
        public class GlobalEvents : IGlobalEvents
        {
        }
    }
  3. In the GlobalScript class, declare functions as in the example below:

    public static DateTime DateConvert(int intDate)
       int cyy;
       int yyyy;
       int mm;
       int dd;   
       cyy = (int)(intDate / 10000);
       mm = (int)((intDate - cyy * 10000) / 100);
       dd = intDate % 100;
       if (cyy < 100)
           yyyy = 1900 + cyy;
       else
           yyyy = 2000 + (cyy % 100);
       DateTime dc = new DateTime(yyyy, mm, dd);
       return dc;
    }
  4. Define your functions. If the function is to be used in replication or expression scripts, add the keyword static after public:

    public static object GetQueryValue(IDbConnection objConnection, String strQuery)
  5. Declare any event handlers as in the following example for the Record_OnExecuteError event. Remember to specify the attribute GlobalEventAttribute.

    [GlobalEventsAttribute("Record_OnExecuteError", "Define a general event for the event EventName")]
        public void MyErrorHandler(String sReplOrGroupName, DBMotoPublic.IRecord  recTarget, Exception e, 
    	ref Boolean bRetryExecute, ref int iSleep, int iIteration)
            {
    	}
  6. If you have added any libraries to the list of imports in the script, click to open the References dialog and add the path to the library.

  7. Use the Compile button to check your script syntax.
    Any script problems are displayed in a separate dialog.

  8. Correct any syntax errors before attempting to run a replication.

Define mapping rules or functions as needed to be used in mapping source and target fields.

Related Topics
Global Script Functions

Global Script Events

Writing a Function to use in Scripts and Expressions

Global Script Function Examples

Replication Script Events

Handling Events for INSERT, UPDATE and DELETE Operations

Replication Script Properties

IRecord Interface

Writing a Replication Script

Writing Scripts with Visual Basic .NET