IRecord Interface
Used as a parameter when writing scripts to handle certain replication events or the Record_OnExecuteError global event, this interface provides access to records identified for replication either via transaction logs/journals during mirroring and synchronization replications, or via mapped source table records during refresh replications. Using the Get and Set methods, you can access and/or modify column values.
ColumnCount (Property)
OperationType (Property)
IsSource (Property)
IsPrimaryKey
enmLogFields(Enumerator)
enmOperationType (Enumerator)
GetValueBefore
GetValueAfter
SetValueBefore
SetValueAfter
GetLogValue
SetLogValue
GetType
GetDBType
GetPrecision
GetScale
GetLength
GetName
ColumnCount (Property)
The number of columns in the IRecord object, that is, it returns the number of mapped columns (within the current replication) in the table to which the IRecord is related.
VB.NET syntax
Public ReadOnly Property ColumnCount As Integer
C# syntax
public int ColumnCount { get; }
Example [VB.NET syntax]
This example prints a message in the log to show how many mapped columns have been found before applying the mapping.
Public Overrides Sub Record_onBeforeMapping(recSource As IRecord, ByRef AbortRecord As Boolean) AddLog("Column Count = " & recSource.ColumnCount, 0)
End Sub
Example [C# syntax]
This example prints a message in the log to show how many mapped columns have been found before applying the mapping.
public override void Record_onBeforeMapping(IRecord recSource, ref bool AbortRecord) { GlobalScript.AddLog("Column Count = " + recSource.ColumnCount, 0); }
OperationType (Property)
A list of constants that define the type of SQL operation associated with the transaction represented in the record. This property is typically used when designing mirroring or synchronization replications which use a transaction log. It provides access to the transaction's SQL operation: INSERT, UPDATE, DELETE, CLEAR or Unknown. If setting up a refresh replication, the only operation type is INSERT. The datatype for this property is the enumerator enmOperationType.
VB.NET syntax
Public Property OperationType As enmOperationType
C# syntax
public enmOperationType OperationType { get; set; }
Example [VB.NET syntax]
The example below checks to see if the operation being performed is an update before proceeding to add the value before changes apply to the Syniti DR log.
Public Overrides Sub Record_onAfterMapping(recSource As IRecord, recTarget As IRecord, ByRef AbortRecord As Boolean) if (recSource.OperationType = enmOperationType.Update) AddLog(recSource.GetValueBefore(0), 0) End if End Sub
Example [C# syntax]
The example below checks to see if the operation being performed is an update before proceeding to add the value before changes apply to the Syniti DR log.
public override void Record_onAfterMapping(DBMotoPublic.IRecord recSource, DBMotoPublic.IRecord recTarget, ref bool AbortRecord) { if (recSource.OperationType == enmOperationType.Update { GlobalScript.AddLog("" + recSource.GetValueBefore(0), 0); } }
enmOperationType (Enumerator)
The datatype for the OperationType property.
Value |
Name |
Description |
0 |
Unknown |
Not typically used |
1 |
Insert |
A SQL INSERT operation |
2 |
Update |
A SQL UPDATE operation |
3 |
Delete |
A SQL DELETE operation |
4 |
Clear |
For Db2/400 only. All values will be cleared from the record. This operation type is supported for mirroring replications only. |
5 |
Internal |
For internal use only. |
IsSource (Property)
This property returns True when the record object has been generated while replicating a transaction from a connection defined under the Source node in the Management Center (often referred to as a source connection) to a connection defined under the Target node in the Management Center (target connection). In other words, all records replicated in a refresh or mirroring replication have this property set to True. In synchronization, it is set to True if the direction of the replication is source to target; False otherwise.
VB.NET syntax
Public Property IsSource as Boolean
C# syntax
public bool IsSource { get; set; }
Example [VB.NET syntax]
This sample function is defined in a synchronization replication and skips (i.e., doesn't propagate to the target) any Delete operations performed on the source table.
Public Overrides Sub Record_onBeforeExecute(recTarget As IRecord, ByRef AbortRecord As Boolean) If (recTarget.IsSource And recTarget.OperationType = enmOperationType.Delete) Then
AbortRecord = True End If End Sub
Example [C# syntax]
This sample function is defined in a synchronization replication and skips (i.e., doesn't propagate to the target) any Delete operations performed on the source table.
public override void Record_onBeforeExecute(IRecord recTarget, ref bool AbortRecord) { if ((recTarget.IsSource & recTarget.OperationType == enmOperationType.Delete)) { AbortRecord = true; } }
IsPrimaryKey
Provides a way to determine if a field is a primary key or part of a primary key.
VB.NET syntax
Public Function IsPrimaryKey (Index As Integer) As Boolean
Public Function IsPrimaryKey (ColumnName As String) As Boolean
C# syntax
public bool IsPrimaryKey(int Index)
public bool IsPrimaryKey(string ColumnName)
Parameters
- Index: an integer that identifies the column for which you want to retrieve the primary key settings
- ColumnName: a string representing the column name for which you want to retrieve the primary key settings
Return value
Returns true if the field is part of the primary key.
Example [VB.NET syntax]
Public Overrides Sub Record_onAfterMapping(recSource As IRecord, recTarget As IRecord, ByRef AbortRecord As Boolean) Dim T As Boolean T=recSource.IsPrimaryKey(1) AddLog("Is the second column a primary key? True or False: " + T.ToString, 0) End Sub
Example [C# syntax]
public override void Record_onAfterMapping(IRecord recSource, IRecord recTarget, ref bool AbortRecord) { bool T = false; T = recSource.IsPrimaryKey(1); GlobalScript.AddLog("Is the second column a primary key? True or False: " + T.ToString, 0); }
GetValueBefore
Returns the value of the column indicated in the record before modification. If the value is not available in the transaction log, this function returns Null. Use this function in mirroring/synchronization replications if you want to retrieve a prior source or target value when the transaction log reports an UPDATE or DELETE operation on a record.
VB.NET syntax
Public Function GetValueBefore (sColName As String) As Object
Public Function GetValueBefore (iIndex As Integer) As Object
C# syntax
public object GetValueBefore(string sColName)
public object GetValueBefore(int iIndex)
Parameters
- sColName: name of the column to be referred to.
- iIndex: index of the column to be referred to.
Example [VB.NET syntax]
The example below checks to see if the operation being performed is a SQL UPDATE. If it is, the AddLog function is called to add the value of the first column (before any changes are applied) to the Syniti DR log. The column value is identified by calling GetValueBefore with an index of 0.
Public Overrides Sub Record_onAfterMapping(recSource As IRecord, recTarget As IRecord, ByRef AbortRecord As Boolean) if (recSource.OperationType = enmOperationType.Update) AddLog(recSource.GetValueBefore(0), 0) End if End Sub
Example [C# syntax]
The example below checks to see if the operation being performed is a SQL UPDATE. If it is, the AddLog function is called to add the value of the first column (before any changes are applied) to the Syniti DR log. The column value is identified by calling GetValueBefore with an index of 0.
public override void Record_onAfterMapping(IRecord recSource, IRecord recTarget, ref bool AbortRecord) { if ((recSource.OperationType == enmOperationType.Update)) { GlobalScript.AddLog(recSource.GetValueBefore(0), 0); } }
GetValueAfter
Returns the value of the column indicated in the record after modification. Use this function in refresh and mirroring/synchronization replications if you want to retrieve a source or target value for an INSERT operation or when the transaction log reports an UPDATE operation on a record.
VB.NET syntax
Public Function GetValueAfter (sColName As String) As Object
Public Function GetValueAfter (iIndex As Integer) As Object
C# syntax
public object GetValueAfter(string sColName)
public object GetValueAfter(int iIndex)
Parameters
- sColName: name of the column to be referred to.
- iIndex: index of the column to be referred to.
Example [VB.NET syntax]
The example below checks to see if the operation being performed is a SQL UPDATE. If it is, the AddLog function is called to add the value of the CUSTOMERID column (after any changes are applied) to the Syniti DR log. The column value is identified by calling GetValueAfter with the column name, and the resulting value is converted to a string.
Public Overrides Sub Record_onAfterMapping(recSource As IRecord, recTarget As IRecord, ByRef AbortRecord As Boolean) if (recSource.OperationType = enmOperationType.Update) AddLog("Value = " + recTarget.GetValueAfter("CUSTOMERID").ToString(), 0) End if End Sub
Example [C# syntax]
The example below checks to see if the operation being performed is a SQL UPDATE. If it is, the AddLog function is called to add the value of the CUSTOMERID column (after any changes are applied) to the Syniti DR log. The column value is identified by calling GetValueAfter with the column name, and the resulting value is converted to a string.
public override void Record_onAfterMapping(IRecord recSource, IRecord recTarget, ref bool AbortRecord) { if ((recSource.OperationType == enmOperationType.Update)) { GlobalScript.AddLog("Value = " + recTarget.GetValueAfter("CUSTOMERID").ToString(), 0); } }
SetValueBefore
Sets the value of the column indicated in the record before modification. This is an unusual method to use, because setting a value in a record before changes apply may ultimately have no effect. The method is included primarily to provide a complete set of methods.
VB.NET syntax
Public Sub SetValueBefore (sColName As String, objValue As Object)
Public Sub SetValueBefore (iIndex As Integer, objValue As Object)
C# syntax
public void SetValueBefore(string sColName, object objValue)
public void SetValueBefore(int iIndex, object objValue)
Parameters
- sColName: name of the column to be referred to.
- iIndex: index of the column to be referred to.
- objValue: the field value to set.
Example [VB.NET syntax]
As mentioned above, it is unlikely that this method will be useful, so the example below demonstrates how to use the method in a fairly unrealistic way. It checks to see if the operation being performed is a SQL UPDATE. If it is, for each updated record, the value before (that is the value to search for in the target table) is set to the first 3 characters concatenated with a blank. The after value is set to the original before value.
' For each updated record, set the value before (that is the value to search into the target table) ' to the first 3 characters concatenated with a blank. Then set the after value to the original before value. Public Overrides Sub Record_onBeforeMapping(recSource As IRecord, ByRef AbortRecord As Boolean) Dim obj As Object If recSource.OperationType = enmOperationType.Update Then obj = recSource.GetValueBefore(0) if Not obj Is Nothing AND Not IsDBNull(obj) Then recSource.SetValueBefore(0 , obj.Substring(0,3) + " ") recSource.SetValueAfter(0 , obj) End if End If End Sub
Example [C# syntax]
As mentioned above, it is unlikely that this method will be useful, so the example below demonstrates how to use the method in a fairly unrealistic way. It checks to see if the operation being performed is a SQL UPDATE. If it is, for each updated record, the value before (that is the value to search for in the target table) is set to the first 3 characters concatenated with a blank. The after value is set to the original before value.
// For each updated record, set the value before (that is the value to search into the target table) // to the first 3 characters concatenated with a blank. Then set the after value to the original before value. public override void Record_onBeforeMapping(IRecord recSource, ref bool AbortRecord) { object obj = null; if (recSource.OperationType == enmOperationType.Update) { obj = recSource.GetValueBefore(0); if ((obj != null) & !Information.IsDBNull(obj)) { recSource.SetValueBefore(0, obj.Substring(0, 3) + " "); recSource.SetValueAfter(0, obj); } } }
SetValueAfter
Sets the value of the column indicated in the record after modification. Use this function in refresh and mirroring/synchronization replications if you want to change a source or target value for an INSERT operation or change a value when the transaction log reports an UPDATE operation on a record.
VB.NET syntax
Public Sub SetValueAfter (sColName As String, objValue As Object)
Public Sub SetValueAfter (iIndex As Integer, objValue As Object)
C# syntax
public void SetValueAfter(string sColName, object objValue)
public void SetValueAfter(int iIndex, object objValue)
Parameters
- sColName: name of the column to be referred to.
- iIndex: index of the column to be referred to.
- objValue: the field value to set.
Example [VB.NET syntax]
The example below checks to see if the operation being performed is a SQL UPDATE. Then, for each update, it replicates the value of the CITY column in uppercase.
' For each update, replicate the value of the column CITY in uppercase. Public Overrides Sub Record_onAfterMapping(recSource As IRecord, recTarget As IRecord, ByRef AbortRecord As Boolean) Dim obj As Object If recSource.OperationType = enmOperationType.Update Then obj = recTarget.GetValueAfter("CITY") if Not obj Is Nothing AND Not IsDBNull(obj) Then recTarget.SetValueAfter("CITY", recTarget.GetValueAfter("CITY").ToUpper) End if End If End Sub
Example [C# syntax]
The example below checks to see if the operation being performed is a SQL UPDATE. Then, for each update, it replicates the value of the CITY column in uppercase.
// For each update, replicate the value of the column CITY in uppercase. public override void Record_onAfterMapping(IRecord recSource, IRecord recTarget, ref bool AbortRecord) { object obj = null; if (recSource.OperationType == enmOperationType.Update) { obj = recTarget.GetValueAfter("CITY"); if ((obj != null) & !Information.IsDBNull(obj)) { recTarget.SetValueAfter("CITY", recTarget.GetValueAfter("CITY").ToUpper); } } }
GetLogValue
Returns the value of the log field indicated. The parameter should be of type enmLogFields.
VB.NET syntax
Public Function GetLogValue (eLogField As enmLogFields) As Object
C# syntax
public object GetLogValue(enmLogFields eLogField)
Parameters
- eLogField: An enmLogFields datatype, this is the name of the field to be referred to.
Example [VB.NET syntax]
The example below retrieves values from the enmLogFields enumerator and adds them to the Syniti DR log.
Public Overrides Sub Record_onBeforeMapping(recSource As IRecord, ByRef AbortRecord As Boolean) AddLog("TransactionID = " + recSource.GetLogValue(enmLogFields.TransactionID).ToString(), 0) AddLog("TransactionTS = " + recSource.GetLogValue(enmLogFields.TransactionTS).ToString(), 0) AddLog("UserID = " + recSource.GetLogValue(enmLogFields.UserID).ToString(), 0) AddLog("RecordID = " + recSource.GetLogValue(enmLogFields.RecordID).ToString(), 0) AddLog("ReceiverLibrary = " + recSource.GetLogValue(enmLogFields.ReceiverLibrary).ToString(), 0) AddLog("ReceiverName = " + recSource.GetLogValue(enmLogFields.ReceiverName).ToString(), 0) End Sub
Example [C# syntax]
The example below retrieves values from the enmLogFields enumerator and adds them to the Syniti DR log.
public override void Record_onBeforeMapping(IRecord recSource, ref bool AbortRecord) { GlobalScript.AddLog("TransactionID = " + recSource.GetLogValue(enmLogFields.TransactionID).ToString(), 0); GlobalScript.AddLog("TransactionTS = " + recSource.GetLogValue(enmLogFields.TransactionTS).ToString(), 0); GlobalScript.AddLog("UserID = " + recSource.GetLogValue(enmLogFields.UserID).ToString(), 0); GlobalScript.AddLog("RecordID = " + recSource.GetLogValue(enmLogFields.RecordID).ToString(), 0); GlobalScript.AddLog("ReceiverLibrary = " + recSource.GetLogValue(enmLogFields.ReceiverLibrary).ToString(), 0); GlobalScript.AddLog("ReceiverName = " + recSource.GetLogValue(enmLogFields.ReceiverName).ToString(), 0); }
enmLogFields (Enumerator)
The datatype for the GetLogValue parameter.
Value |
Name |
Description |
0 |
TransactionID |
The transaction ID for the record from the transaction log/journal |
1 |
TransactionTS |
The transaction timestamp |
2 |
UserID |
The User ID associated with the transaction |
3 |
RecordID |
For Db2/400 this is mapped to the RRN. For Oracle, it is mapped to the Row ID and for SQL Server, the value is always Null. |
4 |
ReceiverLibrary |
For Db2/400 only. The library containing the receiver for this transaction. |
5 |
ReceiverName |
For Db2/400 only. The name of the receiver used for this transaction. |
SetLogValue
Sets a value in the log field indicated. It is strongly recommended not to modify the values of the enmLogFields enumerator because it is used during mirroring and synchronization replications by the Replication Agent.
VB.NET syntax
Public Sub SetLogValue (eLogField As enmLogFields, objValue As Object)
C# syntax
public void SetLogValue(enmLogFields eLogField, object objValue)
Parameters
- eLogField: name of the field to be referred to.
- objValue: the field value to set.
GetType
Returns the .NET type associated with the field type.
VB.NET syntax
Public Function GetType (Index As Integer) As System.Type
Public Function GetType (ColumnName As String) As System.Type
Parameters
- Index: an integer that identifies the column for which you want to retrieve the type
- ColumnName: a string representing the column name for which you want to retrieve the type
Return Value
Returns the System.Type associated with the column, identified either by index or by column name.
GetDBType
Returns the database type associated with the field type.
VB.NET syntax
Public Function GetDBType (Index As Integer) As String
Public Function GetDBType (ColumnName As String) As String
C# syntax
public string GetDBType(int Index)
public string GetDBType(string ColumnName)
Parameters
- Index: an integer that identifies the column for which you want to retrieve the type
- ColumnName: a string representing the column name for which you want to retrieve the type
Return value
Returns the name of the field type associated with the column, identified either by index or by column name.
GetPrecision
Returns the field precision.
VB.NET syntax
Public Function GetPrecision (Index As Integer) As Short
Public Function GetPrecision (ColumnName As String) As Short
C# syntax
public short GetPrecision(int Index)
public short GetPrecision(string ColumnName)
Parameters
- Index: an integer that identifies the column for which you want to retrieve the precision
- ColumnName: a string representing the column name for which you want to retrieve the precision
Return value
Returns the precision of the column identified either by index or by column name
GetScale
Returns the field scale.
VB.NET syntax
Public Function GetScale (Index As Integer) As Short
Public Function GetScale (ColumnName As String) As Short
C# syntax
public short GetScale(int Index)
public short GetScale(string ColumnName)
Parameters
- Index: an integer that identifies the column for which you want to retrieve the scale
- ColumnName: a string representing the column name for which you want to retrieve the scale
Return value
Returns the scale of the column identified either by index or by column name.
GetLength
Returns the field defined size
VB.NET syntax
Public Function GetLength (Index As Integer) As Integer
Public Function GetLength (ColumnName As String) As Integer
C# syntax
public int GetLength(int Index)
public int GetLength(string ColumnName)
Parameters
- eLogField: name of the field to be referred to.
- objValue: the field value to set.
Return value
Returns the defined size of the column identified either by index or by column name.
GetName
Returns the column name associated with the field index.
VB.NET syntax
Public Function GetName (Index As Integer) As String
C# syntax
public string GetName(int Index)
Parameters
- Index: an integer that identifies the column for which you want to retrieve the name
Return Value
Returns the name associated with the column, identified by index.
Example [VB.NET Syntax]
Public Overrides Sub Record_onAfterMapping(recSource As IRecord, recTarget As IRecord, ByRef AbortRecord As Boolean) Dim S As String S=recSource.GetName(0) AddLog("The first column name is:" + S, 0) End Sub
Example [C# Syntax]
public override void Record_onAfterMapping(IRecord recSource, IRecord recTarget, ref bool AbortRecord) { string S = null; S = recSource.GetName(0); GlobalScript.AddLog("The first column name is:" + S, 0); }
Related Topics
Replication Script Events
Handling Events for INSERT, UPDATE and DELETE Operations
Replication Script Properties
Global Script Functions
Writing a Replication Script
Writing a Global Script
Writing Scripts with Visual Basic .NET